car

Dependencies:   mbed

Fork of warehouse by Keegan Hu

Committer:
glintligo
Date:
Sun Jun 03 06:42:12 2018 +0000
Revision:
10:80ccb0dc0784
Parent:
9:238780dda8af
car;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brainliang 0:63af4719467f 1 /*-----------------------------------------------------
brainliang 0:63af4719467f 2 File Name : main.cpp
brainliang 0:63af4719467f 3 Purpose : For esp8266 mbed porting
brainliang 0:63af4719467f 4 Creation Date : 22-06-2017
brainliang 0:63af4719467f 5 Last Modified :
brainliang 0:63af4719467f 6 Created By : Jeasine Ma [jeasinema[at]gmail[dot]com]
brainliang 0:63af4719467f 7 -----------------------------------------------------*/
brainliang 0:63af4719467f 8 #include <cstdarg>
brainliang 0:63af4719467f 9 #include <cstring>
brainliang 0:63af4719467f 10 #include "mbed.h"
brainliang 0:63af4719467f 11 #include "esp8266.h"
brainliang 0:63af4719467f 12
glintligo 10:80ccb0dc0784 13 Serial ser2usb(PA_2, PA_3, 115200);
zhangyx 8:d5e340a59c71 14 DigitalIn ref(PA_12);
zhangyx 8:d5e340a59c71 15 DigitalOut LED(PC_13);
glintligo 10:80ccb0dc0784 16 DigitalOut IN1(PB_4);
glintligo 10:80ccb0dc0784 17 DigitalOut IN2(PB_3);
glintligo 10:80ccb0dc0784 18 DigitalOut IN3(PA_15);
glintligo 10:80ccb0dc0784 19 DigitalOut IN4(PA_12);
glintligo 10:80ccb0dc0784 20 PwmOut ENA(PB_1);
glintligo 10:80ccb0dc0784 21 PwmOut ENB(PB_0);
glintligo 10:80ccb0dc0784 22 InterruptIn switch1(PB_6);
glintligo 10:80ccb0dc0784 23 int state = 0;
glintligo 10:80ccb0dc0784 24 int actionok = 0;
glintligo 10:80ccb0dc0784 25 void run_forwoard();
glintligo 10:80ccb0dc0784 26 void run_backwoard();
glintligo 10:80ccb0dc0784 27 void stop();
glintligo 10:80ccb0dc0784 28 void init();
brainliang 0:63af4719467f 29 int main(void) {
glintligo 10:80ccb0dc0784 30 IN1 = 0;
glintligo 10:80ccb0dc0784 31 IN2 = 0;
glintligo 10:80ccb0dc0784 32 IN3 = 0;
glintligo 10:80ccb0dc0784 33 IN4 = 0;
glintligo 10:80ccb0dc0784 34 ENA.period_ms(10);
glintligo 10:80ccb0dc0784 35 ENB.period_ms(10);
glintligo 10:80ccb0dc0784 36 ENA.pulsewidth_ms(3);
glintligo 10:80ccb0dc0784 37 ENB.pulsewidth_ms(3);
zhangyx 8:d5e340a59c71 38 ser2usb.printf("starting\r\n");
zhangyx 7:49a823b5a935 39
zhangyx 1:3b487c4813a2 40 // 选定与 esp8266 相连接的串口,WiFi 名称和密码
glintligo 9:238780dda8af 41 Esp8266 client(PA_9, PA_10, "iot_b827ebd838be", "ab087c9e");// 参数分别为 TX pin / RX pin / SSID / Password
zyx 3:06080fa094a0 42
zyx 3:06080fa094a0 43 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 44 const char* sensors[][2] = {
glintligo 10:80ccb0dc0784 45 "actionok", "",
zyx 3:06080fa094a0 46 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 47 };
zyx 3:06080fa094a0 48
zyx 3:06080fa094a0 49 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 50 const char* actuators[][2] = {
glintligo 10:80ccb0dc0784 51 "do", "int",
zyx 3:06080fa094a0 52 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 53 };
zhangyx 8:d5e340a59c71 54 ser2usb.printf("connecting...\r\n");
zyx 3:06080fa094a0 55
zyx 3:06080fa094a0 56 //连接到服务器
glintligo 10:80ccb0dc0784 57 client.connect_mqtt_broker("192.168.12.1", "car", sensors, actuators);
zyx 3:06080fa094a0 58
brainliang 0:63af4719467f 59 ser2usb.printf("Initialization done.\r\n");
zyx 3:06080fa094a0 60
zyx 3:06080fa094a0 61 char actuator_name[32], control_value[32];
glintligo 10:80ccb0dc0784 62 // bool ref_last;
glintligo 10:80ccb0dc0784 63 // float last_report=0;
zyx 3:06080fa094a0 64 Timer t;// 定时器用于计量发送传感器数据的时间
zyx 3:06080fa094a0 65 t.start();
glintligo 10:80ccb0dc0784 66 switch1.rise(&init);
brainliang 0:63af4719467f 67 while(1) {
zyx 3:06080fa094a0 68 //检查有没有收到新的执行器控制指令
zyx 3:06080fa094a0 69 if(client.get_control_cmd(actuator_name, control_value)){
zhangyx 8:d5e340a59c71 70 ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value);
zyx 3:06080fa094a0 71 //判断哪个执行器收到命令
glintligo 10:80ccb0dc0784 72 state = atoi(control_value);
glintligo 10:80ccb0dc0784 73 switch (state)
glintligo 10:80ccb0dc0784 74 {
glintligo 10:80ccb0dc0784 75 case 0:
glintligo 10:80ccb0dc0784 76 run_backwoard();
glintligo 10:80ccb0dc0784 77 break;
glintligo 10:80ccb0dc0784 78 case 1: // white to black
glintligo 10:80ccb0dc0784 79 run_forwoard();
glintligo 10:80ccb0dc0784 80 wait(0.2);
glintligo 10:80ccb0dc0784 81 stop();
glintligo 10:80ccb0dc0784 82 actionok = 1;
glintligo 10:80ccb0dc0784 83 break;
glintligo 10:80ccb0dc0784 84 case 2: //black to robo
glintligo 10:80ccb0dc0784 85 run_forwoard();
glintligo 10:80ccb0dc0784 86 break;
glintligo 10:80ccb0dc0784 87 case 3: //robo to white
glintligo 10:80ccb0dc0784 88 run_backwoard();
glintligo 10:80ccb0dc0784 89 break;
glintligo 10:80ccb0dc0784 90 case 4: //robo to black
glintligo 10:80ccb0dc0784 91 run_backwoard();
glintligo 10:80ccb0dc0784 92 break;
glintligo 10:80ccb0dc0784 93 case 5: //white to robo
glintligo 10:80ccb0dc0784 94 run_forwoard();
glintligo 10:80ccb0dc0784 95 break;
glintligo 10:80ccb0dc0784 96 case 6: //black to white
glintligo 10:80ccb0dc0784 97 run_backwoard();
glintligo 10:80ccb0dc0784 98 wait(0.2);
glintligo 10:80ccb0dc0784 99 stop();
glintligo 10:80ccb0dc0784 100 actionok = 1;
glintligo 10:80ccb0dc0784 101 break;
glintligo 10:80ccb0dc0784 102 default:
glintligo 10:80ccb0dc0784 103 break;
glintligo 9:238780dda8af 104
glintligo 10:80ccb0dc0784 105 }
glintligo 10:80ccb0dc0784 106 }
glintligo 10:80ccb0dc0784 107 if (actionok ==1)
glintligo 10:80ccb0dc0784 108 {
glintligo 10:80ccb0dc0784 109 client.publish_value("actionok", "0");
glintligo 10:80ccb0dc0784 110 actionok = 0;
glintligo 10:80ccb0dc0784 111 }
glintligo 10:80ccb0dc0784 112 // bool reflection = ref;
glintligo 10:80ccb0dc0784 113 // if(reflection != ref_last){ //仅在传感器发生变化时汇报数据
glintligo 10:80ccb0dc0784 114 //
glintligo 10:80ccb0dc0784 115 // ref_last = reflection;
glintligo 10:80ccb0dc0784 116 //
glintligo 10:80ccb0dc0784 117 // char val[4];
glintligo 10:80ccb0dc0784 118 // sprintf(val, "%d", (int)reflection);
glintligo 10:80ccb0dc0784 119 //
glintligo 10:80ccb0dc0784 120 // //汇报传感器数据,两个参数分别是传感器名字和值
glintligo 10:80ccb0dc0784 121 // client.publish_value("reflection", val);
glintligo 10:80ccb0dc0784 122 // }
glintligo 10:80ccb0dc0784 123 //
glintligo 10:80ccb0dc0784 124 // if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据
glintligo 10:80ccb0dc0784 125 //
glintligo 10:80ccb0dc0784 126 // //汇报传感器数据,两个参数分别是传感器名字和值
glintligo 10:80ccb0dc0784 127 // //client.publish_value("light", "30 lx");
glintligo 10:80ccb0dc0784 128 //
glintligo 10:80ccb0dc0784 129 // last_report = t.read();
glintligo 10:80ccb0dc0784 130 // }
glintligo 10:80ccb0dc0784 131 }
glintligo 10:80ccb0dc0784 132 }
glintligo 10:80ccb0dc0784 133
glintligo 10:80ccb0dc0784 134
glintligo 10:80ccb0dc0784 135 void run_forwoard()
glintligo 10:80ccb0dc0784 136 { IN1 = 1;
glintligo 10:80ccb0dc0784 137 IN2 = 0;
glintligo 10:80ccb0dc0784 138 IN3 = 0;
glintligo 10:80ccb0dc0784 139 IN4 = 1;
glintligo 10:80ccb0dc0784 140 }
glintligo 10:80ccb0dc0784 141
glintligo 10:80ccb0dc0784 142 void run_backwoard()
glintligo 10:80ccb0dc0784 143 {
glintligo 10:80ccb0dc0784 144 IN1 = 0;
glintligo 10:80ccb0dc0784 145 IN2 = 1;
glintligo 10:80ccb0dc0784 146 IN3 = 1;
glintligo 10:80ccb0dc0784 147 IN4 = 0;
glintligo 10:80ccb0dc0784 148 }
glintligo 10:80ccb0dc0784 149
glintligo 10:80ccb0dc0784 150 void stop()
glintligo 10:80ccb0dc0784 151 {
glintligo 10:80ccb0dc0784 152 IN1 = 0;
glintligo 10:80ccb0dc0784 153 IN2 = 0;
glintligo 10:80ccb0dc0784 154 IN3 = 0;
glintligo 10:80ccb0dc0784 155 IN4 = 0;
glintligo 10:80ccb0dc0784 156 }
glintligo 10:80ccb0dc0784 157
glintligo 10:80ccb0dc0784 158 void init()
glintligo 10:80ccb0dc0784 159 {
glintligo 10:80ccb0dc0784 160 wait(0.01);
glintligo 10:80ccb0dc0784 161
glintligo 10:80ccb0dc0784 162 if(switch1.read() == 1)
glintligo 10:80ccb0dc0784 163 {
glintligo 10:80ccb0dc0784 164 switch (state)
glintligo 10:80ccb0dc0784 165 {
glintligo 10:80ccb0dc0784 166 case 0:
glintligo 10:80ccb0dc0784 167 stop();
glintligo 10:80ccb0dc0784 168 wait(0.01);
glintligo 10:80ccb0dc0784 169 run_forwoard();
glintligo 10:80ccb0dc0784 170 wait(0.1);
glintligo 10:80ccb0dc0784 171 stop();
glintligo 10:80ccb0dc0784 172 actionok = 1;
glintligo 10:80ccb0dc0784 173 break;
glintligo 10:80ccb0dc0784 174 case 2:
glintligo 10:80ccb0dc0784 175 stop();
glintligo 10:80ccb0dc0784 176 wait(0.01);
glintligo 10:80ccb0dc0784 177 run_backwoard();
glintligo 10:80ccb0dc0784 178 wait(0.1);
glintligo 10:80ccb0dc0784 179 stop();
glintligo 10:80ccb0dc0784 180 actionok = 1;
glintligo 10:80ccb0dc0784 181 break;
glintligo 10:80ccb0dc0784 182 case 3:
glintligo 10:80ccb0dc0784 183 stop();
glintligo 10:80ccb0dc0784 184 wait(0.01);
glintligo 10:80ccb0dc0784 185 run_forwoard();
glintligo 10:80ccb0dc0784 186 wait(0.1);
glintligo 10:80ccb0dc0784 187 stop();
glintligo 10:80ccb0dc0784 188 actionok = 1;
glintligo 10:80ccb0dc0784 189 break;
glintligo 10:80ccb0dc0784 190 case 4:
glintligo 10:80ccb0dc0784 191 stop();
glintligo 10:80ccb0dc0784 192 wait(0.01);
glintligo 10:80ccb0dc0784 193 run_forwoard();
glintligo 9:238780dda8af 194 wait(0.5);
glintligo 10:80ccb0dc0784 195 stop();
glintligo 10:80ccb0dc0784 196 actionok = 1;
glintligo 10:80ccb0dc0784 197 break;
glintligo 10:80ccb0dc0784 198 case 5:
glintligo 10:80ccb0dc0784 199 stop();
glintligo 10:80ccb0dc0784 200 wait(0.01);
glintligo 10:80ccb0dc0784 201 run_backwoard();
glintligo 10:80ccb0dc0784 202 wait(0.1);
glintligo 10:80ccb0dc0784 203 stop();
glintligo 10:80ccb0dc0784 204 actionok = 1;
glintligo 10:80ccb0dc0784 205 break;
glintligo 10:80ccb0dc0784 206 default:
glintligo 10:80ccb0dc0784 207 break;
zyx 3:06080fa094a0 208 }
brainliang 0:63af4719467f 209 }
glintligo 10:80ccb0dc0784 210 }