People Counter / Mbed 2 deprecated person_counter

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

Committer:
mmarine3
Date:
Sun Apr 26 19:07:20 2020 +0000
Revision:
19:c78aafa613b8
Parent:
18:7b1d65b007ab
Child:
20:1dc516a6a941
Added code to check directional movement (towards and away). Currently is a little fiddly with directions. Slowing down the sonar thread frequency might solve this issue

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
rmalik8 12:bebfef8d229f 19 //AnalogOut speaker(p18); //Initialize speaker
rmalik8 12:bebfef8d229f 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 };
rmalik8 12:bebfef8d229f 37 float duration[2]= {1, 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;
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 18:7b1d65b007ab 74 if(non_zeros > 1) //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 19:c78aafa613b8 80 if(values[i] < values[(i+1)%5] && values[(i+1)%5] < values[(i+2)%5])
mmarine3 19:c78aafa613b8 81 {
mmarine3 19:c78aafa613b8 82 movingTowards = true;
mmarine3 19:c78aafa613b8 83 }
mmarine3 19:c78aafa613b8 84 if (values[i] > values[(i+1)%5] && values[(i+1)%5] > values[(i+2)%5])
mmarine3 19:c78aafa613b8 85 {
mmarine3 19:c78aafa613b8 86 movingAway = true;
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);
rmalik8 12:bebfef8d229f 117 serial_mutex.unlock();
rmalik8 12:bebfef8d229f 118 while(1) {
rmalik8 10:7a939a54515e 119 serial_mutex.lock();
mmarine3 8:c0a6a3363e43 120 uLCD.cls();
mmarine3 8:c0a6a3363e43 121 dist.lock();
mmarine3 17:b8b68ff5e33a 122 uLCD.printf("Sonar Distance:\n %d\n Counter: %d \n Song: %d", sdist, counter, musicChoice);
mmarine3 8:c0a6a3363e43 123 dist.unlock();
rmalik8 10:7a939a54515e 124 serial_mutex.unlock();
mmarine3 17:b8b68ff5e33a 125 Thread::wait(100); //Allow time to read value before reprint
rmalik8 12:bebfef8d229f 126
rmalik8 12:bebfef8d229f 127 }
rmalik8 12:bebfef8d229f 128 }
rmalik8 12:bebfef8d229f 129
rmalik8 12:bebfef8d229f 130 void sounds(void const *arguments)
rmalik8 12:bebfef8d229f 131 {
rmalik8 12:bebfef8d229f 132 int last_counter = 0;
mmarine3 17:b8b68ff5e33a 133 //FILE *wave_file;
rmalik8 12:bebfef8d229f 134 // setup instance of new SongPlayer class, mySpeaker using pin 26
rmalik8 12:bebfef8d229f 135 // the pin must be a PWM output pin
rmalik8 12:bebfef8d229f 136 SongPlayer mySpeaker(p26);
rmalik8 12:bebfef8d229f 137 // Start song and return once playing starts
rmalik8 12:bebfef8d229f 138 //mySpeaker.PlaySong(note1,duration);
rmalik8 12:bebfef8d229f 139 while(1) {
mmarine3 19:c78aafa613b8 140 if (counter != last_counter) {//CHANGE > to !=
rmalik8 12:bebfef8d229f 141 switch (musicChoice) {
mmarine3 17:b8b68ff5e33a 142 case 0:
mmarine3 17:b8b68ff5e33a 143 printf("Beep\r\n");
rmalik8 12:bebfef8d229f 144 //do nothing
rmalik8 12:bebfef8d229f 145 break;
mmarine3 17:b8b68ff5e33a 146 case 1:
mmarine3 17:b8b68ff5e33a 147 //wave_file=fopen("/sd/test.wav","r");
mmarine3 17:b8b68ff5e33a 148 //if(wave_file==NULL) {printf("file open error!\n\n\r");}
mmarine3 17:b8b68ff5e33a 149 //waver.play(wave_file);
mmarine3 17:b8b68ff5e33a 150 //fclose(wave_file);
rmalik8 12:bebfef8d229f 151 mySpeaker.PlaySong(note1,duration);
rmalik8 12:bebfef8d229f 152 break;
mmarine3 17:b8b68ff5e33a 153 case 2:
rmalik8 12:bebfef8d229f 154 mySpeaker.PlaySong(note2,duration);
rmalik8 12:bebfef8d229f 155 break;
mmarine3 17:b8b68ff5e33a 156 case 3:
rmalik8 12:bebfef8d229f 157 mySpeaker.PlaySong(note3,duration);
rmalik8 12:bebfef8d229f 158 break;
rmalik8 12:bebfef8d229f 159 default:
mmarine3 17:b8b68ff5e33a 160 printf("Invalid Choice\r\n");
rmalik8 12:bebfef8d229f 161 break;
rmalik8 12:bebfef8d229f 162 }
mmarine3 17:b8b68ff5e33a 163 last_counter = counter;
rmalik8 12:bebfef8d229f 164 }
mmarine3 17:b8b68ff5e33a 165 //printf("Last Counter: %d\r\n",last_counter);
rmalik8 12:bebfef8d229f 166 Thread::wait(100);
mmarine3 8:c0a6a3363e43 167 }
mmarine3 8:c0a6a3363e43 168 }
mmarine3 8:c0a6a3363e43 169
rmalik8 10:7a939a54515e 170 void bluetooth(void const *arguments)
rmalik8 10:7a939a54515e 171 {
rmalik8 10:7a939a54515e 172 char bnum=0;
rmalik8 10:7a939a54515e 173 char bhit=0;
rmalik8 12:bebfef8d229f 174 while(1) {
rmalik8 10:7a939a54515e 175 serial_mutex.lock();
rmalik8 12:bebfef8d229f 176 if(!blue.readable()) {
rmalik8 12:bebfef8d229f 177 Thread::yield();
rmalik8 10:7a939a54515e 178 } else {
rmalik8 12:bebfef8d229f 179 if (blue.getc()=='!') {
rmalik8 12:bebfef8d229f 180 if (blue.getc()=='B') { //button data packet
rmalik8 12:bebfef8d229f 181 bnum = blue.getc(); //button number
rmalik8 12:bebfef8d229f 182 bhit = blue.getc(); //1=hit, 0=release
rmalik8 12:bebfef8d229f 183 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
rmalik8 12:bebfef8d229f 184
rmalik8 12:bebfef8d229f 185 switch (bnum) {
rmalik8 12:bebfef8d229f 186 case '1': //number button 1
rmalik8 12:bebfef8d229f 187 if (bhit=='1') {
rmalik8 12:bebfef8d229f 188 musicChoice = 0;
rmalik8 12:bebfef8d229f 189 } else {
rmalik8 12:bebfef8d229f 190 //add release code here
rmalik8 12:bebfef8d229f 191 }
rmalik8 12:bebfef8d229f 192 break;
rmalik8 12:bebfef8d229f 193 case '2': //number button 2
rmalik8 12:bebfef8d229f 194 if (bhit=='1') {
rmalik8 12:bebfef8d229f 195 musicChoice = 1;
rmalik8 12:bebfef8d229f 196 } else {
rmalik8 12:bebfef8d229f 197 //add release code here
rmalik8 12:bebfef8d229f 198 }
rmalik8 12:bebfef8d229f 199 break;
rmalik8 12:bebfef8d229f 200 case '3': //number button 3
rmalik8 12:bebfef8d229f 201 if (bhit=='1') {
rmalik8 12:bebfef8d229f 202 musicChoice = 2;
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 '4': //number button 4
rmalik8 12:bebfef8d229f 208 if (bhit=='1') {
rmalik8 12:bebfef8d229f 209 //add hit code here
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 '5': //button 5 up arrow
rmalik8 12:bebfef8d229f 215 if (bhit=='1') {
rmalik8 12:bebfef8d229f 216 musicChoice = 3;
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 '6': //button 6 down arrow
rmalik8 12:bebfef8d229f 222 if (bhit=='1') {
rmalik8 12:bebfef8d229f 223 //add hit code here
rmalik8 12:bebfef8d229f 224 } else {
rmalik8 12:bebfef8d229f 225 //add release code here
rmalik8 12:bebfef8d229f 226 }
rmalik8 12:bebfef8d229f 227 break;
rmalik8 12:bebfef8d229f 228 case '7': //button 7 left arrow
rmalik8 12:bebfef8d229f 229 if (bhit=='1') {
rmalik8 12:bebfef8d229f 230 //add hit code here
rmalik8 12:bebfef8d229f 231 } else {
rmalik8 12:bebfef8d229f 232 //add release code here
rmalik8 12:bebfef8d229f 233 }
rmalik8 12:bebfef8d229f 234 break;
rmalik8 12:bebfef8d229f 235 case '8': //button 8 right arrow
rmalik8 12:bebfef8d229f 236 if (bhit=='1') {
rmalik8 12:bebfef8d229f 237 //add hit code here
rmalik8 12:bebfef8d229f 238 } else {
rmalik8 12:bebfef8d229f 239 //add release code here
rmalik8 12:bebfef8d229f 240 }
rmalik8 12:bebfef8d229f 241 break;
rmalik8 12:bebfef8d229f 242 default:
rmalik8 12:bebfef8d229f 243 break;
rmalik8 12:bebfef8d229f 244 }
rmalik8 10:7a939a54515e 245 }
rmalik8 10:7a939a54515e 246 }
rmalik8 10:7a939a54515e 247 }
rmalik8 10:7a939a54515e 248 }
rmalik8 10:7a939a54515e 249 serial_mutex.unlock();
rmalik8 10:7a939a54515e 250 }
rmalik8 10:7a939a54515e 251 }
rmalik8 10:7a939a54515e 252
mmarine3 2:1ec843c95914 253 int main()
mmarine3 2:1ec843c95914 254 {
rmalik8 12:bebfef8d229f 255
mmarine3 7:85d42006e380 256 //wait(1.0);
mmarine3 7:85d42006e380 257
mmarine3 7:85d42006e380 258 //blu.attach(&parse_message,Serial::RxIrq);
rmalik8 12:bebfef8d229f 259 //Was used in lab 3 to interupt if reading in a blutooth command
mmarine3 7:85d42006e380 260 //Thread t#(name_of_thread_function);
mmarine3 8:c0a6a3363e43 261 Thread t1(LCD);//Initialize LCD thread
mmarine3 9:f4f03767acc0 262 Thread t2(Sonar);//Initialize Sonar thread
rmalik8 14:88c09f96bdda 263 Thread t3(sounds);//Initialize sound thread
rmalik8 14:88c09f96bdda 264 Thread t4(bluetooth);//Initialize bluetooth thread
rmalik8 12:bebfef8d229f 265
rmalik8 12:bebfef8d229f 266
mmarine3 8:c0a6a3363e43 267 /* //Code to read and play a file
mmarine3 7:85d42006e380 268 FILE *wave_file;
mmarine3 7:85d42006e380 269 //printf("Hello World");
mmarine3 7:85d42006e380 270 Thread::wait(1000);
mmarine3 7:85d42006e380 271 wave_file=fopen("/sd/test.wav","r");
mmarine3 7:85d42006e380 272 //serial_mutex.lock();
mmarine3 7:85d42006e380 273 if(wave_file==NULL) printf("file open error!\n\n\r");
mmarine3 7:85d42006e380 274 //serial_mutex.unlock();
mmarine3 7:85d42006e380 275 waver.play(wave_file);
mmarine3 7:85d42006e380 276 fclose(wave_file);
mmarine3 8:c0a6a3363e43 277 */
rmalik8 12:bebfef8d229f 278
mmarine3 8:c0a6a3363e43 279 //Loop to validate the main loop is executing
rmalik8 12:bebfef8d229f 280 while(1) {
rmalik8 15:827fbc2e07d6 281
rmalik8 15:827fbc2e07d6 282 //this 'state machine' should make sure that individuals are only counted once
rmalik8 15:827fbc2e07d6 283 if ((state != 1) && !allZero){
mmarine3 19:c78aafa613b8 284 //state = 1;
mmarine3 19:c78aafa613b8 285 //do logic to figure out direction remove state = 1 above and counter++ below
mmarine3 19:c78aafa613b8 286 //NEW CODE
mmarine3 19:c78aafa613b8 287 if(movingTowards)
mmarine3 19:c78aafa613b8 288 {
mmarine3 19:c78aafa613b8 289 ++counter;
mmarine3 19:c78aafa613b8 290 state = 1;
mmarine3 19:c78aafa613b8 291 }
mmarine3 19:c78aafa613b8 292 if(movingAway)
mmarine3 19:c78aafa613b8 293 {
mmarine3 19:c78aafa613b8 294 --counter;
mmarine3 19:c78aafa613b8 295 state = 1;
mmarine3 19:c78aafa613b8 296 }
mmarine3 19:c78aafa613b8 297 //
rmalik8 15:827fbc2e07d6 298 //right now just adding to main counter
mmarine3 19:c78aafa613b8 299 //counter++;
rmalik8 15:827fbc2e07d6 300 }
rmalik8 15:827fbc2e07d6 301 if (state == 1 && allZero){
rmalik8 15:827fbc2e07d6 302 state = 0;
rmalik8 15:827fbc2e07d6 303 }
mmarine3 19:c78aafa613b8 304 printf("State %d\r\n", state);
mmarine3 9:f4f03767acc0 305 //MyLED = !MyLED;
rmalik8 15:827fbc2e07d6 306 Thread::wait(100);
mmarine3 8:c0a6a3363e43 307 }
mmarine3 2:1ec843c95914 308 }