![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
car
Fork of warehouse by
Revision 10:80ccb0dc0784, committed 2018-06-03
- Comitter:
- glintligo
- Date:
- Sun Jun 03 06:42:12 2018 +0000
- Parent:
- 9:238780dda8af
- Commit message:
- car;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 238780dda8af -r 80ccb0dc0784 main.cpp --- a/main.cpp Thu May 17 08:29:35 2018 +0000 +++ b/main.cpp Sun Jun 03 06:42:12 2018 +0000 @@ -10,25 +10,31 @@ #include "mbed.h" #include "esp8266.h" -Serial ser2usb(PA_2, PA_3, 115200); +Serial ser2usb(PA_2, PA_3, 115200); DigitalIn ref(PA_12); DigitalOut LED(PC_13); - - -// PA_6 P3 从下往上第二个舵机 1ms 往后倒90度 1.5ms 往后倒45度 2ms 笔直 -// PB_1 P6 从下往上第四个舵机 2ms 张开 1.5ms 中间 1ms 完全合上 -// PA_7 P4 从下往上第一个舵机 1.5ms 从正前方看往右偏45度 1ms 正中间 2ms 右偏度 -// PB_0 P5 从下往上第三个舵机 1.5 中间 1 往后45度 2 往前45 - - -PwmOut mypwm1(PA_7); -PwmOut mypwm2(PA_6); -PwmOut mypwm3(PB_0); -PwmOut mypwm4(PB_1); - - - +DigitalOut IN1(PB_4); +DigitalOut IN2(PB_3); +DigitalOut IN3(PA_15); +DigitalOut IN4(PA_12); +PwmOut ENA(PB_1); +PwmOut ENB(PB_0); +InterruptIn switch1(PB_6); +int state = 0; +int actionok = 0; +void run_forwoard(); +void run_backwoard(); +void stop(); +void init(); int main(void) { + IN1 = 0; + IN2 = 0; + IN3 = 0; + IN4 = 0; + ENA.period_ms(10); + ENB.period_ms(10); + ENA.pulsewidth_ms(3); + ENB.pulsewidth_ms(3); ser2usb.printf("starting\r\n"); // 选定与 esp8266 相连接的串口,WiFi 名称和密码 @@ -36,87 +42,169 @@ //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 const char* sensors[][2] = { - "light", "lx", - "reflection"," ", + "actionok", "", NULL, NULL //最后一行以空指针作为结束标记 }; //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 const char* actuators[][2] = { - "led", "int", + "do", "int", NULL, NULL //最后一行以空指针作为结束标记 }; ser2usb.printf("connecting...\r\n"); //连接到服务器 - client.connect_mqtt_broker("192.168.12.1", "alpha", sensors, actuators); + client.connect_mqtt_broker("192.168.12.1", "car", sensors, actuators); ser2usb.printf("Initialization done.\r\n"); char actuator_name[32], control_value[32]; - bool ref_last; - float last_report=0; +// bool ref_last; +// float last_report=0; Timer t;// 定时器用于计量发送传感器数据的时间 t.start(); - + switch1.rise(&init); while(1) { //检查有没有收到新的执行器控制指令 if(client.get_control_cmd(actuator_name, control_value)){ ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value); //判断哪个执行器收到命令 - if(strcmp(actuator_name, "led")==0){ - mypwm1.period_ms(20); - mypwm2.period_ms(20); - mypwm3.period_ms(20); - mypwm4.period_ms(20); - + state = atoi(control_value); + switch (state) + { + case 0: + run_backwoard(); + break; + case 1: // white to black + run_forwoard(); + wait(0.2); + stop(); + actionok = 1; + break; + case 2: //black to robo + run_forwoard(); + break; + case 3: //robo to white + run_backwoard(); + break; + case 4: //robo to black + run_backwoard(); + break; + case 5: //white to robo + run_forwoard(); + break; + case 6: //black to white + run_backwoard(); + wait(0.2); + stop(); + actionok = 1; + break; + default: + break; - mypwm1.pulsewidth(0.001); - wait(0.5); - mypwm1.pulsewidth(0.0015); - wait(0.5); - mypwm1.pulsewidth(0.002); - wait(0.5); - mypwm1.pulsewidth(0.0015); - wait(0.5); - mypwm1.pulsewidth(0.001); - wait(0.5); - - mypwm3.pulsewidth(0.001); - wait(0.5); - mypwm3.pulsewidth(0.0015); - wait(0.5); - - mypwm4.pulsewidth(0.001); - wait(0.5); - mypwm4.pulsewidth(0.0015); + } + } + if (actionok ==1) + { + client.publish_value("actionok", "0"); + actionok = 0; + } +// bool reflection = ref; +// if(reflection != ref_last){ //仅在传感器发生变化时汇报数据 +// +// ref_last = reflection; +// +// char val[4]; +// sprintf(val, "%d", (int)reflection); +// +// //汇报传感器数据,两个参数分别是传感器名字和值 +// client.publish_value("reflection", val); +// } +// +// if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据 +// +// //汇报传感器数据,两个参数分别是传感器名字和值 +// //client.publish_value("light", "30 lx"); +// +// last_report = t.read(); +// } + } +} + + +void run_forwoard() +{ IN1 = 1; + IN2 = 0; + IN3 = 0; + IN4 = 1; +} + +void run_backwoard() +{ + IN1 = 0; + IN2 = 1; + IN3 = 1; + IN4 = 0; +} + +void stop() +{ + IN1 = 0; + IN2 = 0; + IN3 = 0; + IN4 = 0; +} + +void init() +{ + wait(0.01); + + if(switch1.read() == 1) + { + switch (state) + { + case 0: + stop(); + wait(0.01); + run_forwoard(); + wait(0.1); + stop(); + actionok = 1; + break; + case 2: + stop(); + wait(0.01); + run_backwoard(); + wait(0.1); + stop(); + actionok = 1; + break; + case 3: + stop(); + wait(0.01); + run_forwoard(); + wait(0.1); + stop(); + actionok = 1; + break; + case 4: + stop(); + wait(0.01); + run_forwoard(); wait(0.5); - mypwm4.pulsewidth(0.002); - wait(0.5); - - mypwm3.pulsewidth(0.0015); - wait(0.5); - } - } - - bool reflection = ref; - if(reflection != ref_last){ //仅在传感器发生变化时汇报数据 - - ref_last = reflection; - - char val[4]; - sprintf(val, "%d", (int)reflection); - - //汇报传感器数据,两个参数分别是传感器名字和值 - client.publish_value("reflection", val); - } - - if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据 - - //汇报传感器数据,两个参数分别是传感器名字和值 - //client.publish_value("light", "30 lx"); - - last_report = t.read(); + stop(); + actionok = 1; + break; + case 5: + stop(); + wait(0.01); + run_backwoard(); + wait(0.1); + stop(); + actionok = 1; + break; + default: + break; } } -} +} \ No newline at end of file