Open FFBoard
Open source force feedback firmware
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
27enum class VescState : uint32_t {
34
35};
36
37enum class VescCANMsg : uint8_t {
43 CAN_PACKET_PING = 17,
44 CAN_PACKET_PONG = 18,
46};
47
48enum class VescCmd : uint8_t {
52};
53enum class VescEncoderMode : uint8_t {
54 ENCODER_OFF = 0, ENCODER_ON = 3
55};
56
57enum class VescCAN_commands : uint32_t {
59};
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
110private:
111
112 // Vesc interface and motor state
115 volatile uint8_t vescErrorFlag;
116 bool activeMotor = false;
118 float voltage;
119
120 // Encoder section
121
122 bool useEncoder = true;
123 float lastPos = 0; // last motor position known in "turn unit"
124 float mtPos = 0; // number of turn known
125 float prevPos360 = 0; // previous position in [0-360] position
126 float posOffset = 0; // used to center the wheel
127 volatile uint32_t encCount = 0; // count incoming message
128 volatile uint32_t encStartPeriod = 0; // start time of period to compute encoder Rate
129 volatile float encRate = 0; // encoder rate to test if can speed setting is OK
130 volatile uint32_t lastVescResponse = 0; // record last time when vesc respond
131
132 void saveFlashOffset();
133
134 // CAN section
135
137 int32_t filterId = 0;
138 uint8_t OFFB_can_Id = 0x40; // Default OpenFFBoard CAN ID
139 uint8_t VESC_can_Id = 0xFF; // Default VESC CAN id
140 uint8_t buffer_rx[BUFFER_RX_SIZE]; // Used to store multi-frame can message
141
142 //void setCanRate(uint8_t canRate);
143 void getFirmwareInfo();
145 void sendPing();
146 void setTorque(float torque);
147 void decodeEncoderPosition(float newPos);
148 void askGetValue();
149 void askPositionEncoder();
150 void sendMsg(uint8_t cmd, uint8_t *buffer, uint8_t len);
151 void decode_buffer(uint8_t *buffer, uint8_t len);
152
153 void buffer_append_float32(uint8_t *buffer, float number, float scale, int32_t *index);
154 void buffer_append_int32(uint8_t *buffer, int32_t number, int32_t *index);
155 void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index);
156 uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index);
157 int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index);
158 int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index);
159 float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index);
160 float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index);
161
162 static std::array<uint16_t,256> crc16_tab;
163 static const uint16_t crcpoly = 0x1021;
165
166};
167
168// *** Creation of the first concrete class (VESC_1) used by OpenFFBoard of VescCan interface ***
169class VESC_1 : public VescCAN {
170public:
171 VESC_1() : VescCAN{0} { inUse = true; }
172 ~VESC_1() { inUse = false; }
173
175 static bool inUse;
176
177 static bool isCreatable();
178 const ClassIdentifier getInfo();
179};
180
181// *** Creation of the second concrete class (VESC_2) used by OpenFFBoard of VescCan interface ***
182class VESC_2 : public VescCAN {
183public:
184 VESC_2() : VescCAN{1} { inUse = true; }
185 ~VESC_2() { inUse = false; }
186
188 static bool inUse;
189
190 static bool isCreatable();
191 const ClassIdentifier getInfo();
192};
193
194#endif /* VESC */
195#endif /* USEREXTENSIONS_SRC_VESCCAN_H_ */
CommandStatus
EncoderType
Definition: Encoder.h:27
VescState
Definition: VescCAN.h:27
@ VESC_STATE_READY
@ VESC_STATE_INCOMPATIBLE
@ VESC_STATE_COMPATIBLE
@ VESC_STATE_UNKNOWN
@ VESC_STATE_ERROR
VescEncoderMode
Definition: VescCAN.h:53
VescCAN_commands
Definition: VescCAN.h:57
VescCANMsg
Definition: VescCAN.h:37
@ CAN_PACKET_FILL_RX_BUFFER
@ CAN_PACKET_PROCESS_RX_BUFFER
@ CAN_PACKET_SET_CURRENT_REL
@ CAN_PACKET_POLL_ROTOR_POS
@ CAN_PACKET_PROCESS_SHORT_BUFFER
@ CAN_PACKET_FILL_RX_BUFFER_LONG
VescCmd
Definition: VescCAN.h:48
@ COMM_ROTOR_POSITION
@ COMM_FW_VERSION
@ COMM_GET_VALUES_SELECTIVE
Definition: CAN.h:119
const ClassIdentifier getInfo()
Command handlers always have class infos. Works well with ChoosableClass.
Definition: VescCAN.cpp:29
static ClassIdentifier info
Definition: VescCAN.h:174
~VESC_1()
Definition: VescCAN.h:172
VESC_1()
Definition: VescCAN.h:171
static bool isCreatable()
Definition: VescCAN.cpp:25
static bool inUse
Definition: VescCAN.h:175
static bool isCreatable()
Definition: VescCAN.cpp:42
static ClassIdentifier info
Definition: VescCAN.h:187
~VESC_2()
Definition: VescCAN.h:185
const ClassIdentifier getInfo()
Command handlers always have class infos. Works well with ChoosableClass.
Definition: VescCAN.cpp:46
VESC_2()
Definition: VescCAN.h:184
static bool inUse
Definition: VescCAN.h:188
float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index)
Definition: VescCAN.cpp:707
uint8_t buffer_rx[BUFFER_RX_SIZE]
Definition: VescCAN.h:140
void getFirmwareInfo()
Definition: VescCAN.cpp:369
bool motorReady() override
Definition: VescCAN.cpp:131
volatile VescState state
Definition: VescCAN.h:114
void decodeEncoderPosition(float newPos)
Definition: VescCAN.cpp:441
volatile uint8_t vescErrorFlag
Definition: VescCAN.h:115
int32_t getPos() override
Definition: VescCAN.cpp:211
void buffer_append_int32(uint8_t *buffer, int32_t number, int32_t *index)
Definition: VescCAN.cpp:661
uint8_t OFFB_can_Id
Definition: VescCAN.h:138
bool activeMotor
Definition: VescCAN.h:116
float lastTorque
Definition: VescCAN.h:117
void setTorque(float torque)
Definition: VescCAN.cpp:394
float posOffset
Definition: VescCAN.h:126
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index)
Definition: VescCAN.cpp:686
uint32_t getCpr() override
Definition: VescCAN.cpp:215
virtual std::string getHelpstring()
Definition: VescCAN.h:103
void askPositionEncoder()
Definition: VescCAN.cpp:431
VescFlashAddrs flashAddrs
Definition: VescCAN.h:113
void stopMotor() override
Definition: VescCAN.cpp:104
void askGetValue()
Definition: VescCAN.cpp:412
volatile uint32_t encCount
Definition: VescCAN.h:127
volatile uint32_t lastVescResponse
Definition: VescCAN.h:130
void decode_buffer(uint8_t *buffer, uint8_t len)
Definition: VescCAN.cpp:469
float lastPos
Definition: VescCAN.h:123
void Run()
Definition: VescCAN.cpp:317
void turn(int16_t power) override
Definition: VescCAN.cpp:98
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index)
Definition: VescCAN.cpp:695
virtual ~VescCAN()
Definition: VescCAN.cpp:82
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index)
Definition: VescCAN.cpp:677
volatile float encRate
Definition: VescCAN.h:129
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index)
Definition: VescCAN.cpp:702
float getPos_f() override
Definition: VescCAN.cpp:204
void buffer_append_uint32(uint8_t *buffer, uint32_t number, int32_t *index)
Definition: VescCAN.cpp:669
void sendMsg(uint8_t cmd, uint8_t *buffer, uint8_t len)
Definition: VescCAN.cpp:459
float voltage
Definition: VescCAN.h:118
bool isFirmwareCompatible()
void saveFlashOffset()
Definition: VescCAN.cpp:179
static bool crcTableInitialized
Definition: VescCAN.h:164
float prevPos360
Definition: VescCAN.h:125
static const uint16_t crcpoly
Definition: VescCAN.h:163
uint8_t VESC_can_Id
Definition: VescCAN.h:139
void saveFlash() override
Definition: VescCAN.cpp:164
bool hasIntegratedEncoder() override
Definition: VescCAN.cpp:120
static std::array< uint16_t, 256 > crc16_tab
Definition: VescCAN.h:162
void startMotor() override
Definition: VescCAN.cpp:109
void restoreFlash() override
Definition: VescCAN.cpp:135
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
Definition: VescCAN.cpp:235
float mtPos
Definition: VescCAN.h:124
void sendPing()
Definition: VescCAN.cpp:382
int32_t filterId
Definition: VescCAN.h:137
bool useEncoder
Definition: VescCAN.h:122
void registerCommands()
Definition: VescCAN.cpp:219
void setPos(int32_t pos) override
Definition: VescCAN.cpp:197
void buffer_append_float32(uint8_t *buffer, float number, float scale, int32_t *index)
Definition: VescCAN.cpp:656
VescCAN(uint8_t address)
Definition: VescCAN.cpp:52
Encoder * getEncoder() override
Definition: VescCAN.cpp:113
EncoderType getEncoderType() override
Definition: VescCAN.cpp:124
CANPort * port
Definition: VescCAN.h:136
volatile uint32_t encStartPeriod
Definition: VescCAN.h:128
void canRxPendCallback(CANPort *port, CAN_rx_msg &msg) override
Definition: VescCAN.cpp:566
void setAddress(uint8_t address)
Definition: VescCAN.cpp:88
CANPort & canport
uint8_t const * buffer
Definition: midi_device.h:100
Definition: CAN.h:96
uint16_t offset
Definition: VescCAN.h:64
uint16_t data
Definition: VescCAN.h:63
uint16_t canId
Definition: VescCAN.h:62