-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
422 lines (353 loc) · 15.4 KB
/
code.c
File metadata and controls
422 lines (353 loc) · 15.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "mbed.h"
#include "MCP23017.h"
#include "WattBob_TextLCD.h"
#include "rtos.h"
#include "Math.h"
MCP23017 *par_port;
WattBob_TextLCD *lcd;
Serial pc(USBTX, USBRX);
DigitalOut engine_led(LED1); // led for the engine state
DigitalOut brake_led(LED2); // led for the brake state
DigitalOut monitor_led(LED3); // led for the monitor
DigitalOut cruise_led(LED4); // led for the cruise mode state
bool PID = true; // if the PID is true : PID exponential / PID is false : PID lineair
float odom = 0; // initialization of the odometry
float average_speed = 0; // initialization of the average speed
float current_speed = 0; // initialization of the current speed
int engine = 0; // initialization of the engine state
float stock_speed[3]; // initialization of the stock of current speed
int count1 = 0; // initialization of the count for stocked speed values
int cruse = 0; // initialization of the cruise mode state
float brake = 0; // initialization of the brake value
float acceleration = 0; // initialization of the acceleration value
int cruise_timer = 0; // initialization of the cruise timer
//Semaphores to manage accessing the viriables
Semaphore semaphore_inputs(1); // controls the read and sent inputs
Semaphore semaphore_speed(1); // controls the calculated speed
Semaphore semaphore_average_speed(1); // controls the calculated average speed
Thread thread1;
Thread thread2;
Thread thread3;
Thread thread4;
Thread thread5;
Thread thread6;
Thread thread7;
Thread thread8;
/*-------------------------------------------------------------------------
* Read Break
--------------------------------------------------------------------------
*
*/
void read_brake(void)
{
while(true)
{
semaphore_inputs.wait(); // takes the input semaphore
if (cruse == 0) // if cruse mode disable
{
if ( 1 == par_port -> read_bit(9)) // if the brake button is pressed
{
brake_led = 1; // led_2 = on
brake += 1; // brake + 1
}
else // if the button is not pressed
{
brake_led = 0; // led_2 = off
brake -= 1; // brake - 1
}
// limit the brake to be between [0 ; 10]
if (brake > 10)
{
brake = 10;
}
else if (brake < 0)
{
brake = 0;
}
}
semaphore_inputs.release(); // release input semaphore
Thread::wait(500); // rate of 2Hz
}
}
/*-------------------------------------------------------------------------
* Read Accelerator
--------------------------------------------------------------------------
*
*/
void read_accelerator(void)
{
while(true)
{
semaphore_inputs.wait(); // takes the input semaphore
if (cruse == 0) // if cruse mode disable
{
if (par_port -> read_bit(10) == 1) // if the acceleration button is pressed
{
acceleration += 1; // acceleration + 1
}
else
{
acceleration -= 1; // acceleration - 1
}
// limit the acceleration to be between [0 ; 10]
if (acceleration > 10)
{
acceleration = 10;
}
else if (acceleration < 0)
{
acceleration = 0;
}
}
semaphore_inputs.release(); // release the input semaphore
Thread::wait(500); // rate of 2Hz
}
}
/*-------------------------------------------------------------------------
* Read Engine state
--------------------------------------------------------------------------
*
*/
void read_engine(void)
{
while(true)
{
semaphore_inputs.wait(); // takes the input semaphore
if (1 == par_port -> read_bit(8)) // if cruse mode disable
{
engine += 1; // state engine takes +1
}
semaphore_inputs.release(); // Release the semaphore
if (engine > 1) // if the engine state is above 1 the variable takes 0
{
engine = 0;
}
if(engine == 1) // if the engine is ON
{
engine_led = 1; // led_1 = ON
}
else if(engine == 0) // if the engine is OFF
{
engine_led = 0; // led_1 = OFF
}
Thread::wait(500); // rate of 2Hz
}
}
/*-------------------------------------------------------------------------
* Read average speed
--------------------------------------------------------------------------
*
*/
void read_average_speed()
{
while(true)
{
semaphore_speed.wait(); // takes the semaphore speed
semaphore_average_speed.wait(); // takes the sempagore averages speed
int samples = 3; // number of samples
int sum = 0; // sum is reset to 0
for (int i=0;i < samples; i++)
{
sum = sum + current_speed; // takes the 3 samples of the current speed
}
average_speed = sum / samples; // and make an average of the sample
average_speed = average_speed;
semaphore_speed.release(); // release the semaphore speed
semaphore_average_speed.release(); // release the semaphore average speed
Thread::wait(150); // wait for
}
}
/*-------------------------------------------------------------------------
* monitor speed
--------------------------------------------------------------------------
* rate = 5Hz
*/
void monitor_speed()
{
while(true)
{
semaphore_average_speed.wait(); // takes the average speed semaphore
if (average_speed >= 40) // it the average speed is above 40m/s
{
monitor_led = 1;
wait_ms(40); // make the led blink
monitor_led = 0;
}
semaphore_average_speed.release(); // release the sempahore
Thread::wait(200); // rate of 5Hz
}
}
/*-------------------------------------------------------------------------
* Read simulation
--------------------------------------------------------------------------
* Rate : 25Hz
*/
void simulation()
{
while(true)
{
semaphore_speed.wait(); // takes the semaphore speed
float time = 0.04; // time value is created according to its rate
if (cruse == 0 or ( cruse == 1 and PID == false)) // if the cruise mode is disable
{
current_speed = engine*(current_speed + ((acceleration - brake)*time)); // current speed equation
}
if (current_speed < 0) // limite the current speed to be under 0
{
current_speed = 0;
}
else if (current_speed > 40) // limite the current speed to be over 40
{
current_speed = 40;
}
odom = time*current_speed + odom; // equation of the odometry
semaphore_speed.release(); // release the semaphore
stock_speed[count1] = current_speed; // takes a sample of the current speed for average speed
count1 = count1 + 1;
if(count1 == 3) // once a reach 3 it write over the old values
{
count1 = 0;
}
Thread::wait(40); // rate of 25Hz
}
}
/*-------------------------------------------------------------------------
* Read LCD
--------------------------------------------------------------------------
* rate : 2Hz
*/
void display()
{
while(true)
{
semaphore_inputs.wait();
lcd->locate(0,0); // locate the beginning of the writing at the 1st line and 1st slot
lcd->printf("speed:%.1f \n", average_speed); // write the frequency and the switch state
lcd->locate(1,0); // locate the beginning of the writing at the 2st line and 1st slot
lcd->printf("distance:%.1f\n",odom, acceleration); // write the value of the analogique signal 1 & 2
semaphore_inputs.release();
Thread::wait(500); // rate of 2Hz
}
}
/*-------------------------------------------------------------------------
* Crusing mode
--------------------------------------------------------------------------
* Rate : 20Hz
*/
void crusing_mode()
{
float tmp; // initialization of the tmp variable
float delta_t = 0.05; // initialization of the delta_t variable
while(true)
{
semaphore_inputs.wait();
semaphore_speed.wait();
if (PID == true) // if the PID is in true = 1st order PID
{
if (1 == par_port -> read_bit(11) and engine == 1) // if the Button is pressed
{
cruse += 1; // if the button is pressed the variable goes to 1
if (cruse > 1)
{
cruse = 0; // if the button is pressed the variable was at 1 it goes back to 0
}
if (cruse == 0) // if the variable is at 0 the led is switch off and the cruise mode diasabled
{
cruse = 0;
cruise_led = 0;
cruise_timer = 0;
}
if (cruse == 1 and cruise_timer == 0) // takes the value of the current speed to know where to start on the curve
{
cruise_led = 1; // switch on the led
tmp = 2*log((current_speed/23 + 1)); // know the time on the curve
cruise_timer = tmp/delta_t; // transfom the time into discret time
}
}
if (cruse == 1 and cruise_timer < 200) // once the start time is known
{
current_speed = engine*23*(1-exp(-0.5*delta_t*cruise_timer)); // the current speed will follow the curves
acceleration = engine*11*exp(-0.5*delta_t*cruise_timer); // same for the acceleration
cruise_timer += 1; // increment the discrete time
}
else if (cruse == 1 and cruise_timer == 200) // once the speed reached the acceleration and brake takes 0
{
brake = 0.0;
acceleration = 0.0;
}
}
else // if the PID is in false = linair PID
{
if (1 == par_port -> read_bit(11) and engine == 1) // read the cruise button
{
cruse += 1; // enable cruise mode
cruise_led = 1; // switch on the light
if (cruse > 1)
{
cruse = 0;
}
if (cruse == 0)
{
cruse = 0;
cruise_led = 0;
cruise_timer = 0;
}
if(cruise_timer == 0) // if the cruise timer is at 0
{
acceleration = (22 - current_speed)/10; // the acceleration predetermine lineair value
cruise_timer += 1; // the cruise timer is incremented
}
}
else
{
if (cruse == 1) // if the acceleration has already been set
{
if(cruise_timer > 164) // if the timer is finished
{
acceleration = 0; // set the acceleration at 0
}
else // otherwise the acceleration keeps it value
{
cruise_timer += 1;
}
}
}
}
semaphore_inputs.release(); // release the semaphore
semaphore_speed.release();
Thread::wait(50); // rate of the function 50
}
}
/*-------------------------------------------------------------------------
* MAIN
--------------------------------------------------------------------------
*
*/
int main() {
par_port = new MCP23017(p9, p10, 0x40); // enable switch inputs
lcd = new WattBob_TextLCD(par_port); // enable LCD
par_port->write_bit(1,BL_BIT); //
lcd->cls(); // clear the LCD
lcd->locate(0,0); // Locate the writing at 0,0
thread1.start(read_engine); // start the read engine tread
thread2.start(read_brake); // start the read brake tread
thread3.start(read_average_speed); // start the read average speed tread
thread4.start(read_accelerator); // start the read accelerator tread
thread5.start(monitor_speed); // start the monitor speed tread
thread6.start(display); // start the display tread
thread7.start(crusing_mode); // start the cruising mode tread
thread8.start(simulation); // start the simulation tread
while(1)
{
/*
read_engine();
read_average_speed();
read_accelerator();
monitor_speed();
display();
crusing_mode();
wait(1);
simulation();
*/
}
}