SEDO subject project
Dependencies: ds3231 mbed-rtos mbed DHT
main.cpp@12:1d544cdab2cf, 2017-05-15 (annotated)
- Committer:
- ValenSalLop
- Date:
- Mon May 15 18:30:20 2017 +0000
- Revision:
- 12:1d544cdab2cf
- Parent:
- 11:a9f41d6489c9
final;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ValenSalLop | 0:de99871168ac | 1 | #include "mbed.h" |
ValenSalLop | 0:de99871168ac | 2 | #include "rtos.h" |
ValenSalLop | 0:de99871168ac | 3 | |
ValenSalLop | 9:5e9c4277151d | 4 | #include "main.h" |
ValenSalLop | 9:5e9c4277151d | 5 | #include "real_time_clock.h" |
ValenSalLop | 9:5e9c4277151d | 6 | #include "temp_hum_sensor.h" |
ValenSalLop | 10:f14f47225091 | 7 | #include "motor_cnt.h" |
ValenSalLop | 12:1d544cdab2cf | 8 | #include "light_sensor.h" |
ValenSalLop | 0:de99871168ac | 9 | |
ValenSalLop | 9:5e9c4277151d | 10 | #define TASKS_NUMBER (sizeof(taskList)/sizeof(taskList[0])) |
ValenSalLop | 5:19ccff89f555 | 11 | |
ValenSalLop | 9:5e9c4277151d | 12 | Mutex mutexData; |
ValenSalLop | 9:5e9c4277151d | 13 | SensorData data; |
ValenSalLop | 9:5e9c4277151d | 14 | Mutex mutexPCComm; |
ValenSalLop | 9:5e9c4277151d | 15 | Serial pc(USBTX,USBRX); |
ValenSalLop | 11:a9f41d6489c9 | 16 | |
ValenSalLop | 11:a9f41d6489c9 | 17 | Serial xbee(D10,D2);//(PB_6, PA_10); |
ValenSalLop | 11:a9f41d6489c9 | 18 | |
ValenSalLop | 9:5e9c4277151d | 19 | volatile bool sensors_running = false; |
ValenSalLop | 0:de99871168ac | 20 | |
ValenSalLop | 9:5e9c4277151d | 21 | static struct task_definition_struct taskList[] = { |
ValenSalLop | 9:5e9c4277151d | 22 | { temp_hum_thread, &temp_hum_init, NULL, NULL, osPriorityNormal, 100, NULL, 0 }, |
ValenSalLop | 12:1d544cdab2cf | 23 | { light_thread, &light_init, NULL, NULL, osPriorityNormal, 100, NULL, 0 } |
ValenSalLop | 9:5e9c4277151d | 24 | }; |
ValenSalLop | 7:8065e844e99a | 25 | |
ValenSalLop | 7:8065e844e99a | 26 | |
ValenSalLop | 9:5e9c4277151d | 27 | int main() |
ValenSalLop | 9:5e9c4277151d | 28 | { |
ValenSalLop | 9:5e9c4277151d | 29 | pc.baud(9600); |
ValenSalLop | 9:5e9c4277151d | 30 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 31 | pc.printf("MAIN: init\n"); |
ValenSalLop | 9:5e9c4277151d | 32 | mutexPCComm.unlock(); |
ValenSalLop | 11:a9f41d6489c9 | 33 | xbee.putc('S'); |
ValenSalLop | 9:5e9c4277151d | 34 | // Init RTC |
ValenSalLop | 10:f14f47225091 | 35 | time_t epoch_time, elapsed_time; |
ValenSalLop | 9:5e9c4277151d | 36 | epoch_time = rtc.get_epoch(); |
ValenSalLop | 10:f14f47225091 | 37 | mutexPCComm.lock(); |
ValenSalLop | 10:f14f47225091 | 38 | pc.printf("MAIN: time %s\n", ctime(&epoch_time)); |
ValenSalLop | 10:f14f47225091 | 39 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 40 | // Reset all data status |
ValenSalLop | 9:5e9c4277151d | 41 | mutexData.lock(); |
ValenSalLop | 9:5e9c4277151d | 42 | data.DHTError = -128; |
ValenSalLop | 9:5e9c4277151d | 43 | mutexData.unlock(); |
ValenSalLop | 9:5e9c4277151d | 44 | // Init all tasks |
ValenSalLop | 9:5e9c4277151d | 45 | initTasks(); |
ValenSalLop | 7:8065e844e99a | 46 | while(true) { |
ValenSalLop | 10:f14f47225091 | 47 | epoch_time = rtc.get_epoch(); |
ValenSalLop | 9:5e9c4277151d | 48 | // Start all threads |
ValenSalLop | 9:5e9c4277151d | 49 | sensors_running = true; |
ValenSalLop | 9:5e9c4277151d | 50 | for (int i = 0; i < TASKS_NUMBER; i++) { |
ValenSalLop | 9:5e9c4277151d | 51 | if (taskList[i].retVal == 1) { |
ValenSalLop | 9:5e9c4277151d | 52 | taskList[i].thread->signal_set(0x1); |
ValenSalLop | 9:5e9c4277151d | 53 | } |
ValenSalLop | 9:5e9c4277151d | 54 | } |
ValenSalLop | 9:5e9c4277151d | 55 | publishSensorData(); |
ValenSalLop | 9:5e9c4277151d | 56 | // Stop all remain tasks |
ValenSalLop | 9:5e9c4277151d | 57 | sensors_running = false; |
ValenSalLop | 10:f14f47225091 | 58 | elapsed_time = rtc.get_epoch()-epoch_time; |
ValenSalLop | 12:1d544cdab2cf | 59 | |
ValenSalLop | 12:1d544cdab2cf | 60 | mutexPCComm.lock(); |
ValenSalLop | 12:1d544cdab2cf | 61 | pc.printf("MAIN: elapsed %ld\n",(long)elapsed_time); |
ValenSalLop | 12:1d544cdab2cf | 62 | mutexPCComm.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 63 | if(10-(long)elapsed_time>0) { |
ValenSalLop | 11:a9f41d6489c9 | 64 | Thread::wait((10-elapsed_time)*1000); |
ValenSalLop | 12:1d544cdab2cf | 65 | } else |
ValenSalLop | 12:1d544cdab2cf | 66 | Thread::wait(1000); |
ValenSalLop | 9:5e9c4277151d | 67 | } |
ValenSalLop | 9:5e9c4277151d | 68 | } |
ValenSalLop | 7:8065e844e99a | 69 | |
ValenSalLop | 9:5e9c4277151d | 70 | int publishSensorData(void) |
ValenSalLop | 7:8065e844e99a | 71 | { |
ValenSalLop | 9:5e9c4277151d | 72 | Timer t; |
ValenSalLop | 9:5e9c4277151d | 73 | int error = -1; |
ValenSalLop | 12:1d544cdab2cf | 74 | time_t ts = rtc.get_epoch(); |
ValenSalLop | 9:5e9c4277151d | 75 | t.reset(); |
ValenSalLop | 9:5e9c4277151d | 76 | t.start(); |
ValenSalLop | 9:5e9c4277151d | 77 | while(error != 0 && t.read()<MAX_TIME_TO_WAIT_NODE_COMM) { |
ValenSalLop | 9:5e9c4277151d | 78 | mutexData.lock(); |
ValenSalLop | 9:5e9c4277151d | 79 | // Check if all data are valid |
ValenSalLop | 9:5e9c4277151d | 80 | error = data.DHTError; |
ValenSalLop | 9:5e9c4277151d | 81 | if( error == 0) { |
ValenSalLop | 9:5e9c4277151d | 82 | // Send all info |
ValenSalLop | 9:5e9c4277151d | 83 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 84 | pc.printf("Temp\t%f\n",data.temperature); |
ValenSalLop | 9:5e9c4277151d | 85 | pc.printf("Hum\t%f\n",data.humidity); |
ValenSalLop | 9:5e9c4277151d | 86 | pc.printf("DewP\t%f\n",data.dewPoint); |
ValenSalLop | 12:1d544cdab2cf | 87 | pc.printf("Light\t%f\n",data.light); |
ValenSalLop | 12:1d544cdab2cf | 88 | pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.DHTError); |
ValenSalLop | 12:1d544cdab2cf | 89 | pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.lightError); |
ValenSalLop | 9:5e9c4277151d | 90 | mutexPCComm.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 91 | xbee.printf("{%ld:{DHT:{temp:%4.2f,hum:%4.2f,dew:%4.2f}},{LIGHT:{light:%f}})\n",(long)ts, data.temperature,data.humidity,data.dewPoint,data.light); |
ValenSalLop | 12:1d544cdab2cf | 92 | set_motor_direction(1); |
ValenSalLop | 12:1d544cdab2cf | 93 | set_motor_speed(data.light); |
ValenSalLop | 9:5e9c4277151d | 94 | } |
ValenSalLop | 9:5e9c4277151d | 95 | mutexData.unlock(); |
ValenSalLop | 9:5e9c4277151d | 96 | if(error!=0) { |
ValenSalLop | 9:5e9c4277151d | 97 | Thread::wait(3000); |
ValenSalLop | 9:5e9c4277151d | 98 | } |
ValenSalLop | 9:5e9c4277151d | 99 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 100 | pc.printf("MAIN: req data\t%f\n",t.read()); |
ValenSalLop | 9:5e9c4277151d | 101 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 102 | } // while |
ValenSalLop | 9:5e9c4277151d | 103 | t.stop(); |
ValenSalLop | 9:5e9c4277151d | 104 | // Send valid info only |
ValenSalLop | 9:5e9c4277151d | 105 | if(error!=0) { |
ValenSalLop | 9:5e9c4277151d | 106 | mutexData.lock(); |
ValenSalLop | 9:5e9c4277151d | 107 | if( data.DHTError == 0) { |
ValenSalLop | 9:5e9c4277151d | 108 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 109 | pc.printf("Temp:\t%f\n",data.temperature); |
ValenSalLop | 9:5e9c4277151d | 110 | pc.printf("Hum:\t%f\n",data.humidity); |
ValenSalLop | 9:5e9c4277151d | 111 | pc.printf("DewP:\t%f\n",data.dewPoint); |
ValenSalLop | 12:1d544cdab2cf | 112 | pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.DHTError); |
ValenSalLop | 12:1d544cdab2cf | 113 | xbee.printf("{%ld:{DHT:{temp:%4.2f,hum:%4.2f,dew:%4.2f}}\n",(long)ts, data.temperature,data.humidity,data.dewPoint); |
ValenSalLop | 9:5e9c4277151d | 114 | mutexPCComm.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 115 | } |
ValenSalLop | 12:1d544cdab2cf | 116 | if( data.lightError == 0) { |
ValenSalLop | 9:5e9c4277151d | 117 | mutexPCComm.lock(); |
ValenSalLop | 12:1d544cdab2cf | 118 | pc.printf("Light\t%f\n",data.light); |
ValenSalLop | 12:1d544cdab2cf | 119 | pc.printf("%ld\tlightError:\t%d\n",(long)ts, data.lightError); |
ValenSalLop | 12:1d544cdab2cf | 120 | xbee.printf("{%ld:{LIGHT:{light:%f}})\n",(long)ts, data.light); |
ValenSalLop | 12:1d544cdab2cf | 121 | mutexPCComm.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 122 | set_motor_direction(1); |
ValenSalLop | 12:1d544cdab2cf | 123 | set_motor_speed(data.light); |
ValenSalLop | 12:1d544cdab2cf | 124 | } |
ValenSalLop | 12:1d544cdab2cf | 125 | if( data.lightError != 0 || data.lightError != 0) { |
ValenSalLop | 12:1d544cdab2cf | 126 | mutexPCComm.lock(); |
ValenSalLop | 12:1d544cdab2cf | 127 | pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.DHTError); |
ValenSalLop | 12:1d544cdab2cf | 128 | pc.printf("%ld\tlightError:\t%d\n",(long)ts, data.lightError); |
ValenSalLop | 12:1d544cdab2cf | 129 | xbee.printf("{%ld:{DHT:{error:\t%d},{LIGHT:{error:%f}}}\n",(long)ts, data.DHTError, data.light); |
ValenSalLop | 9:5e9c4277151d | 130 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 131 | } |
ValenSalLop | 9:5e9c4277151d | 132 | mutexData.unlock(); |
ValenSalLop | 7:8065e844e99a | 133 | } |
ValenSalLop | 9:5e9c4277151d | 134 | // Reset all data status |
ValenSalLop | 9:5e9c4277151d | 135 | mutexData.lock(); |
ValenSalLop | 9:5e9c4277151d | 136 | data.DHTError = -128; |
ValenSalLop | 12:1d544cdab2cf | 137 | data.lightError = -128; |
ValenSalLop | 9:5e9c4277151d | 138 | mutexData.unlock(); |
ValenSalLop | 7:8065e844e99a | 139 | return error; |
ValenSalLop | 7:8065e844e99a | 140 | } |
ValenSalLop | 7:8065e844e99a | 141 | |
ValenSalLop | 5:19ccff89f555 | 142 | |
ValenSalLop | 5:19ccff89f555 | 143 | |
ValenSalLop | 9:5e9c4277151d | 144 | static uint32_t initTasks(void) |
ValenSalLop | 9:5e9c4277151d | 145 | { |
ValenSalLop | 9:5e9c4277151d | 146 | uint32_t i; |
ValenSalLop | 9:5e9c4277151d | 147 | uint32_t retval = 1; |
ValenSalLop | 5:19ccff89f555 | 148 | |
ValenSalLop | 9:5e9c4277151d | 149 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 150 | pc.printf("initTasks: init\n"); |
ValenSalLop | 9:5e9c4277151d | 151 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 152 | for (i = 0; i < TASKS_NUMBER; i++) { |
ValenSalLop | 9:5e9c4277151d | 153 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 154 | pc.printf("initTasks: %d\n",i); |
ValenSalLop | 9:5e9c4277151d | 155 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 156 | if (taskList[i].init != NULL) { |
ValenSalLop | 9:5e9c4277151d | 157 | if ((taskList[i].retVal = ((*taskList[i].init)(taskList[i].init_args))) == 0) |
ValenSalLop | 9:5e9c4277151d | 158 | retval = 0; |
ValenSalLop | 9:5e9c4277151d | 159 | } else { |
ValenSalLop | 9:5e9c4277151d | 160 | taskList[i].retVal = 1; |
ValenSalLop | 9:5e9c4277151d | 161 | } |
ValenSalLop | 0:de99871168ac | 162 | } |
ValenSalLop | 5:19ccff89f555 | 163 | |
ValenSalLop | 9:5e9c4277151d | 164 | for (i = 0; i < TASKS_NUMBER; i++) { |
ValenSalLop | 9:5e9c4277151d | 165 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 166 | pc.printf("initTasks: start %d\n",i); |
ValenSalLop | 9:5e9c4277151d | 167 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 168 | Thread::wait(taskList[i].delay); |
ValenSalLop | 5:19ccff89f555 | 169 | |
ValenSalLop | 9:5e9c4277151d | 170 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 171 | pc.printf("initTasks: su1 %d\n",retval); |
ValenSalLop | 9:5e9c4277151d | 172 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 173 | if (taskList[i].retVal == 1) { |
ValenSalLop | 9:5e9c4277151d | 174 | taskList[i].thread = new Thread(taskList[i].task, taskList[i].task_args, taskList[i].priority); |
ValenSalLop | 0:de99871168ac | 175 | } |
ValenSalLop | 5:19ccff89f555 | 176 | |
ValenSalLop | 9:5e9c4277151d | 177 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 178 | pc.printf("initTasks: finish su2 %d\n",retval); |
ValenSalLop | 9:5e9c4277151d | 179 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 180 | } |
ValenSalLop | 5:19ccff89f555 | 181 | |
ValenSalLop | 9:5e9c4277151d | 182 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 183 | pc.printf("initTasks: finish %d\n",retval); |
ValenSalLop | 9:5e9c4277151d | 184 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 185 | return retval; |
ValenSalLop | 9:5e9c4277151d | 186 | } |