basic version

Dependencies:   C12832_lcd USBHost mbed

Committer:
cathal66
Date:
Mon Jan 26 23:01:46 2015 +0000
Revision:
8:b22ef9f44549
Parent:
7:0fa7430ab812
Child:
9:88d70b0c1b8b
Added Comments; Added PWM to the led; Adjusted the value for the LED

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
cathal66 7:0fa7430ab812 5 //Varible
Hjordan 1:ebf16f5cbdec 6 float sonarDistance;
Hjordan 1:ebf16f5cbdec 7 float servoPosition;
Hjordan 1:ebf16f5cbdec 8
cathal66 7:0fa7430ab812 9 //Mutex
cathal66 8:b22ef9f44549 10 Mutex sonarDistance_mutex; //Init Mutex for sensor value
cathal66 8:b22ef9f44549 11 Mutex servoPosition_mutex; //Init Mutex for servo value
cathal66 7:0fa7430ab812 12
cathal66 7:0fa7430ab812 13 //I/O
cathal66 8:b22ef9f44549 14 AnalogIn sonarPin(p17); //Assign pin to read sonar sensor
cathal66 8:b22ef9f44549 15 PwmOut servoPin(p21); //Assign pin for servo output
cathal66 8:b22ef9f44549 16 PwmOut myled1(LED1); //Assign pin for LED1
cathal66 8:b22ef9f44549 17 PwmOut myled2(LED2); //Assign pin for LED2
cathal66 8:b22ef9f44549 18 PwmOut myled3(LED3); //Assign pin for LED3
cathal66 8:b22ef9f44549 19 PwmOut myled4(LED4); //Assign pin for LED4
Hjordan 1:ebf16f5cbdec 20
cathal66 7:0fa7430ab812 21 //LCD Setup
cathal66 8:b22ef9f44549 22 C12832_LCD lcd; //setup LCD screen
Hjordan 1:ebf16f5cbdec 23
Hjordan 1:ebf16f5cbdec 24 void sonarSensor(void const *args){
Hjordan 1:ebf16f5cbdec 25 while(true){
cathal66 8:b22ef9f44549 26 sonarDistance_mutex.lock(); //Mutex lock for the Servo value
cathal66 8:b22ef9f44549 27 sonarDistance = sonarPin.read(); //Read analog voltage and store in variable
cathal66 8:b22ef9f44549 28 sonarDistance_mutex.unlock(); //Mutex unlock for the Servo value
cathal66 8:b22ef9f44549 29 Thread::wait(10); //Pause thread for 10msec
cathal66 6:4c62f9c91b1d 30 } //End of While loop
cathal66 6:4c62f9c91b1d 31 } //End of Thread
Hjordan 1:ebf16f5cbdec 32
Hjordan 1:ebf16f5cbdec 33 void servoControl(void const *args){
Hjordan 1:ebf16f5cbdec 34 while(true){
cathal66 8:b22ef9f44549 35 servoPosition_mutex.lock(); //Mutex lock for the Servo value
cathal66 8:b22ef9f44549 36 servoPin.write(servoPosition/2); //Write servo value to servo pin
cathal66 8:b22ef9f44549 37 servoPosition_mutex.unlock(); //Mutex unlock for the Servo value
cathal66 6:4c62f9c91b1d 38 } //end of while loop
cathal66 6:4c62f9c91b1d 39 } //end of thread
Hjordan 1:ebf16f5cbdec 40
cathal66 6:4c62f9c91b1d 41 void logic(void const *args)
cathal66 6:4c62f9c91b1d 42 {
cathal66 8:b22ef9f44549 43 int Size=16;
cathal66 8:b22ef9f44549 44 float Average_4[16]; //number of value in the array
cathal66 7:0fa7430ab812 45 float Average_Sum=0;
cathal66 6:4c62f9c91b1d 46 while(true)
cathal66 6:4c62f9c91b1d 47 {
cathal66 7:0fa7430ab812 48
cathal66 7:0fa7430ab812 49 for(int i=0;i<Size;i++)
cathal66 6:4c62f9c91b1d 50 {
cathal66 7:0fa7430ab812 51
cathal66 8:b22ef9f44549 52 Average_Sum = Average_Sum-Average_4[i]; //Remove the 4th oldest value from the average sum
cathal66 8:b22ef9f44549 53
cathal66 8:b22ef9f44549 54 sonarDistance_mutex.lock(); //Mutex lock for the Sonar value
cathal66 8:b22ef9f44549 55 Average_4[i]= sonarDistance; //Add the new value to the array
cathal66 8:b22ef9f44549 56 sonarDistance_mutex.unlock(); //Mutex unlock for the Sonar value
cathal66 7:0fa7430ab812 57
cathal66 8:b22ef9f44549 58 Average_Sum = Average_Sum + Average_4[i]; //Add the new array value to the sum
cathal66 8:b22ef9f44549 59
cathal66 8:b22ef9f44549 60 servoPosition_mutex.lock(); //Mutex lock for the servo value
cathal66 8:b22ef9f44549 61 servoPosition = Average_Sum/Size; //Divide the array by the number of element in the array
cathal66 8:b22ef9f44549 62 servoPosition_mutex.unlock(); //Mutex unlock for the servo value
cathal66 7:0fa7430ab812 63
cathal66 6:4c62f9c91b1d 64 }//end for loop
cathal66 6:4c62f9c91b1d 65
cathal66 6:4c62f9c91b1d 66 }//end of while loop
cathal66 6:4c62f9c91b1d 67 }//End of thread
Hjordan 1:ebf16f5cbdec 68
Hjordan 1:ebf16f5cbdec 69 void display(void const *args){
Hjordan 1:ebf16f5cbdec 70 while(true){
cathal66 8:b22ef9f44549 71 sonarDistance_mutex.lock(); //Mutex lock for the sonar value
cathal66 8:b22ef9f44549 72 servoPosition_mutex.lock(); //Mutex lock for the servo value
cathal66 8:b22ef9f44549 73 lcd.cls(); //Clear the lcd
cathal66 8:b22ef9f44549 74 lcd.locate(0,0); //Cursor postion on the lcd
cathal66 8:b22ef9f44549 75 lcd.printf("Sonar : %3.6f \nServo : %3.6f \nSCIENCE!",sonarDistance,servoPosition);
cathal66 8:b22ef9f44549 76 sonarDistance_mutex.unlock(); //Mutex unlock for the sonar value
cathal66 8:b22ef9f44549 77 servoPosition_mutex.unlock(); //Mutex unlock for the servo value
cathal66 8:b22ef9f44549 78 Thread::wait(200); //Thread Wait
Hjordan 1:ebf16f5cbdec 79
Hjordan 1:ebf16f5cbdec 80 }
Hjordan 1:ebf16f5cbdec 81 }
Hjordan 1:ebf16f5cbdec 82
cathal66 7:0fa7430ab812 83 void LED_meter(void const *args) {
cathal66 7:0fa7430ab812 84
cathal66 8:b22ef9f44549 85 float LED_distance = 0; //Create var to store sonar distance
cathal66 7:0fa7430ab812 86
cathal66 7:0fa7430ab812 87 while(true) {
cathal66 8:b22ef9f44549 88 sonarDistance_mutex.lock(); //Mutex lock for the sonar value
cathal66 8:b22ef9f44549 89 LED_distance=sonarDistance; //Store value of sonar sensor in LED_distance
cathal66 8:b22ef9f44549 90 sonarDistance_mutex.unlock(); //Mutex unlock for the sonar value
cathal66 7:0fa7430ab812 91
cathal66 8:b22ef9f44549 92 if(LED_distance<=0.15)
cathal66 7:0fa7430ab812 93 {
cathal66 8:b22ef9f44549 94 myled1.write(((LED_distance-0.15)*-1)*20); //PWM the LED from 0% to 100%
cathal66 7:0fa7430ab812 95 }
cathal66 7:0fa7430ab812 96 else
cathal66 7:0fa7430ab812 97 {
cathal66 8:b22ef9f44549 98 myled1 = 0; //Turn off LED
cathal66 7:0fa7430ab812 99 }
cathal66 7:0fa7430ab812 100
cathal66 8:b22ef9f44549 101 if(LED_distance<=0.10)
cathal66 7:0fa7430ab812 102 {
cathal66 8:b22ef9f44549 103 myled2.write(((LED_distance-0.10)*-1)*20); //PWM the LED from 0% to 100%
cathal66 7:0fa7430ab812 104 }
cathal66 7:0fa7430ab812 105 else
cathal66 7:0fa7430ab812 106 {
cathal66 8:b22ef9f44549 107 myled2 = 0; //Turn off LED
cathal66 7:0fa7430ab812 108 }
cathal66 7:0fa7430ab812 109
cathal66 8:b22ef9f44549 110 if(LED_distance<=0.05)
cathal66 7:0fa7430ab812 111 {
cathal66 8:b22ef9f44549 112 myled3.write(((LED_distance-0.05)*-1)*40); //PWM the LED from 0% to 100%
cathal66 7:0fa7430ab812 113 }
cathal66 7:0fa7430ab812 114 else
cathal66 7:0fa7430ab812 115 {
cathal66 8:b22ef9f44549 116 myled3 = 0; //Turn off LED
cathal66 7:0fa7430ab812 117 }
cathal66 7:0fa7430ab812 118
cathal66 8:b22ef9f44549 119 if(LED_distance<=0.025)
cathal66 7:0fa7430ab812 120 {
cathal66 8:b22ef9f44549 121 myled4.write(((LED_distance-0.025)*-1)*40); //PWM the LED from 0% to 100%
cathal66 7:0fa7430ab812 122 //s2=0.1;
cathal66 7:0fa7430ab812 123 }
cathal66 7:0fa7430ab812 124 else
cathal66 7:0fa7430ab812 125 {
cathal66 8:b22ef9f44549 126 myled4 = 0; //Turn off LED
cathal66 7:0fa7430ab812 127 //s2=1;
cathal66 7:0fa7430ab812 128 }
cathal66 7:0fa7430ab812 129 }
cathal66 7:0fa7430ab812 130 }
cathal66 7:0fa7430ab812 131
Hjordan 1:ebf16f5cbdec 132 int main(){
Hjordan 1:ebf16f5cbdec 133 sonarDistance = 0.0f;
Hjordan 1:ebf16f5cbdec 134 servoPosition = 0.0f;
Hjordan 1:ebf16f5cbdec 135
Hjordan 1:ebf16f5cbdec 136
Hjordan 1:ebf16f5cbdec 137 Thread sonarSensor_thread(sonarSensor);
Hjordan 1:ebf16f5cbdec 138 Thread servoControl_thread(servoControl);
Hjordan 1:ebf16f5cbdec 139 Thread logic_thread(logic);
Hjordan 1:ebf16f5cbdec 140 Thread display_thread(display);
cathal66 7:0fa7430ab812 141 Thread LED_meter_thread(LED_meter);
Hjordan 1:ebf16f5cbdec 142
cathal66 6:4c62f9c91b1d 143 while(true)
cathal66 6:4c62f9c91b1d 144 {
Hjordan 1:ebf16f5cbdec 145 }
Hjordan 1:ebf16f5cbdec 146 }