/
VescUart.cpp
358 lines (305 loc) · 9.46 KB
/
VescUart.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
/*
Copyright 2015 - 2017 Andreas Chaitidis Andreas.Chaitidis@gmail.com
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
#include "VescUart.h"
#include "buffer.h"
#include "crc.h"
bool UnpackPayload(uint8_t* message, int lenMes, uint8_t* payload, int lenPa);
bool ProcessReadPacket(uint8_t* message, bldcMeasure& values, int len);
//HardwareSerial *serial; ///@param num as integer with the serial port in use (0=Serial; 1=Serial1; 2=Serial2; 3=Serial3;)
int ReceiveUartMessage(uint8_t* payloadReceived, int num) {
//Messages <= 255 start with 2. 2nd byte is length
//Messages >255 start with 3. 2nd and 3rd byte is length combined with 1st >>8 and then &0xFF
int counter = 0;
int endMessage = 256;
bool messageRead = false;
uint8_t messageReceived[256];
int lenPayload = 0;
HardwareSerial *serial;
serial=&Serial;
#ifdef __AVR_ATmega2560__
switch (num) {
case 0:
serial=&Serial;
break;
case 1:
serial=&Serial1;
break;
case 2:
serial=&Serial2;
break;
case 3:
serial=&Serial3;
break;
default:
break;
}
#endif
while (serial->available()) {
messageReceived[counter++] = serial->read();
if (counter == 2) {//case if state of 'counter' with last read 1
switch (messageReceived[0])
{
case 2:
endMessage = messageReceived[1] + 5; //Payload size + 2 for sice + 3 for SRC and End.
lenPayload = messageReceived[1];
break;
case 3:
//ToDo: Add Message Handling > 255 (starting with 3)
break;
default:
break;
}
}
if (counter >= sizeof(messageReceived))
{
break;
}
if (counter == endMessage && messageReceived[endMessage - 1] == 3) {//+1: Because of counter++ state of 'counter' with last read = "endMessage"
messageReceived[endMessage] = 0;
#ifdef DEBUG
DEBUGSERIAL.println("End of message reached!");
#endif
messageRead = true;
break; //Exit if end of message is reached, even if there is still more data in buffer.
}
}
bool unpacked = false;
if (messageRead) {
unpacked = UnpackPayload(messageReceived, endMessage, payloadReceived, messageReceived[1]);
}
if (unpacked)
{
return lenPayload; //Message was read
}
else {
return 0; //No Message Read
}
}
bool UnpackPayload(uint8_t* message, int lenMes, uint8_t* payload, int lenPay) {
uint16_t crcMessage = 0;
uint16_t crcPayload = 0;
//Rebuild src:
crcMessage = message[lenMes - 3] << 8;
crcMessage &= 0xFF00;
crcMessage += message[lenMes - 2];
#ifdef DEBUG
DEBUGSERIAL.print("SRC received: "); DEBUGSERIAL.println(crcMessage);
#endif // DEBUG
//Extract payload:
memcpy(payload, &message[2], message[1]);
crcPayload = crc16(payload, message[1]);
#ifdef DEBUG
DEBUGSERIAL.print("SRC calc: "); DEBUGSERIAL.println(crcPayload);
#endif
if (crcPayload == crcMessage)
{
#ifdef DEBUG
DEBUGSERIAL.print("Received: "); SerialPrint(message, lenMes); DEBUGSERIAL.println();
DEBUGSERIAL.print("Payload : "); SerialPrint(payload, message[1] - 1); DEBUGSERIAL.println();
#endif // DEBUG
return true;
}
else
{
return false;
}
}
int PackSendPayload(uint8_t* payload, int lenPay, int num) {
uint16_t crcPayload = crc16(payload, lenPay);
int count = 0;
uint8_t messageSend[256];
if (lenPay <= 256)
{
messageSend[count++] = 2;
messageSend[count++] = lenPay;
}
else
{
messageSend[count++] = 3;
messageSend[count++] = (uint8_t)(lenPay >> 8);
messageSend[count++] = (uint8_t)(lenPay & 0xFF);
}
memcpy(&messageSend[count], payload, lenPay);
count += lenPay;
messageSend[count++] = (uint8_t)(crcPayload >> 8);
messageSend[count++] = (uint8_t)(crcPayload & 0xFF);
messageSend[count++] = 3;
messageSend[count] = NULL;
#ifdef DEBUG
DEBUGSERIAL.print("UART package send: "); SerialPrint(messageSend, count);
#endif // DEBUG
HardwareSerial *serial;
#ifdef __AVR_ATmega2560__
switch (num) {
case 0:
serial=&Serial;
break;
case 1:
serial=&Serial1;
break;
case 2:
serial=&Serial2;
break;
case 3:
serial=&Serial3;
break;
default:
break;
}
#endif
//Sending package
serial->write(messageSend, count);
//Returns number of send bytes
return count;
}
bool ProcessReadPacket(uint8_t* message, bldcMeasure& values, int len) {
COMM_PACKET_ID packetId;
int32_t ind = 0;
packetId = (COMM_PACKET_ID)message[0];
message++;//Eliminates the message id
len--;
switch (packetId)
{
case COMM_GET_VALUES:
ind = 14; //Skipped the first 14 bit.
values.avgMotorCurrent = buffer_get_float32(message, 100.0, &ind);
values.avgInputCurrent = buffer_get_float32(message, 100.0, &ind);
values.dutyCycleNow = buffer_get_float16(message, 1000.0, &ind);
values.rpm = buffer_get_int32(message, &ind);
values.inpVoltage = buffer_get_float16(message, 10.0, &ind);
values.ampHours = buffer_get_float32(message, 10000.0, &ind);
values.ampHoursCharged = buffer_get_float32(message, 10000.0, &ind);
ind += 8; //Skip 9 bit
values.tachometer = buffer_get_int32(message, &ind);
values.tachometerAbs = buffer_get_int32(message, &ind);
return true;
break;
default:
return false;
break;
}
}
bool VescUartGetValue(bldcMeasure& values, int num) {
uint8_t command[1] = { COMM_GET_VALUES };
uint8_t payload[256];
PackSendPayload(command, 1, num);
delay(10); //needed, otherwise data is not read
int lenPayload = ReceiveUartMessage(payload, num);
if (lenPayload > 55) {
bool read = ProcessReadPacket(payload, values, lenPayload); //returns true if sucessful
return read;
}
else
{
return false;
}
}
bool VescUartGetValue(bldcMeasure& values) {
return VescUartGetValue(values, 0);
}
void VescUartSetCurrent(float current, int num) {
int32_t index = 0;
uint8_t payload[5];
payload[index++] = COMM_SET_CURRENT ;
buffer_append_int32(payload, (int32_t)(current * 1000), &index);
PackSendPayload(payload, 5, num);
}
void VescUartSetCurrent(float current){
VescUartSetCurrent(current, 0);
}
void VescUartSetPosition(float position, int num) {
int32_t index = 0;
uint8_t payload[5];
payload[index++] = COMM_SET_POS ;
buffer_append_int32(payload, (int32_t)(position * 1000000.0), &index);
PackSendPayload(payload, 5, num);
}
void VescUartSetPosition(float position) {
VescUartSetPosition(position, 0);
}
void VescUartSetDuty(float duty, int num) {
int32_t index = 0;
uint8_t payload[5];
payload[index++] = COMM_SET_DUTY ;
buffer_append_int32(payload, (int32_t)(duty * 100000), &index);
PackSendPayload(payload, 5, num);
}
void VescUartSetDuty(float duty) {
VescUartSetDuty(duty, 0);
}
void VescUartSetRPM(float rpm, int num) {
int32_t index = 0;
uint8_t payload[5];
payload[index++] = COMM_SET_RPM ;
buffer_append_int32(payload, (int32_t)(rpm), &index);
PackSendPayload(payload, 5, num);
}
void VescUartSetRPM(float rpm) {
VescUartSetRPM(rpm, 0);
}
void VescUartSetCurrentBrake(float brakeCurrent, int num) {
int32_t index = 0;
uint8_t payload[5];
payload[index++] = COMM_SET_CURRENT_BRAKE;
buffer_append_int32(payload, (int32_t)(brakeCurrent * 1000), &index);
PackSendPayload(payload, 5, num);
}
void VescUartSetCurrentBrake(float brakeCurrent) {
VescUartSetCurrentBrake(brakeCurrent, 0);
}
void VescUartSetNunchukValues(remotePackage& data, int num) {
int32_t ind = 0;
uint8_t payload[11];
payload[ind++] = COMM_SET_CHUCK_DATA;
payload[ind++] = data.valXJoy;
payload[ind++] = data.valYJoy;
buffer_append_bool(payload, data.valLowerButton, &ind);
buffer_append_bool(payload, data.valUpperButton, &ind);
//Acceleration Data. Not used, Int16 (2 byte)
payload[ind++] = 0;
payload[ind++] = 0;
payload[ind++] = 0;
payload[ind++] = 0;
payload[ind++] = 0;
payload[ind++] = 0;
#ifdef DEBUG
DEBUGSERIAL.println("Data reached at VescUartSetNunchuckValues:");
DEBUGSERIAL.print("valXJoy = "); DEBUGSERIAL.print(data.valXJoy); DEBUGSERIAL.print(" valYJoy = "); DEBUGSERIAL.println(data.valYJoy);
DEBUGSERIAL.print("LowerButton = "); DEBUGSERIAL.print(data.valLowerButton); DEBUGSERIAL.print(" UpperButton = "); DEBUGSERIAL.println(data.valUpperButton);
#endif
PackSendPayload(payload, 11, num);
}
void VescUartSetNunchukValues(remotePackage& data) {
VescUartSetNunchukValues(data, 0);
}
void SerialPrint(uint8_t* data, int len) {
// DEBUGSERIAL.print("Data to display: "); DEBUGSERIAL.println(sizeof(data));
for (int i = 0; i <= len; i++)
{
DEBUGSERIAL.print(data[i]);
DEBUGSERIAL.print(" ");
}
DEBUGSERIAL.println("");
}
void SerialPrint(const bldcMeasure& values) {
DEBUGSERIAL.print("avgMotorCurrent: "); DEBUGSERIAL.println(values.avgMotorCurrent);
DEBUGSERIAL.print("avgInputCurrent: "); DEBUGSERIAL.println(values.avgInputCurrent);
DEBUGSERIAL.print("dutyCycleNow: "); DEBUGSERIAL.println(values.dutyCycleNow);
DEBUGSERIAL.print("rpm: "); DEBUGSERIAL.println(values.rpm);
DEBUGSERIAL.print("inputVoltage: "); DEBUGSERIAL.println(values.inpVoltage);
DEBUGSERIAL.print("ampHours: "); DEBUGSERIAL.println(values.ampHours);
DEBUGSERIAL.print("ampHoursCharges: "); DEBUGSERIAL.println(values.ampHoursCharged);
DEBUGSERIAL.print("tachometer: "); DEBUGSERIAL.println(values.tachometer);
DEBUGSERIAL.print("tachometerAbs: "); DEBUGSERIAL.println(values.tachometerAbs);
}