mbed Sensor node for Instrumented Booth over ETH.
Dependencies: EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib
Fork of Trenton_Switch_LPC1768_ETH by
sensor_ctl.cpp@46:807e9cf63f4c, 2015-07-16 (annotated)
- Committer:
- andcor02
- Date:
- Thu Jul 16 13:28:49 2015 +0000
- Revision:
- 46:807e9cf63f4c
- Parent:
- 45:d7b4b57574a0
Added Serial VCOM debug over USB.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andcor02 | 25:cb16c5248769 | 1 | /** Implements Sensor Control for CES Instrumented Booth */ |
andcor02 | 25:cb16c5248769 | 2 | |
andcor02 | 25:cb16c5248769 | 3 | #include "mbed.h" |
andcor02 | 25:cb16c5248769 | 4 | #include "sensor_ctl.h" |
erigow01 | 26:4cac6b346e4f | 5 | #include "node_cfg.h" |
andcor02 | 25:cb16c5248769 | 6 | |
andcor02 | 25:cb16c5248769 | 7 | //Sensor Drivers |
andcor02 | 45:d7b4b57574a0 | 8 | #include "htu21d.h" |
andcor02 | 25:cb16c5248769 | 9 | #include "MAX9814.h" |
andcor02 | 25:cb16c5248769 | 10 | #include "sonar.h" |
erigow01 | 27:6017a643f386 | 11 | #include "Presence.h" |
andcor02 | 25:cb16c5248769 | 12 | #include "SHARPIR.h" |
andcor02 | 40:b2e9bc654ca1 | 13 | #include "DoorTrip.h" |
andcor02 | 45:d7b4b57574a0 | 14 | //#include "FXOS8700Q.h" |
andcor02 | 25:cb16c5248769 | 15 | |
andcor02 | 25:cb16c5248769 | 16 | //Sensor MDS Resources |
andcor02 | 25:cb16c5248769 | 17 | #include "door_trip.h" |
andcor02 | 25:cb16c5248769 | 18 | #include "height.h" |
erigow01 | 27:6017a643f386 | 19 | #include "presence_resource.h" |
andcor02 | 25:cb16c5248769 | 20 | #include "sound_level.h" |
andcor02 | 25:cb16c5248769 | 21 | #include "temperature.h" |
erigow01 | 44:f70163204018 | 22 | #include "accelerometer.h" |
erigow01 | 44:f70163204018 | 23 | |
andcor02 | 25:cb16c5248769 | 24 | |
andcor02 | 25:cb16c5248769 | 25 | //Common Sensors |
erigow01 | 27:6017a643f386 | 26 | #if NODE_SENSOR_STATION |
andcor02 | 45:d7b4b57574a0 | 27 | htu21d temphumid(PTE25, PTE24); //Temp humid sensor || SDA, SCL |
andcor02 | 25:cb16c5248769 | 28 | MAX9814 microphone(PTB3); //Analogue in required. |
erigow01 | 27:6017a643f386 | 29 | #if NODE_PIR_STATION |
andcor02 | 40:b2e9bc654ca1 | 30 | presence pir(PTB11, false, PIR_SENSOR_DEBOUNCE_MS); //(InterruptPin), for PIR sensor, |
erigow01 | 27:6017a643f386 | 31 | #endif //NODE PIR STATION |
erigow01 | 27:6017a643f386 | 32 | #if NODE_KIOSK_STATION |
andcor02 | 40:b2e9bc654ca1 | 33 | presence kiosk(PTB10, true, KIOSK_SENSOR_DEBOUNCE_MS); //(Interrupt pinrequired, no timeout) |
erigow01 | 27:6017a643f386 | 34 | #endif //NODE KIOSK STATION |
erigow01 | 27:6017a643f386 | 35 | #if NODE_DOOR_TRIP_STATION |
erigow01 | 44:f70163204018 | 36 | DoorTrip laser(PTC11, true, DOOR_SENSOR_DEBOUNCE_MS ); //(AnalogIn required), for IR door trip |
erigow01 | 27:6017a643f386 | 37 | #endif //NODE TRIP STATION |
erigow01 | 27:6017a643f386 | 38 | #if NODE_HEIGHT_STATION |
erigow01 | 32:c957a1948ac1 | 39 | Timer sonarTimer; |
erigow01 | 32:c957a1948ac1 | 40 | Timer debounceHeight; |
erigow01 | 32:c957a1948ac1 | 41 | Sonar sonar(PTC10, sonarTimer); //(AnalogIn required, Leave as SW2.) |
erigow01 | 32:c957a1948ac1 | 42 | |
erigow01 | 32:c957a1948ac1 | 43 | #define DOOR_HEIGHT_START_MEASURING_THRESHOLD_CM 150 |
erigow01 | 32:c957a1948ac1 | 44 | #define DOOR_HEIGHT_STOP_MEASURING_THRESHOLD_CM 140 |
erigow01 | 32:c957a1948ac1 | 45 | #define DOOR_HEIGHT_STOP_MEASURING_SAMPLE_COUNT 2 |
erigow01 | 32:c957a1948ac1 | 46 | #define DOOR_HEIGHT_SENSOR_MOUNT_HEIGHT_CM 220 |
erigow01 | 27:6017a643f386 | 47 | #endif //NODE HEIGHT STATION |
erigow01 | 44:f70163204018 | 48 | #if NODE_ACCELEROMETER_STATION |
erigow01 | 44:f70163204018 | 49 | FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); |
erigow01 | 44:f70163204018 | 50 | #endif //NODE ACCELEROMETER STATION |
erigow01 | 27:6017a643f386 | 51 | #endif //NODE_SENSOR_STATION |
andcor02 | 25:cb16c5248769 | 52 | |
andcor02 | 25:cb16c5248769 | 53 | |
andcor02 | 25:cb16c5248769 | 54 | //Variables provided to rest of applications |
erigow01 | 44:f70163204018 | 55 | float current_temperature_value = 0; |
erigow01 | 26:4cac6b346e4f | 56 | float current_ambient_noise_value = 0; |
erigow01 | 44:f70163204018 | 57 | float current_ambient_noise_sum = 0; |
erigow01 | 44:f70163204018 | 58 | int current_ambient_noise_count = 0; |
andcor02 | 25:cb16c5248769 | 59 | //Either height XOR kiosk presence XOR PIR station... |
andcor02 | 30:b74aa0729b07 | 60 | float current_height_value = 0; |
erigow01 | 26:4cac6b346e4f | 61 | bool current_presence_value = false; //Either from Kiosk or PIR |
andcor02 | 25:cb16c5248769 | 62 | //And it might have a door trip.. |
erigow01 | 26:4cac6b346e4f | 63 | bool current_door_trip_value = false; |
erigow01 | 44:f70163204018 | 64 | bool current_accelerometer_value = false; |
erigow01 | 26:4cac6b346e4f | 65 | |
erigow01 | 31:389416beb4c3 | 66 | //Door trip... |
erigow01 | 26:4cac6b346e4f | 67 | float door_trip_starting_volts = 0; |
erigow01 | 44:f70163204018 | 68 | float maxValue=0; |
erigow01 | 44:f70163204018 | 69 | bool set=0; |
erigow01 | 32:c957a1948ac1 | 70 | |
erigow01 | 32:c957a1948ac1 | 71 | //Door Height... |
erigow01 | 32:c957a1948ac1 | 72 | float door_height_max_value = 0; |
erigow01 | 32:c957a1948ac1 | 73 | bool door_height_measuring = 0; |
andcor02 | 33:d39c30e9264b | 74 | int door_height_sample_count = 0; |
erigow01 | 31:389416beb4c3 | 75 | |
andcor02 | 25:cb16c5248769 | 76 | //Initialisation |
andcor02 | 25:cb16c5248769 | 77 | void init_sensors() { |
erigow01 | 44:f70163204018 | 78 | // #if NODE_DOOR_TRIP_STATION |
erigow01 | 44:f70163204018 | 79 | // door_trip_starting_volts = sharpir.volt(); |
erigow01 | 44:f70163204018 | 80 | // #endif |
erigow01 | 44:f70163204018 | 81 | #if NODE_PIR_STATION |
erigow01 | 44:f70163204018 | 82 | wait(2); //Wait 2 seconds for sensor calibration period. |
erigow01 | 44:f70163204018 | 83 | #endif //PIR STATION |
erigow01 | 44:f70163204018 | 84 | #if NODE_ACCELEROMETER_STATION |
erigow01 | 44:f70163204018 | 85 | acc.enable(); |
erigow01 | 26:4cac6b346e4f | 86 | #endif |
erigow01 | 32:c957a1948ac1 | 87 | #if NODE_HEIGHT_STATION |
erigow01 | 32:c957a1948ac1 | 88 | sonarTimer.start(); |
erigow01 | 32:c957a1948ac1 | 89 | #endif |
andcor02 | 25:cb16c5248769 | 90 | } |
andcor02 | 25:cb16c5248769 | 91 | |
erigow01 | 27:6017a643f386 | 92 | #if NODE_SENSOR_STATION |
erigow01 | 27:6017a643f386 | 93 | |
andcor02 | 25:cb16c5248769 | 94 | //timer handler functions |
andcor02 | 25:cb16c5248769 | 95 | void handle_temperature_report_timer() { |
andcor02 | 45:d7b4b57574a0 | 96 | current_temperature_value = temphumid.getTemp(); |
andcor02 | 25:cb16c5248769 | 97 | printf("Temperature Sample: %2.2f\r\n", current_temperature_value); |
erigow01 | 26:4cac6b346e4f | 98 | temperature_report(); |
andcor02 | 25:cb16c5248769 | 99 | } |
andcor02 | 25:cb16c5248769 | 100 | |
erigow01 | 44:f70163204018 | 101 | void handle_microphone_sample_timer(){ |
andcor02 | 25:cb16c5248769 | 102 | float sample = microphone.sound_level(); |
erigow01 | 26:4cac6b346e4f | 103 | //printf("Sound Sample: %2.2f\r\n", sample); |
erigow01 | 44:f70163204018 | 104 | //Keep running sum and sample count to compute average... |
erigow01 | 44:f70163204018 | 105 | current_ambient_noise_sum += sample; |
erigow01 | 44:f70163204018 | 106 | current_ambient_noise_count++; |
erigow01 | 44:f70163204018 | 107 | current_ambient_noise_value = current_ambient_noise_sum / current_ambient_noise_count; |
erigow01 | 44:f70163204018 | 108 | // if (sample > current_ambient_noise_value){ |
erigow01 | 44:f70163204018 | 109 | // current_ambient_noise_value = sample; |
erigow01 | 44:f70163204018 | 110 | // } |
andcor02 | 25:cb16c5248769 | 111 | } |
andcor02 | 25:cb16c5248769 | 112 | |
erigow01 | 44:f70163204018 | 113 | void handle_microphone_report_timer(){ |
andcor02 | 25:cb16c5248769 | 114 | //Report. |
erigow01 | 26:4cac6b346e4f | 115 | sound_level_report(); |
andcor02 | 25:cb16c5248769 | 116 | //Reset noise... |
andcor02 | 25:cb16c5248769 | 117 | current_ambient_noise_value = 0; |
erigow01 | 44:f70163204018 | 118 | current_ambient_noise_sum = 0; |
erigow01 | 44:f70163204018 | 119 | current_ambient_noise_count = 0; |
andcor02 | 25:cb16c5248769 | 120 | } |
andcor02 | 25:cb16c5248769 | 121 | |
erigow01 | 27:6017a643f386 | 122 | #if NODE_PIR_STATION |
erigow01 | 26:4cac6b346e4f | 123 | void handle_motion_report_timer(){ |
erigow01 | 27:6017a643f386 | 124 | bool new_pir = pir.isPresent(); |
erigow01 | 26:4cac6b346e4f | 125 | //printf("PIR Sample: %d\r\n", new_pir); |
erigow01 | 26:4cac6b346e4f | 126 | //printf("Old PIR Sample: %d\r\n", current_presence_value); |
erigow01 | 26:4cac6b346e4f | 127 | if(new_pir != current_presence_value) { |
erigow01 | 26:4cac6b346e4f | 128 | //printf("Reporting PIR...\r\n"); |
erigow01 | 26:4cac6b346e4f | 129 | current_presence_value = new_pir; |
erigow01 | 27:6017a643f386 | 130 | presence_report(); |
erigow01 | 26:4cac6b346e4f | 131 | } |
erigow01 | 26:4cac6b346e4f | 132 | } |
erigow01 | 27:6017a643f386 | 133 | #endif //NODE_PIR_STATION |
erigow01 | 26:4cac6b346e4f | 134 | |
erigow01 | 27:6017a643f386 | 135 | #if NODE_KIOSK_STATION |
erigow01 | 26:4cac6b346e4f | 136 | void handle_kiosk_report_timer(){ |
erigow01 | 27:6017a643f386 | 137 | bool new_kiosk = kiosk.isPresent(); |
erigow01 | 26:4cac6b346e4f | 138 | if(new_kiosk != current_presence_value) { |
erigow01 | 26:4cac6b346e4f | 139 | current_presence_value = new_kiosk; |
erigow01 | 27:6017a643f386 | 140 | presence_report(); |
erigow01 | 26:4cac6b346e4f | 141 | } |
erigow01 | 26:4cac6b346e4f | 142 | } |
erigow01 | 27:6017a643f386 | 143 | #endif //NODE_KIOSK_STATION |
erigow01 | 26:4cac6b346e4f | 144 | |
erigow01 | 27:6017a643f386 | 145 | #if NODE_DOOR_TRIP_STATION |
erigow01 | 26:4cac6b346e4f | 146 | void handle_door_trip_report_timer(){ |
andcor02 | 40:b2e9bc654ca1 | 147 | bool new_laser = laser.isPresent(); |
andcor02 | 40:b2e9bc654ca1 | 148 | |
andcor02 | 40:b2e9bc654ca1 | 149 | if(new_laser != current_door_trip_value) { |
andcor02 | 40:b2e9bc654ca1 | 150 | current_door_trip_value = new_laser; |
erigow01 | 26:4cac6b346e4f | 151 | door_trip_report(); |
erigow01 | 26:4cac6b346e4f | 152 | } |
erigow01 | 26:4cac6b346e4f | 153 | } |
erigow01 | 27:6017a643f386 | 154 | #endif //NODE_DOOR_TRIP_STATION |
erigow01 | 26:4cac6b346e4f | 155 | |
erigow01 | 27:6017a643f386 | 156 | #if NODE_HEIGHT_STATION |
andcor02 | 29:9599a156f78b | 157 | void handle_door_height_sample_timer(){ |
erigow01 | 32:c957a1948ac1 | 158 | int height_sample = DOOR_HEIGHT_SENSOR_MOUNT_HEIGHT_CM-sonar.read(); |
erigow01 | 32:c957a1948ac1 | 159 | if(height_sample > DOOR_HEIGHT_START_MEASURING_THRESHOLD_CM) { |
erigow01 | 32:c957a1948ac1 | 160 | door_height_sample_count=0; |
erigow01 | 32:c957a1948ac1 | 161 | if (height_sample > door_height_max_value) { |
erigow01 | 32:c957a1948ac1 | 162 | door_height_max_value = height_sample; |
erigow01 | 32:c957a1948ac1 | 163 | } |
erigow01 | 32:c957a1948ac1 | 164 | door_height_measuring=true; |
andcor02 | 29:9599a156f78b | 165 | } |
andcor02 | 29:9599a156f78b | 166 | |
erigow01 | 32:c957a1948ac1 | 167 | if((height_sample < DOOR_HEIGHT_STOP_MEASURING_THRESHOLD_CM && door_height_measuring )){ |
erigow01 | 32:c957a1948ac1 | 168 | door_height_sample_count++; |
erigow01 | 32:c957a1948ac1 | 169 | if (door_height_sample_count > DOOR_HEIGHT_STOP_MEASURING_SAMPLE_COUNT){ |
erigow01 | 32:c957a1948ac1 | 170 | current_height_value=door_height_max_value; |
erigow01 | 32:c957a1948ac1 | 171 | door_height_max_value=0,door_height_measuring=false; |
erigow01 | 32:c957a1948ac1 | 172 | door_height_sample_count = 0; |
erigow01 | 32:c957a1948ac1 | 173 | height_report(); |
erigow01 | 32:c957a1948ac1 | 174 | } |
andcor02 | 29:9599a156f78b | 175 | } |
andcor02 | 25:cb16c5248769 | 176 | } |
erigow01 | 32:c957a1948ac1 | 177 | #endif //NODE HEIGHT STATION |
andcor02 | 25:cb16c5248769 | 178 | |
erigow01 | 44:f70163204018 | 179 | #if NODE_ACCELEROMETER_STATION |
erigow01 | 44:f70163204018 | 180 | void handle_accelerometer_report_timer(){ |
erigow01 | 44:f70163204018 | 181 | bool new_accelerometer = acc.get_values(); |
erigow01 | 44:f70163204018 | 182 | if(new_accelerometer != current_accelerometer_value) { |
erigow01 | 44:f70163204018 | 183 | |
erigow01 | 44:f70163204018 | 184 | current_accelerometer_value = new_accelerometer; |
erigow01 | 44:f70163204018 | 185 | accelerometer_report(); |
erigow01 | 44:f70163204018 | 186 | } |
erigow01 | 44:f70163204018 | 187 | |
erigow01 | 44:f70163204018 | 188 | } |
erigow01 | 44:f70163204018 | 189 | #endif //NODE_ACCELEROMETER_STATION |
erigow01 | 44:f70163204018 | 190 | |
erigow01 | 27:6017a643f386 | 191 | #endif //NODE_SENSOR_STATION |