Laser_SD_W5500

Dependencies:   MQTT SDFileSystem WIZnet_Library mbed

Fork of EthW5500 by YX ZHANG

Committer:
zhangyx
Date:
Mon Jul 23 04:23:25 2018 +0000
Revision:
5:48c44bebe8fb
Parent:
4:ae6f380a5b41
add Python script

Who changed what in which revision?

UserRevisionLine numberNew 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 5:48c44bebe8fb 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 markdot(float value)
zhangyx 4:ae6f380a5b41 112 {
zhangyx 4:ae6f380a5b41 113 //switch_GS=1;
zhangyx 4:ae6f380a5b41 114 int Ndot = floor(value * dot_max / H_max);
zhangyx 4:ae6f380a5b41 115 for (int i = 0; i < Ndot; i++) {
zhangyx 5:48c44bebe8fb 116 switch_GS = 1; // 开激光
zhangyx 5:48c44bebe8fb 117 wait(dot_last); // 等待
zhangyx 5:48c44bebe8fb 118 switch_GS = 0; // 关激光
zhangyx 4:ae6f380a5b41 119 }
zhangyx 4:ae6f380a5b41 120 }
zhangyx 4:ae6f380a5b41 121
zhangyx 4:ae6f380a5b41 122 void draw()
zhangyx 4:ae6f380a5b41 123 {
zhangyx 4:ae6f380a5b41 124 float x, y, v;
zhangyx 5:48c44bebe8fb 125 fp_drawing = fopen("/sd/write.txt", "r"); // 打开文件
zhangyx 4:ae6f380a5b41 126 for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) {
zhangyx 4:ae6f380a5b41 127 pc.printf("(%f,%f,%f)\r\n", x, y, v);
zhangyx 5:48c44bebe8fb 128 moveto(x, y); // 移动到坐标位置
zhangyx 5:48c44bebe8fb 129 markdot(v); // 打点
zhangyx 4:ae6f380a5b41 130 }
zhangyx 5:48c44bebe8fb 131 fclose(fp_drawing); // 关闭文件
zhangyx 5:48c44bebe8fb 132 }
zhangyx 5:48c44bebe8fb 133 void fuwei()
zhangyx 5:48c44bebe8fb 134 {
zhangyx 5:48c44bebe8fb 135 moveto(0, 0);
zhangyx 5:48c44bebe8fb 136 status = 1;
zhangyx 4:ae6f380a5b41 137 }
zhangyx 0:1170747a672f 138
zhangyx 2:a50b794b8ede 139 void on_control_cmd(const char* actuator_name, const char* control_value)
zhangyx 0:1170747a672f 140 {
zhangyx 4:ae6f380a5b41 141 pc.printf("Received [%s] [%s]\r\n", actuator_name, control_value);
zhangyx 4:ae6f380a5b41 142 if (strcmp(actuator_name, "dat") == 0) {
zhangyx 4:ae6f380a5b41 143 //接受到的坐标数据写入存储卡
zhangyx 4:ae6f380a5b41 144 fprintf(fp_drawing, "%s", control_value);
zhangyx 4:ae6f380a5b41 145 } else if (strcmp(actuator_name, "cmd") == 0) {
zhangyx 4:ae6f380a5b41 146 int the_command = atoi(control_value);
zhangyx 4:ae6f380a5b41 147 if (the_command == 1) {
zhangyx 4:ae6f380a5b41 148 Getawork = 1;
zhangyx 4:ae6f380a5b41 149 } else if (the_command == 2) {
zhangyx 4:ae6f380a5b41 150 Getdata = 1;
zhangyx 4:ae6f380a5b41 151 }
zhangyx 4:ae6f380a5b41 152 }
zhangyx 0:1170747a672f 153 }
zhangyx 2:a50b794b8ede 154
zhangyx 4:ae6f380a5b41 155 int main()
zhangyx 4:ae6f380a5b41 156 {
zhangyx 4:ae6f380a5b41 157 init(); //初始化
zhangyx 4:ae6f380a5b41 158
zhangyx 4:ae6f380a5b41 159 //建立通信
zhangyx 4:ae6f380a5b41 160 status = 1;
zhangyx 4:ae6f380a5b41 161
zhangyx 4:ae6f380a5b41 162 pc.printf("starting\r\n");
zhangyx 4:ae6f380a5b41 163
zhangyx 0:1170747a672f 164 MQTTSocket sock;
zhangyx 0:1170747a672f 165 MClient client(sock);
zhangyx 4:ae6f380a5b41 166
zhangyx 2:a50b794b8ede 167 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
zhangyx 2:a50b794b8ede 168 const char* sensors[][2] = {
zhangyx 4:ae6f380a5b41 169 "report", "",
zhangyx 2:a50b794b8ede 170 NULL, NULL //最后一行以空指针作为结束标记
zhangyx 2:a50b794b8ede 171 };
zhangyx 0:1170747a672f 172
zhangyx 2:a50b794b8ede 173 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
zhangyx 2:a50b794b8ede 174 const char* actuators[][2] = {
zhangyx 4:ae6f380a5b41 175 "dat", "",
zhangyx 4:ae6f380a5b41 176 "cmd", "",
zhangyx 2:a50b794b8ede 177 NULL, NULL //最后一行以空指针作为结束标记
zhangyx 2:a50b794b8ede 178 };
zhangyx 4:ae6f380a5b41 179 pc.printf("connecting...\r\n");
zhangyx 4:ae6f380a5b41 180
zhangyx 2:a50b794b8ede 181 networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd);
zhangyx 0:1170747a672f 182
zhangyx 4:ae6f380a5b41 183 pc.printf("Initialization done.\r\n");
zhangyx 4:ae6f380a5b41 184
zhangyx 4:ae6f380a5b41 185 status = 2;
zhangyx 4:ae6f380a5b41 186
zhangyx 4:ae6f380a5b41 187 publish_value(client, "report", "ready.");
zhangyx 4:ae6f380a5b41 188 pc.printf("laser printer send ready.\r\n");
zhangyx 4:ae6f380a5b41 189
zhangyx 4:ae6f380a5b41 190 Getawork = 0;
zhangyx 4:ae6f380a5b41 191 Getdata = 0;
zhangyx 4:ae6f380a5b41 192
zhangyx 4:ae6f380a5b41 193 while (true) {
zhangyx 4:ae6f380a5b41 194 if (status == 2) {
zhangyx 4:ae6f380a5b41 195 if (Getawork) { //收到新任务
zhangyx 4:ae6f380a5b41 196 Getawork = 0;
zhangyx 4:ae6f380a5b41 197 pc.printf("Begin receiving...\r\n");
zhangyx 4:ae6f380a5b41 198 // 打开文件,准备接受坐标数据
zhangyx 4:ae6f380a5b41 199 fp_drawing = fopen("/sd/write.txt", "w");
zhangyx 4:ae6f380a5b41 200 if (fp_drawing) {
zhangyx 4:ae6f380a5b41 201 pc.printf("File opened\r\n");
zhangyx 4:ae6f380a5b41 202 publish_value(client, "report", "wait data.");
zhangyx 4:ae6f380a5b41 203 status = 3;
zhangyx 4:ae6f380a5b41 204 }
zhangyx 4:ae6f380a5b41 205 }
zhangyx 4:ae6f380a5b41 206 } else if (status == 3) {
zhangyx 4:ae6f380a5b41 207 if (Getdata) { //数据接收完
zhangyx 4:ae6f380a5b41 208 Getdata = 0;
zhangyx 4:ae6f380a5b41 209 pc.printf("End receiving...\r\n");
zhangyx 4:ae6f380a5b41 210 fclose(fp_drawing);
zhangyx 4:ae6f380a5b41 211 publish_value(client, "report", "received.");
zhangyx 4:ae6f380a5b41 212 status = 4;
zhangyx 4:ae6f380a5b41 213 }
zhangyx 4:ae6f380a5b41 214 } else if (status == 4) {
zhangyx 4:ae6f380a5b41 215 // pc.printf("debug4 \r\n");
zhangyx 4:ae6f380a5b41 216 // dealdata();
zhangyx 4:ae6f380a5b41 217 pc.printf("Drawing.. \r\n");
zhangyx 4:ae6f380a5b41 218 // usbprintdata();
zhangyx 4:ae6f380a5b41 219 draw();
zhangyx 5:48c44bebe8fb 220 pc.printf("Finished\r\n");
zhangyx 4:ae6f380a5b41 221 publish_value(client, "report", "done.");
zhangyx 4:ae6f380a5b41 222 status = 2;
zhangyx 0:1170747a672f 223 }
zhangyx 4:ae6f380a5b41 224 client.yield(1000);
zhangyx 0:1170747a672f 225 }
zhangyx 0:1170747a672f 226 }