forked from R0b0shack/VESC-UART-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvesc_uart.cpp
194 lines (159 loc) · 5.75 KB
/
vesc_uart.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
/*
Copyright 2015 - 2017 Andreas Chaitidis [email protected]
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 "vesc_uart.h"
#include "buffer.h"
#include "crc.h"
#include "datatypes.h"
#include "config.h"
int process_received_msg(uint8_t *payloadReceived) {
//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;
while (SERIALIO.available()) {
messageReceived[counter++] = SERIALIO.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;
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 = unpack_payload(messageReceived, endMessage, payloadReceived, messageReceived[1]);
}
if (unpacked)
{
return lenPayload; //Message was read
}
else {
return 0; //No Message Read
}
}
bool unpack_payload(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];
//Extract payload:
memcpy(payload, &message[2], message[1]);
crcPayload = crc16(payload, message[1]);
if (crcPayload == crcMessage)
{
return true;
}
else
{
return false;
}
}
int send_payload(uint8_t* payload, int lenPay) {
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;
//Sending package
SERIALIO.write(messageSend, count);
//Returns number of send bytes
return count;
}
bool process_read_package(uint8_t* message, mc_values& 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 = 0;
values.temp_mos1 = buffer_get_float16(message, 10.0, &ind);
values.temp_mos2 = buffer_get_float16(message, 10.0, &ind);
values.temp_mos3 = buffer_get_float16(message, 10.0, &ind);
values.temp_mos4 = buffer_get_float16(message, 10.0, &ind);
values.temp_mos5 = buffer_get_float16(message, 10.0, &ind);
values.temp_mos6 = buffer_get_float16(message, 10.0, &ind);
values.temp_pcb = buffer_get_float16(message, 10.0, &ind);
values.current_motor = buffer_get_float32(message, 100.0, &ind);
values.current_in = buffer_get_float32(message, 100.0, &ind);
values.duty_now = buffer_get_float16(message, 1000.0, &ind);
values.rpm = buffer_get_int32(message, &ind);
values.v_in = buffer_get_float16(message, 10.0, &ind);
values.amp_hours = buffer_get_float32(message, 10000.0, &ind);
values.amp_hours_charged = buffer_get_float32(message, 10000.0, &ind);
ind += 8; //Skip 9 bit
values.tachometer = buffer_get_int32(message, &ind);
values.tachometer_abs = buffer_get_int32(message, &ind);
values.fault_code = (mc_fault_code)message[ind++];
return true;
break;
default:
return false;
break;
}
}
bool vesc_get_values(mc_values& values) {
uint8_t command[1] = { COMM_GET_VALUES };
uint8_t payload[256];
send_payload(command, 1);
//delay(15); //needed, otherwise data is not read
int lenPayload = process_received_msg(payload);
if (lenPayload > 0) {
bool read = process_read_package(payload, values, lenPayload); //returns true if sucessfull
return read;
}
else
{
return false;
}
}