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:
- 9:c3c90e137cfb
- Parent:
- 8:09b78bc5551f
- Child:
- 10:5f6d15f8fc79
--- a/main.cpp Tue Mar 27 15:11:02 2018 +0000
+++ b/main.cpp Wed Mar 28 10:04:58 2018 +0000
@@ -63,100 +63,72 @@
Mail<mail_t, 100> mail_box;
-void acceleration() // 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;
- //ac.release();
+
Thread::wait(100);
}
}
-void getSpeed() // 20Hz calculation for speed & distance
+void getSpeed() // calculation for speed & distance thread 20
{
while (1) {
- speed = (acc - br)*16.835; // speed in m/s. Max = 55
+ speed = (acc - br)*16.835; // speed in m/s. Max = 55
if (speed < 0) {
speed = 0;
}
- kmph = speed*3.6; // convert speed to km/ph. Change to *2.24 for ~mph
- distance = distance + (speed*0.05)/1000; // distance = speed*time (/1000 for m to km)
+ kmph = speed*3.6; // convert speed to km/ph. Change to *2.24 for ~mph
+ distance = distance + (speed*0.05)/1000; // distance = speed*time /1000 for m to km (/1609.34 for miles)
+
Thread::wait(50);
}
}
-void LCD() // 6. display odometer and speed 2
-{
- lcd->locate(0,0);
- lcd->printf("KM:%0.1f",distance);
- lcd->locate(1,0);
- lcd->printf("KM/H:%.1f",aveSpeed);
- lcd->locate(0,8);
- //ac.wait();
- lcd->printf("sp:%0.1f",kmph);
- //lcd->locate(1,8);
- //ac.release();
- //lcd->printf("b:%d",engine.read());
-}
-void braking() // 4. Brake indicated by LED 2
+void ignition() // 2. Read engine on/off show LED subroutine 2
{
- if ( br > 0) {
- brakeLights = 1;
- } else {
- brakeLights = 0;
- }
-}
-void t246() // 2. Read engine on/off show LED 2
-{
- while (1) {
- if (engine.read() > 0) {
- engLight = 1;
- } else {
- engLight = 0;
- }
- LCD();
- braking();
-
- Thread::wait(500);
+ if (engine.read() > 0) {
+ engLight = 1;
+ } else {
+ engLight = 0;
+ aveSpeed = 0;
}
}
-void speedo() // 3. Average last n speed readings 5
+void speedo() // 3. Average last n speed readings thread 5
{
while (1) {
for (int i = 0; i<3; i++) {
- aveSpeed = kmph + aveSpeed; // in km/h
+ aveSpeed = kmph + aveSpeed; // in km/h
}
aveSpeed = aveSpeed/4;
Thread::wait(200);
}
}
-void lights() // 9. set side lights 1
+void braking() // 4. Brake indicated by LED subroutine 2
{
- while (1) {
- if (lightSwitch.read() == 1) {
- sideLights = 1;
- } else {
- sideLights = 0;
- }
-
- Thread::wait(1000);
+ if ( br > 0) {
+ brakeLights = 1;
+ } else {
+ brakeLights = 0;
}
}
-void t59() // 5. if speed > 88 LED on 1
+void greatScott() // 5. if speed > 88 LED on subroutine 1
{
- while (1) {
- if (kmph > 141.6) { // 141.6 km = 88 miles
- fluxCapacitor = 1;
- } else {
- fluxCapacitor = 0;
- }
- lights();
-
- Thread::wait(1000);
+ if (kmph > 141.6) { // 141.6 km = 88 miles
+ fluxCapacitor = 1;
+ } else {
+ fluxCapacitor = 0;
}
}
-void send_thread (void) // 7. Send speed, acc, brake to MAILq 0.2
+void LCD() // 6. display odometer and speed subroutine 2
+{
+ lcd->locate(0,0);
+ lcd->printf("KM:%0.1f",distance);
+ lcd->locate(1,0);
+ lcd->printf("KM/H:%.1f",aveSpeed);
+}
+void send_thread (void) // 7. speed, acc, brake MAILq thread 0.2
{
while (true) {
mail_t *mail = mail_box.alloc();
@@ -168,7 +140,7 @@
Thread::wait(5000);
}
}
-void toSerial() // 8. MAIL q to serial PC 0.05
+void toSerial() // 8. MAIL q to serial PC thread 0.05
{
while (1) {
osEvent evt = mail_box.get();
@@ -176,15 +148,19 @@
pc.printf("\nSpeed: %.2f \n\r", mail->m_speed);
pc.printf("Acceleration: %.2f \n\r", mail->m_acc);
pc.printf("Braking: %.2f \n\r", mail->m_br);
- //pc.printf("\r\nSpeed: %.2f ", kmph);
- //pc.printf(", Acceleration: %.2f", acc);
- //pc.printf(", Braking: %.2f", br);
-
+
Thread::wait(20000);
}
}
-
-void indicators() // 10. check indicators 0.5
+void lights() // 9. set side lights subroutine 1
+{
+ if (lightSwitch.read() == 1) {
+ sideLights = 1;
+ } else {
+ sideLights = 0;
+ }
+}
+void indicators() // 10. check indicators thread 0.5
{
while (1) {
@@ -229,26 +205,41 @@
Thread::wait(x);
}
}
+void t59() // thread for 5 & 9 1Hz
+{
+ while (1) {
+ greatScott();
+ lights();
+ Thread::wait(1000);
+ }
+}
+void t246() // thread for 2, 4, & 6 2Hz
+{
+ while (1) {
+ ignition();
+ LCD();
+ braking();
+
+ Thread::wait(500);
+ }
+}
int main()
{
par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip
lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display
par_port->write_bit(1,BL_BIT); // turn LCD backlight ON
-
- sp.start(getSpeed); //20 y
- task1.start(acceleration); //10 y
- task246.start(t246); //2 .setPriority(); y 4, 6
- task3.start(speedo); //5 y
- //task4.start(braking); //2 y
- task59.start(t59); //1 y 9
- //task6.start(LCD); //2 y
+ //Hz
+ sp.start(getSpeed); //20
+ task1.start(acceleration); //10
+ task246.start(t246); //2
+ task3.start(speedo); //5
+ task59.start(t59); //1
task7.start(callback(send_thread)); //0.2
task8.start(toSerial); //0.05
- //task9.start(lights); //1 y
- task10.start(indicators); //0.5 y
- indi.start(indicate); // 1-2 y
+ task10.start(indicators); //0.5
+ indi.start(indicate); // 1-2
}