-
Notifications
You must be signed in to change notification settings - Fork 0
/
vesc.cpp
429 lines (344 loc) · 10.8 KB
/
vesc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// VESC communication library
#include <Arduino.h>
#include "can.h"
#include "vesc.h"
#include "vesc_buffer.h"
#include "vesc_crc.h"
#include "vesc_datatypes.h"
#include "vesc_packet.h"
vesc_values_t vesc_values[2];
static uint8_t num_vescs;
static uint8_t receive_buffer[263];
static bool is_vesc(uint8_t id); // check if a vesc exists
static void send(uint8_t id, uint8_t *payload, size_t len); // send a comm command to the Vesc
static void receive(uint8_t id, uint16_t cmd, const uint8_t *payload, size_t len); // receive a comm command from the Vesc
static void process_comm(uint8_t id, size_t len); // process a comm command received from the vesc
// register a vesc
size_t vesc_register(uint8_t id)
{
if(num_vescs == 2)
{
Serial.println("[ERROR] vesc: Max number of Vescs registered!");
return 0;
}
vesc_values[num_vescs].id = id;
num_vescs++;
return num_vescs - 1;
}
// vesc initialization
void vesc_init(void)
{
can_register_cmd(CAN_PACKET_PROCESS_SHORT_BUFFER, receive);
can_register_cmd(CAN_PACKET_FILL_RX_BUFFER, receive);
can_register_cmd(CAN_PACKET_FILL_RX_BUFFER_LONG, receive);
can_register_cmd(CAN_PACKET_PROCESS_RX_BUFFER, receive);
//Serial.println("[INFO] vesc: Initialization complete");
}
// request new values from a vesc
void vesc_update_values(uint8_t id)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[1];
payload[len++] = COMM_GET_VALUES;
send(id, payload, len);
}
// set motor duty
void vesc_set_duty(uint8_t id, float duty)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[5];
payload[len++] = COMM_SET_DUTY;
buffer_append_float32(payload, duty, 1e1, &len);
send(id, payload, len);
}
// set motor current
void vesc_set_current(uint8_t id, float current)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[5];
payload[len++] = COMM_SET_CURRENT;
buffer_append_float32(payload, current, 1e3, &len);
send(id, payload, len);
}
// set motor brake current
void vesc_set_current_brake(uint8_t id, float current)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[5];
payload[len++] = COMM_SET_CURRENT_BRAKE;
buffer_append_float32(payload, current, 1e3, &len);
send(id, payload, len);
}
// set motor rpm
void vesc_set_rpm(uint8_t id, float rpm)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[5];
payload[len++] = COMM_SET_RPM;
buffer_append_float32(payload, rpm, 1, &len);
send(id, payload, len);
}
// set motor position
void vesc_set_position(uint8_t id, float position)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[5];
payload[len++] = COMM_SET_POS;
buffer_append_float32(payload, position, 1e6, &len);
send(id, payload, len);
}
// reboot vesc
void vesc_reboot(uint8_t id)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[1];
payload[len++] = COMM_REBOOT;
send(id, payload, len);
}
// send alive packet
void vesc_keep_alive(uint8_t id)
{
if(!is_vesc(id)) return;
size_t len = 0;
uint8_t payload[1];
payload[len++] = COMM_ALIVE;
send(id, payload, len);
}
/*
// get a description of a VESC fault code
String getFaultMsg(uint8_t id, vesc_fault_code_t code)
{
if(!is_vesc(id)) return;
if(code >= NUM_VESC_FAULT_CODE) {
return "Unknown fault";
}
return VESC_FAULT_CODE_STR[code];
}
*/
// check if a vesc exists
static bool is_vesc(uint8_t id)
{
size_t i;
for(i = 0; i < num_vescs; i++)
{
if(vesc_values[i].id == id) break;
}
if(i == num_vescs)
{
Serial.println("[ERROR] vesc: Vesc ID 0x");
Serial.print(id, HEX);
Serial.println(" not found!");
return false;
}
return true;
}
/*
VESC Comm Protocol:
Since CAN messages are limited to a payload of 8 bytes, messages are split
up amoung multiple CAN messages. Buffer messages are sent first with the
payload data, then a process message is sent to execute the comm command.
CAN_PACKET_PROCESS_SHORT_BUFFER:
If payload len <= 6, send data and proccess in one CAN message.
Byte 0: CAN id that sent the message
Byte 1: Process mode (set to 0 for process and reply)
Bytes 2-7: Comm payload
CAN_PACKET_FILL_RX_BUFFER:
Payload data bytes 0-255.
Byte 0: Comm payload byte index
Bytes 1-7: Comm payload
CAN_PACKET_FILL_RX_BUFFER_LONG:
Payload data bytes 256-65535.
Byte 0: Comm payload byte index (high byte)
Byte 1: Comm payload byte index (low byte)
Byte 2-7: Comm payload
CAN_PACKET_PROCESS_RX_BUFFER:
Process comm command w/ already received payload data.
Byte 0: CAN id that sent the message
Byte 1: Process mode (set to 0 for process and reply)
Byte 2: Comm payload length (high byte)
Byte 3: Comm payload length (low byte)
Byte 4: Comm payload crc (high byte)
Byte 5: Comm payload crc (low byte)
*/
// send a comm command to the Vesc
static void send(uint8_t id, uint8_t *payload, size_t len)
{
size_t payload_index = 0;
uint8_t msg[8];
size_t msg_len;
if(len == 0 || len > PACKET_MAX_PL_LEN || len > 65535)
{
Serial.print("[ERROR] vesc: Comm payload length must be between 1 and ");
Serial.println(PACKET_MAX_PL_LEN, DEC);
return;
}
if(len <= 6)
{
// fit entire comm payload into one can message
msg_len = 0;
msg[msg_len++] = CAN_ID;
msg[msg_len++] = 0; // vesc should process comm message and reply
memcpy(msg + msg_len, payload, len);
msg_len += len;
can_send_cmd(id, CAN_PACKET_PROCESS_SHORT_BUFFER, msg, msg_len);
}
else
{
// split up comm payload across multiple can messages
// send bytes 0 to 255 in Fill RX Buffer msgs, 7 bytes at a time
while(payload_index < len && payload_index <= 255) {
msg_len = 0;
msg[msg_len++] = payload_index;
if((len - payload_index) >= 7) {
memcpy(msg + msg_len, payload + payload_index, 7);
msg_len += 7;
payload_index += 7;
} else {
memcpy(msg + msg_len, payload + payload_index, len - payload_index);
msg_len += len - payload_index;
payload_index += len - payload_index;
}
can_send_cmd(id, CAN_PACKET_FILL_RX_BUFFER, msg, msg_len);
}
// send bytes 256 and beyond in Fill RX Buffer Long msgs, 6 bytes at a time
while(payload_index < len) {
msg_len = 0;
msg[msg_len++] = payload_index >> 8;
msg[msg_len++] = payload_index & 0xFF;
if((len - payload_index) >= 6) {
memcpy(msg + msg_len, payload + payload_index, 6);
msg_len += 6;
payload_index += 6;
} else {
memcpy(msg + msg_len, payload + payload_index, len - payload_index);
msg_len += len - payload_index;
payload_index += len - payload_index;
}
can_send_cmd(id, CAN_PACKET_FILL_RX_BUFFER_LONG, msg, msg_len);
}
// all bytes sent, send can message to process comm packet
msg_len = 0;
msg[msg_len++] = CAN_ID;
msg[msg_len++] = 0; // vesc should process comm message and reply
msg[msg_len++] = len >> 8;
msg[msg_len++] = len & 0xFF;
uint16_t crc = crc16(payload, len);
msg[msg_len++] = crc >> 8;
msg[msg_len++] = crc & 0xFF;
can_send_cmd(id, CAN_PACKET_PROCESS_RX_BUFFER, msg, msg_len);
}
Serial.print("[INFO] vesc: Sent comm command to ID 0x");
Serial.println(id, HEX);
}
// receive a comm command from the Vesc
static void receive(uint8_t id, uint16_t cmd, const uint8_t *payload, size_t len)
{
if(cmd == CAN_PACKET_PROCESS_SHORT_BUFFER)
{
if(len < 3)
{
Serial.println("[WARN] vesc: Received invalid PROCESS_SHORT_BUFFER msg");
return;
}
memcpy(receive_buffer, payload + 2, len - 2);
process_comm(payload[0], len - 2);
}
else if(cmd == CAN_PACKET_FILL_RX_BUFFER)
{
if(len < 2)
{
Serial.println("[WARN] vesc: Received invalid CAN_PACKET_FILL_RX_BUFFER msg");
return;
}
memcpy(receive_buffer + payload[0], payload + 1, len - 1);
}
else if(cmd == CAN_PACKET_FILL_RX_BUFFER_LONG)
{
if(len < 3)
{
Serial.println("[WARN] vesc: Received invalid CAN_PACKET_FILL_RX_BUFFER_LONG msg");
return;
}
Serial.println("[WARN] vesc: CAN_PACKET_FILL_RX_BUFFER_LONG msg not supported");
}
else if(cmd == CAN_PACKET_PROCESS_RX_BUFFER)
{
if(len != 6)
{
Serial.println("[WARN] vesc: Received invalid CAN_PACKET_PROCESS_RX_BUFFER msg");
return;
}
size_t receive_len = (payload[2] << 8) | payload[3];
if(receive_len > 263)
{
Serial.println("[WARN] vesc: Received comm command longer than 263 bytes not supported");
return;
}
uint16_t crc = (payload[4] << 8) | payload[5];
if(crc != crc16(receive_buffer, receive_len))
{
Serial.println("[WARN] vesc: Received comm command with invalid CRC");
return;
}
process_comm(payload[0], receive_len);
}
}
// process a comm command received from the vesc
static void process_comm(uint8_t id, size_t len)
{
if(!is_vesc(id))
{
Serial.print("[WARN] vesc: Received comm command from unknown ID 0x");
Serial.println(id, HEX);
return;
}
if(receive_buffer[0] == COMM_GET_VALUES)
{
//Serial.print("[INFO] vesc: Received COMM_GET_VALUES command from ID 0x");
//Serial.println(id, HEX);
// received reply to get values command
size_t i;
for(i = 0; i < num_vescs; i++)
{
if(vesc_values[i].id == id) break;
}
size_t index = 1;
vesc_values[i].tempFET = buffer_get_float16(receive_buffer, 1e1, &index, len);
vesc_values[i].tempMotor = buffer_get_float16(receive_buffer, 1e1, &index, len);
vesc_values[i].avgMotorCurrent = buffer_get_float32(receive_buffer, 1e2, &index, len);
vesc_values[i].avgInputCurrent = buffer_get_float32(receive_buffer, 1e2, &index, len);
vesc_values[i].avgId = buffer_get_float32(receive_buffer, 1e2, &index, len);
vesc_values[i].avgIq = buffer_get_float32(receive_buffer, 1e2, &index, len);
vesc_values[i].dutyCycle = buffer_get_float16(receive_buffer, 1e3, &index, len);
vesc_values[i].rpm = buffer_get_float32(receive_buffer, 1, &index, len);
vesc_values[i].inputVoltage = buffer_get_float16(receive_buffer, 1e1, &index, len);
vesc_values[i].ampHours = buffer_get_float32(receive_buffer, 1e4, &index, len);
vesc_values[i].ampHoursCharged = buffer_get_float32(receive_buffer, 1e4, &index, len);
vesc_values[i].wattHours = buffer_get_float32(receive_buffer, 1e4, &index, len);
vesc_values[i].wattHoursCharged = buffer_get_float32(receive_buffer, 1e4, &index, len);
vesc_values[i].tachometer = buffer_get_int32(receive_buffer, &index, len);
vesc_values[i].tachometerAbs = buffer_get_int32(receive_buffer, &index, len);
vesc_values[i].fault = (vesc_fault_code_t)buffer_get_uint8(receive_buffer, &index, len);
vesc_values[i].position = buffer_get_float32(receive_buffer, 1e6, &index, len);
index++;
//vesc_values[i].id = buffer_get_uint8(receive_buffer, &index, len);
vesc_values[i].ntc1 = buffer_get_float16(receive_buffer, 1e1, &index, len);
vesc_values[i].ntc2 = buffer_get_float16(receive_buffer, 1e1, &index, len);
vesc_values[i].ntc3 = buffer_get_float16(receive_buffer, 1e1, &index, len);
vesc_values[i].avgVd = buffer_get_float32(receive_buffer, 1e3, &index, len);
vesc_values[i].avgVq = buffer_get_float32(receive_buffer, 1e3, &index, len);
}
else
{
// unknown command
Serial.println("[WARN] vesc: Received unknown comm command from ID 0x");
Serial.println(id, HEX);
}
}