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