People Counter / Mbed 2 deprecated person_counter

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

Committer:
mmarine3
Date:
Sun Apr 26 20:28:51 2020 +0000
Revision:
20:1dc516a6a941
Parent:
19:c78aafa613b8
Child:
21:4fef6bf7582c
Shortened sound time. Added volume setting controlled by Bluetooth

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