Robot that currently does nothing
Dependencies: 4DGL-uLCD-SE SDFileSystem ShiftBrite mbed-rtos mbed wave_player Motor
main.cpp
- Committer:
- jplager3
- Date:
- 2017-03-16
- Revision:
- 8:cde09ae38be7
- Parent:
- 7:df218a6a1a01
- Child:
- 9:37bb5f0e2975
File content as of revision 8:cde09ae38be7:
#include "mbed.h" #include "rtos.h" #include "uLCD_4DGL.h" //#include "ShiftBrite.h" #include "SDFileSystem.h" #include "wave_player.h" #include "Motor.h" Mutex mutex; // Mutex mutex2; // DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); Thread thread1; Thread thread2; Thread thread3; Thread thread4; SPI spi(p11, p12, p13); uLCD_4DGL uLCD(p28, p27, p30); SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card AnalogOut DACout(p18); //must be p18 AnalogIn IR(p20); FILE *wave_file = NULL; //global bc its gotta be changed by Main while running in child thread FILE *wave_file2 = NULL; wave_player waver(&DACout); //create wave_player object for speaker //create objects for Motor Control //PwmOut motorL(p21); // doesnt seem to work //Motor Left(p21, p12, p11); //, p6, p5, 1); // pwm, fwd, rev, can brake Motor Left(p21, p14, p13); // green wires. pwm, fwd, rev, add ", 1" for braking Motor Right(p22, p12, p11); // red wires void LCD_thread1() { while(1){ mutex.lock(); uLCD.filled_circle(64, 64, 12, 0xFF0000); mutex.unlock(); wait(.5); mutex.lock(); uLCD.filled_circle(64, 64, 12, 0x0000FF); mutex.unlock(); wait(.5); } } void sound_thread(){ while(1) { //FILE *wave_file; wave_file=fopen("/sd/vacuum.wav","r"); if (wave_file == NULL){ led1=led4 = 1; // if file read error, outside LEDs are on led2=led3 = 0; } waver.play(wave_file); fclose(wave_file); } } void beep(){ wave_file2=fopen("/sd/beep.wav","r"); if(wave_file2 == NULL) { led1=led4 = 0; // if file read error, inside LEDs are on led2=led3 = 0; } waver.play(wave_file2); fclose(wave_file2); } int main() { Left.speed(0.0); Right.speed(0.0); //thread1.start(IR_thread); // read in IR data thread2.start(LCD_thread1); //thread3.start(Motor_thread); thread4.start(sound_thread); while(1){ //motor testing //motorL.write(.8); for(float i=0.5;i>0.0;i-=0.1){ Left.speed(i); Right.speed(-i); wait(0.75); } //IR testing if(IR>0.3 && IR<0.5) { led1=1; led2=led3=led4=0; } else if (IR>0.5 && IR< 0.7) { led1=led2=1; led3=led4=0; } else if (IR>0.7 && IR<0.9) { led1=led2=led3=1; led4=0; } else if(IR>0.9){ //collision threshold seems to be the same from IR=0.8 to IR=0.95 led1=led2=led3=led4=1; fclose(wave_file); thread4.terminate(); thread4.start(beep); /* reverse motors and turn Roomba here * * * * */ //motorL.write(.5); // write reverse values to the motors for N rotations // execute turn by stopping one wheel, and turning the other (trial and error to find exact rotation?) // jump back to normal behavior wait(5); //wait for beeping to finish fclose(wave_file2); thread4.terminate(); thread4.start(sound_thread); } else led1=led2=led3=led4=0; } // use mutex to lock getc(), printf(), scanf() // don't unlock until you've checked that it's readable() }