Open FFBoard
Open source force feedback firmware
Loading...
Searching...
No Matches
VescCAN.h
Go to the documentation of this file.
1/*
2 * VescCAN.h
3 *
4 * Created on: Aug 08, 2021
5 * Author: Vincent Manoukian (manoukianvg@gmail.com)
6 */
7
8#ifndef USEREXTENSIONS_SRC_VESCCAN_H_
9#define USEREXTENSIONS_SRC_VESCCAN_H_
10#include "MotorDriver.h"
11#include "cpp_target_config.h"
12#include "CAN.h"
13#include "Encoder.h"
14#include "thread.hpp"
15#include "CanHandler.h"
16#include "CommandHandler.h"
17#include "PersistentStorage.h"
18#include <math.h>
19
20#ifdef VESC
21#define VESC_THREAD_MEM 512
22#define VESC_THREAD_PRIO 25 // Must be higher than main thread
23#define BUFFER_RX_SIZE 64
24
25#define FW_MIN_RELEASE ((5 << 16) | (3 << 8) | 51)
26
36
46};
47
53enum class VescEncoderMode : uint8_t {
55};
56
60
62 uint16_t canId = ADR_VESC1_CANID;
63 uint16_t data = ADR_VESC1_DATA;
64 uint16_t offset = ADR_VESC1_OFFSET;
65};
66
67class VescCAN: public MotorDriver,
68 public PersistentStorage,
69 public Encoder,
70 public CanHandler,
71 public CommandHandler,
73public:
74 VescCAN(uint8_t address);
75 virtual ~VescCAN();
76 void setAddress(uint8_t address);
77
78 // MotorDriver impl
79 void turn(int16_t power) override;
80 void stopMotor() override;
81 void startMotor() override;
82 Encoder* getEncoder() override;
83 bool hasIntegratedEncoder() override;
84 EncoderType getEncoderType() override;
85 bool motorReady() override;
86
87 // PersistentStorage impl
88 void saveFlash() override;
89 void restoreFlash() override;
90
91 // Encoder impl
92 float getPos_f() override;
93 uint32_t getCpr() override;
94 int32_t getPos() override;
95 void setPos(int32_t pos) override;
96
97 // CanHandler impl
98 void canRxPendCallback(CANPort* port,CAN_rx_msg& msg) override;
99
100 // CommandHandler impl
101 CommandStatus command(const ParsedCommand& cmd,std::vector<CommandReply>& replies);
102 void registerCommands(); // Function reserved to register commands in the command list.
103 virtual std::string getHelpstring() {
104 return "VESC CAN interface";
105 };
106
107 // Thread impl
108 void Run();
109
110 bool setCanFilter();
111
112private:
113
114 // Vesc interface and motor state
117 volatile uint8_t vescErrorFlag;
118 bool activeMotor = false;
120 float voltage;
121
122 // Encoder section
123
124 bool useEncoder = true;
125 float lastPos = 0; // last motor position known in "turn unit"
126 float mtPos = 0; // number of turn known
127 float prevPos360 = 0; // previous position in [0-360] position
128 float posOffset = 0; // used to center the wheel
129 volatile uint32_t encCount = 0; // count incoming message
130 volatile uint32_t encStartPeriod = 0; // start time of period to compute encoder Rate
131 volatile float encRate = 0; // encoder rate to test if can speed setting is OK
132 volatile uint32_t lastVescResponse = 0; // record last time when vesc respond
133
134 void saveFlashOffset();
135
136 // CAN section
137
139 int32_t filterId = -1;
140 int32_t filterIdVesc = -1;
141 uint8_t OFFB_can_Id = 0x40; // Default OpenFFBoard CAN ID
142 uint8_t VESC_can_Id = 0xFF; // Default VESC CAN id
143 uint8_t buffer_rx[BUFFER_RX_SIZE]; // Used to store multi-frame can message
144
145 //void setCanRate(uint8_t canRate);
146 void getFirmwareInfo();
148 void sendPing();
149 void setTorque(float torque);
150 void decodeEncoderPosition(float newPos);
151 void askGetValue();
152 void askPositionEncoder();
153 void sendMsg(uint8_t cmd, uint8_t *buffer, uint8_t len);
154 void decode_buffer(uint8_t *buffer, uint8_t len);
155
156 void buffer_append_float32(uint8_t *buffer, float number, float scale, int32_t *index);
157 void buffer_append_int32(uint8_t *buffer, int32_t number, int32_t *index);
158 void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index);
159 uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index);
160 int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index);
161 int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index);
162 float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index);
163 float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index);
164
165 static std::array<uint16_t,256> crc16_tab;
166 static const uint16_t crcpoly = 0x1021;
168
169};
170
171// *** Creation of the first concrete class (VESC_1) used by OpenFFBoard of VescCan interface ***
172class VESC_1 : public VescCAN {
173public:
174 VESC_1() : VescCAN{0} { inUse = true; }
175 ~VESC_1() { inUse = false; }
176
177 static ClassIdentifier info;
178 static bool inUse;
179
180 static bool isCreatable();
181 const ClassIdentifier getInfo();
182};
183
184// *** Creation of the second concrete class (VESC_2) used by OpenFFBoard of VescCan interface ***
185class VESC_2 : public VescCAN {
186public:
187 VESC_2() : VescCAN{1} { inUse = true; }
188 ~VESC_2() { inUse = false; }
189
190 static ClassIdentifier info;
191 static bool inUse;
192
193 static bool isCreatable();
194 const ClassIdentifier getInfo();
195};
196
197#endif /* VESC */
198#endif /* USEREXTENSIONS_SRC_VESCCAN_H_ */
CommandStatus
EncoderType
Definition Encoder.h:27
VescState
Definition VescCAN.h:27
@ VESC_STATE_READY
Definition VescCAN.h:32
@ VESC_STATE_INCOMPATIBLE
Definition VescCAN.h:29
@ VESC_STATE_COMPATIBLE
Definition VescCAN.h:31
@ VESC_STATE_PONG
Definition VescCAN.h:30
@ VESC_STATE_UNKNOWN
Definition VescCAN.h:28
@ VESC_STATE_ERROR
Definition VescCAN.h:33
VescEncoderMode
Definition VescCAN.h:53
VescCAN_commands
Definition VescCAN.h:57
VescCANMsg
Definition VescCAN.h:37
@ CAN_PACKET_PING
Definition VescCAN.h:43
@ CAN_PACKET_PONG
Definition VescCAN.h:44
@ CAN_PACKET_FILL_RX_BUFFER
Definition VescCAN.h:38
@ CAN_PACKET_PROCESS_RX_BUFFER
Definition VescCAN.h:40
@ CAN_PACKET_SET_CURRENT_REL
Definition VescCAN.h:42
@ CAN_PACKET_POLL_ROTOR_POS
Definition VescCAN.h:45
@ CAN_PACKET_PROCESS_SHORT_BUFFER
Definition VescCAN.h:41
@ CAN_PACKET_FILL_RX_BUFFER_LONG
Definition VescCAN.h:39
VescCmd
Definition VescCAN.h:48
@ COMM_ROTOR_POSITION
Definition VescCAN.h:50
@ COMM_FW_VERSION
Definition VescCAN.h:49
@ COMM_GET_VALUES_SELECTIVE
Definition VescCAN.h:51
Definition CAN.h:121
CommandHandler(const char *clsname, uint16_t clsid, uint8_t instance=0)
Encoder()
Definition Encoder.cpp:18
const ClassIdentifier getInfo()
Definition VescCAN.cpp:29
~VESC_1()
Definition VescCAN.h:175
VESC_1()
Definition VescCAN.h:174
static bool isCreatable()
Definition VescCAN.cpp:25
static bool inUse
Definition VescCAN.h:178
static bool isCreatable()
Definition VescCAN.cpp:42
~VESC_2()
Definition VescCAN.h:188
const ClassIdentifier getInfo()
Definition VescCAN.cpp:46
VESC_2()
Definition VescCAN.h:187
static bool inUse
Definition VescCAN.h:191
float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index)
Definition VescCAN.cpp:745
uint8_t buffer_rx[BUFFER_RX_SIZE]
Definition VescCAN.h:143
void getFirmwareInfo()
Definition VescCAN.cpp:405
bool motorReady() override
Definition VescCAN.cpp:159
volatile VescState state
Definition VescCAN.h:116
void decodeEncoderPosition(float newPos)
Definition VescCAN.cpp:477
volatile uint8_t vescErrorFlag
Definition VescCAN.h:117
int32_t getPos() override
Definition VescCAN.cpp:239
void buffer_append_int32(uint8_t *buffer, int32_t number, int32_t *index)
Definition VescCAN.cpp:699
uint8_t OFFB_can_Id
Definition VescCAN.h:141
bool activeMotor
Definition VescCAN.h:118
float lastTorque
Definition VescCAN.h:119
void setTorque(float torque)
Definition VescCAN.cpp:430
float posOffset
Definition VescCAN.h:128
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index)
Definition VescCAN.cpp:724
uint32_t getCpr() override
Definition VescCAN.cpp:243
virtual std::string getHelpstring()
Definition VescCAN.h:103
void askPositionEncoder()
Definition VescCAN.cpp:467
VescFlashAddrs flashAddrs
Definition VescCAN.h:115
void stopMotor() override
Definition VescCAN.cpp:132
void askGetValue()
Definition VescCAN.cpp:448
volatile uint32_t encCount
Definition VescCAN.h:129
volatile uint32_t lastVescResponse
Definition VescCAN.h:132
void decode_buffer(uint8_t *buffer, uint8_t len)
Definition VescCAN.cpp:506
float lastPos
Definition VescCAN.h:125
void Run()
Definition VescCAN.cpp:353
int32_t filterIdVesc
Definition VescCAN.h:140
void turn(int16_t power) override
Definition VescCAN.cpp:123
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index)
Definition VescCAN.cpp:733
virtual ~VescCAN()
Definition VescCAN.cpp:76
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index)
Definition VescCAN.cpp:715
volatile float encRate
Definition VescCAN.h:131
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index)
Definition VescCAN.cpp:740
float getPos_f() override
Definition VescCAN.cpp:232
void buffer_append_uint32(uint8_t *buffer, uint32_t number, int32_t *index)
Definition VescCAN.cpp:707
void sendMsg(uint8_t cmd, uint8_t *buffer, uint8_t len)
Definition VescCAN.cpp:495
float voltage
Definition VescCAN.h:120
bool isFirmwareCompatible()
void saveFlashOffset()
Definition VescCAN.cpp:207
static bool crcTableInitialized
Definition VescCAN.h:167
float prevPos360
Definition VescCAN.h:127
static const uint16_t crcpoly
Definition VescCAN.h:166
uint8_t VESC_can_Id
Definition VescCAN.h:142
void saveFlash() override
Definition VescCAN.cpp:192
bool hasIntegratedEncoder() override
Definition VescCAN.cpp:148
static std::array< uint16_t, 256 > crc16_tab
Definition VescCAN.h:165
void startMotor() override
Definition VescCAN.cpp:137
void restoreFlash() override
Definition VescCAN.cpp:163
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
Definition VescCAN.cpp:263
float mtPos
Definition VescCAN.h:126
void sendPing()
Definition VescCAN.cpp:418
int32_t filterId
Definition VescCAN.h:139
bool useEncoder
Definition VescCAN.h:124
void registerCommands()
Definition VescCAN.cpp:247
void setPos(int32_t pos) override
Definition VescCAN.cpp:225
void buffer_append_float32(uint8_t *buffer, float number, float scale, int32_t *index)
Definition VescCAN.cpp:694
VescCAN(uint8_t address)
Definition VescCAN.cpp:52
Encoder * getEncoder() override
Definition VescCAN.cpp:141
EncoderType getEncoderType() override
Definition VescCAN.cpp:152
CANPort * port
Definition VescCAN.h:138
volatile uint32_t encStartPeriod
Definition VescCAN.h:130
void canRxPendCallback(CANPort *port, CAN_rx_msg &msg) override
Definition VescCAN.cpp:603
void setAddress(uint8_t address)
Definition VescCAN.cpp:84
bool setCanFilter()
Definition VescCAN.cpp:94
CANPort & canport
uint8_t const * buffer
uint16_t offset
Definition VescCAN.h:64
uint16_t data
Definition VescCAN.h:63
uint16_t canId
Definition VescCAN.h:62