小车 走

Dependencies:   mbed

Committer:
xmwmx
Date:
Sat Aug 04 04:17:02 2018 +0000
Revision:
2:4a9ba34cd90a
Parent:
1:d89a943c462e
?????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xmwmx 1:d89a943c462e 1 /*-----------------------------------------------------
xmwmx 1:d89a943c462e 2 File Name : main.cpp
xmwmx 1:d89a943c462e 3 Purpose : For esp8266 mbed porting
xmwmx 1:d89a943c462e 4 Creation Date : 22-06-2017
xmwmx 1:d89a943c462e 5 Last Modified :
xmwmx 1:d89a943c462e 6 Created By : Jeasine Ma [jeasinema[at]gmail[dot]com]
xmwmx 1:d89a943c462e 7 -----------------------------------------------------*/
xmwmx 1:d89a943c462e 8 #include <cstdarg>
xmwmx 1:d89a943c462e 9 #include <cstring>
xmwmx 1:d89a943c462e 10 #include "mbed.h"
xmwmx 1:d89a943c462e 11 #include "esp8266.h"
xmwmx 1:d89a943c462e 12
xmwmx 1:d89a943c462e 13 Serial ser2usb(PA_2, PA_3, 115200);
xmwmx 1:d89a943c462e 14 //DigitalIn ref(PA_12);
xmwmx 1:d89a943c462e 15 DigitalOut LED(PC_13);
xmwmx 1:d89a943c462e 16 DigitalOut IN1(PB_0);
xmwmx 1:d89a943c462e 17 DigitalOut IN2(PB_1);
xmwmx 1:d89a943c462e 18 DigitalOut IN3(PB_10);
xmwmx 1:d89a943c462e 19 DigitalOut IN4(PB_11);
xmwmx 1:d89a943c462e 20 //PwmOut ENA(PB_1);
xmwmx 1:d89a943c462e 21 //PwmOut ENB(PB_0);
xmwmx 1:d89a943c462e 22 //InterruptIn switch1(PB_6);
xmwmx 1:d89a943c462e 23 int state = 0;
xmwmx 2:4a9ba34cd90a 24 int x[256];
xmwmx 2:4a9ba34cd90a 25 int y=0;
xmwmx 2:4a9ba34cd90a 26 bool ready,received,start,done;
xmwmx 2:4a9ba34cd90a 27
xmwmx 1:d89a943c462e 28 void run_forwoard();
xmwmx 1:d89a943c462e 29 void run_backwoard();
xmwmx 2:4a9ba34cd90a 30 void turn_left();
xmwmx 2:4a9ba34cd90a 31 void turn_right();
xmwmx 1:d89a943c462e 32 void stop();
xmwmx 1:d89a943c462e 33 void init();
xmwmx 1:d89a943c462e 34 void after_irq();
xmwmx 2:4a9ba34cd90a 35
xmwmx 1:d89a943c462e 36 int main(void) {
xmwmx 1:d89a943c462e 37 for(int i =0;i<4;i++)
xmwmx 1:d89a943c462e 38 {
xmwmx 1:d89a943c462e 39 LED=1;
xmwmx 1:d89a943c462e 40 wait(1.0);
xmwmx 1:d89a943c462e 41 LED=0;
xmwmx 1:d89a943c462e 42 wait(1.0);
xmwmx 1:d89a943c462e 43 }
xmwmx 1:d89a943c462e 44 IN1 = 0;
xmwmx 1:d89a943c462e 45 IN2 = 0;
xmwmx 1:d89a943c462e 46 IN3 = 0;
xmwmx 1:d89a943c462e 47 IN4 = 0;
xmwmx 1:d89a943c462e 48 // ENA.period_ms(10);
xmwmx 1:d89a943c462e 49 // ENB.period_ms(10);
xmwmx 1:d89a943c462e 50 // ENA.pulsewidth_ms(5);
xmwmx 1:d89a943c462e 51 // ENB.pulsewidth_ms(5);
xmwmx 1:d89a943c462e 52
xmwmx 1:d89a943c462e 53
xmwmx 1:d89a943c462e 54 ser2usb.printf("starting\r\n");
xmwmx 1:d89a943c462e 55
xmwmx 1:d89a943c462e 56 // 选定与 esp8266 相连接的串口,WiFi 名称和密码
xmwmx 1:d89a943c462e 57 Esp8266 client(PA_9, PA_10, "iot_b827eb8fb527", "7c02b50b");// 参数分别为 TX pin / RX pin / SSID / Password//"iot_b827eb8fb527", "7c02b50b"
xmwmx 1:d89a943c462e 58
xmwmx 1:d89a943c462e 59 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
xmwmx 1:d89a943c462e 60 const char* sensors[][2] = {
xmwmx 2:4a9ba34cd90a 61 "report", "",
xmwmx 1:d89a943c462e 62 NULL, NULL //最后一行以空指针作为结束标记
xmwmx 1:d89a943c462e 63 };
xmwmx 1:d89a943c462e 64
xmwmx 1:d89a943c462e 65 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
xmwmx 1:d89a943c462e 66 const char* actuators[][2] = {
xmwmx 2:4a9ba34cd90a 67 "command", "int",
xmwmx 2:4a9ba34cd90a 68 "dat", "int",
xmwmx 1:d89a943c462e 69 NULL, NULL //最后一行以空指针作为结束标记
xmwmx 1:d89a943c462e 70 };
xmwmx 1:d89a943c462e 71 ser2usb.printf("connecting...\r\n");
xmwmx 1:d89a943c462e 72
xmwmx 1:d89a943c462e 73 //连接到服务器
xmwmx 1:d89a943c462e 74 client.connect_mqtt_broker("192.168.12.1", "car", sensors, actuators);
xmwmx 1:d89a943c462e 75
xmwmx 1:d89a943c462e 76 ser2usb.printf("Initialization done.\r\n");
xmwmx 1:d89a943c462e 77 char actuator_name[32], control_value[32];
xmwmx 1:d89a943c462e 78 Timer t;// 定时器用于计量发送传感器数据的时间
xmwmx 1:d89a943c462e 79 t.start();
xmwmx 2:4a9ba34cd90a 80 //client.publish_value("sudo", "carinit");
xmwmx 2:4a9ba34cd90a 81 while(1)
xmwmx 2:4a9ba34cd90a 82 {
xmwmx 1:d89a943c462e 83 //检查有没有收到新的执行器控制指令
xmwmx 2:4a9ba34cd90a 84 if(client.get_control_cmd(actuator_name, control_value))
xmwmx 2:4a9ba34cd90a 85 {
xmwmx 2:4a9ba34cd90a 86 ser2usb.printf("Received %s %s\r\n", actuator_name, control_value);
xmwmx 2:4a9ba34cd90a 87 if (strcmp(actuator_name, "command") == 0)
xmwmx 2:4a9ba34cd90a 88 {
xmwmx 2:4a9ba34cd90a 89 ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value);
xmwmx 2:4a9ba34cd90a 90 int the_command = atoi(control_value);
xmwmx 2:4a9ba34cd90a 91 //
xmwmx 2:4a9ba34cd90a 92 if(the_command==1)
xmwmx 2:4a9ba34cd90a 93 {
xmwmx 2:4a9ba34cd90a 94 ready=1;
xmwmx 2:4a9ba34cd90a 95 ser2usb.printf("get ready\r\n");
xmwmx 2:4a9ba34cd90a 96 }
xmwmx 2:4a9ba34cd90a 97 else if(the_command==2)
xmwmx 2:4a9ba34cd90a 98 {
xmwmx 2:4a9ba34cd90a 99 start=1;
xmwmx 2:4a9ba34cd90a 100 ser2usb.printf("get start\r\n");
xmwmx 2:4a9ba34cd90a 101 }
xmwmx 2:4a9ba34cd90a 102 else if(the_command==3)
xmwmx 2:4a9ba34cd90a 103 {
xmwmx 2:4a9ba34cd90a 104 done=1;
xmwmx 2:4a9ba34cd90a 105 ser2usb.printf("get done\r\n");
xmwmx 2:4a9ba34cd90a 106 }
xmwmx 2:4a9ba34cd90a 107 //
xmwmx 2:4a9ba34cd90a 108
xmwmx 2:4a9ba34cd90a 109 }
xmwmx 2:4a9ba34cd90a 110 else if (strcmp(actuator_name, "dat") == 0)
xmwmx 2:4a9ba34cd90a 111 {
xmwmx 2:4a9ba34cd90a 112 int the_command = atoi(control_value);
xmwmx 2:4a9ba34cd90a 113 received=1;
xmwmx 2:4a9ba34cd90a 114 ser2usb.printf("received \r\n");
xmwmx 2:4a9ba34cd90a 115 x[y]=the_command;
xmwmx 2:4a9ba34cd90a 116 y++;
xmwmx 2:4a9ba34cd90a 117 ser2usb.printf("get %s|%d\r\n",control_value,y);
xmwmx 2:4a9ba34cd90a 118 client.publish_value("report", "received.");
xmwmx 2:4a9ba34cd90a 119 client.publish_value("report", " ");
xmwmx 2:4a9ba34cd90a 120 /*
xmwmx 2:4a9ba34cd90a 121 else
xmwmx 2:4a9ba34cd90a 122 {
xmwmx 2:4a9ba34cd90a 123 //received=1;
xmwmx 2:4a9ba34cd90a 124 //client.publish_value("report", "received.");
xmwmx 2:4a9ba34cd90a 125 x[y]=the_command;
xmwmx 2:4a9ba34cd90a 126 y++;
xmwmx 2:4a9ba34cd90a 127 ser2usb.printf("get %s|%d\r\n",control_value,y);
xmwmx 2:4a9ba34cd90a 128 }
xmwmx 2:4a9ba34cd90a 129 */
xmwmx 2:4a9ba34cd90a 130 }
xmwmx 2:4a9ba34cd90a 131 }
xmwmx 2:4a9ba34cd90a 132 if(!ready)
xmwmx 2:4a9ba34cd90a 133 {
xmwmx 2:4a9ba34cd90a 134 client.publish_value( "report", "ready.");
xmwmx 2:4a9ba34cd90a 135 //ser2usb.printf("send ready.%d\r\n",flagw);
xmwmx 2:4a9ba34cd90a 136 ser2usb.printf("ready?\r\n");
xmwmx 2:4a9ba34cd90a 137
xmwmx 2:4a9ba34cd90a 138 }
xmwmx 2:4a9ba34cd90a 139 if(!start&&ready&&!received)
xmwmx 2:4a9ba34cd90a 140 {
xmwmx 2:4a9ba34cd90a 141 ser2usb.printf("send start.\r\n");
xmwmx 2:4a9ba34cd90a 142 client.publish_value("report", "wait data.");
xmwmx 2:4a9ba34cd90a 143 }
xmwmx 2:4a9ba34cd90a 144 if(start)
xmwmx 2:4a9ba34cd90a 145 {
xmwmx 2:4a9ba34cd90a 146 ser2usb.printf("starting\r\n");
xmwmx 2:4a9ba34cd90a 147 for(int i=0;x[i];i++)
xmwmx 2:4a9ba34cd90a 148 {
xmwmx 2:4a9ba34cd90a 149 state = x[i];
xmwmx 2:4a9ba34cd90a 150 //state = y;
xmwmx 2:4a9ba34cd90a 151 ser2usb.printf("starting %d\r\n",state);
xmwmx 2:4a9ba34cd90a 152 switch(state)
xmwmx 2:4a9ba34cd90a 153 {
xmwmx 2:4a9ba34cd90a 154 case 1: // white to black
xmwmx 2:4a9ba34cd90a 155 run_forwoard();
xmwmx 2:4a9ba34cd90a 156 wait(1);
xmwmx 2:4a9ba34cd90a 157 stop();
xmwmx 2:4a9ba34cd90a 158 //client.publish_value("sudo", "carfinish");
xmwmx 2:4a9ba34cd90a 159 break;
xmwmx 2:4a9ba34cd90a 160
xmwmx 2:4a9ba34cd90a 161 case 2:
xmwmx 2:4a9ba34cd90a 162 run_backwoard();
xmwmx 2:4a9ba34cd90a 163 wait(1);
xmwmx 2:4a9ba34cd90a 164 stop();
xmwmx 2:4a9ba34cd90a 165 //client.publish_value("sudo", "carfinish");
xmwmx 2:4a9ba34cd90a 166 break;
xmwmx 2:4a9ba34cd90a 167
xmwmx 2:4a9ba34cd90a 168 case 3:
xmwmx 2:4a9ba34cd90a 169 turn_left();
xmwmx 2:4a9ba34cd90a 170 wait(1);
xmwmx 2:4a9ba34cd90a 171 stop();
xmwmx 2:4a9ba34cd90a 172 //client.publish_value("sudo", "carfinish");
xmwmx 2:4a9ba34cd90a 173 break;
xmwmx 2:4a9ba34cd90a 174
xmwmx 2:4a9ba34cd90a 175 case 4:
xmwmx 2:4a9ba34cd90a 176 turn_right();
xmwmx 2:4a9ba34cd90a 177 wait(1);
xmwmx 2:4a9ba34cd90a 178 stop();
xmwmx 2:4a9ba34cd90a 179 //client.publish_value("sudo", "carfinish");
xmwmx 2:4a9ba34cd90a 180 break;
xmwmx 2:4a9ba34cd90a 181
xmwmx 2:4a9ba34cd90a 182 default:
xmwmx 2:4a9ba34cd90a 183 break;
xmwmx 2:4a9ba34cd90a 184 }
xmwmx 2:4a9ba34cd90a 185 }
xmwmx 2:4a9ba34cd90a 186 client.publish_value("report", "done.");
xmwmx 2:4a9ba34cd90a 187 memset(x,0,sizeof(x));
xmwmx 2:4a9ba34cd90a 188 y=0;
xmwmx 2:4a9ba34cd90a 189 ready=0;
xmwmx 2:4a9ba34cd90a 190 received=0;
xmwmx 2:4a9ba34cd90a 191 start=0;
xmwmx 2:4a9ba34cd90a 192 done=0;
xmwmx 2:4a9ba34cd90a 193 }
xmwmx 2:4a9ba34cd90a 194 /*
xmwmx 1:d89a943c462e 195 ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value);
xmwmx 1:d89a943c462e 196 //判断哪个执行器收到命令
xmwmx 1:d89a943c462e 197 state = atoi(control_value);
xmwmx 1:d89a943c462e 198 switch (state)
xmwmx 1:d89a943c462e 199 {
xmwmx 1:d89a943c462e 200 case 1: // white to black
xmwmx 1:d89a943c462e 201 run_forwoard();
xmwmx 1:d89a943c462e 202 wait(1);
xmwmx 1:d89a943c462e 203 stop();
xmwmx 1:d89a943c462e 204 client.publish_value("sudo", "carfinish");
xmwmx 1:d89a943c462e 205 break;
xmwmx 1:d89a943c462e 206
xmwmx 1:d89a943c462e 207 case 2:
xmwmx 1:d89a943c462e 208 run_backwoard();
xmwmx 1:d89a943c462e 209 wait(1);
xmwmx 1:d89a943c462e 210 stop();
xmwmx 1:d89a943c462e 211 client.publish_value("sudo", "carfinish");
xmwmx 1:d89a943c462e 212 break;
xmwmx 1:d89a943c462e 213
xmwmx 1:d89a943c462e 214 default:
xmwmx 1:d89a943c462e 215 break;
xmwmx 1:d89a943c462e 216
xmwmx 1:d89a943c462e 217 }
xmwmx 2:4a9ba34cd90a 218 */
xmwmx 2:4a9ba34cd90a 219 wait(0.1);
xmwmx 2:4a9ba34cd90a 220
xmwmx 1:d89a943c462e 221 }
xmwmx 1:d89a943c462e 222 }
xmwmx 1:d89a943c462e 223
xmwmx 1:d89a943c462e 224
xmwmx 1:d89a943c462e 225 void run_forwoard()
xmwmx 1:d89a943c462e 226 { IN1 = 1;
xmwmx 1:d89a943c462e 227 IN2 = 0;
xmwmx 1:d89a943c462e 228 IN3 = 1;
xmwmx 1:d89a943c462e 229 IN4 = 0;
xmwmx 1:d89a943c462e 230 }
xmwmx 1:d89a943c462e 231
xmwmx 1:d89a943c462e 232 void run_backwoard()
xmwmx 1:d89a943c462e 233 {
xmwmx 1:d89a943c462e 234 IN1 = 0;
xmwmx 1:d89a943c462e 235 IN2 = 1;
xmwmx 1:d89a943c462e 236 IN3 = 0;
xmwmx 1:d89a943c462e 237 IN4 = 1;
xmwmx 1:d89a943c462e 238 }
xmwmx 1:d89a943c462e 239
xmwmx 2:4a9ba34cd90a 240 void turn_left()
xmwmx 2:4a9ba34cd90a 241 {
xmwmx 2:4a9ba34cd90a 242 IN1 = 0;
xmwmx 2:4a9ba34cd90a 243 IN2 = 1;
xmwmx 2:4a9ba34cd90a 244 IN3 = 1;
xmwmx 2:4a9ba34cd90a 245 IN4 = 0;
xmwmx 2:4a9ba34cd90a 246 }
xmwmx 2:4a9ba34cd90a 247 void turn_right()
xmwmx 2:4a9ba34cd90a 248 {
xmwmx 2:4a9ba34cd90a 249 IN1 = 1;
xmwmx 2:4a9ba34cd90a 250 IN2 = 0;
xmwmx 2:4a9ba34cd90a 251 IN3 = 0;
xmwmx 2:4a9ba34cd90a 252 IN4 = 1;
xmwmx 2:4a9ba34cd90a 253 }
xmwmx 2:4a9ba34cd90a 254
xmwmx 1:d89a943c462e 255 void stop()
xmwmx 1:d89a943c462e 256 {
xmwmx 1:d89a943c462e 257 IN1 = 0;
xmwmx 1:d89a943c462e 258 IN2 = 0;
xmwmx 1:d89a943c462e 259 IN3 = 0;
xmwmx 1:d89a943c462e 260 IN4 = 0;
xmwmx 1:d89a943c462e 261 }