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