Repository navigation
Expand file tree
/
Copy paththrottle_controller.ino
More file actions
404 lines (349 loc) · 10.4 KB
/
Copy paththrottle_controller.ino
File metadata and controls
404 lines (349 loc) · 10.4 KB
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
#include "motor.hpp"
#include "pid.hpp"
#include "comm.hpp"
#include "system.hpp"
#include <avr/wdt.h>
//Beginning of Auto generated function prototypes by Atmel Studio
void wInterrupt();
//End of Auto generated function prototypes by Atmel Studio
/**
* Motor calibration Connected
* Idle position: 980 980
* Full throttle position: 50 288
*/
#define MOTOR_PWM 9
#define MOTOR_A1 10
#define MOTOR_A2 11
#define MOTOR_POS (unsigned char)1
#define REF_IN 0
#define W_INTERRUPT 0
#define N_ENG_MIN 500 //!< [rpm] Min allowed engine speed
#define N_ENG_MAX 2700 //!< [rpm] Max allowed engine speed
#define N_ENG_MAX_SAMPLED 3000 //!< [rpm] Max realistically sampled engine speed
#define N_ENG_MAX_SAMPLE_AGE 1000 //!< [ms] max age of engine speed measurement
Motor motor(MOTOR_PWM, MOTOR_A1, MOTOR_A2);
volatile uint32_t wMicrosDiff_u32;
volatile uint32_t wMillisDiff_u32;
volatile uint32_t wMicrosNow_u32;
volatile uint32_t wMillisNow_u32;
PID pid_servo;
PID pid_n_eng;
Converter convServoPos;
Converter convPot;
Converter convNEngRef;
status_t status;
parameters_t parameters;
TaskTimer taskTimerMain;
TaskTimer taskTimerController;
TaskTimer taskTimerSerial;
/** @brief Get one sample of the engine speed
This function tries to do some basic checks of the engine speed
and flag it appropriately.
*/
void getNEngSample()
{
uint32_t wMicrosDiffTmp_u32;
uint32_t wMillisNowTmp_u32;
uint32_t wSampleAge_u32; //!< [ms] The age of the engine speed measurement
float nEngTmp_f;
// Critical region:
// The interrupts need to be turned off before reading the
// variables used inside the interrupt routine
noInterrupts();
wMillisNowTmp_u32 = wMillisNow_u32;
wMicrosDiffTmp_u32 = wMicrosDiff_u32;
interrupts();
wSampleAge_u32 = millis() - wMillisNowTmp_u32; // Get the age of the engine speed sample
if(wSampleAge_u32 < N_ENG_MAX_SAMPLE_AGE)
{
// If the engine speed measurement is new enough
if(wMicrosDiffTmp_u32 == 0)
{
// Assume this is the initial state, before any interrupts.
// Should never be here actually...
status.nEngStatus_e = INIT;
}
else
{
// Calculate the engine speed from the pulse measurement
// of the alternator. A factor of 4 was found empirically.
nEngTmp_f=4*1000000/wMicrosDiffTmp_u32;
if(nEngTmp_f<N_ENG_MAX_SAMPLED)
{
// If the measured engine speed is realistic
status.nEng_f = nEngTmp_f;
status.nEngStatus_e = OK;
}
else
{
// Unrealistic engine speed. Keep the old value and flag as error.
status.nEngStatus_e = ERROR;
}
}
}
else
{
// Waited too long for a pulse which would happen if the engine is stopped.
// The problem is it could also happen if there is no signal from the alternator.
// Other logic needs to take care of this.
status.nEng_f = 0;
status.nEngStatus_e = TOO_OLD;
}
}
// the setup routine runs once when you press reset:
void setup() {
// wdt_disable();
Serial.begin(19200);
Serial.print("MCUSR WDTCSR: ");
Serial.println(MCUSR, BIN);
Serial.println(WDTCSR, BIN);
cli();
wdt_reset();
/* Clear WDRF in MCUSR */
MCUSR &= ~(1<<WDRF);
/* Write logical one to WDCE and WDE */
/* Keep old prescaler serring to prevent unintentional time-out */
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* Turn off WDT */
WDTCSR = 0x00;
sei();
Serial.println("After:");
Serial.println(MCUSR, BIN);
Serial.println(WDTCSR, BIN);
motor.init();
pid_servo.init();
pid_servo.setPGain(15.0);
pid_n_eng.init();
pid_n_eng.setPGain(0.05);
pid_n_eng.setIGain(0.1);
pid_n_eng.setUMin(0.0);
pid_n_eng.setUMax(100.0);
// Setup conversion parameters
// Pot max: 920
parameters.servoADMax_u16 = 300;
parameters.servoADMin_u16 = 900;
parameters.potADMax_u16 = 1024;
parameters.potADMin_u16 = 600;
parameters.aFiltServo_f = 0.5;
parameters.aFiltPot_f = 0.1;
parameters.aFiltNEng_f = 0.1;
parameters.nEngRefMin = N_ENG_MIN;
parameters.nEngRefMax = N_ENG_MAX;
parameters.potKickdownSet_u16 = 95;
parameters.potKickdownReset_u16 = 30;
parameters.servoPosStart_u16 = 35;
// Setup value conversions
convServoPos.calcKM(parameters.servoADMax_u16, parameters.servoADMin_u16, 100, 0);
convPot.calcKM(parameters.potADMax_u16, parameters.potADMin_u16, 100, 0);
convNEngRef.calcKM(100, 0, 3000, 500);
// Setup task timers
taskTimerMain.init();
taskTimerController.init();
taskTimerSerial.init();
// Init status
status.mode_e = NORMAL; // Assume wakeup from reset in normal mode
status.nEngStatus_e = INIT; // Initial engine speed state
attachInterrupt(W_INTERRUPT,wInterrupt, RISING);
pinMode(LED_BUILTIN, OUTPUT);
}
/**@brief
Handle all inputs
*/
void handleInputs()
{
// Inputs
status.servoPosRaw_u16 = analogRead(MOTOR_POS);
status.potInCabRaw_u16 = analogRead(REF_IN);
getNEngSample();
// y=kx+m conversion of the inputs
status.servoPos_f = convServoPos.convert((float)status.servoPosRaw_u16);
status.potInCab_f = convPot.convert((float)status.potInCabRaw_u16);
// Create filtered values
status.servoPosFilt_f = status.servoPosFilt_f*(1.0-parameters.aFiltServo_f) + status.servoPos_f*parameters.aFiltServo_f;
status.potInCabFilt_f = status.potInCabFilt_f*(1.0-parameters.aFiltPot_f) + status.potInCab_f*parameters.aFiltPot_f;
status.nEngFilt_f = status.nEngFilt_f*(1.0-parameters.aFiltNEng_f) + status.nEng_f*parameters.aFiltNEng_f;
}
uint32_t handleOutputs()
{
static uint32_t millisOld;
status.servoOutput_u16=(int)pid_servo.getOutput();
// Stop motor if the output is small enough
if(abs(status.servoOutput_u16)<40)
{
motor.stop();
}
else
{
// Inverse output
motor.speed(-status.servoOutput_u16);
}
if((millis()-millisOld)> 100)
{
digitalWrite(LED_BUILTIN, HIGH);
millisOld = millis();
}
else
{
digitalWrite(LED_BUILTIN, LOW);
} return millisOld;
}
/** @brief Calculation */
void calculate()
{
float nEngFromPotTemp;
static float u_pid_n_eng = N_ENG_MIN; // Init with minimal engine speed
mode_t modeNext_e=status.mode_e; // The next mode
// Handle modes
switch(status.mode_e)
{
case OFF:
// In this mode, it is assumed that the engine is off
// The servo should be put in starting position to wait for
// the engine speed to rise above a minimum value
if(status.nEngStatus_e == OK &&
status.nEngFilt_f > N_ENG_MIN)
{
// If the filtered engine speed is higher than a minimum value,
// move to START mode
modeNext_e = START;
}
else
{
modeNext_e = OFF;
}
break;
case START:
// In this mode, wait for "kickdown", that is that the throttle is pushed
// to 100%
// The reason for this is that the alternator V-belt seems to be
// slipping right after engine start, causing the measured engine speed
// to be unreliable and resulting in engine hunting.
if(status.potInCabFilt_f>parameters.potKickdownSet_u16)
{
modeNext_e = KICKDOWN;
}
else if(status.nEngStatus_e != OK)
{
modeNext_e = OFF;
}
break;
case KICKDOWN:
// In this state, wait for the throttle (and pot) to be released below
// a setpoint, to allow for smooth transition to engine speed control
if(status.potInCabFilt_f<parameters.potKickdownReset_u16)
{
modeNext_e = NORMAL;
}
else if(status.nEngStatus_e != OK)
{
modeNext_e = OFF;
}
break;
case NORMAL:
// No engine speed is measured. Go to mode OFF
if(status.nEngStatus_e == TOO_OLD)
{
modeNext_e = OFF;
}
else
{
// If there is still a valid engine speed sample, stay in mode NORMAL
modeNext_e = NORMAL;
}
break;
default:
// Should never be here!
break;
}
if(modeNext_e != status.mode_e)
{
// Mode change!
if(modeNext_e == NORMAL)
{
// If transitioning into NORMAL mode,
// the engine speed PID controller should be reset. The I part
// of the controller should be set to the current servo position
// for minimizing the risk of a step in the signal.
pid_n_eng.setIPart(status.servoPosFilt_f);
}
status.mode_e = modeNext_e;
}
// Engine speed PID calculation
if(status.nEngRefExtEnable)
{
// Use external engine speed reference
status.nEngRef_u16 = (float)status.nEngRefExt_u16;
}
else
{
// Use internal engine speed reference
// Limit the pot reference value
nEngFromPotTemp = convNEngRef.convert(status.potInCabFilt_f);
if(nEngFromPotTemp<parameters.nEngRefMin)
{
nEngFromPotTemp = parameters.nEngRefMin;
}
else if(nEngFromPotTemp>parameters.nEngRefMax)
{
nEngFromPotTemp = parameters.nEngRefMax;
}
status.nEngRef_u16 = nEngFromPotTemp;
}
pid_n_eng.calculate((double)status.nEngRef_u16, (double)status.nEngFilt_f);
u_pid_n_eng = pid_n_eng.getOutput();
// Servo PID calculation
if(status.servoPosRefExtEnable)
{
// Use external servo position reference
status.servoPosRef_f = status.servoPosRefExt_f;
}
else
{
if(status.mode_e == NORMAL)
{
// Use internal servo position reference
status.servoPosRef_f = u_pid_n_eng;
}
else
{
// In all modes other than NORMAL, use a fixed servo position
status.servoPosRef_f = parameters.servoPosStart_u16;
}
}
pid_servo.calculate((double)status.servoPosRef_f, (double)status.servoPosFilt_f);
}
// the loop routine runs over and over again forever:
void loop() {
static uint32_t millisOld;
uint32_t millisNow;
static uint32_t tSampleMain=10; //!< [ms] The main sample time to use
millisNow = millis();
taskTimerMain.start();
if((millisNow-millisOld)>=tSampleMain)
{
taskTimerController.start();
handleInputs();
calculate();
handleOutputs();
millisOld = millisNow;
taskTimerController.stop();
}
taskTimerSerial.start();
handleSerialComm();
taskTimerSerial.stop();
taskTimerMain.stop();
}
/** @brief Interrupt callback at every pulse from the alternator
Approximately 2 pulses every 10 ms.
*/
void wInterrupt()
{
static uint32_t microsOld_u32;
static uint32_t millisOld_u32;
wMicrosNow_u32 = micros(); // Timestamp now
wMillisNow_u32 = millis();
wMicrosDiff_u32 = wMicrosNow_u32-microsOld_u32;
wMillisDiff_u32 = wMillisNow_u32-millisOld_u32;
microsOld_u32 = wMicrosNow_u32;
millisOld_u32 = wMillisNow_u32;
}