/
motors.c
448 lines (385 loc) · 13.4 KB
/
motors.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
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2012 Bitcraze AB
*
* 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, in version 3.
*
* 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/>.
*
* motors.c - Motor driver
*
* This code mainly interfacing the PWM peripheral lib of ST.
*/
#define DEBUG_MODULE "MTR-DRV"
#include <stdbool.h>
/* ST includes */
#include "stm32fxxx.h"
#include "motors.h"
#include "pm.h"
#include "debug.h"
//FreeRTOS includes
#include "task.h"
//Logging includes
#include "log.h"
static uint16_t motorsBLConvBitsTo16(uint16_t bits);
static uint16_t motorsBLConv16ToBits(uint16_t bits);
static uint16_t motorsConvBitsTo16(uint16_t bits);
static uint16_t motorsConv16ToBits(uint16_t bits);
uint32_t motor_ratios[] = {0, 0, 0, 0};
void motorsPlayTone(uint16_t frequency, uint16_t duration_msec);
void motorsPlayMelody(uint16_t *notes);
void motorsBeep(int id, bool enable, uint16_t frequency, uint16_t ratio);
#include "motors_def_cf2.c"
const MotorPerifDef** motorMap; /* Current map configuration */
const uint32_t MOTORS[] = { MOTOR_M1, MOTOR_M2, MOTOR_M3, MOTOR_M4 };
const uint16_t testsound[NBR_OF_MOTORS] = {A4, A5, F5, D5 };
const MotorHealthTestDef brushedMotorHealthTestSettings = {
/* onPeriodMsec = */ 50,
/* offPeriodMsec = */ 950,
/* varianceMeasurementStartMsec = */ 0,
/* onPeriodPWMRatio = */ 0xFFFF,
};
const MotorHealthTestDef brushlessMotorHealthTestSettings = {
/* onPeriodMsec = */ 2000,
/* offPeriodMsec = */ 1000,
/* varianceMeasurementStartMsec = */ 1000,
/* onPeriodPWMRatio = */ 0 /* user must set health.propTestPWMRatio explicitly */
};
const MotorHealthTestDef unknownMotorHealthTestSettings = {
/* onPeriodMsec = */ 0,
/* offPeriodMseec = */ 0,
/* varianceMeasurementStartMsec = */ 0,
/* onPeriodPWMRatio = */ 0
};
static bool isInit = false;
/* Private functions */
static uint16_t motorsBLConvBitsTo16(uint16_t bits)
{
return (0xFFFF * (bits - MOTORS_BL_PWM_CNT_FOR_HIGH) / MOTORS_BL_PWM_CNT_FOR_HIGH);
}
static uint16_t motorsBLConv16ToBits(uint16_t bits)
{
return (MOTORS_BL_PWM_CNT_FOR_HIGH + ((bits * MOTORS_BL_PWM_CNT_FOR_HIGH) / 0xFFFF));
}
static uint16_t motorsConvBitsTo16(uint16_t bits)
{
return ((bits) << (16 - MOTORS_PWM_BITS));
}
static uint16_t motorsConv16ToBits(uint16_t bits)
{
return ((bits) >> (16 - MOTORS_PWM_BITS) & ((1 << MOTORS_PWM_BITS) - 1));
}
// We have data that maps PWM to thrust at different supply voltage levels.
// However, it is not the PWM that drives the motors but the voltage and
// amps (= power). With the PWM it is possible to simulate different
// voltage levels. The assumption is that the voltage used will be an
// procentage of the supply voltage, we assume that 50% PWM will result in
// 50% voltage.
//
// Thrust (g) Supply Voltage PWM (%) Voltage needed
// 0.0 4.01 0 0
// 1.6 3.98 6.25 0.24875
// 4.8 3.95 12.25 0.49375
// 7.9 3.82 18.75 0.735
// 10.9 3.88 25 0.97
// 13.9 3.84 31.25 1.2
// 17.3 3.80 37.5 1.425
// 21.0 3.76 43.25 1.6262
// 24.4 3.71 50 1.855
// 28.6 3.67 56.25 2.06438
// 32.8 3.65 62.5 2.28125
// 37.3 3.62 68.75 2.48875
// 41.7 3.56 75 2.67
// 46.0 3.48 81.25 2.8275
// 51.9 3.40 87.5 2.975
// 57.9 3.30 93.75 3.09375
//
// To get Voltage needed from wanted thrust we can get the quadratic
// polyfit coefficients using GNU octave:
//
// thrust = [0.0 1.6 4.8 7.9 10.9 13.9 17.3 21.0 ...
// 24.4 28.6 32.8 37.3 41.7 46.0 51.9 57.9]
//
// volts = [0.0 0.24875 0.49375 0.735 0.97 1.2 1.425 1.6262 1.855 ...
// 2.064375 2.28125 2.48875 2.67 2.8275 2.975 3.09375]
//
// p = polyfit(thrust, volts, 2)
//
// => p = -0.00062390 0.08835522 0.06865956
//
// We will not use the contant term, since we want zero thrust to equal
// zero PWM.
//
// And to get the PWM as a percentage we would need to divide the
// Voltage needed with the Supply voltage.
static uint16_t motorsCompensateBatteryVoltage(uint16_t ithrust)
{
float supply_voltage = pmGetBatteryVoltage();
/*
* A LiPo battery is supposed to be 4.2V charged, 3.7V mid-charge and 3V
* discharged.
*
* A suiteble sanity check for disabiling the voltage compensation would be
* under 2V. That would suggest a damaged battery. This protects against
* rushing the motors on bugs and invalid voltage levels.
*/
if (supply_voltage < 2.0f)
{
return ithrust;
}
float thrust = ((float) ithrust / 65536.0f) * 60;
float volts = -0.0006239f * thrust * thrust + 0.088f * thrust;
float percentage = volts / supply_voltage;
percentage = percentage > 1.0f ? 1.0f : percentage;
return percentage * UINT16_MAX;
}
/* Public functions */
//Initialization. Will set all motors ratio to 0%
void motorsInit(const MotorPerifDef** motorMapSelect)
{
int i;
//Init structures
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
if (isInit)
{
// First to init will configure it
return;
}
motorMap = motorMapSelect;
DEBUG_PRINT("Using %s motor driver\n", motorMap[0]->drvType == BRUSHED ? "brushed" : "brushless");
for (i = 0; i < NBR_OF_MOTORS; i++)
{
//Clock the gpio and the timers
MOTORS_RCC_GPIO_CMD(motorMap[i]->gpioPerif, ENABLE);
MOTORS_RCC_GPIO_CMD(motorMap[i]->gpioPowerswitchPerif, ENABLE);
MOTORS_RCC_TIM_CMD(motorMap[i]->timPerif, ENABLE);
// If there is a power switch, as on Bolt, enable power to ESC by
// switching on mosfet.
if (motorMap[i]->gpioPowerswitchPin != 0)
{
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = motorMap[i]->gpioPowerswitchPin;
GPIO_Init(motorMap[i]->gpioPowerswitchPort, &GPIO_InitStructure);
GPIO_WriteBit(motorMap[i]->gpioPowerswitchPort, motorMap[i]->gpioPowerswitchPin, 1);
}
// Configure the GPIO for the timer output
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = MOTORS_GPIO_MODE;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = motorMap[i]->gpioOType;
GPIO_InitStructure.GPIO_Pin = motorMap[i]->gpioPin;
GPIO_Init(motorMap[i]->gpioPort, &GPIO_InitStructure);
//Map timers to alternate functions
MOTORS_GPIO_AF_CFG(motorMap[i]->gpioPort, motorMap[i]->gpioPinSource, motorMap[i]->gpioAF);
//Timer configuration
TIM_TimeBaseStructure.TIM_Period = motorMap[i]->timPeriod;
TIM_TimeBaseStructure.TIM_Prescaler = motorMap[i]->timPrescaler;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(motorMap[i]->tim, &TIM_TimeBaseStructure);
// PWM channels configuration (All identical!)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = motorMap[i]->timPolarity;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// Configure Output Compare for PWM
motorMap[i]->ocInit(motorMap[i]->tim, &TIM_OCInitStructure);
motorMap[i]->preloadConfig(motorMap[i]->tim, TIM_OCPreload_Enable);
}
// Start the timers
for (i = 0; i < NBR_OF_MOTORS; i++)
{
TIM_Cmd(motorMap[i]->tim, ENABLE);
}
isInit = true;
// Output zero power
motorsSetRatio(MOTOR_M1, 0);
motorsSetRatio(MOTOR_M2, 0);
motorsSetRatio(MOTOR_M3, 0);
motorsSetRatio(MOTOR_M4, 0);
}
void motorsDeInit(const MotorPerifDef** motorMapSelect)
{
int i;
GPIO_InitTypeDef GPIO_InitStructure;
for (i = 0; i < NBR_OF_MOTORS; i++)
{
// Configure default
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = motorMap[i]->gpioPin;
GPIO_Init(motorMap[i]->gpioPort, &GPIO_InitStructure);
//Map timers to alternate functions
GPIO_PinAFConfig(motorMap[i]->gpioPort, motorMap[i]->gpioPinSource, 0x00);
//Deinit timer
TIM_DeInit(motorMap[i]->tim);
}
}
bool motorsTest(void)
{
int i;
for (i = 0; i < sizeof(MOTORS) / sizeof(*MOTORS); i++)
{
if (motorMap[i]->drvType == BRUSHED)
{
#ifdef ACTIVATE_STARTUP_SOUND
motorsBeep(MOTORS[i], true, testsound[i], (uint16_t)(MOTORS_TIM_BEEP_CLK_FREQ / A4)/ 20);
vTaskDelay(M2T(MOTORS_TEST_ON_TIME_MS));
motorsBeep(MOTORS[i], false, 0, 0);
vTaskDelay(M2T(MOTORS_TEST_DELAY_TIME_MS));
#else
motorsSetRatio(MOTORS[i], MOTORS_TEST_RATIO);
vTaskDelay(M2T(MOTORS_TEST_ON_TIME_MS));
motorsSetRatio(MOTORS[i], 0);
vTaskDelay(M2T(MOTORS_TEST_DELAY_TIME_MS));
#endif
}
}
return isInit;
}
// Ithrust is thrust mapped for 65536 <==> 60 grams
void motorsSetRatio(uint32_t id, uint16_t ithrust)
{
if (isInit) {
uint16_t ratio;
ASSERT(id < NBR_OF_MOTORS);
ratio = ithrust;
#ifdef ENABLE_THRUST_BAT_COMPENSATED
if (motorMap[id]->drvType == BRUSHED)
{
// To make sure we provide the correct PWM given current supply voltage
// from the battery, we do calculations based on measurements of PWM,
// voltage and thrust. See comment at function definition for details.
ratio = motorsCompensateBatteryVoltage(ithrust);
motor_ratios[id] = ratio;
}
#endif
if (motorMap[id]->drvType == BRUSHLESS)
{
motorMap[id]->setCompare(motorMap[id]->tim, motorsBLConv16ToBits(ratio));
}
else
{
motorMap[id]->setCompare(motorMap[id]->tim, motorsConv16ToBits(ratio));
}
}
}
int motorsGetRatio(uint32_t id)
{
int ratio;
ASSERT(id < NBR_OF_MOTORS);
if (motorMap[id]->drvType == BRUSHLESS)
{
ratio = motorsBLConvBitsTo16(motorMap[id]->getCompare(motorMap[id]->tim));
}
else
{
ratio = motorsConvBitsTo16(motorMap[id]->getCompare(motorMap[id]->tim));
}
return ratio;
}
void motorsBeep(int id, bool enable, uint16_t frequency, uint16_t ratio)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
ASSERT(id < NBR_OF_MOTORS);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
if (enable)
{
TIM_TimeBaseStructure.TIM_Prescaler = (5 - 1);
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MOTORS_TIM_BEEP_CLK_FREQ / frequency);
}
else
{
TIM_TimeBaseStructure.TIM_Period = motorMap[id]->timPeriod;
TIM_TimeBaseStructure.TIM_Prescaler = motorMap[id]->timPrescaler;
}
// Timer configuration
TIM_TimeBaseInit(motorMap[id]->tim, &TIM_TimeBaseStructure);
motorMap[id]->setCompare(motorMap[id]->tim, ratio);
}
// Play a tone with a given frequency and a specific duration in milliseconds (ms)
void motorsPlayTone(uint16_t frequency, uint16_t duration_msec)
{
motorsBeep(MOTOR_M1, true, frequency, (uint16_t)(MOTORS_TIM_BEEP_CLK_FREQ / frequency)/ 20);
motorsBeep(MOTOR_M2, true, frequency, (uint16_t)(MOTORS_TIM_BEEP_CLK_FREQ / frequency)/ 20);
motorsBeep(MOTOR_M3, true, frequency, (uint16_t)(MOTORS_TIM_BEEP_CLK_FREQ / frequency)/ 20);
motorsBeep(MOTOR_M4, true, frequency, (uint16_t)(MOTORS_TIM_BEEP_CLK_FREQ / frequency)/ 20);
vTaskDelay(M2T(duration_msec));
motorsBeep(MOTOR_M1, false, frequency, 0);
motorsBeep(MOTOR_M2, false, frequency, 0);
motorsBeep(MOTOR_M3, false, frequency, 0);
motorsBeep(MOTOR_M4, false, frequency, 0);
}
// Plays a melody from a note array
void motorsPlayMelody(uint16_t *notes)
{
int i = 0;
uint16_t note; // Note in hz
uint16_t duration; // Duration in ms
do
{
note = notes[i++];
duration = notes[i++];
motorsPlayTone(note, duration);
} while (duration != 0);
}
const MotorHealthTestDef* motorsGetHealthTestSettings(uint32_t id)
{
if (id >= NBR_OF_MOTORS)
{
return &unknownMotorHealthTestSettings;
}
else if (motorMap[id]->drvType == BRUSHLESS)
{
return &brushlessMotorHealthTestSettings;
}
else if (motorMap[id]->drvType == BRUSHED)
{
return &brushedMotorHealthTestSettings;
}
else
{
return &unknownMotorHealthTestSettings;
}
}
/**
* Logging variables of the motors PWM output
*/
LOG_GROUP_START(pwm)
/**
* @brief Current motor 1 PWM output
*/
LOG_ADD(LOG_UINT32, m1_pwm, &motor_ratios[0])
/**
* @brief Current motor 2 PWM output
*/
LOG_ADD(LOG_UINT32, m2_pwm, &motor_ratios[1])
/**
* @brief Current motor 3 PWM output
*/
LOG_ADD(LOG_UINT32, m3_pwm, &motor_ratios[2])
/**
* @brief Current motor 4 PWM output
*/
LOG_ADD(LOG_UINT32, m4_pwm, &motor_ratios[3])
LOG_GROUP_STOP(pwm)