-
Notifications
You must be signed in to change notification settings - Fork 23
/
TaskMain.c
472 lines (385 loc) · 12.6 KB
/
TaskMain.c
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// ###########################################################################
//
// Copyright (C) 2015 GenId SPRL
//
// 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/>.
///
/// \file TaskMAIN.c
/// \author Jean-Sebastien Gonsette
/// \date 20/10/2014
///
/// This is the main application task for housekeeping purpose.
/// Its role is to send the telemetries and to proceed to the device calibration.
// ###########################################################################
#include "BSP.h"
#include "Config.h"
#include "Global.h"
#include "./Tasks/Controller.h"
#include "./Tools/Message.h"
#include "./Tools/PRNG.h"
#include "./Devices/ADXL345.h"
#include "./Devices/ISZ2510.h"
#include "./Tasks/TaskLED.h"
#include "./Tasks/TaskUartTx.h"
#include "./Tasks/TaskMain.h"
#include "./Interfaces/Motor.h"
#include "./Interfaces/Battery.h"
// ===========================================================================
// T Y P E S
// ===========================================================================
// ===========================================================================
// P R I V A T E
// ===========================================================================
/// Application mode
static TMAIN_Mode gs_mode;
/// Telemetry speed (0: stopped)
static int gs_tm;
/// Telemetry counter
static int gs_tmCounter;
/// Config need to be saved
static Bool gs_saveConfig;
/// Telemetries raw buffer
static Byte gs_rawBuffer [200];
/// Circular message buffer for telemetries
static MSG_Buffer gs_tmBuffer;
// ===========================================================================
// P R I V A T E P R O T O
// ===========================================================================
/// \name Internal functions
//@{
static Word InitApplication ();
static void InitPnrg ();
static void SaveConfig ();
static void SendTelemetry (Dword cycleCounter);
static int TestMotor ();
static int SetupAccelerometer ();
static int SetupGyroscope ();
//@}
// ###########################################################################
//
// P U B L I C
//
// ###########################################################################
// ===========================================================================
// <TMAIN_EntryPoint>
///
/// \brief Main application task entry point
///
// ===========================================================================
void TMAIN_EntryPoint (void* param)
{
Word error;
Dword ms, msNext = 0;
Dword cycleCount;
// Init application
error = InitApplication ();
// Show critical error (RED pulses the error number)
if (error != 0)
{
TLED_Off (LED_GREEN);
TLED_Off (LED_YELLOW);
TLED_Pulse (LED_RED, error);
return;
}
//TMAIN_SetMode (MODE_Free);
// Main loop
while (1)
{
// Wait next controller cycle
if (CTRL_WaitCycle (DELAY_MS (500), &cycleCount))
{
// Send telemetries if needed
if (gs_tm > 0) SendTelemetry (cycleCount);
// Save config if needed
if (gs_saveConfig) SaveConfig ();
}
// Check battery level every minute
ms = GetMsCount ();
if (ms >= msNext)
{
msNext = ms + 60000;
if (BAT_GetVoltage () < BAT_MIN_VOLTAGE) break;
}
}
// No more battery. Stop the pendulum
SuspendTask (g_taskUartTx);
SuspendTask (g_taskUartRx);
CTRL_SetMode (CTRL_Stop);
// Warning
TLED_Off (LED_GREEN);
TLED_Off (LED_YELLOW);
TLED_Pulse (LED_RED, 1);
SleepTask (DELAY_S (10));
// Shut down everything to minimize consumption
/// \todo
}
// ===========================================================================
// <TMAIN_CalibrateBattery>
///
/// \brief Calibrate the battery with a new reference voltage
///
/// \param[in] refVoltage Reference voltage measurement [1/100 V]
// ===========================================================================
void TMAIN_CalibrateBattery (Word refVoltage)
{
BAT_Calibrate (refVoltage);
gs_saveConfig = True;
}
// ===========================================================================
// <TMAIN_CalibrateIMU>
///
/// \brief Calibrate the IMU with a new bias and scale factors correction
///
/// \param[in] accScales Measured acc scale factorsbias (x, y) [1/1000]
/// \param[in] accBias Measured acc bias (x, y, z) [µG]
/// \param[in] gyroBias Measured gyro bias [1/1000 °/s]
// ===========================================================================
void TMAIN_CalibrateIMU (Long accScales [2], Long accBias [3], Long gyroBias)
{
ISZ2510_SetCalibrationOffset (g_ISZ2510, gyroBias);
ADXL345_SetCalibrationOffset (g_ADXL345, 0, (Short) (accBias [0] / 1000));
ADXL345_SetCalibrationOffset (g_ADXL345, 1, (Short) (accBias [1] / 1000));
ADXL345_SetCalibrationOffset (g_ADXL345, 2, (Short) (accBias [2] / 1000));
ADXL345_SetCalibrationScaler (g_ADXL345, 0, accScales [0] / 1000.0f);
ADXL345_SetCalibrationScaler (g_ADXL345, 1, accScales [1] / 1000.0f);
gs_saveConfig = True;
}
// ===========================================================================
// <TMAIN_SetTelemetry>
///
/// \brief Enable or disable telemetry uploading
///
/// \param[in] s 0: No telemetry
/// n: Telemetry sent 1 cycle out of n
// ===========================================================================
void TMAIN_SetTelemetry (int n)
{
EnterCritical ();
{
gs_tm = n;
gs_tmCounter = 0;
// Configure the controller if needed: it cannot be stopped
// if we have to send the telemetries
if (gs_mode == MODE_Stop)
{
if (gs_tm > 0) CTRL_SetMode (CTRL_AcqOnly);
else CTRL_SetMode (CTRL_Stop);
}
}
LeaveCritical ();
}
// ===========================================================================
// <TMAIN_Mode>
///
/// \brief Set the application working mode (stopped, free, equilibrium)
///
/// \param[in] mode Application mode
// ===========================================================================
void TMAIN_SetMode (TMAIN_Mode mode)
{
TLED_Off (LED_GREEN);
TLED_Off (LED_RED);
TLED_Off (LED_YELLOW);
EnterCritical ();
{
gs_mode = mode;
// Configure the controller accordingly
switch (mode)
{
default:
case MODE_Stop:
if (gs_tm == 0) CTRL_SetMode (CTRL_Stop);
else CTRL_SetMode (CTRL_AcqOnly);
break;
case MODE_Free:
CTRL_SetMode (CTRL_Free);
TLED_On (LED_GREEN);
break;
case MODE_Control:
CTRL_SetMode (CTRL_Control);
break;
}
}
LeaveCritical ();
}
// ###########################################################################
//
// P R I V A T E
//
// ###########################################################################
// ===========================================================================
// <InitApplication>
///
/// \brief Initialize the whole application
///
/// \return 0 in case of success
// ===========================================================================
static Word InitApplication ()
{
Word error = 0;
// Basic tests and setup
if (SetupGyroscope () != 0) error = 4;
else if (SetupAccelerometer () != 0) error = 3;
else if (TestMotor () != 0) error = 2;
if (error != 0) return error;
// Restore application settings, if any
CFG_Restore ();
// Init our buffer to send the telemetries
MSG_InitBuffer (&gs_tmBuffer, gs_rawBuffer, sizeof (gs_rawBuffer));
// Everything stopped
gs_tm = 0;
gs_saveConfig = False;
// Initialize the Pseudo Random Number Generator
InitPnrg ();
// Start the controller
TMAIN_SetMode (MODE_Control);
// OK, we release the other tasks
ResumeTask (g_taskUartTx);
ResumeTask (g_taskUartRx);
return 0;
}
// ===========================================================================
// <InitPnrg>
///
/// \brief Initialize the random number generator thanks to the sensors noise
// ===========================================================================
static void InitPnrg ()
{
Short acc [3];
// Seed the generator with random data
ADXL345_GetAccelerations (g_ADXL345, acc, acc+1, acc+2);
PRNG_Seed ((Byte*) acc, sizeof (acc));
}
// ===========================================================================
// <SaveConfig>
///
/// \brief Save the current configuration in EEPROM
// ===========================================================================
static void SaveConfig ()
{
gs_saveConfig = False;
CFG_Save ();
}
// ===========================================================================
// <SendTelemetry>
///
/// \brief Collect and push all the telemetries through the UART
///
/// \param cycleCounter Value of the control process cycle counter
// ===========================================================================
static void SendTelemetry (Dword cycleCounter)
{
CTRL_Sensors sensors;
CTRL_State state;
CTRL_State targetState;
CTRL_Command command;
Dword timestamp;
// Telemetries are sent 1 time out of n
if (++gs_tmCounter >= gs_tm) gs_tmCounter = 0;
if (gs_tmCounter != 0) return;
// Get a copy of the last sensor values and pendulum state estimation
CTRL_GetPendulumSensors (&sensors);
CTRL_GetPendulumState (&state);
CTRL_GetPendulumTargetState (&targetState);
CTRL_GetPendulumCommand (&command);
timestamp = GetMsCount ();
// Start of telemetries
MSG_StartTM (&gs_tmBuffer, "TM.Start");
MSG_AddParamLong (&gs_tmBuffer, cycleCounter);
MSG_AddParamLong (&gs_tmBuffer, timestamp);
MSG_FinishAndSend (&gs_tmBuffer);
// Send sensors
MSG_StartTM (&gs_tmBuffer, "SENSORS");
MSG_AddParamLong (&gs_tmBuffer, sensors.bldcSpeed);
MSG_AddParamLong (&gs_tmBuffer, sensors.gyroZ);
MSG_AddParamLong (&gs_tmBuffer, sensors.accXYZ [0]);
MSG_AddParamLong (&gs_tmBuffer, sensors.accXYZ [1]);
MSG_AddParamLong (&gs_tmBuffer, sensors.accXYZ [2]);
MSG_FinishAndSend (&gs_tmBuffer);
// Send state
MSG_StartTM (&gs_tmBuffer, "STATE");
MSG_AddParamLong (&gs_tmBuffer, state.bodyAngle);
MSG_AddParamLong (&gs_tmBuffer, state.bodySpeed);
MSG_AddParamLong (&gs_tmBuffer, state.wheelSpeed);
MSG_AddParamLong (&gs_tmBuffer, targetState.bodyAngle);
MSG_AddParamLong (&gs_tmBuffer, targetState.bodySpeed);
MSG_AddParamLong (&gs_tmBuffer, targetState.wheelSpeed);
MSG_FinishAndSend (&gs_tmBuffer);
// Send motor command
MSG_StartTM (&gs_tmBuffer, "CTRL");
MSG_AddParamLong (&gs_tmBuffer, command.brake? 1:0);
MSG_AddParamLong (&gs_tmBuffer, command.torque);
MSG_FinishAndSend (&gs_tmBuffer);
// End of telemetries
MSG_StartTM (&gs_tmBuffer, "TM.End");
MSG_FinishAndSend (&gs_tmBuffer);
}
// ===========================================================================
// <TestMotor>
///
/// \brief Test if we can drive the motor and get feedback
///
/// \return 0 if successful
// ===========================================================================
static int TestMotor ()
{
Word speed0, speed1;
// Apply a small torque (5%) on the motor
MOTOR_RawCommand (MOTOR_Forward, 50);
// Wait in order to get some speed
// Measure speed
SleepTask (DELAY_MS (500));
speed0 = BSP_BLDCGetSpeed (NULL);
// Brake, wait and check motor is stopped
MOTOR_RawCommand (MOTOR_Disabled, 0);
SleepTask (DELAY_MS (1500));
speed1 = BSP_BLDCGetSpeed (NULL);
if (speed0 < 300 || speed1 != 0) return -1;
return 0;
}
// ===========================================================================
// <SetupAccelerometer>
///
/// \brief Get access to the ADXL345 accelerometers and perform initialization
///
/// \return 0 if successful
// ===========================================================================
static int SetupAccelerometer ()
{
// Basic setup of the device
if (ADXL345_Init (g_ADXL345) != 0) return -1;
// Put the device in normal measurement mode @ 100 samples/s
ADXL345_SetBaudrate (g_ADXL345, ADXL345_BR_100);
ADXL345_SetLowPowerMode (g_ADXL345, 0);
ADXL345_SetMode (g_ADXL345, ADXL345_MODE_MEASURE);
return 0;
}
// ===========================================================================
// <SetupGyroscope>
///
/// \brief Get access to the ISZ2510 Gyro and perform initialization
///
/// \return 0 if successful
// ===========================================================================
static int SetupGyroscope ()
{
Word d;
// Basic setup of the device
if (ISZ2510_Init (g_ISZ2510) != 0) return -1;
// Configure bandwidth and sample rate
ISZ2510_SetBandwidth (g_ISZ2510, ISZ2510_BW_41);
d = ISZ2510_SetBaudrate (g_ISZ2510, 50);
return 0;
}
// End