示教机械臂

Dependencies:   MQTT SDFileSystem WIZnet_Library mbed

Fork of wmx_laser_copy by w mx

Committer:
xmwmx
Date:
Tue Jul 31 14:18:04 2018 +0000
Revision:
11:568789ce1397
Parent:
10:acc53f50c9df
Child:
12:51be919e2c78
????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xmwmx 8:e76bebe9c45f 1 /************************************************************************************************
xmwmx 8:e76bebe9c45f 2 File name: main.cpp
xmwmx 8:e76bebe9c45f 3 Description: 项目激光部分客户端代码
xmwmx 8:e76bebe9c45f 4 Author: 秦智
xmwmx 8:e76bebe9c45f 5 Date: June 4, 2018
xmwmx 8:e76bebe9c45f 6 Others: 本程序编写在mbed平台(https://os.mbed.com/)上
xmwmx 8:e76bebe9c45f 7
xmwmx 8:e76bebe9c45f 8 *************************************************************************************************/
xmwmx 8:e76bebe9c45f 9
xmwmx 8:e76bebe9c45f 10 #include "mbed.h"
xmwmx 8:e76bebe9c45f 11 #include <math.h>
xmwmx 8:e76bebe9c45f 12 #include <cstring>
xmwmx 8:e76bebe9c45f 13 #include <stdlib.h>
xmwmx 8:e76bebe9c45f 14 #include "SDFileSystem.h"
xmwmx 8:e76bebe9c45f 15 #include "networking.h"
xmwmx 9:a3f9faa9ca1f 16
xmwmx 8:e76bebe9c45f 17
xmwmx 8:e76bebe9c45f 18
xmwmx 10:acc53f50c9df 19 typedef enum {
xmwmx 10:acc53f50c9df 20 PointerS,PointerW,PointerX,PointerY,PointerZ,PointerA,PointerL,PointerWW,PointerP
xmwmx 10:acc53f50c9df 21 } STATE; //定义结构体枚举类型STATE
xmwmx 8:e76bebe9c45f 22
xmwmx 8:e76bebe9c45f 23
xmwmx 10:acc53f50c9df 24 #define run 32000
xmwmx 10:acc53f50c9df 25 #define rad 360
xmwmx 10:acc53f50c9df 26 #define turn 1
xmwmx 10:acc53f50c9df 27
xmwmx 8:e76bebe9c45f 28 /////调试
xmwmx 8:e76bebe9c45f 29 Serial pc(PA_9, PA_10);
xmwmx 10:acc53f50c9df 30 Serial bt(PB_10, PB_11);
xmwmx 8:e76bebe9c45f 31 DigitalOut LED(PB_8);
xmwmx 8:e76bebe9c45f 32 /////
xmwmx 8:e76bebe9c45f 33
xmwmx 8:e76bebe9c45f 34 //硬件接口
xmwmx 8:e76bebe9c45f 35 // mosi, miso, sclk, cs, name
xmwmx 8:e76bebe9c45f 36 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd");
xmwmx 8:e76bebe9c45f 37 //步进电机
xmwmx 10:acc53f50c9df 38 DigitalOut step[3] = {DigitalOut(PC_5), DigitalOut(PA_5),DigitalOut(PC_3)}; //0--x P9,1--y P17
xmwmx 10:acc53f50c9df 39 DigitalOut dir[3] = {DigitalOut(PC_4), DigitalOut(PA_4),DigitalOut(PC_2)}; //0--x,1--y
xmwmx 10:acc53f50c9df 40 DigitalOut en[3] = {DigitalOut(PD_2), DigitalOut(PA_2),DigitalOut(PC_13)}; //0--x,1--y
xmwmx 8:e76bebe9c45f 41 //电子开关,激光开关
xmwmx 8:e76bebe9c45f 42 DigitalOut switch_GS(PC_15);
xmwmx 8:e76bebe9c45f 43 //行程开关
xmwmx 10:acc53f50c9df 44 DigitalIn switch_pos1(PC_8); //P25
xmwmx 8:e76bebe9c45f 45 DigitalIn switch_pos2(PA_6); //P26
xmwmx 10:acc53f50c9df 46 DigitalIn switch_pos3(PC_6); //P27
xmwmx 8:e76bebe9c45f 47
xmwmx 8:e76bebe9c45f 48 //运行中的全局变量
xmwmx 8:e76bebe9c45f 49 volatile bool Working, Getawork, Isend, Dataused, Getdata;
xmwmx 10:acc53f50c9df 50 //FILE *fp_drawing;// 存储图案的文件
xmwmx 8:e76bebe9c45f 51 int status = 0; //0: 初始化; 1:建立通信; 2:等候任务; 3:等待数据 4:正在执行一个任务
xmwmx 10:acc53f50c9df 52 int now_x, now_y,now_z;
xmwmx 8:e76bebe9c45f 53 int Endoffile = 0;
xmwmx 8:e76bebe9c45f 54
xmwmx 8:e76bebe9c45f 55 // float thedata[50][3];
xmwmx 8:e76bebe9c45f 56 // char sdata[1024];
xmwmx 8:e76bebe9c45f 57 int Nofdata;
xmwmx 8:e76bebe9c45f 58 bool ifreceive;
xmwmx 8:e76bebe9c45f 59
xmwmx 8:e76bebe9c45f 60 //与实际有关参数
xmwmx 8:e76bebe9c45f 61 float Lenoflattice = 1 ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100
xmwmx 9:a3f9faa9ca1f 62 int unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
xmwmx 8:e76bebe9c45f 63 int unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
xmwmx 8:e76bebe9c45f 64 int unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
xmwmx 8:e76bebe9c45f 65 ///Ticker ticker_step;
xmwmx 8:e76bebe9c45f 66 float step_halfperiod = 0.0001;//0.0002;
xmwmx 8:e76bebe9c45f 67 int max_x, max_y;
xmwmx 8:e76bebe9c45f 68 int dir_x = 1;///调试时调整
xmwmx 8:e76bebe9c45f 69 int dir_y = 1;///调试时调整
xmwmx 10:acc53f50c9df 70 int dir_z = 1;///调试时调整
xmwmx 10:acc53f50c9df 71
xmwmx 11:568789ce1397 72 bool ready = 0;
xmwmx 11:568789ce1397 73 bool start = 0;
xmwmx 11:568789ce1397 74 bool done =0;
xmwmx 11:568789ce1397 75
xmwmx 10:acc53f50c9df 76 char buf[256];
xmwmx 10:acc53f50c9df 77 int cur;
xmwmx 10:acc53f50c9df 78 bool received;
xmwmx 10:acc53f50c9df 79 bool flagx, flagy , flagz , flaga , flagw , flagp;
xmwmx 10:acc53f50c9df 80 float X1 , X2 , Y1 , Y2 , Z , A; // 定义浮点数值用以储存返回给机械臂的执行步数
xmwmx 8:e76bebe9c45f 81
xmwmx 8:e76bebe9c45f 82 void rotate(int id ,int pix);
xmwmx 8:e76bebe9c45f 83
xmwmx 8:e76bebe9c45f 84
xmwmx 8:e76bebe9c45f 85
xmwmx 10:acc53f50c9df 86 void beginning()
xmwmx 8:e76bebe9c45f 87 {
xmwmx 10:acc53f50c9df 88 // x
xmwmx 10:acc53f50c9df 89 // ##
xmwmx 10:acc53f50c9df 90 pc.printf("y\n"); // ||
xmwmx 10:acc53f50c9df 91 while(switch_pos2.read()) // |====||=====|||#y
xmwmx 10:acc53f50c9df 92 { // ||
xmwmx 10:acc53f50c9df 93 rotate(1,-5); // ##
xmwmx 10:acc53f50c9df 94 } // z
xmwmx 10:acc53f50c9df 95 //
xmwmx 10:acc53f50c9df 96 pc.printf("z\n"); //
xmwmx 10:acc53f50c9df 97 while(switch_pos3.read()) //
xmwmx 10:acc53f50c9df 98 { //
xmwmx 10:acc53f50c9df 99 rotate(2,-5); //
xmwmx 8:e76bebe9c45f 100 } //
xmwmx 10:acc53f50c9df 101 //
xmwmx 10:acc53f50c9df 102 pc.printf("x\n"); //
xmwmx 10:acc53f50c9df 103 while(switch_pos1.read()) //
xmwmx 10:acc53f50c9df 104 { //
xmwmx 10:acc53f50c9df 105 rotate(0,-5); //
xmwmx 10:acc53f50c9df 106 }
xmwmx 10:acc53f50c9df 107
xmwmx 8:e76bebe9c45f 108 }
xmwmx 8:e76bebe9c45f 109
xmwmx 10:acc53f50c9df 110 void init_zero()
xmwmx 8:e76bebe9c45f 111 {
xmwmx 8:e76bebe9c45f 112 now_x = 0;
xmwmx 8:e76bebe9c45f 113 now_y = 0;
xmwmx 10:acc53f50c9df 114 now_z = 0;
xmwmx 10:acc53f50c9df 115 strcpy(buf,"");
xmwmx 10:acc53f50c9df 116 // step[0] = 0;
xmwmx 10:acc53f50c9df 117 // step[1] = 0;
xmwmx 10:acc53f50c9df 118 // step[2] = 0;
xmwmx 8:e76bebe9c45f 119
xmwmx 8:e76bebe9c45f 120 }
xmwmx 8:e76bebe9c45f 121 void rotate(int id, int pix) //id= 0--x,1--y pix=3200为一圈
xmwmx 8:e76bebe9c45f 122 {
xmwmx 8:e76bebe9c45f 123 if (pix >= 0) {
xmwmx 8:e76bebe9c45f 124 dir[0] = dir_x;
xmwmx 8:e76bebe9c45f 125 dir[1] = dir_y;
xmwmx 10:acc53f50c9df 126 dir[2] = dir_z;
xmwmx 8:e76bebe9c45f 127 } else {
xmwmx 8:e76bebe9c45f 128 pix = -pix;
xmwmx 8:e76bebe9c45f 129 dir[0] = 1 - dir_x;
xmwmx 8:e76bebe9c45f 130 dir[1] = 1 - dir_y;
xmwmx 10:acc53f50c9df 131 dir[2] = 1 - dir_z;
xmwmx 8:e76bebe9c45f 132 }
xmwmx 8:e76bebe9c45f 133 for (int i = 0; i < pix; i++) {
xmwmx 8:e76bebe9c45f 134 step[id] = 1;
xmwmx 8:e76bebe9c45f 135 wait(step_halfperiod);
xmwmx 8:e76bebe9c45f 136 step[id] = 0;
xmwmx 8:e76bebe9c45f 137 wait(step_halfperiod);
xmwmx 8:e76bebe9c45f 138 }
xmwmx 8:e76bebe9c45f 139 }
xmwmx 8:e76bebe9c45f 140
xmwmx 10:acc53f50c9df 141 void moveto(float x, float y,float z)
xmwmx 8:e76bebe9c45f 142 {
xmwmx 10:acc53f50c9df 143 rotate(0, (x - now_x)*run/rad);
xmwmx 10:acc53f50c9df 144 rotate(1, (y - now_y)*run/rad);
xmwmx 10:acc53f50c9df 145 rotate(2, (z - now_z)*run/rad);
xmwmx 8:e76bebe9c45f 146 now_x = x;
xmwmx 8:e76bebe9c45f 147 now_y = y;
xmwmx 10:acc53f50c9df 148 now_z = z;
xmwmx 8:e76bebe9c45f 149 }
xmwmx 10:acc53f50c9df 150
xmwmx 10:acc53f50c9df 151
xmwmx 8:e76bebe9c45f 152
xmwmx 8:e76bebe9c45f 153 void draw()
xmwmx 8:e76bebe9c45f 154 {
xmwmx 10:acc53f50c9df 155 //unit_xy=unit_xy*k;
xmwmx 10:acc53f50c9df 156 float x, y, z,a;
xmwmx 10:acc53f50c9df 157 FILE *fp_drawing = fopen("/sd/write.txt", "r");
xmwmx 8:e76bebe9c45f 158 pc.printf("reading\r\n");
xmwmx 10:acc53f50c9df 159 flagp=0;
xmwmx 10:acc53f50c9df 160 for (int i = 0; fscanf(fp_drawing, "(%f %f %f %f)", &x, &y, &z,&a) == 4&&flagp==0; i++)
xmwmx 8:e76bebe9c45f 161 {
xmwmx 10:acc53f50c9df 162 pc.printf("(%f,%f,%f)|%d\r\n", x, y, z,i);
xmwmx 10:acc53f50c9df 163 moveto(x, y,z);
xmwmx 8:e76bebe9c45f 164 // moveto(thedata[i][0],thedata[i][1]);
xmwmx 8:e76bebe9c45f 165 // markd??>ot(thedata[i][2]);
xmwmx 8:e76bebe9c45f 166 }
xmwmx 8:e76bebe9c45f 167 fclose(fp_drawing);
xmwmx 10:acc53f50c9df 168 flagw = false;
xmwmx 10:acc53f50c9df 169 flagp = false;
xmwmx 10:acc53f50c9df 170 pc.printf("Here4!\r\n");
xmwmx 8:e76bebe9c45f 171 }
xmwmx 8:e76bebe9c45f 172
xmwmx 10:acc53f50c9df 173 void btInterrupt() //中断接收函数
xmwmx 8:e76bebe9c45f 174 {
xmwmx 10:acc53f50c9df 175 if(!bt.readable())
xmwmx 10:acc53f50c9df 176 return;
xmwmx 10:acc53f50c9df 177 char strRe1; //定义字符串
xmwmx 10:acc53f50c9df 178 static STATE State = PointerS; // 初始状态待检查
xmwmx 10:acc53f50c9df 179 strRe1 = bt.getc(); //读取一个byte数据
xmwmx 10:acc53f50c9df 180
xmwmx 10:acc53f50c9df 181 switch(State)
xmwmx 10:acc53f50c9df 182 {
xmwmx 10:acc53f50c9df 183 case PointerS:
xmwmx 10:acc53f50c9df 184 if (strRe1 == (char)'S')
xmwmx 10:acc53f50c9df 185 {
xmwmx 10:acc53f50c9df 186 State = PointerW; //准备跳转至数据选择
xmwmx 10:acc53f50c9df 187 cur = 0;
xmwmx 10:acc53f50c9df 188 }
xmwmx 10:acc53f50c9df 189 else
xmwmx 10:acc53f50c9df 190 State = PointerS;
xmwmx 10:acc53f50c9df 191 break;
xmwmx 10:acc53f50c9df 192 case PointerW:
xmwmx 10:acc53f50c9df 193 switch (strRe1)
xmwmx 8:e76bebe9c45f 194 {
xmwmx 10:acc53f50c9df 195 case (char)'X':
xmwmx 10:acc53f50c9df 196 State = PointerX;
xmwmx 10:acc53f50c9df 197 break;
xmwmx 10:acc53f50c9df 198 case (char)'Y':
xmwmx 10:acc53f50c9df 199 State = PointerY;
xmwmx 10:acc53f50c9df 200 break;
xmwmx 10:acc53f50c9df 201 case (char)'Z':
xmwmx 10:acc53f50c9df 202 State = PointerZ;
xmwmx 10:acc53f50c9df 203 break;
xmwmx 10:acc53f50c9df 204 case (char)'S':
xmwmx 10:acc53f50c9df 205 State = PointerS;
xmwmx 10:acc53f50c9df 206 break;
xmwmx 10:acc53f50c9df 207 case (char)'L':
xmwmx 10:acc53f50c9df 208 State = PointerL;
xmwmx 10:acc53f50c9df 209 break;
xmwmx 10:acc53f50c9df 210 case (char)'A':
xmwmx 10:acc53f50c9df 211 State = PointerA;
xmwmx 10:acc53f50c9df 212 break;
xmwmx 10:acc53f50c9df 213 case (char)'W':
xmwmx 10:acc53f50c9df 214 State = PointerWW;
xmwmx 10:acc53f50c9df 215 break;
xmwmx 10:acc53f50c9df 216 case (char)'P':
xmwmx 10:acc53f50c9df 217 State = PointerP;
xmwmx 10:acc53f50c9df 218 break;
xmwmx 10:acc53f50c9df 219 case (char)'K':
xmwmx 10:acc53f50c9df 220 State = PointerS;
xmwmx 10:acc53f50c9df 221 pc.printf("beginning\r\n");
xmwmx 10:acc53f50c9df 222 beginning();
xmwmx 10:acc53f50c9df 223 break;
xmwmx 10:acc53f50c9df 224
xmwmx 8:e76bebe9c45f 225 }
xmwmx 10:acc53f50c9df 226 //case PointerX:
xmwmx 10:acc53f50c9df 227 // if (strRe1 == (char)'\n') {
xmwmx 10:acc53f50c9df 228 // buf[cur]='\0';
xmwmx 10:acc53f50c9df 229 // pc.printf("%s\n",buf);
xmwmx 10:acc53f50c9df 230 // X = atof(buf+1);
xmwmx 10:acc53f50c9df 231 // State = PointerS;
xmwmx 10:acc53f50c9df 232 // pc.printf("%f\n",X);
xmwmx 10:acc53f50c9df 233 // } else {
xmwmx 10:acc53f50c9df 234 // buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 235 // }
xmwmx 10:acc53f50c9df 236 // break;
xmwmx 10:acc53f50c9df 237 // case PointerY:
xmwmx 10:acc53f50c9df 238 // if (strRe1 == (char)'\n') {
xmwmx 10:acc53f50c9df 239 // buf[cur]='\0';
xmwmx 10:acc53f50c9df 240 // pc.printf("%s\n",buf);
xmwmx 10:acc53f50c9df 241 // Y = atof(buf+1);
xmwmx 10:acc53f50c9df 242 // State = PointerS;
xmwmx 10:acc53f50c9df 243 // pc.printf("%f\n",Y);
xmwmx 10:acc53f50c9df 244 // } else {
xmwmx 10:acc53f50c9df 245 // buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 246 // }
xmwmx 10:acc53f50c9df 247 // break;
xmwmx 10:acc53f50c9df 248 // case PointerZ:
xmwmx 10:acc53f50c9df 249 // if (strRe1 == (char)'\n') {
xmwmx 10:acc53f50c9df 250 // buf[cur]='\0';
xmwmx 10:acc53f50c9df 251 // pc.printf("%s\n",buf);
xmwmx 10:acc53f50c9df 252 // Z = atof(buf+1);
xmwmx 10:acc53f50c9df 253 // State = PointerS;
xmwmx 10:acc53f50c9df 254 // pc.printf("%f\n",Z);
xmwmx 10:acc53f50c9df 255 // } else {
xmwmx 10:acc53f50c9df 256 // buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 257 // }
xmwmx 10:acc53f50c9df 258 // break;
xmwmx 10:acc53f50c9df 259 case PointerX:
xmwmx 10:acc53f50c9df 260 if (strRe1 == (char)'\n')
xmwmx 10:acc53f50c9df 261 {
xmwmx 10:acc53f50c9df 262 buf[cur]='\0';
xmwmx 10:acc53f50c9df 263 pc.printf("%s\r\n",buf);
xmwmx 10:acc53f50c9df 264 X1 = atof(buf+1);
xmwmx 10:acc53f50c9df 265 State = PointerS;
xmwmx 10:acc53f50c9df 266 pc.printf("%f\r\n",X1); //32000bu = 360du
xmwmx 10:acc53f50c9df 267 flagx = true;
xmwmx 10:acc53f50c9df 268 }
xmwmx 10:acc53f50c9df 269 else
xmwmx 10:acc53f50c9df 270 {
xmwmx 10:acc53f50c9df 271 buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 272 }
xmwmx 10:acc53f50c9df 273 break;
xmwmx 10:acc53f50c9df 274 case PointerY:
xmwmx 10:acc53f50c9df 275 if (strRe1 == (char)'\n')
xmwmx 10:acc53f50c9df 276 {
xmwmx 10:acc53f50c9df 277 buf[cur]='\0';
xmwmx 10:acc53f50c9df 278 pc.printf("%s\r\n",buf);
xmwmx 10:acc53f50c9df 279 Y1 = atof(buf+1);
xmwmx 10:acc53f50c9df 280 //Y2 = Y1;
xmwmx 10:acc53f50c9df 281 // Y1 = Y2 - Y1;
xmwmx 10:acc53f50c9df 282 State = PointerS;
xmwmx 10:acc53f50c9df 283 pc.printf("%f\r\n",Y1);
xmwmx 10:acc53f50c9df 284 flagy = true;
xmwmx 10:acc53f50c9df 285 }
xmwmx 10:acc53f50c9df 286 else
xmwmx 10:acc53f50c9df 287 {
xmwmx 10:acc53f50c9df 288 buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 289 }
xmwmx 10:acc53f50c9df 290 break;
xmwmx 10:acc53f50c9df 291 case PointerZ:
xmwmx 10:acc53f50c9df 292 if (strRe1 == (char)'\n')
xmwmx 10:acc53f50c9df 293 {
xmwmx 10:acc53f50c9df 294 buf[cur]='\0';
xmwmx 10:acc53f50c9df 295 pc.printf("%s\r\n",buf);
xmwmx 10:acc53f50c9df 296 Z = atof(buf+1);
xmwmx 10:acc53f50c9df 297 State = PointerS;
xmwmx 10:acc53f50c9df 298 pc.printf("%f\r\n",Z);
xmwmx 10:acc53f50c9df 299 flagz = true;
xmwmx 10:acc53f50c9df 300 }
xmwmx 10:acc53f50c9df 301 else
xmwmx 10:acc53f50c9df 302 {
xmwmx 10:acc53f50c9df 303 buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 304 }
xmwmx 10:acc53f50c9df 305 break;
xmwmx 10:acc53f50c9df 306 case PointerA:
xmwmx 10:acc53f50c9df 307 if (strRe1 == (char)'\n')
xmwmx 10:acc53f50c9df 308 {
xmwmx 10:acc53f50c9df 309 buf[cur]='\0';
xmwmx 10:acc53f50c9df 310 pc.printf("%s\n",buf);
xmwmx 10:acc53f50c9df 311 A = atof(buf+1);
xmwmx 10:acc53f50c9df 312 State = PointerS;
xmwmx 10:acc53f50c9df 313 pc.printf("%f\n",A);
xmwmx 10:acc53f50c9df 314 }
xmwmx 10:acc53f50c9df 315 else
xmwmx 10:acc53f50c9df 316 {
xmwmx 10:acc53f50c9df 317 buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 318 }
xmwmx 10:acc53f50c9df 319 break;
xmwmx 10:acc53f50c9df 320 case PointerL:
xmwmx 10:acc53f50c9df 321 if (strRe1 == (char)'\n')
xmwmx 10:acc53f50c9df 322 {
xmwmx 10:acc53f50c9df 323 buf[cur]='\0';
xmwmx 10:acc53f50c9df 324 State = PointerS;
xmwmx 10:acc53f50c9df 325 received = true;
xmwmx 10:acc53f50c9df 326 pc.printf("received\r\n");
xmwmx 10:acc53f50c9df 327 }
xmwmx 10:acc53f50c9df 328 else
xmwmx 10:acc53f50c9df 329 {
xmwmx 10:acc53f50c9df 330 buf[cur++]=strRe1;
xmwmx 10:acc53f50c9df 331 }
xmwmx 10:acc53f50c9df 332 break;
xmwmx 10:acc53f50c9df 333 case PointerWW:
xmwmx 10:acc53f50c9df 334 flagw = true;
xmwmx 10:acc53f50c9df 335 State = PointerS;
xmwmx 10:acc53f50c9df 336 break;
xmwmx 10:acc53f50c9df 337 case PointerP:
xmwmx 10:acc53f50c9df 338 flagp = true;
xmwmx 10:acc53f50c9df 339 State = PointerS;
xmwmx 10:acc53f50c9df 340 break;
xmwmx 8:e76bebe9c45f 341 }
xmwmx 8:e76bebe9c45f 342 }
xmwmx 8:e76bebe9c45f 343
xmwmx 11:568789ce1397 344 void on_control_cmd(const char* actuator_name, const char* control_value)
xmwmx 11:568789ce1397 345 {
xmwmx 11:568789ce1397 346
xmwmx 11:568789ce1397 347 if (strcmp(actuator_name, "create") == 0)
xmwmx 11:568789ce1397 348 {
xmwmx 11:568789ce1397 349 int the_command = atoi(control_value);
xmwmx 11:568789ce1397 350 //
xmwmx 11:568789ce1397 351 if(the_command==0)
xmwmx 11:568789ce1397 352 {
xmwmx 11:568789ce1397 353 ready=1;
xmwmx 11:568789ce1397 354 pc.printf("get ready\r\n");
xmwmx 11:568789ce1397 355 }
xmwmx 11:568789ce1397 356 else if(the_command==1)
xmwmx 11:568789ce1397 357 {
xmwmx 11:568789ce1397 358 start=1;
xmwmx 11:568789ce1397 359 pc.printf("get start\r\n");
xmwmx 11:568789ce1397 360 }
xmwmx 11:568789ce1397 361 else if(the_command==2)
xmwmx 11:568789ce1397 362 {
xmwmx 11:568789ce1397 363 done=1;
xmwmx 11:568789ce1397 364 pc.printf("get done\r\n");
xmwmx 11:568789ce1397 365 }
xmwmx 11:568789ce1397 366 //
xmwmx 11:568789ce1397 367
xmwmx 11:568789ce1397 368 }
xmwmx 11:568789ce1397 369 }
xmwmx 11:568789ce1397 370
xmwmx 11:568789ce1397 371
xmwmx 8:e76bebe9c45f 372 int main()
xmwmx 8:e76bebe9c45f 373 {
xmwmx 10:acc53f50c9df 374 init_zero(); //初始化
xmwmx 10:acc53f50c9df 375 bt.attach(btInterrupt);
xmwmx 10:acc53f50c9df 376 pc.printf("beginning\r\n");
xmwmx 10:acc53f50c9df 377 beginning();
xmwmx 8:e76bebe9c45f 378 pc.printf("finish\r\n");
xmwmx 11:568789ce1397 379
xmwmx 11:568789ce1397 380 MQTTSocket sock;
xmwmx 11:568789ce1397 381 MClient client(sock);
xmwmx 11:568789ce1397 382 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
xmwmx 11:568789ce1397 383 const char* sensors[][2] = {
xmwmx 11:568789ce1397 384 "report", "",
xmwmx 11:568789ce1397 385 NULL, NULL //最后一行以空指针作为结束标记
xmwmx 11:568789ce1397 386 };
xmwmx 11:568789ce1397 387
xmwmx 11:568789ce1397 388 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
xmwmx 11:568789ce1397 389 const char* actuators[][2] = {
xmwmx 11:568789ce1397 390 "create", "",
xmwmx 11:568789ce1397 391 NULL, NULL //最后一行以空指针作为结束标记
xmwmx 11:568789ce1397 392 };
xmwmx 11:568789ce1397 393 pc.printf("connecting...\r\n");
xmwmx 11:568789ce1397 394
xmwmx 11:568789ce1397 395 networking_init(sock, client, "192.168.1.100", sensors, actuators, on_control_cmd);
xmwmx 11:568789ce1397 396
xmwmx 11:568789ce1397 397 pc.printf("Initialization done.\r\n");
xmwmx 11:568789ce1397 398 pc.printf("laser printer send ready.haha\r\n");
xmwmx 11:568789ce1397 399 ready=0;
xmwmx 11:568789ce1397 400 start=0;
xmwmx 11:568789ce1397 401 done=0;
xmwmx 11:568789ce1397 402
xmwmx 11:568789ce1397 403 while(1)
xmwmx 11:568789ce1397 404 {
xmwmx 11:568789ce1397 405 if(!ready)
xmwmx 11:568789ce1397 406 {
xmwmx 11:568789ce1397 407 publish_value(client, "report", "ready.");
xmwmx 11:568789ce1397 408 pc.printf("send ready.%d\r\n",flagw);
xmwmx 11:568789ce1397 409
xmwmx 11:568789ce1397 410 }
xmwmx 11:568789ce1397 411 if(!start&&ready)
xmwmx 11:568789ce1397 412 {
xmwmx 11:568789ce1397 413 pc.printf("send start.%d\r\n",flagw);
xmwmx 11:568789ce1397 414 publish_value(client, "report", "wait to start.");
xmwmx 11:568789ce1397 415
xmwmx 11:568789ce1397 416 }
xmwmx 10:acc53f50c9df 417 // btInterrupt();
xmwmx 10:acc53f50c9df 418 //wait(0.1);
xmwmx 10:acc53f50c9df 419 //if (sw.read() == 0)
xmwmx 10:acc53f50c9df 420 //{
xmwmx 10:acc53f50c9df 421 // pc.printf("%s.\r\n", strX);
xmwmx 10:acc53f50c9df 422 // pc.printf("%s.\r\n", strY);
xmwmx 10:acc53f50c9df 423 // pc.printf("%s.\r\n", strZ);
xmwmx 10:acc53f50c9df 424 // pc.printf("%s.\r\n", strA);
xmwmx 10:acc53f50c9df 425 //}
xmwmx 10:acc53f50c9df 426
xmwmx 10:acc53f50c9df 427 if (flagx)
xmwmx 9:a3f9faa9ca1f 428 {
xmwmx 10:acc53f50c9df 429 moveto(X1,now_y,now_z);
xmwmx 10:acc53f50c9df 430 flagx = false;
xmwmx 9:a3f9faa9ca1f 431 }
xmwmx 10:acc53f50c9df 432 if (flagy)
xmwmx 10:acc53f50c9df 433 {
xmwmx 10:acc53f50c9df 434 moveto(now_x,Y1,now_z);
xmwmx 10:acc53f50c9df 435 flagy = false;
xmwmx 10:acc53f50c9df 436 }
xmwmx 10:acc53f50c9df 437 if (flagz)
xmwmx 10:acc53f50c9df 438 {
xmwmx 10:acc53f50c9df 439 moveto(now_x,now_y,Z);
xmwmx 10:acc53f50c9df 440 flagz = false;
xmwmx 10:acc53f50c9df 441 }
xmwmx 11:568789ce1397 442 if(flagw||start)
xmwmx 9:a3f9faa9ca1f 443 {
xmwmx 11:568789ce1397 444 pc.printf("openning %d\r\n",flagw);
xmwmx 10:acc53f50c9df 445
xmwmx 10:acc53f50c9df 446 FILE *fp = fopen("/sd/write.txt", "r"); //打开文件,路径以“/sd/”开头
xmwmx 10:acc53f50c9df 447
xmwmx 10:acc53f50c9df 448 if (fp == NULL) //打开失败,原因可能是文件不存在,或卡没有连接好
xmwmx 10:acc53f50c9df 449 {
xmwmx 10:acc53f50c9df 450 printf("open error!!\r\n");
xmwmx 10:acc53f50c9df 451 return 1;
xmwmx 8:e76bebe9c45f 452 }
xmwmx 10:acc53f50c9df 453 fgets(buf, sizeof(buf), fp); //读入一行的C函数
xmwmx 10:acc53f50c9df 454 pc.printf("read\r\n");
xmwmx 10:acc53f50c9df 455 fclose(fp); //关闭文件,释放资源
xmwmx 10:acc53f50c9df 456
xmwmx 10:acc53f50c9df 457 pc.printf("%s\r\n",buf);
xmwmx 10:acc53f50c9df 458 char *p = buf;
xmwmx 10:acc53f50c9df 459 float a,x,y,z;
xmwmx 10:acc53f50c9df 460 while(true)
xmwmx 9:a3f9faa9ca1f 461 {
xmwmx 10:acc53f50c9df 462 p = strchr(p, '(');
xmwmx 10:acc53f50c9df 463 if(!p) break;
xmwmx 10:acc53f50c9df 464 p++;
xmwmx 10:acc53f50c9df 465 sscanf(p, "%f%f%f%f",&x,&y,&z,&a);
xmwmx 10:acc53f50c9df 466 pc.printf("%f %f %f %f\r\n",x,y,z,a);
xmwmx 10:acc53f50c9df 467 moveto(x,y,z);
xmwmx 10:acc53f50c9df 468 wait(0.5);
xmwmx 9:a3f9faa9ca1f 469 }
xmwmx 10:acc53f50c9df 470 flagw = false;
xmwmx 10:acc53f50c9df 471 pc.printf("%d\r\n",flagw);
xmwmx 11:568789ce1397 472 if(!done)
xmwmx 11:568789ce1397 473 {
xmwmx 11:568789ce1397 474 publish_value(client, "report", "done.");
xmwmx 11:568789ce1397 475 pc.printf("send done.%d\r\n",flagw);
xmwmx 11:568789ce1397 476
xmwmx 11:568789ce1397 477 }
xmwmx 11:568789ce1397 478 ready=0;
xmwmx 11:568789ce1397 479 start=0;
xmwmx 11:568789ce1397 480 done=0;
xmwmx 10:acc53f50c9df 481 }
xmwmx 10:acc53f50c9df 482 /*if (flagw)
xmwmx 10:acc53f50c9df 483 {
xmwmx 10:acc53f50c9df 484 //if (Arr[][] !=
xmwmx 10:acc53f50c9df 485 //{
xmwmx 10:acc53f50c9df 486 pc.printf("Here1!\r\n");
xmwmx 10:acc53f50c9df 487
xmwmx 10:acc53f50c9df 488 pc.printf("Here2!\r\n");
xmwmx 10:acc53f50c9df 489 init_zero();
xmwmx 10:acc53f50c9df 490 beginning();
xmwmx 10:acc53f50c9df 491 draw();
xmwmx 10:acc53f50c9df 492 pc.printf("Here3!\r\n");
xmwmx 10:acc53f50c9df 493 //}
xmwmx 10:acc53f50c9df 494 } */
xmwmx 10:acc53f50c9df 495 if(received)
xmwmx 8:e76bebe9c45f 496 {
xmwmx 10:acc53f50c9df 497 pc.printf("%s\r\n",buf);
xmwmx 10:acc53f50c9df 498 FILE *fp_drawing = fopen("/sd/write.txt", "w");
xmwmx 10:acc53f50c9df 499 pc.printf("opening\r\n");
xmwmx 10:acc53f50c9df 500 if (fp_drawing == NULL)
xmwmx 10:acc53f50c9df 501 {
xmwmx 10:acc53f50c9df 502 printf("open error2!!\r\n");
xmwmx 10:acc53f50c9df 503 return 1;
xmwmx 10:acc53f50c9df 504 }
xmwmx 10:acc53f50c9df 505 fprintf(fp_drawing, "%s",buf);
xmwmx 10:acc53f50c9df 506 pc.printf("close\r\n");
xmwmx 10:acc53f50c9df 507 fclose(fp_drawing);
xmwmx 10:acc53f50c9df 508 pc.printf("write end");
xmwmx 10:acc53f50c9df 509 received = false;
xmwmx 8:e76bebe9c45f 510 }
xmwmx 11:568789ce1397 511 client.yield(1000);
xmwmx 11:568789ce1397 512 }
xmwmx 8:e76bebe9c45f 513 }