Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MCP23017 mbed-rtos WattBob_TextLCD
Diff: main.cpp
- Revision:
- 5:e62e0e3084fc
- Parent:
- 4:72c487215487
- Child:
- 6:a026e0651ad5
diff -r 72c487215487 -r e62e0e3084fc main.cpp
--- a/main.cpp Tue Mar 27 10:15:57 2018 +0000
+++ b/main.cpp Tue Mar 27 12:56:10 2018 +0000
@@ -12,16 +12,14 @@
AnalogIn accel(p15); // Analog
AnalogIn brake(p16);
-float speed = 0;
+float speed;
+float kmph;
float aveSpeed;
-float acc = 0;
-float br = 0;
-float dif;
+float acc;
+float br;
float distance = 0;
Timer tim, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;// integrate with wait?
-long int t;
-int r;
int indicount = 0;
@@ -33,10 +31,10 @@
DigitalOut brakeLights(p6); // redbox out
DigitalOut fluxCapacitor(p7);
-DigitalIn lightSwitch(p8); // switches
-DigitalIn lIndicate(p9);
-DigitalIn rIndicate(p10);
-DigitalIn engine(p5);
+DigitalIn engine(p5); // switches
+DigitalIn lightSwitch(p8);
+DigitalIn lIndicate(p11);
+DigitalIn rIndicate(p12);
Serial pc(USBTX, USBRX); // serial tx, rx
@@ -51,6 +49,7 @@
Thread task8;
Thread task9;
Thread task10;
+Semaphore ac(1);
typedef struct { // mail
float m_speed;
@@ -61,35 +60,38 @@
Mail<mail_t, 100> mail_box;
-void acceleration() //run at 20hz // 1. read brake and accelerator 10
+void acceleration() // 1. read brake and accelerator 10
{
while (1) {
+ //ac.wait();
acc = accel.read()*3.3;
br = brake.read()*3.3;
- Thread::wait(500-r);
+ //ac.release();
+ Thread::wait(100);
}
}
void getSpeed() // 20Hz calculation for speed & distance
{
while (1) {
- dif = acc - br;
- t = tim.read();
- speed = speed + (dif*t*3.6);
+ speed = (acc - br)*16.835;
if (speed < 0) {
speed = 0;
}
- distance = speed*t + (0.5*dif*(t*t))/1000;
-
- Thread::wait(100);
+ kmph = speed*3.6;
+ distance = distance + (speed*0.05)/1000;
+ Thread::wait(50);
}
}
void ignition() // 2. Read engine on/off show LED 2
{
while (1) {
- if (engine == 1) {
+ if (engine.read() > 0) {
engLight = 1;
}
- Thread::wait(500-r);
+ else{
+ engLight = 0;
+ }
+ Thread::wait(500);
}
}
void speedo() // 3. Average last n speed readings 5
@@ -98,7 +100,7 @@
for (int i = 0; i<3; i++) {
aveSpeed = speed + aveSpeed;
}
- speed = speed/4;
+ aveSpeed = speed/4;
Thread::wait(200);
}
@@ -106,9 +108,12 @@
void braking() // 4. Brake indicated by LED 2
{
while (1) {
- if ( br>0) {
+ if ( br > 0) {
brakeLights = 1;
}
+ else{
+ brakeLights = 0;
+ }
Thread::wait(500);
}
@@ -126,13 +131,17 @@
void LCD() // 6. display odometer and speed 2
{
while (1) {
- t = tim.read();
- distance = speed * (t/3600);
lcd->locate(0,0);
lcd->printf("KM:%0.1f",distance);
lcd->locate(1,0);
- lcd->printf("KMPH:%0.1f",speed);
+ lcd->printf("KMPH:%.1f",kmph);
+ lcd->locate(0,8);
+ //ac.wait();
+ //lcd->printf("a:%0.1f",dif);
+ lcd->locate(1,8);
+ //ac.release();
+ //lcd->printf("b:%d",engine.read());
Thread::wait(500);
}
@@ -216,15 +225,16 @@
sp.start(getSpeed); //20
task1.start(acceleration); //10
- task2.start(ignition); //2
- task3.start(speedo); //5
- task4.start(braking); //2
- task5.start(greatScott); //1
+// task2.start(ignition); //2 .setPriority();
+// task3.start(speedo); //5
+// task4.start(braking); //2
+// task5.start(greatScott); //1
task6.start(LCD); //2
- task7.start(callback(send_thread)); //0.2
- task8.start(toSerial); //0.05
- task9.start(lights); //1
- task10.start(indicators); //0.5
+// task7.start(callback(send_thread)); //0.2
+// task8.start(toSerial); //0.05
+// task9.start(lights); //1
+// task10.start(indicators); //0.5
+
}