test

Dependencies:   mbed-STM32F103C8T6 mbed-rtos mbed-dev

Committer:
bbw
Date:
Wed Apr 29 03:14:26 2020 +0000
Revision:
8:a32b83084287
Parent:
7:155d5b6a416f
Child:
9:e859598fa5d8
Child:
10:ef9fa7e34eff
0427 control through mobile phone

Who changed what in which revision?

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