People Counter / Mbed 2 deprecated person_counter

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library

Committer:
rmalik8
Date:
Tue Apr 28 20:35:55 2020 +0000
Revision:
25:577a78b97309
Parent:
24:fd113f826c98
Child:
26:ee962b024242
removed wave_player and SDFileSystem code and libararies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmarine3 2:1ec843c95914 1 #include "mbed.h"
rmalik8 4:7d02b3f3bee6 2 #include "rtos.h"
mmarine3 2:1ec843c95914 3 #include "uLCD_4DGL.h"
rmalik8 4:7d02b3f3bee6 4 #include "ultrasonic.h"
rmalik8 12:bebfef8d229f 5 #include "SongPlayer.h"
rmalik8 4:7d02b3f3bee6 6
rmalik8 3:b5cdd40e99e9 7 //#include <stdio.h>
mmarine3 2:1ec843c95914 8 #include <string>
mmarine3 2:1ec843c95914 9
rmalik8 4:7d02b3f3bee6 10
mmarine3 9:f4f03767acc0 11 uLCD_4DGL uLCD(p13, p14, p12); // Initialize uLCD serial tx, serial rx, reset pin;
rmalik8 25:577a78b97309 12
rmalik8 10:7a939a54515e 13 RawSerial blue(p9,p10); //Initialize Blutooth
rmalik8 12:bebfef8d229f 14 //Serial BT(p9,p10);
rmalik8 12:bebfef8d229f 15
mmarine3 20:1dc516a6a941 16 AnalogOut speaker(p18); //Initialize speaker
rmalik8 25:577a78b97309 17
rmalik8 12:bebfef8d229f 18
rmalik8 10:7a939a54515e 19 Mutex serial_mutex;
mmarine3 8:c0a6a3363e43 20 Mutex dist;
mmarine3 2:1ec843c95914 21 DigitalOut MyLED(LED1);
mmarine3 2:1ec843c95914 22
rmalik8 10:7a939a54515e 23 volatile int musicChoice = 0;
rmalik8 10:7a939a54515e 24 //musicChoice can be values 0 to 3
rmalik8 11:f5d0c8bf1849 25 volatile int sdist = 0;
rmalik8 12:bebfef8d229f 26 volatile int counter = 0;
mmarine3 21:4fef6bf7582c 27 volatile int numTowards = 0;
mmarine3 21:4fef6bf7582c 28 volatile int numAway = 0;
rmalik8 12:bebfef8d229f 29
rmalik8 12:bebfef8d229f 30 float note1[2]= {1568.0, 0.0
rmalik8 12:bebfef8d229f 31 };
rmalik8 13:3ba367bc16bc 32 float note2[2]= {1396.9, 0.0
rmalik8 12:bebfef8d229f 33 };
rmalik8 13:3ba367bc16bc 34 float note3[2]= {1244.5, 0.0
rmalik8 12:bebfef8d229f 35 };
mmarine3 20:1dc516a6a941 36 float duration[2]= {0.5, 0.0
rmalik8 12:bebfef8d229f 37 };
rmalik8 15:827fbc2e07d6 38
rmalik8 15:827fbc2e07d6 39 volatile int values[5] = {0,0,0,0,0};
rmalik8 15:827fbc2e07d6 40 volatile int index = 0;
mmarine3 20:1dc516a6a941 41 volatile float volume = 1.0;
rmalik8 15:827fbc2e07d6 42 volatile bool allZero = true;
mmarine3 19:c78aafa613b8 43 volatile bool movingTowards = false;
mmarine3 19:c78aafa613b8 44 volatile bool movingAway = false;
rmalik8 15:827fbc2e07d6 45 int state = 0;
mmarine3 18:7b1d65b007ab 46 int non_zeros = 0;
rmalik8 12:bebfef8d229f 47
rmalik8 12:bebfef8d229f 48
mmarine3 9:f4f03767acc0 49 void sonar(int distance)
rmalik8 4:7d02b3f3bee6 50 {
rmalik8 4:7d02b3f3bee6 51 //put code here to execute when the sonar distance has changed
mmarine3 9:f4f03767acc0 52 dist.lock();
mmarine3 9:f4f03767acc0 53 sdist = distance;
rmalik8 15:827fbc2e07d6 54 values[index] = distance;
mmarine3 9:f4f03767acc0 55 dist.unlock();
rmalik8 15:827fbc2e07d6 56
rmalik8 16:5250c9fe0408 57 //fill array with 5 values
rmalik8 15:827fbc2e07d6 58 index++;
rmalik8 15:827fbc2e07d6 59 if (index > 4)
rmalik8 15:827fbc2e07d6 60 {
rmalik8 15:827fbc2e07d6 61 index = 0;
rmalik8 15:827fbc2e07d6 62 }
rmalik8 15:827fbc2e07d6 63
mmarine3 18:7b1d65b007ab 64 //check if more than one value is zero (noise cancellation)
rmalik8 15:827fbc2e07d6 65 allZero = true;
mmarine3 19:c78aafa613b8 66 movingTowards = false;//NEW CODE
mmarine3 19:c78aafa613b8 67 movingAway = false;// NEW CODE
mmarine3 18:7b1d65b007ab 68 for (int i = 0; i < 5; i++) //Walk the list
mmarine3 18:7b1d65b007ab 69 {
mmarine3 18:7b1d65b007ab 70 if (values[i] != 0) //If value is not zero
rmalik8 15:827fbc2e07d6 71 {
mmarine3 18:7b1d65b007ab 72 ++non_zeros; //Incriment non_zero counter
mmarine3 18:7b1d65b007ab 73 //allZero = false;
mmarine3 24:fd113f826c98 74 if(non_zeros > 2) //If there is more than one non zero
mmarine3 18:7b1d65b007ab 75 {
mmarine3 18:7b1d65b007ab 76 allZero = false; //Change the allZero flag to false
mmarine3 18:7b1d65b007ab 77 }
rmalik8 15:827fbc2e07d6 78 }
mmarine3 19:c78aafa613b8 79 //NEW CODE
mmarine3 24:fd113f826c98 80 if(values[i] < values[(i+1)%5] && values[(i+1)%5] < values[(i+2)%5] && values[i] !=0)
mmarine3 19:c78aafa613b8 81 {
mmarine3 23:f877b13f22b7 82 movingAway = true; //Person moving away from sensor
mmarine3 19:c78aafa613b8 83 }
mmarine3 24:fd113f826c98 84 if (values[i] > values[(i+1)%5] && values[(i+1)%5] > values[(i+2)%5] && values[(i+2)%5] !=0)
mmarine3 19:c78aafa613b8 85 {
mmarine3 23:f877b13f22b7 86 movingTowards = true; //Person moving to the sensor
mmarine3 19:c78aafa613b8 87 }
mmarine3 19:c78aafa613b8 88 //
rmalik8 15:827fbc2e07d6 89 }
mmarine3 18:7b1d65b007ab 90 non_zeros = 0;
mmarine3 18:7b1d65b007ab 91
rmalik8 12:bebfef8d229f 92 //printf("Sonar Distance:\r\n %d", sdist);
mmarine3 9:f4f03767acc0 93 }
rmalik8 4:7d02b3f3bee6 94
mmarine3 9:f4f03767acc0 95 ultrasonic mu(p30, p29, .1, 1, &sonar); //Set the trigger pin to p30 and the echo pin to p29
rmalik8 12:bebfef8d229f 96 //have updates every .1 seconds and a timeout after 1
rmalik8 12:bebfef8d229f 97 //second, and call sonar when the distance changes
mmarine3 9:f4f03767acc0 98
mmarine3 9:f4f03767acc0 99 void Sonar(void const* arguments)
mmarine3 9:f4f03767acc0 100 {
mmarine3 9:f4f03767acc0 101 mu.startUpdates();//start measuring the distance
rmalik8 12:bebfef8d229f 102 while(1) {
mmarine3 9:f4f03767acc0 103 //Do something else here
mmarine3 9:f4f03767acc0 104 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
rmalik8 12:bebfef8d229f 105 //the class checks if dist needs to be called.
mmarine3 9:f4f03767acc0 106 Thread::wait(10);
rmalik8 4:7d02b3f3bee6 107 }
rmalik8 4:7d02b3f3bee6 108 }
mmarine3 2:1ec843c95914 109
mmarine3 8:c0a6a3363e43 110 //Thread to print the TOF and Sonar Values to the LCD
mmarine3 8:c0a6a3363e43 111 void LCD(void const *arguments)
mmarine3 8:c0a6a3363e43 112 {
mmarine3 8:c0a6a3363e43 113 Thread::wait(1000); //Wait for lidar and sonar setup
rmalik8 12:bebfef8d229f 114 serial_mutex.lock();
rmalik8 12:bebfef8d229f 115 uLCD.cls();
rmalik8 12:bebfef8d229f 116 uLCD.baudrate(BAUD_3000000);
mmarine3 21:4fef6bf7582c 117 uLCD.printf("Sonar Dist:\nTowards: \nAway: \nCounter: \nSong: \n");
rmalik8 12:bebfef8d229f 118 serial_mutex.unlock();
mmarine3 21:4fef6bf7582c 119 char str1[4];
mmarine3 21:4fef6bf7582c 120
rmalik8 12:bebfef8d229f 121 while(1) {
rmalik8 10:7a939a54515e 122 serial_mutex.lock();
mmarine3 21:4fef6bf7582c 123 //uLCD.cls();
mmarine3 8:c0a6a3363e43 124 dist.lock();
mmarine3 21:4fef6bf7582c 125 //uLCD.filled_rectangle(90,0,128,40,BLACK);
mmarine3 23:f877b13f22b7 126 sprintf(str1,"%d ",sdist);
mmarine3 21:4fef6bf7582c 127 uLCD.text_string(str1,12,0,FONT_5X7,GREEN);
mmarine3 21:4fef6bf7582c 128 sprintf(str1,"%d ",numTowards);
mmarine3 21:4fef6bf7582c 129 uLCD.text_string(str1,12,1,FONT_5X7,GREEN);
mmarine3 21:4fef6bf7582c 130 sprintf(str1,"%d ",numAway);
mmarine3 21:4fef6bf7582c 131 uLCD.text_string(str1,12,2,FONT_5X7,GREEN);
mmarine3 21:4fef6bf7582c 132 sprintf(str1,"%d ",counter);
mmarine3 21:4fef6bf7582c 133 uLCD.text_string(str1,12,3,FONT_5X7,GREEN);
mmarine3 21:4fef6bf7582c 134 sprintf(str1,"%d ",musicChoice);
mmarine3 21:4fef6bf7582c 135 uLCD.text_string(str1,12,4,FONT_5X7,GREEN);
mmarine3 21:4fef6bf7582c 136 //uLCD.printf("Sonar Dist:%d\nTowards: %d\nAway: %d\nCounter: %d \nSong: %d\n", sdist,numTowards,numAway, counter, musicChoice);
mmarine3 8:c0a6a3363e43 137 dist.unlock();
rmalik8 10:7a939a54515e 138 serial_mutex.unlock();
mmarine3 17:b8b68ff5e33a 139 Thread::wait(100); //Allow time to read value before reprint
rmalik8 12:bebfef8d229f 140
rmalik8 12:bebfef8d229f 141 }
rmalik8 12:bebfef8d229f 142 }
rmalik8 12:bebfef8d229f 143
rmalik8 12:bebfef8d229f 144 void sounds(void const *arguments)
rmalik8 12:bebfef8d229f 145 {
rmalik8 12:bebfef8d229f 146 int last_counter = 0;
mmarine3 17:b8b68ff5e33a 147 //FILE *wave_file;
rmalik8 12:bebfef8d229f 148 // setup instance of new SongPlayer class, mySpeaker using pin 26
rmalik8 12:bebfef8d229f 149 // the pin must be a PWM output pin
rmalik8 12:bebfef8d229f 150 SongPlayer mySpeaker(p26);
rmalik8 12:bebfef8d229f 151 // Start song and return once playing starts
rmalik8 12:bebfef8d229f 152 //mySpeaker.PlaySong(note1,duration);
rmalik8 12:bebfef8d229f 153 while(1) {
mmarine3 19:c78aafa613b8 154 if (counter != last_counter) {//CHANGE > to !=
rmalik8 12:bebfef8d229f 155 switch (musicChoice) {
mmarine3 17:b8b68ff5e33a 156 case 0:
mmarine3 17:b8b68ff5e33a 157 printf("Beep\r\n");
rmalik8 12:bebfef8d229f 158 //do nothing
rmalik8 12:bebfef8d229f 159 break;
mmarine3 17:b8b68ff5e33a 160 case 1:
mmarine3 17:b8b68ff5e33a 161 //wave_file=fopen("/sd/test.wav","r");
mmarine3 17:b8b68ff5e33a 162 //if(wave_file==NULL) {printf("file open error!\n\n\r");}
mmarine3 17:b8b68ff5e33a 163 //waver.play(wave_file);
mmarine3 17:b8b68ff5e33a 164 //fclose(wave_file);
mmarine3 20:1dc516a6a941 165 mySpeaker.PlaySong(note1,duration,volume);
rmalik8 12:bebfef8d229f 166 break;
mmarine3 17:b8b68ff5e33a 167 case 2:
mmarine3 20:1dc516a6a941 168 mySpeaker.PlaySong(note2,duration,volume);
rmalik8 12:bebfef8d229f 169 break;
mmarine3 17:b8b68ff5e33a 170 case 3:
mmarine3 20:1dc516a6a941 171 mySpeaker.PlaySong(note3,duration,volume);
rmalik8 12:bebfef8d229f 172 break;
rmalik8 12:bebfef8d229f 173 default:
mmarine3 17:b8b68ff5e33a 174 printf("Invalid Choice\r\n");
rmalik8 12:bebfef8d229f 175 break;
rmalik8 12:bebfef8d229f 176 }
mmarine3 17:b8b68ff5e33a 177 last_counter = counter;
rmalik8 12:bebfef8d229f 178 }
mmarine3 17:b8b68ff5e33a 179 //printf("Last Counter: %d\r\n",last_counter);
rmalik8 12:bebfef8d229f 180 Thread::wait(100);
mmarine3 8:c0a6a3363e43 181 }
mmarine3 8:c0a6a3363e43 182 }
mmarine3 8:c0a6a3363e43 183
rmalik8 10:7a939a54515e 184 void bluetooth(void const *arguments)
rmalik8 10:7a939a54515e 185 {
rmalik8 10:7a939a54515e 186 char bnum=0;
rmalik8 10:7a939a54515e 187 char bhit=0;
rmalik8 12:bebfef8d229f 188 while(1) {
rmalik8 10:7a939a54515e 189 serial_mutex.lock();
rmalik8 12:bebfef8d229f 190 if(!blue.readable()) {
rmalik8 12:bebfef8d229f 191 Thread::yield();
rmalik8 10:7a939a54515e 192 } else {
rmalik8 12:bebfef8d229f 193 if (blue.getc()=='!') {
rmalik8 12:bebfef8d229f 194 if (blue.getc()=='B') { //button data packet
rmalik8 12:bebfef8d229f 195 bnum = blue.getc(); //button number
rmalik8 12:bebfef8d229f 196 bhit = blue.getc(); //1=hit, 0=release
rmalik8 12:bebfef8d229f 197 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
rmalik8 12:bebfef8d229f 198
rmalik8 12:bebfef8d229f 199 switch (bnum) {
rmalik8 12:bebfef8d229f 200 case '1': //number button 1
rmalik8 12:bebfef8d229f 201 if (bhit=='1') {
rmalik8 12:bebfef8d229f 202 musicChoice = 0;
rmalik8 12:bebfef8d229f 203 } else {
rmalik8 12:bebfef8d229f 204 //add release code here
rmalik8 12:bebfef8d229f 205 }
rmalik8 12:bebfef8d229f 206 break;
rmalik8 12:bebfef8d229f 207 case '2': //number button 2
rmalik8 12:bebfef8d229f 208 if (bhit=='1') {
rmalik8 12:bebfef8d229f 209 musicChoice = 1;
rmalik8 12:bebfef8d229f 210 } else {
rmalik8 12:bebfef8d229f 211 //add release code here
rmalik8 12:bebfef8d229f 212 }
rmalik8 12:bebfef8d229f 213 break;
rmalik8 12:bebfef8d229f 214 case '3': //number button 3
rmalik8 12:bebfef8d229f 215 if (bhit=='1') {
rmalik8 12:bebfef8d229f 216 musicChoice = 2;
rmalik8 12:bebfef8d229f 217 } else {
rmalik8 12:bebfef8d229f 218 //add release code here
rmalik8 12:bebfef8d229f 219 }
rmalik8 12:bebfef8d229f 220 break;
rmalik8 12:bebfef8d229f 221 case '4': //number button 4
rmalik8 12:bebfef8d229f 222 if (bhit=='1') {
mmarine3 20:1dc516a6a941 223 musicChoice = 3;
rmalik8 12:bebfef8d229f 224 //add hit code here
rmalik8 12:bebfef8d229f 225 } else {
rmalik8 12:bebfef8d229f 226 //add release code here
rmalik8 12:bebfef8d229f 227 }
rmalik8 12:bebfef8d229f 228 break;
rmalik8 12:bebfef8d229f 229 case '5': //button 5 up arrow
rmalik8 12:bebfef8d229f 230 if (bhit=='1') {
mmarine3 20:1dc516a6a941 231 volume += .1;
rmalik8 12:bebfef8d229f 232 } else {
rmalik8 12:bebfef8d229f 233 //add release code here
rmalik8 12:bebfef8d229f 234 }
rmalik8 12:bebfef8d229f 235 break;
rmalik8 12:bebfef8d229f 236 case '6': //button 6 down arrow
rmalik8 12:bebfef8d229f 237 if (bhit=='1') {
mmarine3 20:1dc516a6a941 238 volume -= .1;
rmalik8 12:bebfef8d229f 239 //add hit code here
rmalik8 12:bebfef8d229f 240 } else {
rmalik8 12:bebfef8d229f 241 //add release code here
rmalik8 12:bebfef8d229f 242 }
rmalik8 12:bebfef8d229f 243 break;
rmalik8 12:bebfef8d229f 244 case '7': //button 7 left arrow
rmalik8 12:bebfef8d229f 245 if (bhit=='1') {
mmarine3 21:4fef6bf7582c 246 volume = .05;
rmalik8 12:bebfef8d229f 247 //add hit code here
rmalik8 12:bebfef8d229f 248 } else {
rmalik8 12:bebfef8d229f 249 //add release code here
rmalik8 12:bebfef8d229f 250 }
rmalik8 12:bebfef8d229f 251 break;
rmalik8 12:bebfef8d229f 252 case '8': //button 8 right arrow
rmalik8 12:bebfef8d229f 253 if (bhit=='1') {
mmarine3 21:4fef6bf7582c 254 volume = 1;
rmalik8 12:bebfef8d229f 255 //add hit code here
rmalik8 12:bebfef8d229f 256 } else {
rmalik8 12:bebfef8d229f 257 //add release code here
rmalik8 12:bebfef8d229f 258 }
rmalik8 12:bebfef8d229f 259 break;
rmalik8 12:bebfef8d229f 260 default:
rmalik8 12:bebfef8d229f 261 break;
rmalik8 12:bebfef8d229f 262 }
mmarine3 20:1dc516a6a941 263 if(volume > 1)
mmarine3 20:1dc516a6a941 264 {
mmarine3 20:1dc516a6a941 265 volume = 1;
mmarine3 20:1dc516a6a941 266 }
mmarine3 20:1dc516a6a941 267 if(volume < 0)
mmarine3 20:1dc516a6a941 268 {
mmarine3 20:1dc516a6a941 269 volume = 0;
mmarine3 20:1dc516a6a941 270 }
rmalik8 10:7a939a54515e 271 }
rmalik8 10:7a939a54515e 272 }
rmalik8 10:7a939a54515e 273 }
rmalik8 10:7a939a54515e 274 }
rmalik8 10:7a939a54515e 275 serial_mutex.unlock();
rmalik8 10:7a939a54515e 276 }
rmalik8 10:7a939a54515e 277 }
rmalik8 10:7a939a54515e 278
mmarine3 2:1ec843c95914 279 int main()
mmarine3 2:1ec843c95914 280 {
rmalik8 12:bebfef8d229f 281
mmarine3 7:85d42006e380 282 //blu.attach(&parse_message,Serial::RxIrq);
rmalik8 12:bebfef8d229f 283 //Was used in lab 3 to interupt if reading in a blutooth command
mmarine3 7:85d42006e380 284 //Thread t#(name_of_thread_function);
mmarine3 8:c0a6a3363e43 285 Thread t1(LCD);//Initialize LCD thread
mmarine3 9:f4f03767acc0 286 Thread t2(Sonar);//Initialize Sonar thread
rmalik8 14:88c09f96bdda 287 Thread t3(sounds);//Initialize sound thread
rmalik8 14:88c09f96bdda 288 Thread t4(bluetooth);//Initialize bluetooth thread
rmalik8 12:bebfef8d229f 289
rmalik8 12:bebfef8d229f 290
mmarine3 8:c0a6a3363e43 291 /* //Code to read and play a file
mmarine3 7:85d42006e380 292 FILE *wave_file;
mmarine3 7:85d42006e380 293 //printf("Hello World");
mmarine3 7:85d42006e380 294 Thread::wait(1000);
mmarine3 7:85d42006e380 295 wave_file=fopen("/sd/test.wav","r");
mmarine3 7:85d42006e380 296 //serial_mutex.lock();
mmarine3 7:85d42006e380 297 if(wave_file==NULL) printf("file open error!\n\n\r");
mmarine3 7:85d42006e380 298 //serial_mutex.unlock();
mmarine3 7:85d42006e380 299 waver.play(wave_file);
mmarine3 7:85d42006e380 300 fclose(wave_file);
mmarine3 8:c0a6a3363e43 301 */
rmalik8 12:bebfef8d229f 302
mmarine3 8:c0a6a3363e43 303 //Loop to validate the main loop is executing
rmalik8 12:bebfef8d229f 304 while(1) {
rmalik8 15:827fbc2e07d6 305
rmalik8 15:827fbc2e07d6 306 //this 'state machine' should make sure that individuals are only counted once
rmalik8 15:827fbc2e07d6 307 if ((state != 1) && !allZero){
mmarine3 19:c78aafa613b8 308 //state = 1;
mmarine3 19:c78aafa613b8 309 //do logic to figure out direction remove state = 1 above and counter++ below
mmarine3 19:c78aafa613b8 310 //NEW CODE
mmarine3 19:c78aafa613b8 311 if(movingTowards)
mmarine3 19:c78aafa613b8 312 {
mmarine3 21:4fef6bf7582c 313 ++numTowards;
mmarine3 21:4fef6bf7582c 314 counter++;
mmarine3 19:c78aafa613b8 315 state = 1;
mmarine3 21:4fef6bf7582c 316 }else if(movingAway)
mmarine3 19:c78aafa613b8 317 {
mmarine3 21:4fef6bf7582c 318 ++numAway;
mmarine3 21:4fef6bf7582c 319 counter++;
mmarine3 19:c78aafa613b8 320 state = 1;
mmarine3 19:c78aafa613b8 321 }
mmarine3 19:c78aafa613b8 322 //
rmalik8 15:827fbc2e07d6 323 //right now just adding to main counter
rmalik8 15:827fbc2e07d6 324 }
rmalik8 15:827fbc2e07d6 325 if (state == 1 && allZero){
rmalik8 15:827fbc2e07d6 326 state = 0;
rmalik8 15:827fbc2e07d6 327 }
mmarine3 9:f4f03767acc0 328 //MyLED = !MyLED;
rmalik8 15:827fbc2e07d6 329 Thread::wait(100);
mmarine3 8:c0a6a3363e43 330 }
mmarine3 2:1ec843c95914 331 }