-
Notifications
You must be signed in to change notification settings - Fork 16
/
teensy_hoverboard.ino
294 lines (255 loc) · 7.97 KB
/
teensy_hoverboard.ino
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
/******************************************************************************
* @file hoverboard sketch
* @author Rémi Pincent - INRIA
* @date 28/11/2016
*
* @brief Hoverboard sketch example to control hoverboard.
*
* Project : hoverbot
* Contact: Rémi Pincent - remi.pincent@inria.fr
*
* Revision History:
* https://github.com/OpHaCo/hoverbot.git
*
* LICENSE :
* hoverbot (c) by Rémi Pincent
* project_name is licensed under a
* Creative Commons Attribution-NonCommercial 3.0 Unported License.
*
* You should have received a copy of the license along with this
* work. If not, see <http://creativecommons.org/licenses/by-nc/3.0/>.
*****************************************************************************/
/**************************************************************************
* Include Files
**************************************************************************/
#include <logger_config.h>
#include <hoverboard.h>
#include <brushless_hall_sensor.h>
/**************************************************************************
* Manifest Constants
**************************************************************************/
#define LOG_SPEED 500000
#define LOG_SERIAL Serial
#define MAX_CMD_LENGTH 0xFFU
#define SET_SPEED_CMD_LENGTH 13U
#define POWER_ON_CMD_LENGTH 1U
#define POWER_OFF_CMD_LENGTH 1U
#define STOP_CMD_LENGTH 1U
/**************************************************************************
* Type Definitions
**************************************************************************/
/*****************************************
* CONTROL cmd format
* __________________________________
* | | | |
* |CMD_ID | CMD_PARAMS | TODO CRC |
* |_______|_____________|____________|
*
****************************************/
typedef enum
{
SET_SPEED = 0, /* ARGS : SPEED1 (short LE) - SPEED2 (short LE) */
POWER_ON = 1,
POWER_OFF = 2,
STOP = 3
}EControlCmd;
typedef enum
{
ACK = 0,
SPEED_STATUS = 1
}EFeedbackCmds;
typedef enum
{
ACK_OK = 0,
ACK_BAD_STATE = 1
}ECmdAckType;
/**
* Class that listen hoverboard commands over UART to control it.
*/
class HoverboardListenerExample : public HoverboardListener
{
public:
HoverboardListenerExample(Hoverboard* );
void init(void);
void calibrateHoverboard(void);
void onSpeedChanged(float arg_f_motor1Speed, float arg_f_motor2Speed);
void onSpeedApplied(void);
void onPowerOn(void);
void onPowerOff(void);
void pollCmd(void);
void sendCmd(EFeedbackCmds arg_e_cmdType, uint8_t arg_au8_cmd[], uint8_t arg_u8_cmdLength);
private:
void waitKey(uint8_t arg_u8_key);
void handleCommand(uint8_t arg_u8_cmd[], uint16_t arg_u16_cmdLength);
private:
Hoverboard* _p_hoverboard;
};
/**************************************************************************
* Static variables definitions
**************************************************************************/
static uint8_t _au8_rcvCmd[MAX_CMD_LENGTH] = {0};
/** ItCBS will be filled by hoverboard */
static BrushlessHallSensor::ItCbs motor1ItCbs;
/** Hall sensor 1 - Give pin here */
static BrushlessHallSensor::Config _hallSensor1Conf(17, 18, 19, motor1ItCbs);
/** ItCBS will be filled by hoverboard */
static BrushlessHallSensor::ItCbs motor2ItCbs;
/** Hall sensor 2 - Give pin here */
static BrushlessHallSensor::Config _hallSensor2Conf(13, 14, 15, motor2ItCbs);
static Hoverboard::Config _hoverboardConf(20,
21,
&_hallSensor1Conf,
&_hallSensor2Conf,
Serial1,
Serial3);
static Hoverboard _hoverboard(_hoverboardConf);
static HoverboardListenerExample _hoverboardListenerExample(&_hoverboard);
/**************************************************************************
* Global functions Definitions
**************************************************************************/
void setup() {
LOG_SERIAL.begin(500000);
LOG_INIT_STREAM(LOG_LEVEL, &LOG_SERIAL);
/** Wait 1s to get setup logs */
delay(2000);
LOG_INFO_LN("starting hoverboard hack...");
_hoverboard.init();
_hoverboardListenerExample.init();
}
void loop() {
_hoverboard.idleControl();
_hoverboardListenerExample.pollCmd();
}
/**************************************************************************
* HoverboardListenerExample Definitions
**************************************************************************/
HoverboardListenerExample::HoverboardListenerExample(Hoverboard* arg_p_hoverboardExample)
{
_p_hoverboard = arg_p_hoverboardExample;
}
void HoverboardListenerExample::init(void)
{
_p_hoverboard->registerListener(this);
}
void HoverboardListenerExample::onSpeedChanged(float arg_f_motor1Speed, float arg_f_motor2Speed)
{
LOG_INFO_LN("New speed (%f, %f)", arg_f_motor1Speed, arg_f_motor2Speed);
}
void HoverboardListenerExample::onSpeedApplied(void)
{
LOG_INFO_LN("SPEED_APPLIED");
}
void HoverboardListenerExample::onPowerOn(void)
{
LOG_INFO_LN("POWERED_ON");
}
void HoverboardListenerExample::onPowerOff(void)
{
LOG_INFO_LN("POWERED_OFF");
}
void HoverboardListenerExample::waitKey(uint8_t arg_u8_key)
{
while(1)
{
_p_hoverboard->idleControl();
if(LOG_SERIAL.available() && LOG_SERIAL.read() == arg_u8_key)break;
}
}
/**
* Manual calibration : user must refer instructions on UART
*/
void HoverboardListenerExample::calibrateHoverboard(void)
{
LOG_INFO_LN("Hoverboard calibration");
LOG_INFO_LN("Power off hoverboard - press enter when done");
waitKey('\r');
LOG_INFO_LN("Power on hoverboard - press enter when done");
waitKey('\r');
LOG_INFO_LN("Press hoverboard button during a time given in hoverboard manual - press enter when done");
waitKey('\r');
LOG_INFO_LN("Calibrating...");
/** 5s calibration */
uint32_t loc_u32_time = millis();
while(millis() - loc_u32_time < 7000)
{
_p_hoverboard->idleControl();
}
LOG_INFO_LN("Calibration done - press hoverboard button during a time given in hoverboard manual to exit calibration - press enter when done");
waitKey('\r');
}
void HoverboardListenerExample::pollCmd(void)
{
uint16_t loc_u16_cmdLength = 0;
while(LOG_SERIAL.available())
{
if(loc_u16_cmdLength > MAX_CMD_LENGTH)
{
LOG_ERROR("Invalid command length");
return;
}
_au8_rcvCmd[loc_u16_cmdLength++] = LOG_SERIAL.read();
}
if(loc_u16_cmdLength > 0)
{
handleCommand(_au8_rcvCmd, loc_u16_cmdLength);
}
}
void HoverboardListenerExample::sendCmd(EFeedbackCmds arg_e_cmdType, uint8_t arg_au8_cmd[], uint8_t arg_u8_cmdLength)
{
//TODO
}
void HoverboardListenerExample::handleCommand(uint8_t arg_u8_cmd[], uint16_t arg_u16_cmdLength)
{
if(arg_u8_cmd[0] == POWER_ON && arg_u16_cmdLength == POWER_ON_CMD_LENGTH)
{
LOG_INFO_LN("POWER_ON");
_p_hoverboard->powerOn();
}
else if(arg_u8_cmd[0] == POWER_OFF && arg_u16_cmdLength == POWER_OFF_CMD_LENGTH)
{
LOG_INFO_LN("POWER_OFF");
_p_hoverboard->powerOff();
}
else if(arg_u8_cmd[0] == SET_SPEED && arg_u16_cmdLength == SET_SPEED_CMD_LENGTH)
{
/** Data in little endian */
float _speed1, _speed2;
uint32_t _rampUpDur;
uint32_t temp_val = arg_u8_cmd[1] |
arg_u8_cmd[2] << 8 |
arg_u8_cmd[3] << 16 |
arg_u8_cmd[4] << 24;
_speed1 = *(float*)(&temp_val);
temp_val = arg_u8_cmd[5] |
arg_u8_cmd[6] << 8 |
arg_u8_cmd[7] << 16 |
arg_u8_cmd[8] << 24;
_speed2 = *(float*)(&temp_val);
_rampUpDur = arg_u8_cmd[9] |
arg_u8_cmd[10] << 8 |
arg_u8_cmd[11] << 16 |
arg_u8_cmd[12] << 24;
if(_rampUpDur <= 0)
{
LOG_ERROR("Invalid ramp up duration given");
}
else
{
LOG_INFO_LN("SET_SPEED TO (%f, %f) in %d ms",
_speed1,
_speed2,
_rampUpDur);
_p_hoverboard->setSpeedAsync(_speed1, _speed2, _rampUpDur);
}
}
else if(arg_u8_cmd[0] == STOP && arg_u16_cmdLength == STOP_CMD_LENGTH)
{
LOG_INFO_LN("STOP TODO");
}
else
{
LOG_INFO_LN("Command %d of length %d not handled",
arg_u8_cmd[0],
arg_u16_cmdLength);
}
}