test
Dependencies: mbed-STM32F103C8T6 mbed-rtos mbed-dev
Revision 11:9dc8bbb8dda3, committed 2020-05-06
- Comitter:
- bbw
- Date:
- Wed May 06 10:03:17 2020 +0000
- Parent:
- 10:ef9fa7e34eff
- Commit message:
- test only;
Changed in this revision
--- a/main.cpp Tue May 05 11:15:56 2020 +0000
+++ b/main.cpp Wed May 06 10:03:17 2020 +0000
@@ -5,123 +5,162 @@
#include "stdlib.h"
#include "rtos.h"
-DigitalOut led1(PC_13);
+#define MAX_LENGTH_STEPS 55
+#define MIN_LENGTH_STEPS 10
+#define MOVING_UP 1
+#define MOVING_DOWN 2
+#define MOVING_FORWARD 1
+#define MOVING_BACKWARD 2
+#define STOP 0
+#define DEBOUNCE 4 /*4*20ms*/
-RawSerial debug_uart(PB_10, PB_11, 115200);
-RawSerial wifi_uart(PA_2, PA_3, 115200);
+DigitalOut MOTOA1(PB_4);
+DigitalOut MOTOB1(PB_5);
-Timer timer;
+DigitalOut MOTOA2(PB_8);
+DigitalOut MOTOB2(PB_9);
+
+AnalogIn SensorCurrent(PA_0);
-char rxBuf[32];
-char wifi_rxBuf[32]; //receive msg from xiaomi cloud
-short g_isCloud = 0; //flag for connected xiaomi cloud
+void motor1_move(uint8_t dir);
+void motor2_move(uint8_t dir);
+
+uint8_t sensor_cnt,cal_cnt, cur_cnt, tar_cnt, pre_sensor_cnt;
+float sense_value;
+uint8_t ov_flag, init_flag;
+uint8_t open_flag,close_flag;
-//char set_property[] = {"down set_properties "}
-//char get_property[] = {"down get_properties "}
-int i = 0;
-//---------------------------------------------------
+#if 1 /*WIFI related*/
+DigitalOut led1(PC_13);
+Serial debug_uart(PB_10, PB_11, 115200);
+Serial wifi_uart(PA_2, PA_3, 115200);
+Timer timer; /*used by led0_thread*/
+string a(""); /*reserved*/
+string b(""); /*used by led0_thread*/
+char g_isCloud = 0; //flag for connected xiaomi cloud
+#endif
-string a("");
-string b("");
-
+Timer timer_m;
int flag = 0;
-#if 0
-void led1_thread(void const *argument) {
-
- while (true)
- {
- wifi_uart.printf("get_down\r\n");
- while(1){
- if(wifi_uart.readable())
- {
- char ch2 = wifi_uart.getc();
- //f((ch2=='\n')||(ch2=='\r'))
- if(ch2=='\r')
- {
- debug_uart.printf("got string from wifi uart: '%s' \r\n", b.c_str());
- if(!strncmp(b.c_str(),"down none",9))
- {
- //debug_uart.printf("--- none\r\n");
- }
- if(!(strncmp(b.c_str(),"down set_properties",19)))
- {
- debug_uart.printf("--- set_properties\r\n");
- wifi_uart.printf("result 2 7 0\r\n");
- }
- if(!(strncmp(b.c_str(),"down get_properties",19)))
- {
- debug_uart.printf("--- get_properties\r\n");
- //wifi_uart.printf("result 2 7 0\r\n");
- }
- if(!strncmp(b.c_str(),"down MIIO_net_change",20))
- {
- if((!strncmp(b.c_str()+21, "offline", 7)))
- {
- debug_uart.printf("offline\r\n");
- }
- if((!strncmp(b.c_str()+21, "local", 5)))
- {
- debug_uart.printf("local\r\n");
- }
- if((!strncmp(b.c_str()+21, "cloud", 5)))
- {
- debug_uart.printf("cloud\r\n");
- }
- }
- b.clear();
- break;
- }else{
- b += ch2;
- }
- }
- }
- //wait_ms(400);
+void Power_thread(void const *argument){/*detect current*/
+ char i = 0;
+ while(true){
+ Thread::wait(200); /*unit millisec*/
+ sense_value = SensorCurrent.read();
+ //debug_uart.printf("dfdfdfdfddfdf\r\n");
+ if(sense_value>0.5){
+ debug_uart.printf("Power_thread: sense_value = %0.4f > 0.5 \r\n", sense_value);
+ i++;
+ if(i>1){
+ ov_flag = 1;
+ }
+ }else{
+ i = 0;
+ }
+ }
+}
+
+char CheckStopper(int ch){
+ DigitalIn Stopper1(PA_13);
+ DigitalIn Stopper2(PA_15);
+ timer_m.reset();
+ char i = 0;
+ while(ch == MOVING_UP){
+ if(!Stopper1){i++;};
+ if(i>DEBOUNCE){break;}
+ if(timer_m.read_ms()>5000){return 1;}
+ wait_ms(20);
}
+ while(ch == MOVING_DOWN){
+ if(!Stopper2){i++;};
+ if(i>DEBOUNCE){break;}
+ if(timer_m.read_ms()>5000){return 1;}
+ wait_ms(20);
+ }
+ return 0;
}
-#endif
-
-#if 0
-void led0_thread(void const *argument) {
- while (1) {
- if(debug_uart.readable()){
- char ch1 = debug_uart.getc();
- if(ch1=='\n'){
- debug_uart.printf("sent string to wifi uart: '%s' \r\n", a.c_str());
- wifi_uart.printf("%s\r\n",a.c_str());
- a.clear();
- }else{
- a += ch1;
- }
- }
- if(wifi_uart.readable())
- {
- char ch2 = wifi_uart.getc();
- if((ch2=='\n')||(ch2=='\r')){
- debug_uart.printf("got string from wifi uart: '%s' \r\n", b.c_str());
- b.clear();
- }else{
- b += ch2;
- }
- }
- //wait(0.5);
- }
+void Motor_thread(void const *argument){
+ while(true){
+ Thread::wait(200); /*unit millisec*/
+ if(!init_flag){
+ wait(1);
+ debug_uart.printf("Motor_thread: motor2 move up\r\n");
+ motor2_move(MOVING_UP);
+ if(CheckStopper(MOVING_UP)){
+ debug_uart.printf("Motor_thread: motor2 timeout, stop motor2 and finish the calibration\r\n");
+ init_flag = 1;
+ motor2_move(STOP);
+ continue;
+ }
+ motor2_move(STOP);
+ wait(1);
+ debug_uart.printf("Motor_thread: motor1 move forward\r\n");
+ sensor_cnt = 0;
+ motor1_move(MOVING_FORWARD);
+ while(1){
+ if(ov_flag){
+ motor1_move(STOP);
+ cal_cnt = sensor_cnt;
+ ov_flag = 0;
+ break;
+ }
+ }
+ wait(1);
+ debug_uart.printf("Motor_thread: motor2 move down\r\n");
+ motor2_move(MOVING_DOWN);
+ if(CheckStopper(MOVING_DOWN)){
+ debug_uart.printf("Motor_thread: motor2 timeout, stop motor2 and finish the calibration\r\n");
+ init_flag = 1;
+ motor2_move(STOP);
+ continue;
+ }
+ motor2_move(STOP);
+ wait(1);
+ debug_uart.printf("Motor_thread: motor1 move backward\r\n");
+ sensor_cnt = 0;
+ motor1_move(MOVING_BACKWARD);
+ while(1){
+ if(ov_flag){
+ motor1_move(STOP);
+ ov_flag = 0;
+ debug_uart.printf("Motor_thread: cal_cnt = %d, 1/2(cal_cnt+sensor_cnt)\r\n", (cal_cnt+sensor_cnt)/2);
+ break;
+ }
+ if(sensor_cnt>=cal_cnt){
+ motor1_move(STOP);
+ debug_uart.printf("Motor_thread: cal_cnt = %d\r\n", cal_cnt);
+ break;
+ }
+ }
+ cur_cnt = 0;
+ wait(1);
+ debug_uart.printf("Motor_thread: motor2 move to center\r\n");
+ motor2_move(MOVING_UP);
+ wait(1.6);
+ motor2_move(STOP);
+ init_flag = 1;
+ debug_uart.printf("Motor_thread: auto-curtain length was calibrated sucessfully\r\n");
+ }else{
+ debug_uart.printf("Motor_thread: auto-curtain is waiting for command\r\n");
+ wait(3);
+ }
+ }
}
-#endif
void led0_thread(void const *argument) {
int err;
char rxBuf[32];
char wifi_rxBuf[32];
char i;
- int position;
- char dat[2];
-
+ int position;
+ char dat[2];
+
while (1) {
wifi_uart.printf("get_down\r");
- timer.reset();
- b.clear();
+ timer.reset();
+ b.clear();
while(timer.read_ms()<5000){
if(wifi_uart.readable()){
char ch = wifi_uart.getc();
@@ -129,89 +168,176 @@
b += ch;
}else{
debug_uart.printf("get string: %s \r\n", b.c_str());
- #if 1
- if(!strncmp(b.c_str(),"down none",9))
+ #if 1
+ if(!strncmp(b.c_str(),"down none",9))
{
//debug_uart.printf("--- none\r\n");
}
if(!(strncmp(b.c_str(),"down set_properties",19)))
{
- debug_uart.printf("--- set_properties\r\n");
- b.erase(0,b.length()-3);
- position = atoi(b.c_str());
- debug_uart.printf("position = %d\r\n",position);
+ //debug_uart.printf("--- set_properties\r\n");
+ b.erase(0,b.length()-3);
+ position = atoi(b.c_str());
+ debug_uart.printf("position = %d\r\n",position);
wifi_uart.printf("result 2 7 0\r\n");
- b.clear();
- continue;
+ b.clear();
+ continue;
}
if(!(strncmp(b.c_str(),"down get_properties",19)))
{
- debug_uart.printf("--- get_properties\r\n");
- wifi_uart.printf("result 2 4 0 10 2 6 0 %d 2 7 30\r\n", position);
- b.clear();
- continue;
+ //debug_uart.printf("--- get_properties\r\n");
+ wifi_uart.printf("result 2 4 0 10 2 6 0 %d 2 7 30\r\n", position);
+ b.clear();
+ continue;
}
if(!strncmp(b.c_str(),"down MIIO_net_change",20))
{
if((!strncmp(b.c_str()+21, "offline", 7)))
{
- debug_uart.printf("offline\r\n");
+ //debug_uart.printf("offline\r\n");
}
if((!strncmp(b.c_str()+21, "local", 5)))
{
- debug_uart.printf("local\r\n");
+ //debug_uart.printf("local\r\n");
}
if((!strncmp(b.c_str()+21, "cloud", 5)))
{
- debug_uart.printf("cloud\r\n");
+ //debug_uart.printf("cloud\r\n");
}
}
- #endif
+ #endif
break;
}
}
}
- while(wifi_uart.readable()){wifi_uart.getc();}
+ while(wifi_uart.readable()){wifi_uart.getc();}
wait_ms(400);
}
}
-void wifi_debug(void const *argument){
- while(1){
- if(wifi_uart.readable()){
- debug_uart.putc(wifi_uart.getc());
+void serialprocess(void const *argument){
+ while(1){
+ if(debug_uart.readable()){
+ char ch = debug_uart.getc();
+ if(ch!=0x0d){
+ a += ch;
+ }else{
+ debug_uart.printf("get command from uart: '%s'\r\n", a.c_str());
+ if(!strncmp(a.c_str(),"stp1",4)){
+ debug_uart.printf("stepper1 ready\r\n");
+ }
+ if(!strncmp(a.c_str(),"stp2",4)){
+ debug_uart.printf("stepper2 ready\r\n");
+ }
+ if(!strncmp(a.c_str(),"ovflag",6)){
+ debug_uart.printf("ovflag = 1\r\n");
+ }
+ a.clear();
+ }
+ }
}
- if(debug_uart.readable()){
- wifi_uart.putc(debug_uart.getc());
+}
+
+void serialIRQ(void){
+ debug_uart.putc(debug_uart.getc());
+}
+
+void sensor_capture_cb(void){
+ sensor_cnt++;
+ if(open_flag){
+ if(cur_cnt<tar_cnt){
+ cur_cnt++;
+ }
+ }
+ if(close_flag){
+ if(cur_cnt>0){
+ cur_cnt--;
}
}
}
+void motor1_move(uint8_t dir){/*main motor*/
+ if(dir==1){/*forward*/
+ MOTOA1 = 0;
+ MOTOB1 = 1;
+ }else if(dir==2){/*backward*/
+ MOTOA1 = 1;
+ MOTOB1 = 0;
+ }else{ /*stop*/
+ MOTOA1 = 0;
+ MOTOB1 = 0;
+ }
+}
+
+void motor2_move(uint8_t dir){/*assistant motor*/
+ if(dir==1){/*up*/
+ MOTOA2 = 0;
+ MOTOB2 = 1;
+ }else if(dir==2){/*down*/
+ MOTOA2 = 1;
+ MOTOB2 = 0;
+ }else{ /*stop*/
+ MOTOA2 = 0;
+ MOTOB2 = 0;
+ }
+}
+
+
+void system_init(){
+ MOTOA1 = 0;
+ MOTOB1 = 0;
+ MOTOA2 = 0;
+ MOTOB2 = 0;
+ init_flag = 0;
+ cur_cnt = 0;
+ cal_cnt = 0;
+ sense_value = 0;
+ debug_uart.printf("\r\n");
+ debug_uart.printf("************************************************\r\n");
+ debug_uart.printf("******************LAIWU TECH********************\r\n");
+ debug_uart.printf("************************************************\r\n");
+ debug_uart.printf("****system init done, wait 3 seconds to start***\r\n");
+ wait(3);
+}
+
int main() {
- debug_uart.printf("hello world\r\n");
+ system_init();
+
led1 = 1;
- flag = 0;
+ flag = 0;
+
+ wait(3);
+
+ InterruptIn Hall1(PA_14);
+ Hall1.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event
- wait(3);
+ debug_uart.printf("************************************************\r\n");
+ debug_uart.printf("***************hall sensor init*****************\r\n");
+
- Thread thread1(led0_thread, NULL, osPriorityHigh, DEFAULT_STACK_SIZE); /*check the real-time current*/
-
+ //Thread thread2(Power_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/
+ debug_uart.printf("**********************2*************************\r\n");
+ //Thread thread3(Motor_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/
+ debug_uart.printf("**********************3*************************\r\n");
+ Thread thread1(led0_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the wifi connection*/
+ debug_uart.printf("**********************1*************************\r\n");
- timer.start();
+ debug_uart.printf("**************three threads init****************\r\n");
+
+ timer.start();
+ timer_m.start();
+
+ debug_uart.printf("************************************************\r\n");
+ debug_uart.printf("****************two timer init*****************\r\n");
+
+ debug_uart.attach(&serialIRQ, Serial::RxIrq);
while(1)
{
- wait(1);
-#if 0
- if(flag){
- debug_uart.printf("get string: '%s'\r\n", b.c_str());
- b.clear();
- while(wifi_uart.readable()){wifi_uart.getc();}
- flag = 0;
- debug_uart.printf("sent get_down to module\r\n");
- wifi_uart.printf("get_down\r\n");
- }
-#endif
+ wait(1);
+ //wifi_uart.printf("get_down\r");
+ //debug_uart.printf("************************************************\r\n");
+ //debug_uart.printf("********************%%%%%***********************\r\n");
}
}
\ No newline at end of file
--- a/mbed-STM32F103C8T6.lib Tue May 05 11:15:56 2020 +0000 +++ b/mbed-STM32F103C8T6.lib Wed May 06 10:03:17 2020 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/hudakz/code/mbed-STM32F103C8T6/#fe3c80e86183 +https://os.mbed.com/users/bbw/code/mbed-STM32F103C8T6/#fe3c80e86183
--- a/mbed-rtos.lib Tue May 05 11:15:56 2020 +0000 +++ b/mbed-rtos.lib Wed May 06 10:03:17 2020 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e +http://mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706