示教机械臂
Dependencies: MQTT SDFileSystem WIZnet_Library mbed
Fork of wmx_laser_copy by
mainwmx.cpp@9:a3f9faa9ca1f, 2018-07-31 (annotated)
- Committer:
- xmwmx
- Date:
- Tue Jul 31 03:56:05 2018 +0000
- Revision:
- 9:a3f9faa9ca1f
- Parent:
- 8:e76bebe9c45f
- Child:
- 10:acc53f50c9df
??x?y??????????????0?0?1????
Who changed what in which revision?
User | Revision | Line number | New 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 | //#include <string> |
xmwmx | 9:a3f9faa9ca1f | 17 | |
xmwmx | 8:e76bebe9c45f | 18 | |
xmwmx | 8:e76bebe9c45f | 19 | |
xmwmx | 9:a3f9faa9ca1f | 20 | #define edgex1 125 // 上x(正朝向开关)下y(负朝向开关) |
xmwmx | 9:a3f9faa9ca1f | 21 | #define edgex2 155 // __ |
xmwmx | 9:a3f9faa9ca1f | 22 | #define edgey1 70 // || |
xmwmx | 9:a3f9faa9ca1f | 23 | #define edgey2 100 // 125mm y2 |
xmwmx | 9:a3f9faa9ca1f | 24 | #define widthx 30 // x2 || x1 |
xmwmx | 9:a3f9faa9ca1f | 25 | #define widthy 30 // //=====210mm====//35mm/|开关 |
xmwmx | 8:e76bebe9c45f | 26 | #define mm 100//100步1mm // || |
xmwmx | 8:e76bebe9c45f | 27 | // || |
xmwmx | 8:e76bebe9c45f | 28 | // || |
xmwmx | 8:e76bebe9c45f | 29 | // __ y1 |
xmwmx | 8:e76bebe9c45f | 30 | // |
xmwmx | 8:e76bebe9c45f | 31 | // -- 开关 15mm |
xmwmx | 8:e76bebe9c45f | 32 | |
xmwmx | 8:e76bebe9c45f | 33 | |
xmwmx | 8:e76bebe9c45f | 34 | /////调试 |
xmwmx | 8:e76bebe9c45f | 35 | Serial pc(PA_9, PA_10); |
xmwmx | 8:e76bebe9c45f | 36 | DigitalOut LED(PB_8); |
xmwmx | 8:e76bebe9c45f | 37 | ///// |
xmwmx | 8:e76bebe9c45f | 38 | |
xmwmx | 8:e76bebe9c45f | 39 | //硬件接口 |
xmwmx | 8:e76bebe9c45f | 40 | // mosi, miso, sclk, cs, name |
xmwmx | 8:e76bebe9c45f | 41 | SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd"); |
xmwmx | 8:e76bebe9c45f | 42 | //步进电机 |
xmwmx | 8:e76bebe9c45f | 43 | DigitalOut step[2] = {DigitalOut(PC_5), DigitalOut(PA_5)}; //0--x P9,1--y P17 |
xmwmx | 8:e76bebe9c45f | 44 | DigitalOut dir[2] = {DigitalOut(PC_4), DigitalOut(PA_4)}; //0--x,1--y |
xmwmx | 8:e76bebe9c45f | 45 | DigitalOut en[2] = {DigitalOut(PD_2), DigitalOut(PA_2)}; //0--x,1--y |
xmwmx | 8:e76bebe9c45f | 46 | //电子开关,激光开关 |
xmwmx | 8:e76bebe9c45f | 47 | DigitalOut switch_GS(PC_15); |
xmwmx | 8:e76bebe9c45f | 48 | //行程开关 |
xmwmx | 8:e76bebe9c45f | 49 | DigitalIn switch_pos1(PC_8); //P25 |
xmwmx | 8:e76bebe9c45f | 50 | DigitalIn switch_pos2(PA_6); //P26 |
xmwmx | 8:e76bebe9c45f | 51 | DigitalIn switch_pos3(PA_11); //P27 |
xmwmx | 8:e76bebe9c45f | 52 | |
xmwmx | 8:e76bebe9c45f | 53 | //运行中的全局变量 |
xmwmx | 8:e76bebe9c45f | 54 | volatile bool Working, Getawork, Isend, Dataused, Getdata; |
xmwmx | 8:e76bebe9c45f | 55 | FILE *fp_drawing; // 存储图案的文件 |
xmwmx | 8:e76bebe9c45f | 56 | int status = 0; //0: 初始化; 1:建立通信; 2:等候任务; 3:等待数据 4:正在执行一个任务 |
xmwmx | 8:e76bebe9c45f | 57 | int now_x, now_y; |
xmwmx | 8:e76bebe9c45f | 58 | int Endoffile = 0; |
xmwmx | 8:e76bebe9c45f | 59 | |
xmwmx | 8:e76bebe9c45f | 60 | // float thedata[50][3]; |
xmwmx | 8:e76bebe9c45f | 61 | // char sdata[1024]; |
xmwmx | 8:e76bebe9c45f | 62 | int Nofdata; |
xmwmx | 8:e76bebe9c45f | 63 | bool ifreceive; |
xmwmx | 8:e76bebe9c45f | 64 | |
xmwmx | 8:e76bebe9c45f | 65 | //与实际有关参数 |
xmwmx | 8:e76bebe9c45f | 66 | float Lenoflattice = 1 ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100 |
xmwmx | 9:a3f9faa9ca1f | 67 | int unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
xmwmx | 8:e76bebe9c45f | 68 | int unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
xmwmx | 8:e76bebe9c45f | 69 | int unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
xmwmx | 8:e76bebe9c45f | 70 | ///Ticker ticker_step; |
xmwmx | 8:e76bebe9c45f | 71 | float step_halfperiod = 0.0001;//0.0002; |
xmwmx | 8:e76bebe9c45f | 72 | int max_x, max_y; |
xmwmx | 8:e76bebe9c45f | 73 | int dir_x = 1;///调试时调整 |
xmwmx | 8:e76bebe9c45f | 74 | int dir_y = 1;///调试时调整 |
xmwmx | 8:e76bebe9c45f | 75 | int dot_max = 30; //灰度最大值对应多少次激光点击 |
xmwmx | 8:e76bebe9c45f | 76 | float dot_last = 0.005; //每次激光点击持续多久 |
xmwmx | 8:e76bebe9c45f | 77 | int H_max = 255; //灰度值最大值 |
xmwmx | 8:e76bebe9c45f | 78 | double k = 1.0; //缩放比例 |
xmwmx | 8:e76bebe9c45f | 79 | double y=0;//图片y边长 |
xmwmx | 8:e76bebe9c45f | 80 | double x=0;//图片x边长 |
xmwmx | 8:e76bebe9c45f | 81 | int key=0;//判断变量 |
xmwmx | 9:a3f9faa9ca1f | 82 | int dot=0;//点计数 |
xmwmx | 9:a3f9faa9ca1f | 83 | int flag=0; |
xmwmx | 9:a3f9faa9ca1f | 84 | //char w[4294967295]; |
xmwmx | 8:e76bebe9c45f | 85 | |
xmwmx | 8:e76bebe9c45f | 86 | void rotate(int id ,int pix); |
xmwmx | 8:e76bebe9c45f | 87 | |
xmwmx | 8:e76bebe9c45f | 88 | |
xmwmx | 8:e76bebe9c45f | 89 | |
xmwmx | 8:e76bebe9c45f | 90 | void biginning() |
xmwmx | 8:e76bebe9c45f | 91 | { |
xmwmx | 8:e76bebe9c45f | 92 | pc.printf("x\n"); // 上x(正朝向开关)下y(负朝向开关) |
xmwmx | 8:e76bebe9c45f | 93 | while(!switch_pos1.read()) // __ y2 |
xmwmx | 8:e76bebe9c45f | 94 | { // || |
xmwmx | 8:e76bebe9c45f | 95 | rotate(0,5); // 115mm |
xmwmx | 8:e76bebe9c45f | 96 | } // x2 || x1 |
xmwmx | 8:e76bebe9c45f | 97 | rotate(0,-mm*edgex2); // //=====200mm====//42mm/|开关 |
xmwmx | 8:e76bebe9c45f | 98 | pc.printf("y\n"); // || |
xmwmx | 8:e76bebe9c45f | 99 | while(!switch_pos2.read()) // || |
xmwmx | 8:e76bebe9c45f | 100 | { // || |
xmwmx | 8:e76bebe9c45f | 101 | rotate(1,-5); // __ y1 |
xmwmx | 8:e76bebe9c45f | 102 | } // |
xmwmx | 8:e76bebe9c45f | 103 | rotate(1,mm*edgey1); // __ 开关 15mm |
xmwmx | 9:a3f9faa9ca1f | 104 | //定位 |
xmwmx | 9:a3f9faa9ca1f | 105 | for (int i = 0; i < 500; i++) |
xmwmx | 9:a3f9faa9ca1f | 106 | { |
xmwmx | 9:a3f9faa9ca1f | 107 | switch_GS = 1; |
xmwmx | 9:a3f9faa9ca1f | 108 | wait(0.005); |
xmwmx | 9:a3f9faa9ca1f | 109 | switch_GS = 0; |
xmwmx | 9:a3f9faa9ca1f | 110 | wait(0.005); |
xmwmx | 9:a3f9faa9ca1f | 111 | } |
xmwmx | 8:e76bebe9c45f | 112 | //走轮廓 |
xmwmx | 8:e76bebe9c45f | 113 | rotate(0,mm*widthx); |
xmwmx | 8:e76bebe9c45f | 114 | rotate(1,mm*widthy); |
xmwmx | 8:e76bebe9c45f | 115 | rotate(0,mm*-widthx); |
xmwmx | 8:e76bebe9c45f | 116 | rotate(1,mm*-widthy); |
xmwmx | 8:e76bebe9c45f | 117 | } |
xmwmx | 8:e76bebe9c45f | 118 | |
xmwmx | 8:e76bebe9c45f | 119 | void report() |
xmwmx | 8:e76bebe9c45f | 120 | { |
xmwmx | 8:e76bebe9c45f | 121 | // ser2usb.printf("-1- status: %d \r\n", status); |
xmwmx | 8:e76bebe9c45f | 122 | // ser2usb.printf("-2- now_x: %d now_y: %d \r\n", now_x,now_y); |
xmwmx | 8:e76bebe9c45f | 123 | //ser2usb.printf("-3- switch_GS: %i \r\n", switch_GS.read()); |
xmwmx | 8:e76bebe9c45f | 124 | //ser2usb.printf("-4- switch_pos1: %i switch_pos2: %i switch_pos3: %i \r\n", switch_pos1.read(),switch_pos2.read(),switch_pos3.read()); |
xmwmx | 8:e76bebe9c45f | 125 | //ser2usb.printf("-5- en[0]: %i en[1]: %i \r\n", en[0].read(),en[1].read()); |
xmwmx | 8:e76bebe9c45f | 126 | //ser2usb.printf("-6- dir[0]: %i dir[1]: %i \r\n", dir[0].read(),dir[1].read()); |
xmwmx | 8:e76bebe9c45f | 127 | //ser2usb.printf("-7- step[0]: %i step[1]: %i \r\n", step[0].read(),step[1].read()); |
xmwmx | 8:e76bebe9c45f | 128 | } |
xmwmx | 8:e76bebe9c45f | 129 | |
xmwmx | 8:e76bebe9c45f | 130 | void init() |
xmwmx | 8:e76bebe9c45f | 131 | { |
xmwmx | 8:e76bebe9c45f | 132 | status = 0; |
xmwmx | 8:e76bebe9c45f | 133 | now_x = 0; |
xmwmx | 8:e76bebe9c45f | 134 | now_y = 0; |
xmwmx | 8:e76bebe9c45f | 135 | step[0] = 0; |
xmwmx | 8:e76bebe9c45f | 136 | step[1] = 0; |
xmwmx | 8:e76bebe9c45f | 137 | //上x下y |
xmwmx | 8:e76bebe9c45f | 138 | |
xmwmx | 8:e76bebe9c45f | 139 | } |
xmwmx | 8:e76bebe9c45f | 140 | void rotate(int id, int pix) //id= 0--x,1--y pix=3200为一圈 |
xmwmx | 8:e76bebe9c45f | 141 | { |
xmwmx | 8:e76bebe9c45f | 142 | if (pix >= 0) { |
xmwmx | 8:e76bebe9c45f | 143 | dir[0] = dir_x; |
xmwmx | 8:e76bebe9c45f | 144 | dir[1] = dir_y; |
xmwmx | 8:e76bebe9c45f | 145 | } else { |
xmwmx | 8:e76bebe9c45f | 146 | pix = -pix; |
xmwmx | 8:e76bebe9c45f | 147 | dir[0] = 1 - dir_x; |
xmwmx | 8:e76bebe9c45f | 148 | dir[1] = 1 - dir_y; |
xmwmx | 8:e76bebe9c45f | 149 | } |
xmwmx | 8:e76bebe9c45f | 150 | for (int i = 0; i < pix; i++) { |
xmwmx | 8:e76bebe9c45f | 151 | step[id] = 1; |
xmwmx | 8:e76bebe9c45f | 152 | wait(step_halfperiod); |
xmwmx | 8:e76bebe9c45f | 153 | step[id] = 0; |
xmwmx | 8:e76bebe9c45f | 154 | wait(step_halfperiod); |
xmwmx | 8:e76bebe9c45f | 155 | } |
xmwmx | 8:e76bebe9c45f | 156 | } |
xmwmx | 8:e76bebe9c45f | 157 | |
xmwmx | 8:e76bebe9c45f | 158 | void moveto(float x, float y) |
xmwmx | 8:e76bebe9c45f | 159 | { |
xmwmx | 8:e76bebe9c45f | 160 | rotate(0, (x - now_x)*unit_xy); |
xmwmx | 8:e76bebe9c45f | 161 | rotate(1, (y - now_y)*unit_xy); |
xmwmx | 8:e76bebe9c45f | 162 | now_x = x; |
xmwmx | 8:e76bebe9c45f | 163 | now_y = y; |
xmwmx | 8:e76bebe9c45f | 164 | } |
xmwmx | 8:e76bebe9c45f | 165 | void fuwei() |
xmwmx | 8:e76bebe9c45f | 166 | { |
xmwmx | 8:e76bebe9c45f | 167 | moveto(0, 0); |
xmwmx | 8:e76bebe9c45f | 168 | status = 1; |
xmwmx | 8:e76bebe9c45f | 169 | } |
xmwmx | 8:e76bebe9c45f | 170 | void markdot(float value) |
xmwmx | 8:e76bebe9c45f | 171 | { |
xmwmx | 8:e76bebe9c45f | 172 | //switch_GS=1; |
xmwmx | 8:e76bebe9c45f | 173 | int Ndot = floor(value * dot_max / H_max); |
xmwmx | 8:e76bebe9c45f | 174 | for (int i = 0; i < Ndot; i++) { |
xmwmx | 8:e76bebe9c45f | 175 | switch_GS = 1; |
xmwmx | 8:e76bebe9c45f | 176 | wait(dot_last); |
xmwmx | 8:e76bebe9c45f | 177 | switch_GS = 0; |
xmwmx | 8:e76bebe9c45f | 178 | } |
xmwmx | 8:e76bebe9c45f | 179 | } |
xmwmx | 8:e76bebe9c45f | 180 | /* |
xmwmx | 8:e76bebe9c45f | 181 | void dealdata(){ |
xmwmx | 8:e76bebe9c45f | 182 | ser2usb.printf("getdata: %s \r\n",sdata); |
xmwmx | 8:e76bebe9c45f | 183 | int data_size = 0;//strlen(pch); |
xmwmx | 8:e76bebe9c45f | 184 | char * pch; |
xmwmx | 8:e76bebe9c45f | 185 | pch = strtok (sdata,"\r\n ,.-)("); |
xmwmx | 8:e76bebe9c45f | 186 | while (pch != NULL) |
xmwmx | 8:e76bebe9c45f | 187 | { |
xmwmx | 8:e76bebe9c45f | 188 | thedata[data_size/3][data_size%3]=atof(pch); |
xmwmx | 8:e76bebe9c45f | 189 | pch = strtok (NULL, "\r\n ,.-)("); |
xmwmx | 8:e76bebe9c45f | 190 | data_size++; |
xmwmx | 8:e76bebe9c45f | 191 | } |
xmwmx | 8:e76bebe9c45f | 192 | Nofdata = (data_size)/3; |
xmwmx | 8:e76bebe9c45f | 193 | } |
xmwmx | 8:e76bebe9c45f | 194 | void usbprintdata(){ |
xmwmx | 8:e76bebe9c45f | 195 | ser2usb.printf("dealdata: \r\n <<<<<<<<<<<<<<<<"); |
xmwmx | 8:e76bebe9c45f | 196 | for(int i=0;i<Nofdata;i++){ |
xmwmx | 8:e76bebe9c45f | 197 | ser2usb.printf("<<< i: %i x: %f y: %f v: %f \r\n",i,thedata[i][0],thedata[i][1],thedata[i][2]); |
xmwmx | 8:e76bebe9c45f | 198 | } |
xmwmx | 8:e76bebe9c45f | 199 | ser2usb.printf("dealdata done! \r\n >>>>>>>>>>>>"); |
xmwmx | 8:e76bebe9c45f | 200 | } |
xmwmx | 8:e76bebe9c45f | 201 | */ |
xmwmx | 8:e76bebe9c45f | 202 | |
xmwmx | 8:e76bebe9c45f | 203 | void draw() |
xmwmx | 8:e76bebe9c45f | 204 | { |
xmwmx | 8:e76bebe9c45f | 205 | unit_xy=unit_xy*k; |
xmwmx | 8:e76bebe9c45f | 206 | float x, y, v; |
xmwmx | 8:e76bebe9c45f | 207 | fp_drawing = fopen("/sd/write.txt", "r"); |
xmwmx | 8:e76bebe9c45f | 208 | pc.printf("reading\r\n"); |
xmwmx | 8:e76bebe9c45f | 209 | for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) |
xmwmx | 8:e76bebe9c45f | 210 | { |
xmwmx | 9:a3f9faa9ca1f | 211 | pc.printf("(%f,%f,%f)|%d|%d\r\n", x, y, v,i,dot); |
xmwmx | 8:e76bebe9c45f | 212 | moveto(x, y); |
xmwmx | 8:e76bebe9c45f | 213 | markdot(v); |
xmwmx | 8:e76bebe9c45f | 214 | // moveto(thedata[i][0],thedata[i][1]); |
xmwmx | 8:e76bebe9c45f | 215 | // markd??>ot(thedata[i][2]); |
xmwmx | 8:e76bebe9c45f | 216 | } |
xmwmx | 8:e76bebe9c45f | 217 | fclose(fp_drawing); |
xmwmx | 8:e76bebe9c45f | 218 | } |
xmwmx | 8:e76bebe9c45f | 219 | |
xmwmx | 8:e76bebe9c45f | 220 | void on_control_cmd(const char* actuator_name, const char* control_value) |
xmwmx | 8:e76bebe9c45f | 221 | { |
xmwmx | 8:e76bebe9c45f | 222 | //pc.printf("Received [%s] [%s]\r\n", actuator_name, control_value); |
xmwmx | 9:a3f9faa9ca1f | 223 | if (strcmp(actuator_name, "dat") == 0) { |
xmwmx | 8:e76bebe9c45f | 224 | //接受到的坐标数据写入存储卡 |
xmwmx | 9:a3f9faa9ca1f | 225 | //fprintf(fp_drawing, "%s", control_value); |
xmwmx | 8:e76bebe9c45f | 226 | fprintf(fp_drawing, "%s", control_value); |
xmwmx | 9:a3f9faa9ca1f | 227 | dot++; |
xmwmx | 9:a3f9faa9ca1f | 228 | if(strcmp(control_value, "(0,0,1)") == 0) |
xmwmx | 9:a3f9faa9ca1f | 229 | { |
xmwmx | 9:a3f9faa9ca1f | 230 | flag=1; |
xmwmx | 9:a3f9faa9ca1f | 231 | pc.printf("received (0,0,1) %d|%d\r\n",status,dot); |
xmwmx | 9:a3f9faa9ca1f | 232 | } |
xmwmx | 8:e76bebe9c45f | 233 | } |
xmwmx | 8:e76bebe9c45f | 234 | else if (strcmp(actuator_name, "cmd") == 0) { |
xmwmx | 8:e76bebe9c45f | 235 | int the_command = atoi(control_value); |
xmwmx | 8:e76bebe9c45f | 236 | // |
xmwmx | 8:e76bebe9c45f | 237 | if(key!=0) |
xmwmx | 8:e76bebe9c45f | 238 | { |
xmwmx | 8:e76bebe9c45f | 239 | if (key==1) { |
xmwmx | 8:e76bebe9c45f | 240 | k=the_command*0.1; |
xmwmx | 8:e76bebe9c45f | 241 | pc.printf("Received k %f\n",k); |
xmwmx | 8:e76bebe9c45f | 242 | |
xmwmx | 8:e76bebe9c45f | 243 | } |
xmwmx | 8:e76bebe9c45f | 244 | if (key==2) { |
xmwmx | 8:e76bebe9c45f | 245 | x=the_command; |
xmwmx | 9:a3f9faa9ca1f | 246 | pc.printf("Received x %f\n",x); |
xmwmx | 8:e76bebe9c45f | 247 | |
xmwmx | 8:e76bebe9c45f | 248 | } |
xmwmx | 8:e76bebe9c45f | 249 | if (key==3) { |
xmwmx | 8:e76bebe9c45f | 250 | y=the_command; |
xmwmx | 8:e76bebe9c45f | 251 | pc.printf("Received y %f\n",y); |
xmwmx | 8:e76bebe9c45f | 252 | } |
xmwmx | 8:e76bebe9c45f | 253 | |
xmwmx | 8:e76bebe9c45f | 254 | key=0; |
xmwmx | 8:e76bebe9c45f | 255 | } |
xmwmx | 8:e76bebe9c45f | 256 | else |
xmwmx | 8:e76bebe9c45f | 257 | { |
xmwmx | 8:e76bebe9c45f | 258 | if (the_command == 1&&status==2&&key==0) { |
xmwmx | 8:e76bebe9c45f | 259 | Getawork = 1; |
xmwmx | 8:e76bebe9c45f | 260 | } |
xmwmx | 8:e76bebe9c45f | 261 | if (the_command == 2&&status==3&&key==0) { |
xmwmx | 8:e76bebe9c45f | 262 | Getdata = 1; |
xmwmx | 8:e76bebe9c45f | 263 | pc.printf("Received Getdata %d\n",Getdata); |
xmwmx | 8:e76bebe9c45f | 264 | } |
xmwmx | 9:a3f9faa9ca1f | 265 | /*else if (the_command == 3&&status==2&&key==0) {//传k |
xmwmx | 8:e76bebe9c45f | 266 | key=1; |
xmwmx | 9:a3f9faa9ca1f | 267 | }*/ |
xmwmx | 8:e76bebe9c45f | 268 | else if (the_command == 4&&status==2&&key==0) {//传x |
xmwmx | 8:e76bebe9c45f | 269 | key=2; |
xmwmx | 8:e76bebe9c45f | 270 | } |
xmwmx | 8:e76bebe9c45f | 271 | else if (the_command == 5&&status==2&&key==0) {//传y |
xmwmx | 8:e76bebe9c45f | 272 | key=3; |
xmwmx | 8:e76bebe9c45f | 273 | } |
xmwmx | 8:e76bebe9c45f | 274 | } |
xmwmx | 8:e76bebe9c45f | 275 | // |
xmwmx | 8:e76bebe9c45f | 276 | |
xmwmx | 8:e76bebe9c45f | 277 | } |
xmwmx | 8:e76bebe9c45f | 278 | } |
xmwmx | 8:e76bebe9c45f | 279 | |
xmwmx | 8:e76bebe9c45f | 280 | int main() |
xmwmx | 8:e76bebe9c45f | 281 | { |
xmwmx | 8:e76bebe9c45f | 282 | init(); //初始化 |
xmwmx | 8:e76bebe9c45f | 283 | |
xmwmx | 8:e76bebe9c45f | 284 | //建立通信 |
xmwmx | 8:e76bebe9c45f | 285 | status = 1; |
xmwmx | 8:e76bebe9c45f | 286 | |
xmwmx | 8:e76bebe9c45f | 287 | pc.printf("starting\r\n"); |
xmwmx | 8:e76bebe9c45f | 288 | |
xmwmx | 8:e76bebe9c45f | 289 | MQTTSocket sock; |
xmwmx | 8:e76bebe9c45f | 290 | MClient client(sock); |
xmwmx | 8:e76bebe9c45f | 291 | |
xmwmx | 8:e76bebe9c45f | 292 | //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 |
xmwmx | 8:e76bebe9c45f | 293 | const char* sensors[][2] = { |
xmwmx | 8:e76bebe9c45f | 294 | "report", "", |
xmwmx | 8:e76bebe9c45f | 295 | NULL, NULL //最后一行以空指针作为结束标记 |
xmwmx | 8:e76bebe9c45f | 296 | }; |
xmwmx | 8:e76bebe9c45f | 297 | |
xmwmx | 8:e76bebe9c45f | 298 | //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 |
xmwmx | 8:e76bebe9c45f | 299 | const char* actuators[][2] = { |
xmwmx | 8:e76bebe9c45f | 300 | "dat", "", |
xmwmx | 8:e76bebe9c45f | 301 | "cmd", "", |
xmwmx | 8:e76bebe9c45f | 302 | NULL, NULL //最后一行以空指针作为结束标记 |
xmwmx | 8:e76bebe9c45f | 303 | }; |
xmwmx | 8:e76bebe9c45f | 304 | pc.printf("connecting...\r\n"); |
xmwmx | 8:e76bebe9c45f | 305 | |
xmwmx | 8:e76bebe9c45f | 306 | networking_init(sock, client, "192.168.1.100", sensors, actuators, on_control_cmd); |
xmwmx | 8:e76bebe9c45f | 307 | |
xmwmx | 8:e76bebe9c45f | 308 | pc.printf("Initialization done.\r\n"); |
xmwmx | 8:e76bebe9c45f | 309 | |
xmwmx | 8:e76bebe9c45f | 310 | status = 2; |
xmwmx | 8:e76bebe9c45f | 311 | |
xmwmx | 8:e76bebe9c45f | 312 | |
xmwmx | 8:e76bebe9c45f | 313 | pc.printf("laser printer send ready.haha\r\n"); |
xmwmx | 8:e76bebe9c45f | 314 | |
xmwmx | 8:e76bebe9c45f | 315 | Getawork = 0; |
xmwmx | 8:e76bebe9c45f | 316 | Getdata = 0; |
xmwmx | 8:e76bebe9c45f | 317 | |
xmwmx | 9:a3f9faa9ca1f | 318 | //pc.printf("yes%f|%f|%f\r\n",y,k,y*k); |
xmwmx | 9:a3f9faa9ca1f | 319 | //pc.printf("ready.\r\n"); |
xmwmx | 8:e76bebe9c45f | 320 | |
xmwmx | 8:e76bebe9c45f | 321 | |
xmwmx | 8:e76bebe9c45f | 322 | pc.printf("biginning\r\n"); |
xmwmx | 8:e76bebe9c45f | 323 | biginning(); |
xmwmx | 8:e76bebe9c45f | 324 | pc.printf("finish\r\n"); |
xmwmx | 8:e76bebe9c45f | 325 | |
xmwmx | 9:a3f9faa9ca1f | 326 | //draw(); |
xmwmx | 9:a3f9faa9ca1f | 327 | |
xmwmx | 8:e76bebe9c45f | 328 | while (true) |
xmwmx | 8:e76bebe9c45f | 329 | { |
xmwmx | 9:a3f9faa9ca1f | 330 | if(x==0||y==0&&status==2) |
xmwmx | 9:a3f9faa9ca1f | 331 | { |
xmwmx | 9:a3f9faa9ca1f | 332 | while(x==0&&status==2) |
xmwmx | 9:a3f9faa9ca1f | 333 | { |
xmwmx | 9:a3f9faa9ca1f | 334 | pc.printf("waiting x\r\n"); |
xmwmx | 9:a3f9faa9ca1f | 335 | publish_value(client, "report", "input x:"); |
xmwmx | 9:a3f9faa9ca1f | 336 | client.yield(5000); |
xmwmx | 9:a3f9faa9ca1f | 337 | } |
xmwmx | 9:a3f9faa9ca1f | 338 | while(y==0&&status==2) |
xmwmx | 9:a3f9faa9ca1f | 339 | { |
xmwmx | 9:a3f9faa9ca1f | 340 | pc.printf("waiting y\r\n"); |
xmwmx | 9:a3f9faa9ca1f | 341 | publish_value(client, "report", "input y:"); |
xmwmx | 9:a3f9faa9ca1f | 342 | client.yield(5000); |
xmwmx | 9:a3f9faa9ca1f | 343 | } |
xmwmx | 9:a3f9faa9ca1f | 344 | publish_value(client, "report", "ready."); |
xmwmx | 9:a3f9faa9ca1f | 345 | } |
xmwmx | 9:a3f9faa9ca1f | 346 | while(y*k>widthy&&status==2) |
xmwmx | 9:a3f9faa9ca1f | 347 | { |
xmwmx | 9:a3f9faa9ca1f | 348 | k-=0.01; |
xmwmx | 9:a3f9faa9ca1f | 349 | pc.printf("no%f|%f|%f\r\n",y,k,y*k); |
xmwmx | 9:a3f9faa9ca1f | 350 | wait(0.001); |
xmwmx | 9:a3f9faa9ca1f | 351 | } |
xmwmx | 8:e76bebe9c45f | 352 | if (status == 2) |
xmwmx | 8:e76bebe9c45f | 353 | { |
xmwmx | 9:a3f9faa9ca1f | 354 | |
xmwmx | 8:e76bebe9c45f | 355 | if (Getawork) |
xmwmx | 8:e76bebe9c45f | 356 | { //收到新任务 |
xmwmx | 8:e76bebe9c45f | 357 | Getawork = 0; |
xmwmx | 8:e76bebe9c45f | 358 | pc.printf("Begin receiving... %d\r\n",status); |
xmwmx | 8:e76bebe9c45f | 359 | // 打开文件,准备接受坐标数据 |
xmwmx | 8:e76bebe9c45f | 360 | fp_drawing = fopen("/sd/write.txt", "w"); |
xmwmx | 8:e76bebe9c45f | 361 | if (fp_drawing) |
xmwmx | 8:e76bebe9c45f | 362 | { |
xmwmx | 8:e76bebe9c45f | 363 | pc.printf("File opened\r\n"); |
xmwmx | 8:e76bebe9c45f | 364 | status = 3; |
xmwmx | 8:e76bebe9c45f | 365 | publish_value(client, "report", "wait data."); |
xmwmx | 9:a3f9faa9ca1f | 366 | pc.printf("wait data.%d\r\n",status); |
xmwmx | 8:e76bebe9c45f | 367 | } |
xmwmx | 8:e76bebe9c45f | 368 | } |
xmwmx | 9:a3f9faa9ca1f | 369 | |
xmwmx | 8:e76bebe9c45f | 370 | } |
xmwmx | 8:e76bebe9c45f | 371 | |
xmwmx | 8:e76bebe9c45f | 372 | |
xmwmx | 9:a3f9faa9ca1f | 373 | if (status == 3) |
xmwmx | 8:e76bebe9c45f | 374 | { |
xmwmx | 9:a3f9faa9ca1f | 375 | if(flag==1) |
xmwmx | 9:a3f9faa9ca1f | 376 | { |
xmwmx | 9:a3f9faa9ca1f | 377 | //wait(15); |
xmwmx | 9:a3f9faa9ca1f | 378 | |
xmwmx | 9:a3f9faa9ca1f | 379 | publish_value(client, "report", "all wrote."); |
xmwmx | 9:a3f9faa9ca1f | 380 | pc.printf("wrote finish %d\r\n",status); |
xmwmx | 9:a3f9faa9ca1f | 381 | } |
xmwmx | 8:e76bebe9c45f | 382 | if (Getdata) |
xmwmx | 8:e76bebe9c45f | 383 | { //数据接收完 |
xmwmx | 8:e76bebe9c45f | 384 | Getdata = 0; |
xmwmx | 8:e76bebe9c45f | 385 | pc.printf("End receiving... %d\r\n",status); |
xmwmx | 8:e76bebe9c45f | 386 | fclose(fp_drawing); |
xmwmx | 8:e76bebe9c45f | 387 | publish_value(client, "report", "received."); |
xmwmx | 8:e76bebe9c45f | 388 | status = 4; |
xmwmx | 8:e76bebe9c45f | 389 | } |
xmwmx | 8:e76bebe9c45f | 390 | } |
xmwmx | 9:a3f9faa9ca1f | 391 | if (status == 4) |
xmwmx | 8:e76bebe9c45f | 392 | { |
xmwmx | 8:e76bebe9c45f | 393 | // pc.printf("debug4 \r\n"); |
xmwmx | 8:e76bebe9c45f | 394 | // dealdata(); |
xmwmx | 9:a3f9faa9ca1f | 395 | pc.printf("Drawing.. %d |%d\r\n",status,dot); |
xmwmx | 8:e76bebe9c45f | 396 | // usbprintdata(); |
xmwmx | 8:e76bebe9c45f | 397 | draw(); |
xmwmx | 8:e76bebe9c45f | 398 | publish_value(client, "report", "done."); |
xmwmx | 9:a3f9faa9ca1f | 399 | pc.printf("done. %d \r\n",status); |
xmwmx | 8:e76bebe9c45f | 400 | status = 2; |
xmwmx | 9:a3f9faa9ca1f | 401 | x=0; |
xmwmx | 9:a3f9faa9ca1f | 402 | y=0; |
xmwmx | 9:a3f9faa9ca1f | 403 | Getawork = 0; |
xmwmx | 9:a3f9faa9ca1f | 404 | Getdata = 0; |
xmwmx | 9:a3f9faa9ca1f | 405 | unit_xy=200; |
xmwmx | 9:a3f9faa9ca1f | 406 | k=1.0; |
xmwmx | 9:a3f9faa9ca1f | 407 | flag=0; |
xmwmx | 9:a3f9faa9ca1f | 408 | dot=0; |
xmwmx | 9:a3f9faa9ca1f | 409 | //strcpy(w,""); |
xmwmx | 8:e76bebe9c45f | 410 | } |
xmwmx | 8:e76bebe9c45f | 411 | client.yield(1000); |
xmwmx | 8:e76bebe9c45f | 412 | } |
xmwmx | 9:a3f9faa9ca1f | 413 | |
xmwmx | 8:e76bebe9c45f | 414 | } |