Laser_SD_W5500
Dependencies: MQTT SDFileSystem WIZnet_Library mbed
Fork of EthW5500 by
main.cpp@4:ae6f380a5b41, 2018-07-22 (annotated)
- Committer:
- zhangyx
- Date:
- Sun Jul 22 15:53:24 2018 +0000
- Revision:
- 4:ae6f380a5b41
- Parent:
- 2:a50b794b8ede
- Child:
- 5:48c44bebe8fb
working code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zhangyx | 4:ae6f380a5b41 | 1 | /************************************************************************************************ |
zhangyx | 4:ae6f380a5b41 | 2 | File name: main.cpp |
zhangyx | 4:ae6f380a5b41 | 3 | Description: 项目激光部分客户端代码 |
zhangyx | 4:ae6f380a5b41 | 4 | Author: 秦智 |
zhangyx | 4:ae6f380a5b41 | 5 | Date: June 4, 2018 |
zhangyx | 4:ae6f380a5b41 | 6 | Others: 本程序编写在mbed平台(https://os.mbed.com/)上 |
zhangyx | 4:ae6f380a5b41 | 7 | |
zhangyx | 4:ae6f380a5b41 | 8 | *************************************************************************************************/ |
zhangyx | 4:ae6f380a5b41 | 9 | |
zhangyx | 0:1170747a672f | 10 | #include "mbed.h" |
zhangyx | 4:ae6f380a5b41 | 11 | #include <math.h> |
zhangyx | 4:ae6f380a5b41 | 12 | #include <cstring> |
zhangyx | 4:ae6f380a5b41 | 13 | #include <stdlib.h> |
zhangyx | 4:ae6f380a5b41 | 14 | #include "SDFileSystem.h" |
zhangyx | 2:a50b794b8ede | 15 | #include "networking.h" |
zhangyx | 0:1170747a672f | 16 | |
zhangyx | 4:ae6f380a5b41 | 17 | |
zhangyx | 4:ae6f380a5b41 | 18 | /////调试 |
zhangyx | 4:ae6f380a5b41 | 19 | Serial pc(PA_9, PA_10); |
zhangyx | 2:a50b794b8ede | 20 | DigitalOut LED(PB_8); |
zhangyx | 4:ae6f380a5b41 | 21 | ///// |
zhangyx | 4:ae6f380a5b41 | 22 | |
zhangyx | 4:ae6f380a5b41 | 23 | //硬件接口 |
zhangyx | 4:ae6f380a5b41 | 24 | // mosi, miso, sclk, cs, name |
zhangyx | 4:ae6f380a5b41 | 25 | SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd"); |
zhangyx | 4:ae6f380a5b41 | 26 | //步进电机 |
zhangyx | 4:ae6f380a5b41 | 27 | DigitalOut step[2] = {DigitalOut(PC_5), DigitalOut(PA_5)}; //0--x P9,1--y P17 |
zhangyx | 4:ae6f380a5b41 | 28 | DigitalOut dir[2] = {DigitalOut(PC_4), DigitalOut(PA_4)}; //0--x,1--y |
zhangyx | 4:ae6f380a5b41 | 29 | DigitalOut en[2] = {DigitalOut(PD_2), DigitalOut(PA_2)}; //0--x,1--y |
zhangyx | 4:ae6f380a5b41 | 30 | //电子开关,激光开关 |
zhangyx | 4:ae6f380a5b41 | 31 | DigitalOut switch_GS(PC_15); |
zhangyx | 4:ae6f380a5b41 | 32 | //行程开关 |
zhangyx | 4:ae6f380a5b41 | 33 | // DigitalIn switch_pos1(PB_1); //P25 |
zhangyx | 4:ae6f380a5b41 | 34 | // DigitalIn switch_pos2(PA_15); //P26 |
zhangyx | 4:ae6f380a5b41 | 35 | // DigitalIn switch_pos3(PA_11); //P27 |
zhangyx | 4:ae6f380a5b41 | 36 | |
zhangyx | 4:ae6f380a5b41 | 37 | //运行中的全局变量 |
zhangyx | 4:ae6f380a5b41 | 38 | volatile bool Working, Getawork, Isend, Dataused, Getdata; |
zhangyx | 4:ae6f380a5b41 | 39 | FILE *fp_drawing; // 存储图案的文件 |
zhangyx | 4:ae6f380a5b41 | 40 | int status = 0; //0: 初始化; 1:建立通信; 2:等候任务; 3:等待数据 4:正在执行一个任务 |
zhangyx | 4:ae6f380a5b41 | 41 | int now_x, now_y; |
zhangyx | 4:ae6f380a5b41 | 42 | int Endoffile = 0; |
zhangyx | 4:ae6f380a5b41 | 43 | |
zhangyx | 4:ae6f380a5b41 | 44 | // float thedata[50][3]; |
zhangyx | 4:ae6f380a5b41 | 45 | // char sdata[1024]; |
zhangyx | 4:ae6f380a5b41 | 46 | int Nofdata; |
zhangyx | 4:ae6f380a5b41 | 47 | bool ifreceive; |
zhangyx | 4:ae6f380a5b41 | 48 | |
zhangyx | 4:ae6f380a5b41 | 49 | //与实际有关参数 |
zhangyx | 4:ae6f380a5b41 | 50 | float Lenoflattice = 1 ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100 |
zhangyx | 4:ae6f380a5b41 | 51 | int unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
zhangyx | 4:ae6f380a5b41 | 52 | int unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
zhangyx | 4:ae6f380a5b41 | 53 | int unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
zhangyx | 4:ae6f380a5b41 | 54 | ///Ticker ticker_step; |
zhangyx | 4:ae6f380a5b41 | 55 | float step_halfperiod = 0.002;//0.02; |
zhangyx | 4:ae6f380a5b41 | 56 | int max_x, max_y; |
zhangyx | 4:ae6f380a5b41 | 57 | int dir_x = 1;///调试时调整 |
zhangyx | 4:ae6f380a5b41 | 58 | int dir_y = 1;///调试时调整 |
zhangyx | 4:ae6f380a5b41 | 59 | int dot_max = 10; //灰度最大值对应多少次激光点击 |
zhangyx | 4:ae6f380a5b41 | 60 | float dot_last = 0.5; //每次激光点击持续多久 |
zhangyx | 4:ae6f380a5b41 | 61 | int H_max = 255; //灰度值最大值 |
zhangyx | 4:ae6f380a5b41 | 62 | |
zhangyx | 4:ae6f380a5b41 | 63 | |
zhangyx | 4:ae6f380a5b41 | 64 | |
zhangyx | 4:ae6f380a5b41 | 65 | |
zhangyx | 4:ae6f380a5b41 | 66 | |
zhangyx | 4:ae6f380a5b41 | 67 | void report() |
zhangyx | 4:ae6f380a5b41 | 68 | { |
zhangyx | 4:ae6f380a5b41 | 69 | // ser2usb.printf("-1- status: %d \r\n", status); |
zhangyx | 4:ae6f380a5b41 | 70 | // ser2usb.printf("-2- now_x: %d now_y: %d \r\n", now_x,now_y); |
zhangyx | 4:ae6f380a5b41 | 71 | //ser2usb.printf("-3- switch_GS: %i \r\n", switch_GS.read()); |
zhangyx | 4:ae6f380a5b41 | 72 | //ser2usb.printf("-4- switch_pos1: %i switch_pos2: %i switch_pos3: %i \r\n", switch_pos1.read(),switch_pos2.read(),switch_pos3.read()); |
zhangyx | 4:ae6f380a5b41 | 73 | //ser2usb.printf("-5- en[0]: %i en[1]: %i \r\n", en[0].read(),en[1].read()); |
zhangyx | 4:ae6f380a5b41 | 74 | //ser2usb.printf("-6- dir[0]: %i dir[1]: %i \r\n", dir[0].read(),dir[1].read()); |
zhangyx | 4:ae6f380a5b41 | 75 | //ser2usb.printf("-7- step[0]: %i step[1]: %i \r\n", step[0].read(),step[1].read()); |
zhangyx | 4:ae6f380a5b41 | 76 | } |
zhangyx | 4:ae6f380a5b41 | 77 | |
zhangyx | 4:ae6f380a5b41 | 78 | void init() |
zhangyx | 4:ae6f380a5b41 | 79 | { |
zhangyx | 4:ae6f380a5b41 | 80 | status = 0; |
zhangyx | 4:ae6f380a5b41 | 81 | now_x = 0; |
zhangyx | 4:ae6f380a5b41 | 82 | now_y = 0; |
zhangyx | 4:ae6f380a5b41 | 83 | step[0] = 0; |
zhangyx | 4:ae6f380a5b41 | 84 | step[1] = 0; |
zhangyx | 4:ae6f380a5b41 | 85 | } |
zhangyx | 4:ae6f380a5b41 | 86 | void rotate(int id, int pix) //id= 0--x,1--y pix=3200为一圈 |
zhangyx | 4:ae6f380a5b41 | 87 | { |
zhangyx | 4:ae6f380a5b41 | 88 | if (pix >= 0) { |
zhangyx | 4:ae6f380a5b41 | 89 | dir[0] = dir_x; |
zhangyx | 4:ae6f380a5b41 | 90 | dir[1] = dir_y; |
zhangyx | 4:ae6f380a5b41 | 91 | } else { |
zhangyx | 4:ae6f380a5b41 | 92 | pix = -pix; |
zhangyx | 4:ae6f380a5b41 | 93 | dir[0] = 1 - dir_x; |
zhangyx | 4:ae6f380a5b41 | 94 | dir[1] = 1 - dir_y; |
zhangyx | 4:ae6f380a5b41 | 95 | } |
zhangyx | 4:ae6f380a5b41 | 96 | for (int i = 0; i < pix; i++) { |
zhangyx | 4:ae6f380a5b41 | 97 | step[id] = 1; |
zhangyx | 4:ae6f380a5b41 | 98 | wait(step_halfperiod); |
zhangyx | 4:ae6f380a5b41 | 99 | step[id] = 0; |
zhangyx | 4:ae6f380a5b41 | 100 | wait(step_halfperiod); |
zhangyx | 4:ae6f380a5b41 | 101 | } |
zhangyx | 4:ae6f380a5b41 | 102 | } |
zhangyx | 4:ae6f380a5b41 | 103 | |
zhangyx | 4:ae6f380a5b41 | 104 | void moveto(float x, float y) |
zhangyx | 4:ae6f380a5b41 | 105 | { |
zhangyx | 4:ae6f380a5b41 | 106 | rotate(0, (x - now_x)*unit_xy); |
zhangyx | 4:ae6f380a5b41 | 107 | rotate(1, (y - now_y)*unit_xy); |
zhangyx | 4:ae6f380a5b41 | 108 | now_x = x; |
zhangyx | 4:ae6f380a5b41 | 109 | now_y = y; |
zhangyx | 4:ae6f380a5b41 | 110 | } |
zhangyx | 4:ae6f380a5b41 | 111 | void fuwei() |
zhangyx | 4:ae6f380a5b41 | 112 | { |
zhangyx | 4:ae6f380a5b41 | 113 | moveto(0, 0); |
zhangyx | 4:ae6f380a5b41 | 114 | status = 1; |
zhangyx | 4:ae6f380a5b41 | 115 | } |
zhangyx | 4:ae6f380a5b41 | 116 | void markdot(float value) |
zhangyx | 4:ae6f380a5b41 | 117 | { |
zhangyx | 4:ae6f380a5b41 | 118 | //switch_GS=1; |
zhangyx | 4:ae6f380a5b41 | 119 | int Ndot = floor(value * dot_max / H_max); |
zhangyx | 4:ae6f380a5b41 | 120 | for (int i = 0; i < Ndot; i++) { |
zhangyx | 4:ae6f380a5b41 | 121 | switch_GS = 1; |
zhangyx | 4:ae6f380a5b41 | 122 | wait(dot_last); |
zhangyx | 4:ae6f380a5b41 | 123 | switch_GS = 0; |
zhangyx | 4:ae6f380a5b41 | 124 | } |
zhangyx | 4:ae6f380a5b41 | 125 | } |
zhangyx | 4:ae6f380a5b41 | 126 | /* |
zhangyx | 4:ae6f380a5b41 | 127 | void dealdata(){ |
zhangyx | 4:ae6f380a5b41 | 128 | ser2usb.printf("getdata: %s \r\n",sdata); |
zhangyx | 4:ae6f380a5b41 | 129 | int data_size = 0;//strlen(pch); |
zhangyx | 4:ae6f380a5b41 | 130 | char * pch; |
zhangyx | 4:ae6f380a5b41 | 131 | pch = strtok (sdata,"\r\n ,.-)("); |
zhangyx | 4:ae6f380a5b41 | 132 | while (pch != NULL) |
zhangyx | 4:ae6f380a5b41 | 133 | { |
zhangyx | 4:ae6f380a5b41 | 134 | thedata[data_size/3][data_size%3]=atof(pch); |
zhangyx | 4:ae6f380a5b41 | 135 | pch = strtok (NULL, "\r\n ,.-)("); |
zhangyx | 4:ae6f380a5b41 | 136 | data_size++; |
zhangyx | 4:ae6f380a5b41 | 137 | } |
zhangyx | 4:ae6f380a5b41 | 138 | Nofdata = (data_size)/3; |
zhangyx | 4:ae6f380a5b41 | 139 | } |
zhangyx | 4:ae6f380a5b41 | 140 | void usbprintdata(){ |
zhangyx | 4:ae6f380a5b41 | 141 | ser2usb.printf("dealdata: \r\n <<<<<<<<<<<<<<<<"); |
zhangyx | 4:ae6f380a5b41 | 142 | for(int i=0;i<Nofdata;i++){ |
zhangyx | 4:ae6f380a5b41 | 143 | ser2usb.printf("<<< i: %i x: %f y: %f v: %f \r\n",i,thedata[i][0],thedata[i][1],thedata[i][2]); |
zhangyx | 4:ae6f380a5b41 | 144 | } |
zhangyx | 4:ae6f380a5b41 | 145 | ser2usb.printf("dealdata done! \r\n >>>>>>>>>>>>"); |
zhangyx | 4:ae6f380a5b41 | 146 | } |
zhangyx | 4:ae6f380a5b41 | 147 | */ |
zhangyx | 4:ae6f380a5b41 | 148 | |
zhangyx | 4:ae6f380a5b41 | 149 | void draw() |
zhangyx | 4:ae6f380a5b41 | 150 | { |
zhangyx | 4:ae6f380a5b41 | 151 | float x, y, v; |
zhangyx | 4:ae6f380a5b41 | 152 | fp_drawing = fopen("/sd/write.txt", "r"); |
zhangyx | 4:ae6f380a5b41 | 153 | for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) { |
zhangyx | 4:ae6f380a5b41 | 154 | pc.printf("(%f,%f,%f)\r\n", x, y, v); |
zhangyx | 4:ae6f380a5b41 | 155 | moveto(x, y); |
zhangyx | 4:ae6f380a5b41 | 156 | markdot(v); |
zhangyx | 4:ae6f380a5b41 | 157 | // moveto(thedata[i][0],thedata[i][1]); |
zhangyx | 4:ae6f380a5b41 | 158 | // markdot(thedata[i][2]); |
zhangyx | 4:ae6f380a5b41 | 159 | } |
zhangyx | 4:ae6f380a5b41 | 160 | fclose(fp_drawing); |
zhangyx | 4:ae6f380a5b41 | 161 | } |
zhangyx | 0:1170747a672f | 162 | |
zhangyx | 2:a50b794b8ede | 163 | void on_control_cmd(const char* actuator_name, const char* control_value) |
zhangyx | 0:1170747a672f | 164 | { |
zhangyx | 4:ae6f380a5b41 | 165 | pc.printf("Received [%s] [%s]\r\n", actuator_name, control_value); |
zhangyx | 4:ae6f380a5b41 | 166 | if (strcmp(actuator_name, "dat") == 0) { |
zhangyx | 4:ae6f380a5b41 | 167 | //接受到的坐标数据写入存储卡 |
zhangyx | 4:ae6f380a5b41 | 168 | fprintf(fp_drawing, "%s", control_value); |
zhangyx | 4:ae6f380a5b41 | 169 | } else if (strcmp(actuator_name, "cmd") == 0) { |
zhangyx | 4:ae6f380a5b41 | 170 | int the_command = atoi(control_value); |
zhangyx | 4:ae6f380a5b41 | 171 | if (the_command == 1) { |
zhangyx | 4:ae6f380a5b41 | 172 | Getawork = 1; |
zhangyx | 4:ae6f380a5b41 | 173 | } else if (the_command == 2) { |
zhangyx | 4:ae6f380a5b41 | 174 | Getdata = 1; |
zhangyx | 4:ae6f380a5b41 | 175 | } |
zhangyx | 4:ae6f380a5b41 | 176 | } |
zhangyx | 0:1170747a672f | 177 | } |
zhangyx | 2:a50b794b8ede | 178 | |
zhangyx | 4:ae6f380a5b41 | 179 | int main() |
zhangyx | 4:ae6f380a5b41 | 180 | { |
zhangyx | 4:ae6f380a5b41 | 181 | init(); //初始化 |
zhangyx | 4:ae6f380a5b41 | 182 | |
zhangyx | 4:ae6f380a5b41 | 183 | //建立通信 |
zhangyx | 4:ae6f380a5b41 | 184 | status = 1; |
zhangyx | 4:ae6f380a5b41 | 185 | |
zhangyx | 4:ae6f380a5b41 | 186 | pc.printf("starting\r\n"); |
zhangyx | 4:ae6f380a5b41 | 187 | |
zhangyx | 0:1170747a672f | 188 | MQTTSocket sock; |
zhangyx | 0:1170747a672f | 189 | MClient client(sock); |
zhangyx | 4:ae6f380a5b41 | 190 | |
zhangyx | 2:a50b794b8ede | 191 | //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 |
zhangyx | 2:a50b794b8ede | 192 | const char* sensors[][2] = { |
zhangyx | 4:ae6f380a5b41 | 193 | "report", "", |
zhangyx | 2:a50b794b8ede | 194 | NULL, NULL //最后一行以空指针作为结束标记 |
zhangyx | 2:a50b794b8ede | 195 | }; |
zhangyx | 0:1170747a672f | 196 | |
zhangyx | 2:a50b794b8ede | 197 | //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 |
zhangyx | 2:a50b794b8ede | 198 | const char* actuators[][2] = { |
zhangyx | 4:ae6f380a5b41 | 199 | "dat", "", |
zhangyx | 4:ae6f380a5b41 | 200 | "cmd", "", |
zhangyx | 2:a50b794b8ede | 201 | NULL, NULL //最后一行以空指针作为结束标记 |
zhangyx | 2:a50b794b8ede | 202 | }; |
zhangyx | 4:ae6f380a5b41 | 203 | pc.printf("connecting...\r\n"); |
zhangyx | 4:ae6f380a5b41 | 204 | |
zhangyx | 2:a50b794b8ede | 205 | networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd); |
zhangyx | 0:1170747a672f | 206 | |
zhangyx | 4:ae6f380a5b41 | 207 | pc.printf("Initialization done.\r\n"); |
zhangyx | 4:ae6f380a5b41 | 208 | |
zhangyx | 4:ae6f380a5b41 | 209 | status = 2; |
zhangyx | 4:ae6f380a5b41 | 210 | |
zhangyx | 4:ae6f380a5b41 | 211 | publish_value(client, "report", "ready."); |
zhangyx | 4:ae6f380a5b41 | 212 | pc.printf("laser printer send ready.\r\n"); |
zhangyx | 4:ae6f380a5b41 | 213 | |
zhangyx | 4:ae6f380a5b41 | 214 | Getawork = 0; |
zhangyx | 4:ae6f380a5b41 | 215 | Getdata = 0; |
zhangyx | 4:ae6f380a5b41 | 216 | |
zhangyx | 4:ae6f380a5b41 | 217 | while (true) { |
zhangyx | 4:ae6f380a5b41 | 218 | if (status == 2) { |
zhangyx | 4:ae6f380a5b41 | 219 | if (Getawork) { //收到新任务 |
zhangyx | 4:ae6f380a5b41 | 220 | Getawork = 0; |
zhangyx | 4:ae6f380a5b41 | 221 | pc.printf("Begin receiving...\r\n"); |
zhangyx | 4:ae6f380a5b41 | 222 | // 打开文件,准备接受坐标数据 |
zhangyx | 4:ae6f380a5b41 | 223 | fp_drawing = fopen("/sd/write.txt", "w"); |
zhangyx | 4:ae6f380a5b41 | 224 | if (fp_drawing) { |
zhangyx | 4:ae6f380a5b41 | 225 | pc.printf("File opened\r\n"); |
zhangyx | 4:ae6f380a5b41 | 226 | publish_value(client, "report", "wait data."); |
zhangyx | 4:ae6f380a5b41 | 227 | status = 3; |
zhangyx | 4:ae6f380a5b41 | 228 | } |
zhangyx | 4:ae6f380a5b41 | 229 | } |
zhangyx | 4:ae6f380a5b41 | 230 | } else if (status == 3) { |
zhangyx | 4:ae6f380a5b41 | 231 | if (Getdata) { //数据接收完 |
zhangyx | 4:ae6f380a5b41 | 232 | Getdata = 0; |
zhangyx | 4:ae6f380a5b41 | 233 | pc.printf("End receiving...\r\n"); |
zhangyx | 4:ae6f380a5b41 | 234 | fclose(fp_drawing); |
zhangyx | 4:ae6f380a5b41 | 235 | publish_value(client, "report", "received."); |
zhangyx | 4:ae6f380a5b41 | 236 | status = 4; |
zhangyx | 4:ae6f380a5b41 | 237 | } |
zhangyx | 4:ae6f380a5b41 | 238 | } else if (status == 4) { |
zhangyx | 4:ae6f380a5b41 | 239 | // pc.printf("debug4 \r\n"); |
zhangyx | 4:ae6f380a5b41 | 240 | // dealdata(); |
zhangyx | 4:ae6f380a5b41 | 241 | pc.printf("Drawing.. \r\n"); |
zhangyx | 4:ae6f380a5b41 | 242 | // usbprintdata(); |
zhangyx | 4:ae6f380a5b41 | 243 | draw(); |
zhangyx | 4:ae6f380a5b41 | 244 | publish_value(client, "report", "done."); |
zhangyx | 4:ae6f380a5b41 | 245 | status = 2; |
zhangyx | 0:1170747a672f | 246 | } |
zhangyx | 4:ae6f380a5b41 | 247 | client.yield(1000); |
zhangyx | 0:1170747a672f | 248 | } |
zhangyx | 4:ae6f380a5b41 | 249 | |
zhangyx | 0:1170747a672f | 250 | } |