test
Dependencies: mbed-STM32F103C8T6 mbed-rtos mbed-dev
main.cpp@9:e859598fa5d8, 2020-04-29 (annotated)
- Committer:
- bbw
- Date:
- Wed Apr 29 12:14:31 2020 +0000
- Revision:
- 9:e859598fa5d8
- Parent:
- 8:a32b83084287
not working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
techneo | 0:217105958c2d | 1 | #include "mbed.h" |
bbw | 1:0fe432e5dfc4 | 2 | #include "stm32f103c8t6.h" |
hankzhang | 3:30d61fa10b98 | 3 | #include "string.h" |
hankzhang | 3:30d61fa10b98 | 4 | #include "main.h" |
hankzhang | 4:864bb8bde75c | 5 | #include "stdio.h" |
hankzhang | 4:864bb8bde75c | 6 | #include "stdlib.h" |
bbw | 1:0fe432e5dfc4 | 7 | |
bbw | 6:a9cc2c424cf9 | 8 | DigitalOut MOTOA1(PB_4); |
bbw | 6:a9cc2c424cf9 | 9 | DigitalOut MOTOB1(PB_5); |
bbw | 6:a9cc2c424cf9 | 10 | |
bbw | 6:a9cc2c424cf9 | 11 | DigitalOut MOTOA2(PB_8); |
bbw | 6:a9cc2c424cf9 | 12 | DigitalOut MOTOB2(PB_9); |
bbw | 6:a9cc2c424cf9 | 13 | |
bbw | 6:a9cc2c424cf9 | 14 | AnalogIn SensorCurrent(PA_0); |
bbw | 6:a9cc2c424cf9 | 15 | |
bbw | 6:a9cc2c424cf9 | 16 | void motor1_move(uint8_t dir); |
bbw | 6:a9cc2c424cf9 | 17 | void motor2_move(uint8_t dir); |
bbw | 6:a9cc2c424cf9 | 18 | |
bbw | 6:a9cc2c424cf9 | 19 | void system_init(); |
bbw | 6:a9cc2c424cf9 | 20 | |
bbw | 8:a32b83084287 | 21 | uint8_t sensor_cnt,cal_cnt, cal_cnt2, cur_cnt, tar_cnt, pre_sensor_cnt; |
bbw | 6:a9cc2c424cf9 | 22 | float sense_value; |
bbw | 6:a9cc2c424cf9 | 23 | uint8_t ov_flag, init_flag, motor1_ready_flag, motor2_ready_flag, sensor_flag, ready_flag; |
bbw | 6:a9cc2c424cf9 | 24 | uint8_t open_flag,close_flag; |
bbw | 6:a9cc2c424cf9 | 25 | |
hankzhang | 7:155d5b6a416f | 26 | #define MAX_LENGTH_STEPS 55 |
hankzhang | 7:155d5b6a416f | 27 | #define MIN_LENGTH_STEPS 10 |
hankzhang | 7:155d5b6a416f | 28 | #define MOVING_UP 1 |
hankzhang | 7:155d5b6a416f | 29 | #define MOVING_DOWN 2 |
hankzhang | 7:155d5b6a416f | 30 | #define MOVING_FORWARD 1 |
hankzhang | 7:155d5b6a416f | 31 | #define MOVING_BACKWARD 2 |
hankzhang | 7:155d5b6a416f | 32 | #define STOP 0 |
bbw | 6:a9cc2c424cf9 | 33 | |
bbw | 9:e859598fa5d8 | 34 | #define DEBOUNCE 20 |
bbw | 9:e859598fa5d8 | 35 | |
hankzhang | 7:155d5b6a416f | 36 | //--------------------------------------------------- |
bbw | 9:e859598fa5d8 | 37 | DigitalOut WIFI_PWREN(PC_13); |
hankzhang | 3:30d61fa10b98 | 38 | |
hankzhang | 3:30d61fa10b98 | 39 | UARTSerial *_serial; |
bbw | 1:0fe432e5dfc4 | 40 | |
hankzhang | 3:30d61fa10b98 | 41 | UARTSerial debug_uart(PB_10, PB_11, 115200); |
hankzhang | 3:30d61fa10b98 | 42 | UARTSerial wifi_uart(PA_2, PA_3, 115200); |
hankzhang | 3:30d61fa10b98 | 43 | |
bbw | 8:a32b83084287 | 44 | InterruptIn DebugKey(PA_12); |
bbw | 8:a32b83084287 | 45 | volatile bool button1_pressed = false; // Used in the main loop |
bbw | 8:a32b83084287 | 46 | volatile bool button1_enabled = true; // Used for debouncing |
bbw | 8:a32b83084287 | 47 | Timeout button1_timeout; // Used for debouncing |
bbw | 8:a32b83084287 | 48 | |
bbw | 8:a32b83084287 | 49 | // Enables button when bouncing is over |
bbw | 9:e859598fa5d8 | 50 | void button1_enabled_cb(void){ |
bbw | 9:e859598fa5d8 | 51 | button1_enabled = true; |
bbw | 8:a32b83084287 | 52 | } |
bbw | 8:a32b83084287 | 53 | |
bbw | 8:a32b83084287 | 54 | // ISR handling button pressed event |
bbw | 9:e859598fa5d8 | 55 | void button1_onpressed_cb(void){ |
bbw | 9:e859598fa5d8 | 56 | if (button1_enabled){ // Disabled while the button is bouncing |
bbw | 9:e859598fa5d8 | 57 | button1_enabled = false; |
bbw | 9:e859598fa5d8 | 58 | button1_pressed = true; // To be read by the main loop |
bbw | 9:e859598fa5d8 | 59 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
bbw | 9:e859598fa5d8 | 60 | } |
bbw | 8:a32b83084287 | 61 | } |
bbw | 8:a32b83084287 | 62 | |
bbw | 8:a32b83084287 | 63 | |
hankzhang | 4:864bb8bde75c | 64 | //Serial wifi_uart(PA_2, PA_3, 115200); |
hankzhang | 4:864bb8bde75c | 65 | //Serial debug_uart(PB_10, PB_11, 115200); |
hankzhang | 4:864bb8bde75c | 66 | |
hankzhang | 4:864bb8bde75c | 67 | char rxBuf[32]; |
hankzhang | 4:864bb8bde75c | 68 | char wifi_rxBuf[32]; //receive msg from xiaomi cloud |
hankzhang | 4:864bb8bde75c | 69 | short g_isCloud = 0; //flag for connected xiaomi cloud |
hankzhang | 4:864bb8bde75c | 70 | |
hankzhang | 4:864bb8bde75c | 71 | //char set_property[] = {"down set_properties "} |
hankzhang | 4:864bb8bde75c | 72 | //char get_property[] = {"down get_properties "} |
hankzhang | 3:30d61fa10b98 | 73 | int i = 0; |
hankzhang | 7:155d5b6a416f | 74 | //--------------------------------------------------- |
hankzhang | 3:30d61fa10b98 | 75 | |
hankzhang | 3:30d61fa10b98 | 76 | ATCmdParser *_parser; |
bbw | 9:e859598fa5d8 | 77 | |
hankzhang | 7:155d5b6a416f | 78 | void sensor_capture_cb(void){ |
bbw | 8:a32b83084287 | 79 | sensor_cnt++; |
bbw | 8:a32b83084287 | 80 | if(open_flag){ |
bbw | 9:e859598fa5d8 | 81 | if(cur_cnt<tar_cnt){ |
bbw | 9:e859598fa5d8 | 82 | cur_cnt++; |
bbw | 9:e859598fa5d8 | 83 | } |
bbw | 8:a32b83084287 | 84 | } |
bbw | 8:a32b83084287 | 85 | if(close_flag){ |
bbw | 8:a32b83084287 | 86 | if(cur_cnt>0){ |
bbw | 9:e859598fa5d8 | 87 | cur_cnt--; |
bbw | 8:a32b83084287 | 88 | } |
bbw | 8:a32b83084287 | 89 | } |
hankzhang | 7:155d5b6a416f | 90 | } |
hankzhang | 7:155d5b6a416f | 91 | |
hankzhang | 7:155d5b6a416f | 92 | void Power_thread(){/*detect current*/ |
bbw | 8:a32b83084287 | 93 | char len[50]; |
bbw | 8:a32b83084287 | 94 | char i = 0; |
hankzhang | 7:155d5b6a416f | 95 | while(true){ |
bbw | 8:a32b83084287 | 96 | Thread::wait(200); /*unit millisec*/ |
hankzhang | 7:155d5b6a416f | 97 | sense_value = SensorCurrent.read(); |
bbw | 9:e859598fa5d8 | 98 | //if((sense_value>0.5)&&sensor_flag){ |
bbw | 9:e859598fa5d8 | 99 | if(sense_value>0.5){ |
bbw | 8:a32b83084287 | 100 | sprintf(len, "Power_thread: sense_value = %0.4f > 0.5 \r\n", sense_value); |
bbw | 8:a32b83084287 | 101 | debug_uart.write(len, sizeof(len)); |
bbw | 8:a32b83084287 | 102 | i++; |
bbw | 8:a32b83084287 | 103 | if(i>1){ |
bbw | 8:a32b83084287 | 104 | ov_flag = 1; |
bbw | 8:a32b83084287 | 105 | } |
bbw | 8:a32b83084287 | 106 | }else{ |
bbw | 9:e859598fa5d8 | 107 | i = 0; |
bbw | 9:e859598fa5d8 | 108 | } |
hankzhang | 7:155d5b6a416f | 109 | } |
hankzhang | 7:155d5b6a416f | 110 | } |
hankzhang | 7:155d5b6a416f | 111 | |
bbw | 8:a32b83084287 | 112 | void Motor1_thread(){ |
bbw | 8:a32b83084287 | 113 | char len[50]; |
bbw | 8:a32b83084287 | 114 | while(true){ |
bbw | 8:a32b83084287 | 115 | Thread::wait(300); /*unit millisec*/ |
bbw | 8:a32b83084287 | 116 | if(!init_flag){ |
bbw | 8:a32b83084287 | 117 | while(1){ |
bbw | 8:a32b83084287 | 118 | if(motor2_ready_flag){break;}else{ |
bbw | 8:a32b83084287 | 119 | wait(1); |
bbw | 8:a32b83084287 | 120 | debug_uart.write("Motor1_thread: -------------------------------\r\n",48); |
bbw | 8:a32b83084287 | 121 | } |
bbw | 8:a32b83084287 | 122 | } |
bbw | 8:a32b83084287 | 123 | motor2_ready_flag = 0; |
bbw | 8:a32b83084287 | 124 | sensor_cnt = 0; |
bbw | 8:a32b83084287 | 125 | motor1_move(MOVING_FORWARD); |
bbw | 8:a32b83084287 | 126 | wait(1); |
bbw | 9:e859598fa5d8 | 127 | //sensor_flag = 1; /*enable current monitoring*/ |
bbw | 8:a32b83084287 | 128 | while(!ov_flag){ |
bbw | 8:a32b83084287 | 129 | debug_uart.write("Motor1_thread: waiting for ov_flag = 1 \r\n",41); |
bbw | 8:a32b83084287 | 130 | wait(1); |
bbw | 8:a32b83084287 | 131 | } |
bbw | 8:a32b83084287 | 132 | motor1_move(STOP); |
bbw | 8:a32b83084287 | 133 | ov_flag = 0; |
bbw | 8:a32b83084287 | 134 | motor2_ready_flag = 0; |
bbw | 8:a32b83084287 | 135 | cal_cnt = sensor_cnt; |
bbw | 8:a32b83084287 | 136 | sprintf(len, "Motor1_thread: primary calibrated cnt is %d \r\n", cal_cnt); |
bbw | 8:a32b83084287 | 137 | debug_uart.write(len, sizeof(len)); |
bbw | 8:a32b83084287 | 138 | wait(1); |
bbw | 8:a32b83084287 | 139 | motor1_ready_flag = 1; |
bbw | 8:a32b83084287 | 140 | while(1){ |
bbw | 8:a32b83084287 | 141 | if(motor2_ready_flag){break;}else{ |
bbw | 8:a32b83084287 | 142 | wait(1); |
bbw | 8:a32b83084287 | 143 | debug_uart.write("Motor1_thread: -------------------------------\r\n",48); |
hankzhang | 7:155d5b6a416f | 144 | } |
bbw | 8:a32b83084287 | 145 | } |
bbw | 8:a32b83084287 | 146 | motor2_ready_flag = 0; |
bbw | 8:a32b83084287 | 147 | sensor_cnt = 0; |
bbw | 8:a32b83084287 | 148 | motor1_move(MOVING_BACKWARD); |
bbw | 8:a32b83084287 | 149 | while(!ov_flag){ |
bbw | 8:a32b83084287 | 150 | debug_uart.write("Motor1_thread: waiting for ov_flag = 1 \r\n",41); |
bbw | 8:a32b83084287 | 151 | wait(1); |
bbw | 8:a32b83084287 | 152 | } |
bbw | 8:a32b83084287 | 153 | motor1_move(STOP); |
bbw | 8:a32b83084287 | 154 | cal_cnt2 = sensor_cnt; |
bbw | 8:a32b83084287 | 155 | sensor_cnt = 0; |
bbw | 8:a32b83084287 | 156 | cal_cnt = (cal_cnt2+cal_cnt)/2; |
bbw | 8:a32b83084287 | 157 | sprintf(len, "Motor1_thread: final calibrated cnt is %d \r\n", cal_cnt); |
bbw | 8:a32b83084287 | 158 | debug_uart.write(len, sizeof(len)); |
bbw | 8:a32b83084287 | 159 | wait(1); |
bbw | 8:a32b83084287 | 160 | motor1_ready_flag = 1; |
bbw | 8:a32b83084287 | 161 | while(!init_flag){debug_uart.write("Motor1_thread: -------------------------------\r\n",48);wait(1);} |
bbw | 8:a32b83084287 | 162 | }else{ |
bbw | 9:e859598fa5d8 | 163 | debug_uart.write("Motor1_thread: -----check motor2_ready_flag-----\r\n",50); |
bbw | 8:a32b83084287 | 164 | while(1){ |
bbw | 9:e859598fa5d8 | 165 | if(motor2_ready_flag){break;} |
bbw | 9:e859598fa5d8 | 166 | debug_uart.write("Motor1_thread: -----check motor2_ready_flag-----\r\n",50); |
bbw | 9:e859598fa5d8 | 167 | wait(1); |
bbw | 8:a32b83084287 | 168 | } |
bbw | 9:e859598fa5d8 | 169 | debug_uart.write("Motor1_thread: ------motor2_ready_flag = 1------\r\n",50); |
bbw | 8:a32b83084287 | 170 | motor2_ready_flag = 0; |
bbw | 9:e859598fa5d8 | 171 | sprintf(len, "Motor1_thread: cur_cnt = %d. \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 172 | debug_uart.write(len, sizeof(len)); |
bbw | 8:a32b83084287 | 173 | if(open_flag){ |
bbw | 9:e859598fa5d8 | 174 | motor1_move(MOVING_FORWARD); |
bbw | 9:e859598fa5d8 | 175 | while(1){ |
bbw | 9:e859598fa5d8 | 176 | sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 177 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 178 | sprintf(len, "Motor1_thread: tar_cnt is %d \r\n", tar_cnt); |
bbw | 9:e859598fa5d8 | 179 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 180 | wait(1); |
bbw | 9:e859598fa5d8 | 181 | if(cur_cnt>=tar_cnt){ |
bbw | 9:e859598fa5d8 | 182 | debug_uart.write("Motor1_thread: *********************************\r\n", 50); |
bbw | 9:e859598fa5d8 | 183 | sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 184 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 185 | break; |
bbw | 8:a32b83084287 | 186 | } |
bbw | 9:e859598fa5d8 | 187 | if(ov_flag){ |
bbw | 9:e859598fa5d8 | 188 | debug_uart.write("Motor1_thread: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", 50); |
bbw | 9:e859598fa5d8 | 189 | break; |
bbw | 9:e859598fa5d8 | 190 | } |
bbw | 9:e859598fa5d8 | 191 | } |
bbw | 8:a32b83084287 | 192 | } |
bbw | 8:a32b83084287 | 193 | if(close_flag){ |
bbw | 9:e859598fa5d8 | 194 | motor1_move(MOVING_BACKWARD); |
bbw | 9:e859598fa5d8 | 195 | while(1){ |
bbw | 9:e859598fa5d8 | 196 | sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 197 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 198 | sprintf(len, "Motor1_thread: tar_cnt is %d \r\n", tar_cnt); |
bbw | 9:e859598fa5d8 | 199 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 200 | wait(1); |
bbw | 9:e859598fa5d8 | 201 | if(cur_cnt<=tar_cnt){ |
bbw | 9:e859598fa5d8 | 202 | debug_uart.write("Motor1_thread: *********************************\r\n", 50); |
bbw | 9:e859598fa5d8 | 203 | sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 204 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 205 | break; |
bbw | 8:a32b83084287 | 206 | } |
bbw | 9:e859598fa5d8 | 207 | if(ov_flag){ |
bbw | 9:e859598fa5d8 | 208 | debug_uart.write("Motor1_thread: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", 50); |
bbw | 9:e859598fa5d8 | 209 | break; |
bbw | 9:e859598fa5d8 | 210 | } |
bbw | 9:e859598fa5d8 | 211 | } |
bbw | 8:a32b83084287 | 212 | } |
bbw | 8:a32b83084287 | 213 | motor1_move(STOP); |
bbw | 8:a32b83084287 | 214 | if(!ov_flag){ |
bbw | 9:e859598fa5d8 | 215 | cur_cnt = tar_cnt; |
bbw | 9:e859598fa5d8 | 216 | debug_uart.write("Motor1_thread: xxxxxxcur_cnt = tar_cntxxxxxxxxxx\r\n", 50); |
bbw | 8:a32b83084287 | 217 | }else{ |
bbw | 9:e859598fa5d8 | 218 | if(open_flag){cur_cnt = cal_cnt;} |
bbw | 9:e859598fa5d8 | 219 | if(close_flag){cur_cnt = 0;} |
bbw | 9:e859598fa5d8 | 220 | ov_flag = 0; |
bbw | 8:a32b83084287 | 221 | } |
bbw | 8:a32b83084287 | 222 | motor1_ready_flag = 1; |
bbw | 8:a32b83084287 | 223 | } |
bbw | 8:a32b83084287 | 224 | } |
hankzhang | 7:155d5b6a416f | 225 | } |
hankzhang | 7:155d5b6a416f | 226 | |
bbw | 8:a32b83084287 | 227 | void Motor2_thread(){ |
bbw | 8:a32b83084287 | 228 | uint8_t sta1,sta2; |
bbw | 8:a32b83084287 | 229 | uint8_t i; |
bbw | 8:a32b83084287 | 230 | DigitalIn Stopper1(PA_13); |
bbw | 8:a32b83084287 | 231 | DigitalIn Stopper2(PA_15); |
bbw | 8:a32b83084287 | 232 | char len[50]; |
bbw | 8:a32b83084287 | 233 | while(true){ |
bbw | 8:a32b83084287 | 234 | Thread::wait(300); /*unit millisec*/ |
bbw | 8:a32b83084287 | 235 | if(!init_flag){ |
bbw | 8:a32b83084287 | 236 | wait(1); |
bbw | 8:a32b83084287 | 237 | debug_uart.write("Motor2_thread: motor2 move up\r\n", 31); |
bbw | 8:a32b83084287 | 238 | motor2_move(MOVING_UP); |
bbw | 8:a32b83084287 | 239 | while(Stopper1){;} |
bbw | 8:a32b83084287 | 240 | motor2_move(STOP); |
bbw | 8:a32b83084287 | 241 | debug_uart.write("Motor2_thread: Up stopper triggered \r\n",38); |
bbw | 8:a32b83084287 | 242 | motor2_ready_flag = 1; |
bbw | 8:a32b83084287 | 243 | wait(1); |
bbw | 8:a32b83084287 | 244 | while(1){ |
bbw | 8:a32b83084287 | 245 | if(motor1_ready_flag){break;}else{ |
bbw | 8:a32b83084287 | 246 | wait(1);debug_uart.write("Motor2_thread: --------------------------------- \r\n",51); |
bbw | 8:a32b83084287 | 247 | } |
bbw | 8:a32b83084287 | 248 | } |
bbw | 8:a32b83084287 | 249 | motor1_ready_flag = 0; |
bbw | 8:a32b83084287 | 250 | motor2_move(MOVING_DOWN); |
bbw | 8:a32b83084287 | 251 | while(Stopper2){;} |
bbw | 8:a32b83084287 | 252 | motor2_move(STOP); |
bbw | 8:a32b83084287 | 253 | debug_uart.write("Motor2_thread: Down stopper triggered \r\n", 40); |
bbw | 8:a32b83084287 | 254 | motor2_ready_flag = 1; |
bbw | 8:a32b83084287 | 255 | while(1){ |
bbw | 8:a32b83084287 | 256 | if(motor1_ready_flag){break;}else{ |
bbw | 8:a32b83084287 | 257 | wait(1);debug_uart.write("Motor2_thread: --------------------------------- \r\n",51); |
hankzhang | 7:155d5b6a416f | 258 | } |
bbw | 8:a32b83084287 | 259 | } |
bbw | 8:a32b83084287 | 260 | motor1_ready_flag = 0; |
bbw | 8:a32b83084287 | 261 | debug_uart.write("Motor2_thread: motor2 move to center\r\n",38); |
bbw | 8:a32b83084287 | 262 | ov_flag = 0; |
bbw | 8:a32b83084287 | 263 | ready_flag = 1; |
bbw | 8:a32b83084287 | 264 | motor2_move(MOVING_UP); |
bbw | 8:a32b83084287 | 265 | wait(1.6); |
bbw | 8:a32b83084287 | 266 | motor2_move(STOP); |
bbw | 8:a32b83084287 | 267 | ready_flag = 0; |
bbw | 8:a32b83084287 | 268 | debug_uart.write("Motor2_thread: motor2 thread done\r\n",35); |
bbw | 8:a32b83084287 | 269 | cur_cnt = 0; |
bbw | 8:a32b83084287 | 270 | init_flag = 1; |
bbw | 8:a32b83084287 | 271 | }else{ |
bbw | 9:e859598fa5d8 | 272 | debug_uart.write("Motor2_thread: ------------command?-------------\r\n", 50); |
bbw | 9:e859598fa5d8 | 273 | sprintf(len, "Motor2_thread: cur_cnt = %d. \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 274 | debug_uart.write(len, sizeof(len)); |
bbw | 8:a32b83084287 | 275 | while(1){ |
bbw | 9:e859598fa5d8 | 276 | if(open_flag){break;} |
bbw | 9:e859598fa5d8 | 277 | if(close_flag){break;} |
bbw | 9:e859598fa5d8 | 278 | debug_uart.write("Motor2_thread: ------------command?-------------\r\n", 50); |
bbw | 9:e859598fa5d8 | 279 | wait(1); |
bbw | 8:a32b83084287 | 280 | } |
bbw | 8:a32b83084287 | 281 | if(open_flag){ |
bbw | 9:e859598fa5d8 | 282 | debug_uart.write("Motor2_thread: --------------open---------------\r\n", 50); |
bbw | 8:a32b83084287 | 283 | motor2_move(MOVING_UP); |
bbw | 9:e859598fa5d8 | 284 | while(1){ |
bbw | 9:e859598fa5d8 | 285 | if(!Stopper1){i++;}; |
bbw | 9:e859598fa5d8 | 286 | if(i>DEBOUNCE){break;} |
bbw | 9:e859598fa5d8 | 287 | } |
bbw | 8:a32b83084287 | 288 | } |
bbw | 8:a32b83084287 | 289 | if(close_flag){ |
bbw | 9:e859598fa5d8 | 290 | debug_uart.write("Motor2_thread: -------------close---------------\r\n", 50); |
bbw | 8:a32b83084287 | 291 | motor2_move(MOVING_DOWN); |
bbw | 8:a32b83084287 | 292 | while(1){ |
bbw | 9:e859598fa5d8 | 293 | if(!Stopper2){i++;}; |
bbw | 9:e859598fa5d8 | 294 | if(i>DEBOUNCE){break;} |
bbw | 8:a32b83084287 | 295 | } |
bbw | 8:a32b83084287 | 296 | } |
bbw | 9:e859598fa5d8 | 297 | i = 0; |
bbw | 9:e859598fa5d8 | 298 | motor2_move(STOP); |
bbw | 9:e859598fa5d8 | 299 | motor2_ready_flag = 1; |
bbw | 8:a32b83084287 | 300 | while(1){ |
bbw | 9:e859598fa5d8 | 301 | if(motor1_ready_flag){break;} |
bbw | 9:e859598fa5d8 | 302 | debug_uart.write("Motor2_thread: ---check motor1_ready_flag-------\r\n",50); |
bbw | 9:e859598fa5d8 | 303 | wait(1); |
bbw | 8:a32b83084287 | 304 | } |
bbw | 9:e859598fa5d8 | 305 | debug_uart.write("Motor2_thread: -------motor1_ready_flag = 1-------\r\n",50); |
bbw | 8:a32b83084287 | 306 | motor1_ready_flag = 0; |
bbw | 8:a32b83084287 | 307 | wait(1); |
bbw | 9:e859598fa5d8 | 308 | debug_uart.write("Motor2_thread: -------motor2 move to center-------\r\n",50); |
bbw | 8:a32b83084287 | 309 | if(open_flag){ |
bbw | 8:a32b83084287 | 310 | motor2_move(MOVING_DOWN); |
bbw | 8:a32b83084287 | 311 | } |
bbw | 8:a32b83084287 | 312 | if(close_flag){ |
bbw | 8:a32b83084287 | 313 | motor2_move(MOVING_UP); |
bbw | 8:a32b83084287 | 314 | } |
bbw | 8:a32b83084287 | 315 | wait(1.6); |
bbw | 8:a32b83084287 | 316 | motor2_move(STOP); |
bbw | 9:e859598fa5d8 | 317 | debug_uart.write("Motor2_thread: --------motor2 thread done-------\r\n",50); |
bbw | 9:e859598fa5d8 | 318 | sensor_cnt = 0; |
bbw | 9:e859598fa5d8 | 319 | pre_sensor_cnt = 0; |
bbw | 8:a32b83084287 | 320 | open_flag = 0; |
bbw | 8:a32b83084287 | 321 | close_flag = 0; |
hankzhang | 7:155d5b6a416f | 322 | } |
bbw | 8:a32b83084287 | 323 | } |
hankzhang | 7:155d5b6a416f | 324 | } |
hankzhang | 7:155d5b6a416f | 325 | |
hankzhang | 7:155d5b6a416f | 326 | |
hankzhang | 4:864bb8bde75c | 327 | |
hankzhang | 4:864bb8bde75c | 328 | void led1_thread() { |
hankzhang | 4:864bb8bde75c | 329 | int length; |
hankzhang | 4:864bb8bde75c | 330 | char len[20]; |
hankzhang | 4:864bb8bde75c | 331 | char l; |
hankzhang | 4:864bb8bde75c | 332 | int position = 0; |
hankzhang | 4:864bb8bde75c | 333 | int error = 0; |
hankzhang | 4:864bb8bde75c | 334 | |
hankzhang | 4:864bb8bde75c | 335 | while (true) |
hankzhang | 4:864bb8bde75c | 336 | { |
bbw | 8:a32b83084287 | 337 | wifi_uart.write("get_down\r\n", 10); |
hankzhang | 4:864bb8bde75c | 338 | if(wifi_uart.readable()) |
hankzhang | 4:864bb8bde75c | 339 | { |
hankzhang | 4:864bb8bde75c | 340 | length = wifi_uart.read(wifi_rxBuf, sizeof(wifi_rxBuf)); |
hankzhang | 4:864bb8bde75c | 341 | if(!(strncmp(wifi_rxBuf,"down none",9))) |
hankzhang | 4:864bb8bde75c | 342 | { |
hankzhang | 4:864bb8bde75c | 343 | //if return "down none" |
bbw | 8:a32b83084287 | 344 | //debug_uart.write("--- none\r\n",10); |
hankzhang | 4:864bb8bde75c | 345 | } |
hankzhang | 4:864bb8bde75c | 346 | else if(!(strncmp(wifi_rxBuf,"down set_properties ",20))) |
hankzhang | 4:864bb8bde75c | 347 | { |
hankzhang | 4:864bb8bde75c | 348 | //if return "down set_properties" |
bbw | 8:a32b83084287 | 349 | //debug_uart.write("--- set:\r\n", 10); |
bbw | 8:a32b83084287 | 350 | //debug_uart.write(wifi_rxBuf, length); |
hankzhang | 4:864bb8bde75c | 351 | |
hankzhang | 4:864bb8bde75c | 352 | //set properties |
hankzhang | 4:864bb8bde75c | 353 | if(wifi_rxBuf[22] == '7') |
hankzhang | 4:864bb8bde75c | 354 | { |
bbw | 8:a32b83084287 | 355 | //set target-position |
bbw | 8:a32b83084287 | 356 | position = atoi(&wifi_rxBuf[24]); |
bbw | 8:a32b83084287 | 357 | sprintf(len, "position:%d\r\n", position); |
bbw | 8:a32b83084287 | 358 | debug_uart.write(len, sizeof(len)); |
bbw | 8:a32b83084287 | 359 | /*add by bob*/ |
bbw | 8:a32b83084287 | 360 | if(position<=10){//0 |
bbw | 8:a32b83084287 | 361 | tar_cnt = 0; |
bbw | 8:a32b83084287 | 362 | }else if(position<=30){//25% |
bbw | 8:a32b83084287 | 363 | tar_cnt = 30; |
bbw | 8:a32b83084287 | 364 | }else if(position<=60){//50% |
bbw | 8:a32b83084287 | 365 | tar_cnt = 50; |
bbw | 8:a32b83084287 | 366 | }else if(position<=85){//75% |
bbw | 8:a32b83084287 | 367 | tar_cnt = 70; |
bbw | 8:a32b83084287 | 368 | }else{//100% |
bbw | 8:a32b83084287 | 369 | tar_cnt = 90; |
bbw | 8:a32b83084287 | 370 | } |
bbw | 8:a32b83084287 | 371 | if(tar_cnt>cur_cnt){ |
bbw | 8:a32b83084287 | 372 | open_flag = 1; |
bbw | 8:a32b83084287 | 373 | debug_uart.write("open flag = 1\r\n", 15); |
bbw | 8:a32b83084287 | 374 | } |
bbw | 8:a32b83084287 | 375 | if(tar_cnt<cur_cnt){ |
bbw | 8:a32b83084287 | 376 | close_flag = 1; |
bbw | 8:a32b83084287 | 377 | debug_uart.write("close flag = 1\r\n", 16); |
bbw | 8:a32b83084287 | 378 | } |
bbw | 8:a32b83084287 | 379 | } |
hankzhang | 4:864bb8bde75c | 380 | //report result to cloud |
bbw | 8:a32b83084287 | 381 | wifi_uart.write("result 2 7 0\r\n", 14); |
hankzhang | 4:864bb8bde75c | 382 | if(wifi_uart.readable()) |
hankzhang | 4:864bb8bde75c | 383 | { |
bbw | 8:a32b83084287 | 384 | //length = wifi_uart.read(wifi_rxBuf, sizeof(wifi_rxBuf)); |
bbw | 8:a32b83084287 | 385 | //debug_uart.write(wifi_rxBuf, length); |
hankzhang | 4:864bb8bde75c | 386 | } |
hankzhang | 4:864bb8bde75c | 387 | } |
hankzhang | 4:864bb8bde75c | 388 | else if(!(strncmp(wifi_rxBuf,"down get_properties ",20))) |
hankzhang | 4:864bb8bde75c | 389 | { |
hankzhang | 4:864bb8bde75c | 390 | //if return "down get_properties" |
bbw | 8:a32b83084287 | 391 | //debug_uart.write("--- get:\r\n", 10); |
bbw | 8:a32b83084287 | 392 | //debug_uart.write(wifi_rxBuf, length); |
hankzhang | 4:864bb8bde75c | 393 | |
hankzhang | 4:864bb8bde75c | 394 | //report result to cloud |
hankzhang | 4:864bb8bde75c | 395 | } |
hankzhang | 4:864bb8bde75c | 396 | else if(!(strncmp(wifi_rxBuf,"down MIIO_net_change ",21))) |
hankzhang | 4:864bb8bde75c | 397 | { |
hankzhang | 4:864bb8bde75c | 398 | //if return "down MIIO_net_change" |
bbw | 8:a32b83084287 | 399 | //debug_uart.write(wifi_rxBuf, length); |
bbw | 8:a32b83084287 | 400 | //debug_uart.write("--- net:\r\n",10); |
hankzhang | 4:864bb8bde75c | 401 | if((!strncmp(&wifi_rxBuf[21], "offline", 7))) |
hankzhang | 4:864bb8bde75c | 402 | { |
hankzhang | 4:864bb8bde75c | 403 | //连接中 |
bbw | 8:a32b83084287 | 404 | debug_uart.write("offline\r\n", 9); |
hankzhang | 4:864bb8bde75c | 405 | } |
hankzhang | 4:864bb8bde75c | 406 | else if((!strncmp(&wifi_rxBuf[21], "local", 5))) |
hankzhang | 4:864bb8bde75c | 407 | { |
hankzhang | 4:864bb8bde75c | 408 | //连上路由器但未连上服务器 |
bbw | 8:a32b83084287 | 409 | debug_uart.write("local\r\n", 7); |
hankzhang | 4:864bb8bde75c | 410 | } |
hankzhang | 4:864bb8bde75c | 411 | else if((!strncmp(&wifi_rxBuf[21], "cloud", 5))) |
hankzhang | 4:864bb8bde75c | 412 | { |
hankzhang | 4:864bb8bde75c | 413 | //连上小米云服务器 |
bbw | 8:a32b83084287 | 414 | debug_uart.write("cloud\r\n", 7); |
hankzhang | 4:864bb8bde75c | 415 | g_isCloud = 1; |
hankzhang | 4:864bb8bde75c | 416 | } |
hankzhang | 4:864bb8bde75c | 417 | } |
hankzhang | 4:864bb8bde75c | 418 | else |
hankzhang | 4:864bb8bde75c | 419 | { |
hankzhang | 4:864bb8bde75c | 420 | debug_uart.write(wifi_rxBuf, length); |
hankzhang | 4:864bb8bde75c | 421 | } |
hankzhang | 4:864bb8bde75c | 422 | |
hankzhang | 4:864bb8bde75c | 423 | } |
hankzhang | 4:864bb8bde75c | 424 | thread_sleep_for(400); |
techneo | 0:217105958c2d | 425 | } |
techneo | 0:217105958c2d | 426 | } |
techneo | 0:217105958c2d | 427 | |
bbw | 1:0fe432e5dfc4 | 428 | |
hankzhang | 2:f48b0967b6cc | 429 | void led0_thread() { |
hankzhang | 3:30d61fa10b98 | 430 | int length; |
hankzhang | 3:30d61fa10b98 | 431 | while (1) { |
hankzhang | 3:30d61fa10b98 | 432 | if(debug_uart.readable()) |
hankzhang | 3:30d61fa10b98 | 433 | { |
hankzhang | 3:30d61fa10b98 | 434 | length = debug_uart.read(rxBuf, sizeof(rxBuf)); |
hankzhang | 3:30d61fa10b98 | 435 | debug_uart.write(rxBuf, length); |
hankzhang | 3:30d61fa10b98 | 436 | wifi_uart.write(rxBuf, length); |
hankzhang | 3:30d61fa10b98 | 437 | debug_uart.write("111\r",4); |
hankzhang | 3:30d61fa10b98 | 438 | } |
hankzhang | 3:30d61fa10b98 | 439 | if(wifi_uart.readable()) |
hankzhang | 3:30d61fa10b98 | 440 | { |
hankzhang | 3:30d61fa10b98 | 441 | length = wifi_uart.read(rxBuf, sizeof(rxBuf)); |
hankzhang | 3:30d61fa10b98 | 442 | debug_uart.write(rxBuf, length); |
hankzhang | 3:30d61fa10b98 | 443 | debug_uart.write("222\r",4); |
hankzhang | 3:30d61fa10b98 | 444 | |
hankzhang | 3:30d61fa10b98 | 445 | } |
hankzhang | 3:30d61fa10b98 | 446 | wait(0.5); |
bbw | 1:0fe432e5dfc4 | 447 | } |
bbw | 1:0fe432e5dfc4 | 448 | } |
hankzhang | 3:30d61fa10b98 | 449 | |
hankzhang | 3:30d61fa10b98 | 450 | |
bbw | 6:a9cc2c424cf9 | 451 | void motor1_move(uint8_t dir){/*main motor*/ |
bbw | 6:a9cc2c424cf9 | 452 | if(dir==1){/*forward*/ |
bbw | 6:a9cc2c424cf9 | 453 | MOTOA1 = 0; |
bbw | 6:a9cc2c424cf9 | 454 | MOTOB1 = 1; |
bbw | 6:a9cc2c424cf9 | 455 | }else if(dir==2){/*backward*/ |
bbw | 6:a9cc2c424cf9 | 456 | MOTOA1 = 1; |
bbw | 6:a9cc2c424cf9 | 457 | MOTOB1 = 0; |
bbw | 6:a9cc2c424cf9 | 458 | }else{ /*stop*/ |
bbw | 6:a9cc2c424cf9 | 459 | MOTOA1 = 0; |
bbw | 6:a9cc2c424cf9 | 460 | MOTOB1 = 0; |
bbw | 6:a9cc2c424cf9 | 461 | } |
bbw | 6:a9cc2c424cf9 | 462 | } |
bbw | 6:a9cc2c424cf9 | 463 | |
bbw | 6:a9cc2c424cf9 | 464 | void motor2_move(uint8_t dir){/*assistant motor*/ |
bbw | 6:a9cc2c424cf9 | 465 | if(dir==1){/*up*/ |
bbw | 6:a9cc2c424cf9 | 466 | MOTOA2 = 0; |
bbw | 6:a9cc2c424cf9 | 467 | MOTOB2 = 1; |
bbw | 6:a9cc2c424cf9 | 468 | }else if(dir==2){/*down*/ |
bbw | 6:a9cc2c424cf9 | 469 | MOTOA2 = 1; |
bbw | 6:a9cc2c424cf9 | 470 | MOTOB2 = 0; |
bbw | 6:a9cc2c424cf9 | 471 | }else{ /*stop*/ |
bbw | 6:a9cc2c424cf9 | 472 | MOTOA2 = 0; |
bbw | 6:a9cc2c424cf9 | 473 | MOTOB2 = 0; |
bbw | 6:a9cc2c424cf9 | 474 | } |
bbw | 6:a9cc2c424cf9 | 475 | } |
bbw | 6:a9cc2c424cf9 | 476 | |
bbw | 9:e859598fa5d8 | 477 | void system_init(){ |
bbw | 6:a9cc2c424cf9 | 478 | MOTOA1 = 0; |
bbw | 6:a9cc2c424cf9 | 479 | MOTOB1 = 0; |
bbw | 6:a9cc2c424cf9 | 480 | MOTOA2 = 0; |
bbw | 6:a9cc2c424cf9 | 481 | MOTOB2 = 0; |
bbw | 8:a32b83084287 | 482 | init_flag = 1; |
bbw | 8:a32b83084287 | 483 | cur_cnt = 0; |
bbw | 8:a32b83084287 | 484 | cal_cnt = 68; |
bbw | 6:a9cc2c424cf9 | 485 | motor1_ready_flag = 0; |
bbw | 6:a9cc2c424cf9 | 486 | motor2_ready_flag = 0; |
bbw | 6:a9cc2c424cf9 | 487 | sense_value = 0; |
bbw | 6:a9cc2c424cf9 | 488 | ready_flag = 0; |
bbw | 9:e859598fa5d8 | 489 | debug_uart.write("************************************************\r\n",50); |
bbw | 9:e859598fa5d8 | 490 | debug_uart.write("******************LAIWU TECH********************\r\n",50); |
bbw | 9:e859598fa5d8 | 491 | debug_uart.write("************************************************\r\n",50); |
bbw | 9:e859598fa5d8 | 492 | debug_uart.write("****system init done, wait 3 seconds to start***\r\n",50); |
bbw | 6:a9cc2c424cf9 | 493 | wait(3); |
hankzhang | 7:155d5b6a416f | 494 | } |
hankzhang | 7:155d5b6a416f | 495 | |
hankzhang | 7:155d5b6a416f | 496 | int main() { |
bbw | 9:e859598fa5d8 | 497 | int length; |
bbw | 9:e859598fa5d8 | 498 | char len[50]; |
bbw | 9:e859598fa5d8 | 499 | system_init(); |
bbw | 9:e859598fa5d8 | 500 | WIFI_PWREN = 1; |
bbw | 9:e859598fa5d8 | 501 | InterruptIn Hall1(PA_14); |
bbw | 9:e859598fa5d8 | 502 | Hall1.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event |
bbw | 9:e859598fa5d8 | 503 | DebugKey.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
bbw | 9:e859598fa5d8 | 504 | debug_uart.write("************************************************\r\n",50); |
bbw | 9:e859598fa5d8 | 505 | debug_uart.write("************Hall sensor init done***************\r\n",50); |
bbw | 9:e859598fa5d8 | 506 | |
bbw | 9:e859598fa5d8 | 507 | //Thread thread0(osPriorityNormal, 512, nullptr, nullptr); |
bbw | 9:e859598fa5d8 | 508 | Thread thread1(osPriorityNormal, 512, nullptr, nullptr); |
bbw | 9:e859598fa5d8 | 509 | //Thread thread2(osPriorityNormal, 512, nullptr, nullptr); |
bbw | 9:e859598fa5d8 | 510 | |
bbw | 9:e859598fa5d8 | 511 | Thread thread2(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ |
bbw | 9:e859598fa5d8 | 512 | //debug_uart.printf("thread1~~~~~~~~~~~~~~~~\r\n"); |
bbw | 9:e859598fa5d8 | 513 | Thread thread3(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ |
bbw | 9:e859598fa5d8 | 514 | //debug_uart.printf("thread2~~~~~~~~~~~~~~~~\r\n"); |
bbw | 9:e859598fa5d8 | 515 | Thread thread4(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ |
bbw | 9:e859598fa5d8 | 516 | //debug_uart.printf("thread3~~~~~~~~~~~~~~~~\r\n"); |
bbw | 9:e859598fa5d8 | 517 | |
bbw | 9:e859598fa5d8 | 518 | //thread0.start(led0_thread); |
bbw | 9:e859598fa5d8 | 519 | thread1.start(led1_thread); |
bbw | 9:e859598fa5d8 | 520 | thread2.start(Power_thread); |
bbw | 9:e859598fa5d8 | 521 | thread3.start(Motor1_thread); |
bbw | 9:e859598fa5d8 | 522 | thread4.start(Motor2_thread); |
bbw | 9:e859598fa5d8 | 523 | |
bbw | 9:e859598fa5d8 | 524 | debug_uart.write("************************************************\r\n",50); |
bbw | 9:e859598fa5d8 | 525 | debug_uart.write("************Four Threads created****************\r\n",50); |
bbw | 9:e859598fa5d8 | 526 | |
bbw | 9:e859598fa5d8 | 527 | while(1){ |
bbw | 9:e859598fa5d8 | 528 | if(g_isCloud){ |
bbw | 9:e859598fa5d8 | 529 | debug_uart.write("***********WIFI Status: connected***************\r\n",50); |
bbw | 9:e859598fa5d8 | 530 | } |
bbw | 9:e859598fa5d8 | 531 | if (button1_pressed) { // Set when button is pressed |
bbw | 9:e859598fa5d8 | 532 | button1_pressed = false; |
bbw | 9:e859598fa5d8 | 533 | debug_uart.write("-------------restore wifi module----------------\r\n",50); |
bbw | 9:e859598fa5d8 | 534 | wifi_uart.write("restore\r\n", 9); |
bbw | 9:e859598fa5d8 | 535 | } |
bbw | 9:e859598fa5d8 | 536 | #if 0 |
bbw | 9:e859598fa5d8 | 537 | if(!open_flag&&!close_flag){ |
bbw | 9:e859598fa5d8 | 538 | if(sensor_cnt>(pre_sensor_cnt+4)){ |
bbw | 9:e859598fa5d8 | 539 | if(cur_cnt<5){ |
bbw | 9:e859598fa5d8 | 540 | open_flag = 1; |
bbw | 9:e859598fa5d8 | 541 | tar_cnt = cal_cnt; |
bbw | 9:e859598fa5d8 | 542 | cur_cnt = cur_cnt+sensor_cnt; |
bbw | 9:e859598fa5d8 | 543 | debug_uart.write("main_thread: ---------------open----------------\r\n", 50); |
bbw | 9:e859598fa5d8 | 544 | }else{ |
bbw | 9:e859598fa5d8 | 545 | close_flag = 1; |
bbw | 9:e859598fa5d8 | 546 | tar_cnt = 0; |
bbw | 9:e859598fa5d8 | 547 | cur_cnt = cur_cnt-sensor_cnt; |
bbw | 9:e859598fa5d8 | 548 | debug_uart.write("main_thread: ---------------close---------------\r\n", 50); |
bbw | 9:e859598fa5d8 | 549 | } |
bbw | 9:e859598fa5d8 | 550 | sprintf(len, "main_thread: cur_cnt = %d. \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 551 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 552 | } |
bbw | 9:e859598fa5d8 | 553 | pre_sensor_cnt = sensor_cnt; |
bbw | 9:e859598fa5d8 | 554 | } |
bbw | 9:e859598fa5d8 | 555 | #endif |
hankzhang | 7:155d5b6a416f | 556 | wait(3); |
bbw | 9:e859598fa5d8 | 557 | sprintf(len, "main_thread: cur_cnt = %d. \r\n", cur_cnt); |
bbw | 9:e859598fa5d8 | 558 | debug_uart.write(len, sizeof(len)); |
bbw | 9:e859598fa5d8 | 559 | } |
hankzhang | 2:f48b0967b6cc | 560 | } |