basic version

Dependencies:   C12832_lcd USBHost mbed

Committer:
Hjordan
Date:
Thu Jan 15 16:32:56 2015 +0000
Revision:
4:429d9a63eb79
Parent:
3:be269540df58
Child:
5:e82e00f12634
Smoother movement, maybe...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hjordan 1:ebf16f5cbdec 1 #include "mbed.h"
Hjordan 1:ebf16f5cbdec 2 #include "rtos.h"
Hjordan 1:ebf16f5cbdec 3 #include "C12832_lcd.h"
Hjordan 1:ebf16f5cbdec 4
Hjordan 1:ebf16f5cbdec 5 float sonarDistance;
Hjordan 1:ebf16f5cbdec 6 float servoPosition;
Hjordan 1:ebf16f5cbdec 7
Hjordan 1:ebf16f5cbdec 8 Mutex sonarDistance_mutex;
Hjordan 1:ebf16f5cbdec 9 Mutex servoPosition_mutex;
Hjordan 1:ebf16f5cbdec 10
Hjordan 1:ebf16f5cbdec 11 AnalogIn sonarPin(p17);
Hjordan 1:ebf16f5cbdec 12 PwmOut servoPin(p21);
Hjordan 1:ebf16f5cbdec 13
Hjordan 1:ebf16f5cbdec 14 C12832_LCD lcd;
Hjordan 1:ebf16f5cbdec 15
Hjordan 1:ebf16f5cbdec 16 void sonarSensor(void const *args){
Hjordan 1:ebf16f5cbdec 17 while(true){
Hjordan 1:ebf16f5cbdec 18 sonarDistance_mutex.lock();
Hjordan 1:ebf16f5cbdec 19 sonarDistance = sonarPin.read();
Hjordan 1:ebf16f5cbdec 20 sonarDistance_mutex.unlock();
Hjordan 1:ebf16f5cbdec 21 }
Hjordan 1:ebf16f5cbdec 22 }
Hjordan 1:ebf16f5cbdec 23
Hjordan 1:ebf16f5cbdec 24 void servoControl(void const *args){
Hjordan 1:ebf16f5cbdec 25 while(true){
Hjordan 1:ebf16f5cbdec 26 servoPosition_mutex.lock();
Hjordan 1:ebf16f5cbdec 27 servoPin.write(servoPosition);
Hjordan 1:ebf16f5cbdec 28 servoPosition_mutex.unlock();
Hjordan 1:ebf16f5cbdec 29 }
Hjordan 1:ebf16f5cbdec 30 }
Hjordan 1:ebf16f5cbdec 31
Hjordan 1:ebf16f5cbdec 32 void logic(void const *args){
Hjordan 1:ebf16f5cbdec 33 while(true){
Hjordan 1:ebf16f5cbdec 34 sonarDistance_mutex.lock();
Hjordan 1:ebf16f5cbdec 35 servoPosition_mutex.lock();
Hjordan 4:429d9a63eb79 36 servoPosition = (sonarDistance - servoPosition) / 2;
Hjordan 1:ebf16f5cbdec 37 sonarDistance_mutex.unlock();
Hjordan 1:ebf16f5cbdec 38 servoPosition_mutex.unlock();
Hjordan 1:ebf16f5cbdec 39 }
Hjordan 1:ebf16f5cbdec 40 }
Hjordan 1:ebf16f5cbdec 41
Hjordan 1:ebf16f5cbdec 42 void display(void const *args){
Hjordan 1:ebf16f5cbdec 43 while(true){
Hjordan 1:ebf16f5cbdec 44 sonarDistance_mutex.lock();
Hjordan 1:ebf16f5cbdec 45 servoPosition_mutex.lock();
Hjordan 1:ebf16f5cbdec 46 lcd.cls();
Hjordan 1:ebf16f5cbdec 47 lcd.locate(0,0);
Hjordan 3:be269540df58 48 lcd.printf("Sonar : %3.2f \nServo : %3.2f \nSCIENCE!",sonarDistance,servoPosition);
Hjordan 1:ebf16f5cbdec 49 sonarDistance_mutex.unlock();
Hjordan 1:ebf16f5cbdec 50 servoPosition_mutex.unlock();
Hjordan 1:ebf16f5cbdec 51 Thread::wait(200);
Hjordan 1:ebf16f5cbdec 52
Hjordan 1:ebf16f5cbdec 53 }
Hjordan 1:ebf16f5cbdec 54 }
Hjordan 1:ebf16f5cbdec 55
Hjordan 1:ebf16f5cbdec 56 int main(){
Hjordan 1:ebf16f5cbdec 57 sonarDistance = 0.0f;
Hjordan 1:ebf16f5cbdec 58 servoPosition = 0.0f;
Hjordan 1:ebf16f5cbdec 59
Hjordan 1:ebf16f5cbdec 60
Hjordan 1:ebf16f5cbdec 61 Thread sonarSensor_thread(sonarSensor);
Hjordan 1:ebf16f5cbdec 62 Thread servoControl_thread(servoControl);
Hjordan 1:ebf16f5cbdec 63 Thread logic_thread(logic);
Hjordan 1:ebf16f5cbdec 64 Thread display_thread(display);
Hjordan 1:ebf16f5cbdec 65
Hjordan 1:ebf16f5cbdec 66 while(true){
Hjordan 1:ebf16f5cbdec 67 }
Hjordan 1:ebf16f5cbdec 68 }