![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Coding for vehicle rear assistant
Fork of Vehicle_Rear_Assistant by
Vehicle Rear Assistant ELEC 2645 el14a2o
main.h@0:53f28990602d, 2015-05-08 (annotated)
- Committer:
- aminomar
- Date:
- Fri May 08 21:54:44 2015 +0000
- Revision:
- 0:53f28990602d
code finished;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aminomar | 0:53f28990602d | 1 | /** |
aminomar | 0:53f28990602d | 2 | @file main.h |
aminomar | 0:53f28990602d | 3 | @brief Header file containing functions prototypes, defines and global variables. |
aminomar | 0:53f28990602d | 4 | @brief The software of Vehicle Rear Assistant. |
aminomar | 0:53f28990602d | 5 | @brief ELEC 2645 |
aminomar | 0:53f28990602d | 6 | @author Aminuddin Omar |
aminomar | 0:53f28990602d | 7 | @date APRIL 2015 |
aminomar | 0:53f28990602d | 8 | */ |
aminomar | 0:53f28990602d | 9 | |
aminomar | 0:53f28990602d | 10 | #ifndef MAIN_H |
aminomar | 0:53f28990602d | 11 | #define MAIN_H |
aminomar | 0:53f28990602d | 12 | |
aminomar | 0:53f28990602d | 13 | //Declare libraries to be used |
aminomar | 0:53f28990602d | 14 | #include "mbed.h" |
aminomar | 0:53f28990602d | 15 | #include "PowerControl/PowerControl.h" |
aminomar | 0:53f28990602d | 16 | #include "PowerControl/EthernetPowerControl.h" |
aminomar | 0:53f28990602d | 17 | #include "N5110.h" |
aminomar | 0:53f28990602d | 18 | #include "SRF02.h" |
aminomar | 0:53f28990602d | 19 | #include "beep.h" |
aminomar | 0:53f28990602d | 20 | |
aminomar | 0:53f28990602d | 21 | /** |
aminomar | 0:53f28990602d | 22 | @namespace lcd |
aminomar | 0:53f28990602d | 23 | @brief GPIO pins for LCD display |
aminomar | 0:53f28990602d | 24 | @namespace sensor |
aminomar | 0:53f28990602d | 25 | @brief GPIO pins for SRF02 |
aminomar | 0:53f28990602d | 26 | @namespace buzzer |
aminomar | 0:53f28990602d | 27 | @brief PWM pin for piezo buzzer |
aminomar | 0:53f28990602d | 28 | @namespace danger |
aminomar | 0:53f28990602d | 29 | @brief PWM pin for RED LED |
aminomar | 0:53f28990602d | 30 | @namespace caution |
aminomar | 0:53f28990602d | 31 | @brief PWM pin for YELLOW LED |
aminomar | 0:53f28990602d | 32 | @namespace safe |
aminomar | 0:53f28990602d | 33 | @brief PWM pin for GREEN LED |
aminomar | 0:53f28990602d | 34 | @namespace lcdBrightness |
aminomar | 0:53f28990602d | 35 | @brief PWM pin for LCD backlight |
aminomar | 0:53f28990602d | 36 | @namespace potBrightness |
aminomar | 0:53f28990602d | 37 | @brief AnalogIn pin for potentiometer |
aminomar | 0:53f28990602d | 38 | @namespace button |
aminomar | 0:53f28990602d | 39 | @brief InterruptIn pin for button |
aminomar | 0:53f28990602d | 40 | */ |
aminomar | 0:53f28990602d | 41 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); //(LCD_VCC, sce, rst, dc, mosi, sck, backlight) |
aminomar | 0:53f28990602d | 42 | SRF02 sensor(p28, p27); |
aminomar | 0:53f28990602d | 43 | Beep buzzer(p21); |
aminomar | 0:53f28990602d | 44 | PwmOut danger(p24); //danger = red |
aminomar | 0:53f28990602d | 45 | PwmOut caution(p23); //caution = yellow |
aminomar | 0:53f28990602d | 46 | PwmOut safe(p22); //safe = green |
aminomar | 0:53f28990602d | 47 | PwmOut lcdBrightness(p26); //lcd backlight (pin26) |
aminomar | 0:53f28990602d | 48 | AnalogIn potBrightness(p20); //potentiometer (pin20) |
aminomar | 0:53f28990602d | 49 | InterruptIn button(p17); //push button to display value of distance |
aminomar | 0:53f28990602d | 50 | |
aminomar | 0:53f28990602d | 51 | //variables |
aminomar | 0:53f28990602d | 52 | int distance; /*!< measure distance between car and object */ |
aminomar | 0:53f28990602d | 53 | int buttonFlag = 0; /*!< print flag set in ISR */ |
aminomar | 0:53f28990602d | 54 | int displayReading = 0; /*!< display the value of distance on LCD */ |
aminomar | 0:53f28990602d | 55 | |
aminomar | 0:53f28990602d | 56 | //Initialise all the functions |
aminomar | 0:53f28990602d | 57 | |
aminomar | 0:53f28990602d | 58 | /** |
aminomar | 0:53f28990602d | 59 | Print welcome message when switch is turned on |
aminomar | 0:53f28990602d | 60 | @param a - set number of letters that can be written including space |
aminomar | 0:53f28990602d | 61 | @param message - Print HELLO |
aminomar | 0:53f28990602d | 62 | @param rear - Print REAR |
aminomar | 0:53f28990602d | 63 | @param assistant - Print ASSISTANT |
aminomar | 0:53f28990602d | 64 | @param name - Print A.OMAR |
aminomar | 0:53f28990602d | 65 | @param number - Print 200906568 |
aminomar | 0:53f28990602d | 66 | */ |
aminomar | 0:53f28990602d | 67 | void welcomeText(); |
aminomar | 0:53f28990602d | 68 | |
aminomar | 0:53f28990602d | 69 | /** |
aminomar | 0:53f28990602d | 70 | Measure distance between car and objects |
aminomar | 0:53f28990602d | 71 | @param distance - use SRF02 to measure distance |
aminomar | 0:53f28990602d | 72 | */ |
aminomar | 0:53f28990602d | 73 | void getDistance(); |
aminomar | 0:53f28990602d | 74 | |
aminomar | 0:53f28990602d | 75 | /** |
aminomar | 0:53f28990602d | 76 | Print "CAR" and a block on LCD according to distance measured |
aminomar | 0:53f28990602d | 77 | @param lcd.drawRect - draw rectangle to represent object |
aminomar | 0:53f28990602d | 78 | @param lcd.printString - draw "CAR" to represent car |
aminomar | 0:53f28990602d | 79 | @param lcd.refresh() - refresh LCD display |
aminomar | 0:53f28990602d | 80 | @param distanceSafe() - GREEN LED and buzzer is activated in safe mode |
aminomar | 0:53f28990602d | 81 | @param distanceCaution() - YELLOW LED and buzzer is activated in caution mode |
aminomar | 0:53f28990602d | 82 | @param distanceDanger() - RED LED and buzzer is activated in danger mode |
aminomar | 0:53f28990602d | 83 | */ |
aminomar | 0:53f28990602d | 84 | void readDistance(); |
aminomar | 0:53f28990602d | 85 | |
aminomar | 0:53f28990602d | 86 | /** |
aminomar | 0:53f28990602d | 87 | This function is called when in safe mode |
aminomar | 0:53f28990602d | 88 | @param safe - only GREEN LED light up |
aminomar | 0:53f28990602d | 89 | @brief buzzer is called and beep according to distance |
aminomar | 0:53f28990602d | 90 | */ |
aminomar | 0:53f28990602d | 91 | void distanceSafe(); |
aminomar | 0:53f28990602d | 92 | |
aminomar | 0:53f28990602d | 93 | /** |
aminomar | 0:53f28990602d | 94 | This function is called when in caution mode |
aminomar | 0:53f28990602d | 95 | @param safe - only YELLOW LED light up |
aminomar | 0:53f28990602d | 96 | @brief buzzer is called and beep according to distance |
aminomar | 0:53f28990602d | 97 | */ |
aminomar | 0:53f28990602d | 98 | void distanceCaution(); |
aminomar | 0:53f28990602d | 99 | |
aminomar | 0:53f28990602d | 100 | /** |
aminomar | 0:53f28990602d | 101 | This function is called when in danger mode |
aminomar | 0:53f28990602d | 102 | @param safe - only RED LED light up |
aminomar | 0:53f28990602d | 103 | @brief buzzer is called and beep according to distance |
aminomar | 0:53f28990602d | 104 | */ |
aminomar | 0:53f28990602d | 105 | void distanceDanger(); |
aminomar | 0:53f28990602d | 106 | |
aminomar | 0:53f28990602d | 107 | /** |
aminomar | 0:53f28990602d | 108 | This function is called when button is pressed |
aminomar | 0:53f28990602d | 109 | @param buttonFlag - set the button flag to 1 |
aminomar | 0:53f28990602d | 110 | */ |
aminomar | 0:53f28990602d | 111 | void buttonPressed(); |
aminomar | 0:53f28990602d | 112 | |
aminomar | 0:53f28990602d | 113 | |
aminomar | 0:53f28990602d | 114 | //Print welcome message (letters are print one by one) |
aminomar | 0:53f28990602d | 115 | void welcomeText() |
aminomar | 0:53f28990602d | 116 | { |
aminomar | 0:53f28990602d | 117 | int a[9] = {12,18,24,30,36,42,48,54,60}; |
aminomar | 0:53f28990602d | 118 | const char message[6] = {'H','E','L','L','O','!'}; |
aminomar | 0:53f28990602d | 119 | char rear[4] = {'R','E','A','R'}; |
aminomar | 0:53f28990602d | 120 | char assistant[9] = {'A','S','S','I','S','T','A','N','T'}; |
aminomar | 0:53f28990602d | 121 | char name[6] = {'A','.','O','M','A','R'}; |
aminomar | 0:53f28990602d | 122 | char number[9] = {'2','0','0','9','0','6','5','6','8'}; |
aminomar | 0:53f28990602d | 123 | int i; |
aminomar | 0:53f28990602d | 124 | for(i=0; i<6; i++) { |
aminomar | 0:53f28990602d | 125 | lcd.printChar(message[i],a[i],0); |
aminomar | 0:53f28990602d | 126 | wait(0.05); |
aminomar | 0:53f28990602d | 127 | } |
aminomar | 0:53f28990602d | 128 | for(i=0; i<4; i++) { |
aminomar | 0:53f28990602d | 129 | lcd.printChar(rear[i],a[i],2); |
aminomar | 0:53f28990602d | 130 | wait(0.05); |
aminomar | 0:53f28990602d | 131 | } |
aminomar | 0:53f28990602d | 132 | for(i=0; i<9; i++) { |
aminomar | 0:53f28990602d | 133 | lcd.printChar(assistant[i],a[i],3); |
aminomar | 0:53f28990602d | 134 | wait(0.05); |
aminomar | 0:53f28990602d | 135 | } |
aminomar | 0:53f28990602d | 136 | for(i=0; i<6; i++) { |
aminomar | 0:53f28990602d | 137 | lcd.printChar(name[i],a[i],4); |
aminomar | 0:53f28990602d | 138 | wait(0.05); |
aminomar | 0:53f28990602d | 139 | } |
aminomar | 0:53f28990602d | 140 | for(i=0; i<9; i++) { |
aminomar | 0:53f28990602d | 141 | lcd.printChar(number[i],a[i],5); |
aminomar | 0:53f28990602d | 142 | wait(0.05); |
aminomar | 0:53f28990602d | 143 | } |
aminomar | 0:53f28990602d | 144 | lcd.refresh(); |
aminomar | 0:53f28990602d | 145 | wait(2.5); |
aminomar | 0:53f28990602d | 146 | } |
aminomar | 0:53f28990602d | 147 | |
aminomar | 0:53f28990602d | 148 | //sensor is set to get the distance |
aminomar | 0:53f28990602d | 149 | void getDistance() |
aminomar | 0:53f28990602d | 150 | { |
aminomar | 0:53f28990602d | 151 | distance = sensor.getDistanceCm(); |
aminomar | 0:53f28990602d | 152 | } |
aminomar | 0:53f28990602d | 153 | |
aminomar | 0:53f28990602d | 154 | //sensor read distance & lcd display 'CAR' and 'rectangle' |
aminomar | 0:53f28990602d | 155 | void readDistance() |
aminomar | 0:53f28990602d | 156 | { |
aminomar | 0:53f28990602d | 157 | if (distance >= 230) |
aminomar | 0:53f28990602d | 158 | { |
aminomar | 0:53f28990602d | 159 | lcd.drawRect(76,0,7,48,1); //WALL lcd.drawRect(76(x),0(y),7(w),48(h),1(type)) |
aminomar | 0:53f28990602d | 160 | lcd.printString("CAR",5,3); //CAR |
aminomar | 0:53f28990602d | 161 | lcd.refresh(); |
aminomar | 0:53f28990602d | 162 | distanceSafe(); |
aminomar | 0:53f28990602d | 163 | } |
aminomar | 0:53f28990602d | 164 | else if (distance >= 190 & distance <= 229) |
aminomar | 0:53f28990602d | 165 | { |
aminomar | 0:53f28990602d | 166 | lcd.drawRect(76,0,7,48,1); //WALL |
aminomar | 0:53f28990602d | 167 | lcd.printString("CAR",15,3);//CAR |
aminomar | 0:53f28990602d | 168 | lcd.refresh(); |
aminomar | 0:53f28990602d | 169 | distanceSafe(); |
aminomar | 0:53f28990602d | 170 | } |
aminomar | 0:53f28990602d | 171 | else if (distance >= 150 & distance <= 189) |
aminomar | 0:53f28990602d | 172 | { |
aminomar | 0:53f28990602d | 173 | lcd.drawRect(76,0,7,48,1); //WALL |
aminomar | 0:53f28990602d | 174 | lcd.printString("CAR",25,3);//CAR |
aminomar | 0:53f28990602d | 175 | lcd.refresh(); |
aminomar | 0:53f28990602d | 176 | distanceSafe(); |
aminomar | 0:53f28990602d | 177 | } |
aminomar | 0:53f28990602d | 178 | else if (distance >= 120 & distance <= 149) |
aminomar | 0:53f28990602d | 179 | { |
aminomar | 0:53f28990602d | 180 | lcd.drawRect(76,0,7,48,1); //WALL |
aminomar | 0:53f28990602d | 181 | lcd.printString("CAR",30,3);//CAR |
aminomar | 0:53f28990602d | 182 | lcd.refresh(); |
aminomar | 0:53f28990602d | 183 | distanceCaution(); |
aminomar | 0:53f28990602d | 184 | } |
aminomar | 0:53f28990602d | 185 | else if (distance >= 100 & distance <= 119) |
aminomar | 0:53f28990602d | 186 | { |
aminomar | 0:53f28990602d | 187 | lcd.drawRect(76,0,7,48,1); //WALL |
aminomar | 0:53f28990602d | 188 | lcd.printString("CAR",39,3);//CAR |
aminomar | 0:53f28990602d | 189 | lcd.refresh(); |
aminomar | 0:53f28990602d | 190 | distanceCaution(); |
aminomar | 0:53f28990602d | 191 | } |
aminomar | 0:53f28990602d | 192 | else if (distance >= 70 & distance <= 99) |
aminomar | 0:53f28990602d | 193 | { |
aminomar | 0:53f28990602d | 194 | lcd.drawRect(76,0,7,48,1); //WALL |
aminomar | 0:53f28990602d | 195 | lcd.printString("CAR",48,3);//CAR |
aminomar | 0:53f28990602d | 196 | lcd.refresh(); |
aminomar | 0:53f28990602d | 197 | distanceCaution(); |
aminomar | 0:53f28990602d | 198 | } |
aminomar | 0:53f28990602d | 199 | else if (distance >= 45& distance <= 69) |
aminomar | 0:53f28990602d | 200 | { |
aminomar | 0:53f28990602d | 201 | lcd.drawRect(76,0,7,48,1); //WALL |
aminomar | 0:53f28990602d | 202 | lcd.printString("CAR",55,3);//CAR |
aminomar | 0:53f28990602d | 203 | lcd.refresh(); |
aminomar | 0:53f28990602d | 204 | distanceDanger(); |
aminomar | 0:53f28990602d | 205 | } |
aminomar | 0:53f28990602d | 206 | else if (distance <= 44) |
aminomar | 0:53f28990602d | 207 | { |
aminomar | 0:53f28990602d | 208 | lcd.printString("!!STOP!!",25,3);//print "STOP" only |
aminomar | 0:53f28990602d | 209 | lcd.refresh(); |
aminomar | 0:53f28990602d | 210 | distanceDanger(); |
aminomar | 0:53f28990602d | 211 | } |
aminomar | 0:53f28990602d | 212 | } |
aminomar | 0:53f28990602d | 213 | |
aminomar | 0:53f28990602d | 214 | //safe to reverse car |
aminomar | 0:53f28990602d | 215 | void distanceSafe() |
aminomar | 0:53f28990602d | 216 | { |
aminomar | 0:53f28990602d | 217 | danger = 0; |
aminomar | 0:53f28990602d | 218 | caution = 0; |
aminomar | 0:53f28990602d | 219 | safe = 1; //>>>>>>>>>safe = green led light up in safe mode |
aminomar | 0:53f28990602d | 220 | buzzer.beep(1000,1); |
aminomar | 0:53f28990602d | 221 | wait(1); |
aminomar | 0:53f28990602d | 222 | buzzer.nobeep(); |
aminomar | 0:53f28990602d | 223 | } |
aminomar | 0:53f28990602d | 224 | |
aminomar | 0:53f28990602d | 225 | //caution : reverse car carefuly |
aminomar | 0:53f28990602d | 226 | void distanceCaution() |
aminomar | 0:53f28990602d | 227 | { |
aminomar | 0:53f28990602d | 228 | danger = 0; |
aminomar | 0:53f28990602d | 229 | caution = 1; //>>>>>>caution = yellow led light up in caution mode |
aminomar | 0:53f28990602d | 230 | safe = 0; |
aminomar | 0:53f28990602d | 231 | buzzer.beep(1000,1); |
aminomar | 0:53f28990602d | 232 | wait(0.4); |
aminomar | 0:53f28990602d | 233 | buzzer.nobeep(); |
aminomar | 0:53f28990602d | 234 | } |
aminomar | 0:53f28990602d | 235 | |
aminomar | 0:53f28990602d | 236 | //danger : stop reverse car |
aminomar | 0:53f28990602d | 237 | void distanceDanger() |
aminomar | 0:53f28990602d | 238 | { |
aminomar | 0:53f28990602d | 239 | danger = 1; //>>>>>>>danger = red led light up in danger mode |
aminomar | 0:53f28990602d | 240 | caution = 0; |
aminomar | 0:53f28990602d | 241 | safe = 0; |
aminomar | 0:53f28990602d | 242 | buzzer.beep(1000,1); |
aminomar | 0:53f28990602d | 243 | wait(0.2); |
aminomar | 0:53f28990602d | 244 | buzzer.nobeep(); |
aminomar | 0:53f28990602d | 245 | } |
aminomar | 0:53f28990602d | 246 | |
aminomar | 0:53f28990602d | 247 | |
aminomar | 0:53f28990602d | 248 | //INTERRUPT SERVICE ROUTINE |
aminomar | 0:53f28990602d | 249 | void buttonPressed() |
aminomar | 0:53f28990602d | 250 | { |
aminomar | 0:53f28990602d | 251 | buttonFlag = 1; //set flag |
aminomar | 0:53f28990602d | 252 | } |
aminomar | 0:53f28990602d | 253 | |
aminomar | 0:53f28990602d | 254 | #endif |