Sonar Proximity Audio Alert for Robot
Dependencies: HC_SR04_Ultrasonic_Library Motor mbed-rtos mbed
Fork of rtos_basic by
Revision 12:42d63181dbdb, committed 2017-03-13
- Comitter:
- juubs
- Date:
- Mon Mar 13 18:35:56 2017 +0000
- Parent:
- 11:0309bef74ba8
- Commit message:
- Lab 4 Final
Changed in this revision
diff -r 0309bef74ba8 -r 42d63181dbdb HC_SR04_Ultrasonic_Library.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HC_SR04_Ultrasonic_Library.lib Mon Mar 13 18:35:56 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
diff -r 0309bef74ba8 -r 42d63181dbdb Motor.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.lib Mon Mar 13 18:35:56 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/simon/code/Motor/#f265e441bcd9
diff -r 0309bef74ba8 -r 42d63181dbdb Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Mon Mar 13 18:35:56 2017 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +// a new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; \ No newline at end of file
diff -r 0309bef74ba8 -r 42d63181dbdb main.cpp --- a/main.cpp Wed Feb 15 14:04:02 2017 -0600 +++ b/main.cpp Mon Mar 13 18:35:56 2017 +0000 @@ -1,22 +1,109 @@ #include "mbed.h" +#include "Speaker.h" +#include "ultrasonic.h" +#include "Motor.h" #include "rtos.h" - -DigitalOut led1(LED1); -DigitalOut led2(LED2); -Thread thread; - -void led2_thread() { - while (true) { - led2 = !led2; - Thread::wait(1000); + +RawSerial pc(USBTX, USBRX); +RawSerial bluetooth(p28, p27); +Motor leftWheel(p21, p20, p19); +Motor rightWheel(p22, p15, p14); +Speaker speaker(p23); + +float speed = .5; + +void bluetooth_thread(void const *args) { + char cmd, bhit, bnum; + while(true) { + if (bluetooth.getc() == '!') { + cmd = bluetooth.getc(); + switch (cmd) { + case 'B': // button press + bnum = bluetooth.getc(); + bhit = bluetooth.getc(); + if (bluetooth.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum + switch (bnum) { + case '1': + speed = 1; + break; + case '2': + speed = .75; + break; + case '3': + speed = .5; + break; + case '4': + speed = .25; + break; + case '5': //up + if (bhit == '1') { + leftWheel.speed(speed); + rightWheel.speed(speed); + } else { + leftWheel.speed(0); + rightWheel.speed(0); + } + break; + case '6': //down + if (bhit == '1') { + leftWheel.speed(-1 * speed); + rightWheel.speed(-1 * speed); + } else { + leftWheel.speed(0); + rightWheel.speed(0); + } + break; + case '7': //left + if (bhit == '1') { + leftWheel.speed(-.5); + rightWheel.speed(.5); + } else { + leftWheel.speed(0); + rightWheel.speed(0); + } + break; + case '8': //right + if (bhit == '1') { + leftWheel.speed(.5); + rightWheel.speed(-.5); + } else { + leftWheel.speed(0); + rightWheel.speed(0); + } + break; + default: + break; + } + } + break; + default: + break; + } + } } } - + +void dist(int distance) { + pc.printf("Distance %d mm\r\n", distance); + if (distance < 50) { + speaker.PlayNote(150, 0.15, 0.5); + } else if (distance < 100) { + speaker.PlayNote(150, 0.25, 0.02); + } else if (distance < 200) { + speaker.PlayNote(150, 0.5, 0.02); + } else if (distance < 300) { + speaker.PlayNote(150, 0.75, 0.02); + } + Thread::wait(50); +} + +ultrasonic sonar(p6, p7, .1, 1, &dist); + int main() { - thread.start(led2_thread); + sonar.startUpdates(); - while (true) { - led1 = !led1; - Thread::wait(500); + Thread bt_t(bluetooth_thread); + while(1) { + sonar.checkDistance(); } }