Basic funcitonality
Dependencies: mbed mbed-rtos SevenSegmentSerial HC_SR04_Ultrasonic_Library
Diff: main.cpp
- Revision:
- 5:99be83f824b8
- Parent:
- 3:293a771fc470
- Child:
- 6:c8f14de0e3f4
--- a/main.cpp Mon Apr 19 20:28:11 2021 +0000 +++ b/main.cpp Tue Apr 27 16:34:57 2021 +0000 @@ -1,11 +1,12 @@ -//************************** -// Mbed to Raspberry Pi -//************************** -// 0 - pass starting line -// 1 - pass checkpoint 1 -// 2 - pass checkpoint 2 -// ... -// 9 - end race +//****************************************************** +// Mbed to Raspberry Pi ** Raspberry Pi to Mbed * +//****************************************************** +// 0 - pass starting line ** S - start race * +// 1 - pass checkpoint 1 ** R - reset * +// 2 - pass checkpoint 2 ** * +// ... ** * +// 9 - end race ** * +//****************************************************** #include "mbed.h" #include "ultrasonic.h" @@ -24,13 +25,16 @@ DigitalOut rgbCtrl2(p9); DigitalOut rgbCtrl3(p10); -int width[3]; //width of each checkpoint, mm -int sensitivity = 5; //sensitivity, adjust with testing, mm +#define B4NOTE 500 //Hz +#define E5NOTE 659 //Hz + +int width[3]; //width of each checkpoint, mm +int threshold[3]; //how much distance has to change, mm +int sensitivity[3] = {0.05,0.05,0.05}; int total_laps = 3; int current_lap = 0; -int B4Note = 500; -int E5Note = 659; int i = 500; + bool early_start = 0; bool start_done = 0; @@ -40,12 +44,15 @@ enum state_type {start,past_start,past_cp1,past_cp2}; state_type track_state = start; -// Race start lights and sound +//Mbed reset +extern "C" void mbed_reset(); + +//Starting countdown void start_race() { // 3 rgbRed = 1; - speaker.period(1.0/B4Note); + speaker.period(1.0/B4NOTE); speaker =0.5; rgbCtrl1 = 1; Thread::wait(500); @@ -67,7 +74,7 @@ rgbRed = 0; rgbGreen = 1; start_done = 1; - speaker.period(1.0/E5Note); + speaker.period(1.0/E5NOTE); speaker =0.5; Thread::wait(500); speaker = 0; @@ -79,7 +86,7 @@ // Plays bleep for crossing finish line void play_audio() { - speaker.period(1.0/B4Note); // 500hz period + speaker.period(1.0/B4NOTE); // 500hz period speaker =0.5; //50% duty cycle - max volume Thread::wait(150); speaker=0.0; // turn off audio @@ -89,14 +96,15 @@ void start_line(int distance) { if (setup[0]) { - pc.printf("Setup\n"); - pc.printf("%d",distance); width[0] = distance; + threshold[0] = distance*sensitivity[0]; setup[0] = !setup[0]; + pc.printf("Setup: Starting Line\n"); + pc.printf("Distance: %3d\n\n",distance); } - if (width[0] - distance >= sensitivity) { - pc.printf("Past start line\n"); + if (width[0] - distance >= threshold[0]) { + pc.printf("Past starting line\n"); led0 = 1; led1 = 0; led2 = 0; @@ -112,14 +120,15 @@ void checkpoint1(int distance) { if (setup[1]) { - pc.printf("Setup\n"); - pc.printf("%d",distance); width[1] = distance; + threshold[1] = distance*sensitivity[1]; setup[1] = !setup[1]; + pc.printf("Setup: Starting Line\n"); + pc.printf("Distance: %3d\n\n",distance); } - pc.printf("Past Checkpoint 1\n"); - if (width[1] - distance >= sensitivity) { + if (width[1] - distance >= threshold[1]) { + pc.printf("Past Checkpoint 1\n"); led1 = 1; track_state = past_cp1; pc.putc('1'); @@ -130,14 +139,15 @@ void checkpoint2(int distance) { if (setup[2]) { - pc.printf("Setup\n"); - pc.printf("%d",distance); width[2] = distance; - setup[2] = !setup[2]; + threshold[2] = distance*sensitivity[2]; + setup[2] = !setup[1]; + pc.printf("Setup: Starting Line\n"); + pc.printf("Distance: %3d\n\n",distance); } pc.printf("Past Checkpoint 2\n"); - if (width[2] - distance >= sensitivity) { + if (width[2] - distance >= threshold[2]) { led2 = 1; track_state = past_cp2; pc.putc('2'); @@ -150,19 +160,42 @@ int main() { + //sonar initialization sonar0.startUpdates(); sonar1.startUpdates(); sonar2.startUpdates(); + //sonar calibration sonar0.checkDistance(); sonar1.checkDistance(); sonar2.checkDistance(); + //wait for start char from Pi + while (1) { + if (pc.getc() == 'S') { + break; + } + Thread::wait(50); + } + + //starting countdown thread2.start(&start_race); + //false start detect while(!start_done) { if (current_lap) { - // Flash everything red. Play bad sounds. + rgbCtrl1 = rgbCtrl2 = rgbCtrl3 = 0; + rgbRed = 1; + rgbGreen = 0; + while (1) { + rgbCtrl1 = !rgbCtrl1; + rgbCtrl2 = !rgbCtrl2; + rgbCtrl3 = !rgbCtrl3; + if (pc.getc() == 'R') { + mbed_reset(); + } + Thread::wait(50); + } } Thread::wait(50); } @@ -184,8 +217,11 @@ } if (current_lap > total_laps) { pc.putc('9'); + pc.printf("Race Over"); while(1) { - pc.printf("Race Over"); + if (pc.getc() == 'R') { + mbed_reset(); + } Thread::wait(50); } }