于之前基础上增加功能:py启动后输入缩放比例,十秒内未输入,则初始化后重启py,方可按默认设置执行
Dependencies: MQTT SDFileSystem WIZnet_Library mbed
Fork of Laser_SD_W5500 by
main.cpp
00001 /************************************************************************************************ 00002 File name: main.cpp 00003 Description: 项目激光部分客户端代码 00004 Author: 秦智 00005 Date: June 4, 2018 00006 Others: 本程序编写在mbed平台(https://os.mbed.com/)上 00007 00008 *************************************************************************************************/ 00009 00010 #include "mbed.h" 00011 #include <math.h> 00012 #include <cstring> 00013 #include <stdlib.h> 00014 #include "SDFileSystem.h" 00015 #include "networking.h" 00016 00017 00018 /////调试 00019 Serial pc(PA_9, PA_10); 00020 DigitalOut LED(PB_8); 00021 ///// 00022 00023 //硬件接口 00024 // mosi, miso, sclk, cs, name 00025 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd"); 00026 //步进电机 00027 DigitalOut step[2] = {DigitalOut(PC_5), DigitalOut(PA_5)}; //0--x P9,1--y P17 00028 DigitalOut dir[2] = {DigitalOut(PC_4), DigitalOut(PA_4)}; //0--x,1--y 00029 DigitalOut en[2] = {DigitalOut(PD_2), DigitalOut(PA_2)}; //0--x,1--y 00030 //电子开关,激光开关 00031 DigitalOut switch_GS(PC_15); 00032 //行程开关 00033 // DigitalIn switch_pos1(PB_1); //P25 00034 // DigitalIn switch_pos2(PA_15); //P26 00035 // DigitalIn switch_pos3(PA_11); //P27 00036 00037 //运行中的全局变量 00038 volatile bool Working, Getawork, Isend, Dataused, Getdata; 00039 FILE *fp_drawing; // 存储图案的文件 00040 int status = 0; //0: 初始化; 1:建立通信; 2:等候任务; 3:等待数据 4:正在执行一个任务 00041 int now_x, now_y; 00042 int Endoffile = 0; 00043 00044 // float thedata[50][3]; 00045 // char sdata[1024]; 00046 int Nofdata; 00047 bool ifreceive; 00048 00049 //与实际有关参数 00050 float Lenoflattice = 1 ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100 00051 int unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step 00052 int unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step 00053 int unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step 00054 ///Ticker ticker_step; 00055 float step_halfperiod = 0.002;//0.02; 00056 int max_x, max_y; 00057 int dir_x = 1;///调试时调整 00058 int dir_y = 1;///调试时调整 00059 int dot_max = 10; //灰度最大值对应多少次激光点击 00060 float dot_last = 0.5; //每次激光点击持续多久 00061 int H_max = 255; //灰度值最大值 00062 00063 00064 00065 00066 00067 void report() 00068 { 00069 // ser2usb.printf("-1- status: %d \r\n", status); 00070 // ser2usb.printf("-2- now_x: %d now_y: %d \r\n", now_x,now_y); 00071 //ser2usb.printf("-3- switch_GS: %i \r\n", switch_GS.read()); 00072 //ser2usb.printf("-4- switch_pos1: %i switch_pos2: %i switch_pos3: %i \r\n", switch_pos1.read(),switch_pos2.read(),switch_pos3.read()); 00073 //ser2usb.printf("-5- en[0]: %i en[1]: %i \r\n", en[0].read(),en[1].read()); 00074 //ser2usb.printf("-6- dir[0]: %i dir[1]: %i \r\n", dir[0].read(),dir[1].read()); 00075 //ser2usb.printf("-7- step[0]: %i step[1]: %i \r\n", step[0].read(),step[1].read()); 00076 } 00077 00078 void init() 00079 { 00080 status = 0; 00081 now_x = 0; 00082 now_y = 0; 00083 step[0] = 0; 00084 step[1] = 0; 00085 } 00086 void rotate(int id, int pix) //id= 0--x,1--y pix=3200为一圈 00087 { 00088 if (pix >= 0) { 00089 dir[0] = dir_x; 00090 dir[1] = dir_y; 00091 } else { 00092 pix = -pix; 00093 dir[0] = 1 - dir_x; 00094 dir[1] = 1 - dir_y; 00095 } 00096 for (int i = 0; i < pix; i++) { 00097 step[id] = 1; 00098 wait(step_halfperiod); 00099 step[id] = 0; 00100 wait(step_halfperiod); 00101 } 00102 } 00103 00104 void moveto(float x, float y) 00105 { 00106 rotate(0, (x - now_x)*unit_xy); //电机旋转 00107 rotate(1, (y - now_y)*unit_xy); 00108 now_x = x; 00109 now_y = y; 00110 } 00111 void markdot(float value) 00112 { 00113 //switch_GS=1; 00114 int Ndot = floor(value * dot_max / H_max); 00115 for (int i = 0; i < Ndot; i++) { 00116 switch_GS = 1; // 开激光 00117 wait(dot_last); // 等待 00118 switch_GS = 0; // 关激光 00119 } 00120 } 00121 00122 void draw() 00123 { 00124 float x, y, v; 00125 fp_drawing = fopen("/sd/write.txt", "r"); // 打开文件 00126 for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) { 00127 pc.printf("(%f,%f,%f)\r\n", x, y, v); 00128 moveto(x, y); // 移动到坐标位置 00129 markdot(v); // 打点 00130 } 00131 fclose(fp_drawing); // 关闭文件 00132 } 00133 void fuwei() 00134 { 00135 moveto(0, 0); 00136 status = 1; 00137 } 00138 00139 void on_control_cmd(const char* actuator_name, const char* control_value) 00140 { 00141 pc.printf("Received [%s] [%s]\r\n", actuator_name, control_value); 00142 if (strcmp(actuator_name, "dat") == 0) { 00143 //接受到的坐标数据写入存储卡 00144 fprintf(fp_drawing, "%s", control_value); 00145 } else if (strcmp(actuator_name, "cmd") == 0) { 00146 int the_command = atoi(control_value); 00147 if (the_command == 1) { 00148 Getawork = 1; 00149 } else if (the_command == 2) { 00150 Getdata = 1; 00151 } 00152 } 00153 } 00154 00155 int main() 00156 { 00157 init(); //初始化 00158 00159 //建立通信 00160 status = 1; 00161 00162 pc.printf("starting\r\n"); 00163 00164 MQTTSocket sock; 00165 MClient client(sock); 00166 00167 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 00168 const char* sensors[][2] = { 00169 "report", "", 00170 NULL, NULL //最后一行以空指针作为结束标记 00171 }; 00172 00173 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 00174 const char* actuators[][2] = { 00175 "dat", "", 00176 "cmd", "", 00177 NULL, NULL //最后一行以空指针作为结束标记 00178 }; 00179 pc.printf("connecting...\r\n"); 00180 00181 networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd); 00182 00183 pc.printf("Initialization done.\r\n"); 00184 00185 status = 2; 00186 00187 publish_value(client, "report", "ready."); 00188 pc.printf("laser printer send ready.\r\n"); 00189 00190 Getawork = 0; 00191 Getdata = 0; 00192 00193 while (true) { 00194 if (status == 2) { 00195 if (Getawork) { //收到新任务 00196 Getawork = 0; 00197 pc.printf("Begin receiving...\r\n"); 00198 // 打开文件,准备接受坐标数据 00199 fp_drawing = fopen("/sd/write.txt", "w"); 00200 if (fp_drawing) { 00201 pc.printf("File opened\r\n"); 00202 publish_value(client, "report", "wait data."); 00203 status = 3; 00204 } 00205 } 00206 } else if (status == 3) { 00207 if (Getdata) { //数据接收完 00208 Getdata = 0; 00209 pc.printf("End receiving...\r\n"); 00210 fclose(fp_drawing); 00211 publish_value(client, "report", "received."); 00212 status = 4; 00213 } 00214 } else if (status == 4) { 00215 // pc.printf("debug4 \r\n"); 00216 // dealdata(); 00217 pc.printf("Drawing.. \r\n"); 00218 // usbprintdata(); 00219 draw(); 00220 pc.printf("Finished\r\n"); 00221 publish_value(client, "report", "done."); 00222 status = 2; 00223 } 00224 client.yield(1000); 00225 } 00226 }
Generated on Sat Jul 16 2022 19:47:56 by 1.7.2