收到xy自动缩放

Dependencies:   MQTT SDFileSystem WIZnet_Library mbed

Fork of wmx_laser by w mx

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mainwmx.cpp Source File

mainwmx.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 //#include <string>
00017 
00018 
00019 
00020 #define edgex1 125         //          上x(正朝向开关)下y(负朝向开关)
00021 #define edgex2 155        //              __
00022 #define edgey1 70         //              ||
00023 #define edgey2 100        //              125mm y2
00024 #define widthx 30        //         x2   ||         x1
00025 #define widthy 30        //      //=====210mm====//35mm/|开关
00026 #define mm 100//100步1mm  //              ||
00027                           //              ||
00028                           //              ||
00029                           //              __       y1
00030                           //              
00031                           //              -- 开关   15mm
00032 
00033 
00034 /////调试
00035 Serial pc(PA_9, PA_10);
00036 DigitalOut LED(PB_8);
00037 /////
00038 
00039 //硬件接口
00040 // mosi, miso, sclk, cs, name
00041 SDFileSystem sd(PB_15, PB_14, PB_13, PB_12, "sd");
00042 //步进电机
00043 DigitalOut step[2] = {DigitalOut(PC_5), DigitalOut(PA_5)}; //0--x P9,1--y P17
00044 DigitalOut dir[2] = {DigitalOut(PC_4), DigitalOut(PA_4)}; //0--x,1--y
00045 DigitalOut en[2] = {DigitalOut(PD_2), DigitalOut(PA_2)}; //0--x,1--y
00046 //电子开关,激光开关
00047 DigitalOut switch_GS(PC_15);
00048 //行程开关
00049  DigitalIn switch_pos1(PC_8);   //P25
00050  DigitalIn switch_pos2(PA_6);  //P26
00051  DigitalIn switch_pos3(PA_11);  //P27
00052 
00053 //运行中的全局变量
00054 volatile bool Working, Getawork, Isend, Dataused, Getdata;
00055 FILE *fp_drawing; // 存储图案的文件
00056 int status = 0; //0: 初始化;  1:建立通信;  2:等候任务;  3:等待数据    4:正在执行一个任务
00057 int now_x, now_y;
00058 int Endoffile = 0;
00059 
00060 // float thedata[50][3];
00061 // char sdata[1024];
00062 int Nofdata;
00063 bool ifreceive;
00064 
00065 //与实际有关参数
00066 float Lenoflattice = 1 ;//mm  取1mm为xy单元  unit_xy/Lenoflattice=100
00067 int unit_xy = 100;  //单位长度(xy移动一格)对应unit_xy转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
00068 int unit_x = 103;  //单位长度(xy移动一格)对应unit_x转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
00069 int unit_y = 103;  //单位长度(xy移动一格)对应unit_y转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
00070 ///Ticker ticker_step;
00071 float step_halfperiod = 0.0001;//0.0002;
00072 int max_x, max_y;
00073 int dir_x = 1;///调试时调整
00074 int dir_y = 1;///调试时调整
00075 int dot_max = 30;      //灰度最大值对应多少次激光点击
00076 float dot_last = 0.005;    //每次激光点击持续多久
00077 int H_max = 255;  //灰度值最大值
00078 double k = 1.0; //缩放比例
00079 double y=0;//图片y边长
00080 double x=0;//图片x边长
00081 int key=0;//判断变量
00082 int dot=0;//点计数
00083 int flag=0;
00084 int numbers[100][3];
00085 //char buf[100];
00086 
00087 
00088 void rotate(int id ,int pix);
00089 
00090 
00091 
00092 void biginning()
00093 {
00094     pc.printf("x\n");           //          上x(正朝向开关)下y(负朝向开关)
00095     while(!switch_pos1.read())  //              __      y2
00096     {                           //              ||
00097     rotate(0,5);                //              115mm
00098     }                           //      x2      ||      x1                      
00099     rotate(0,-mm*edgex2);      //      //=====200mm====//42mm/|开关
00100     pc.printf("y\n");           //              ||
00101     while(!switch_pos2.read())  //              ||
00102     {                           //              ||
00103     rotate(1,-5);               //              __      y1
00104     }                           //
00105      rotate(1,mm*edgey1);      //              __ 开关   15mm
00106      //定位
00107     for (int i = 0; i < 500; i++) 
00108     {
00109         switch_GS = 1;
00110         wait(0.005);
00111         switch_GS = 0;
00112         wait(0.005);
00113     }
00114      //走轮廓
00115      rotate(0,mm*widthx);  
00116      rotate(1,mm*widthy);
00117      rotate(0,mm*-widthx);
00118      rotate(1,mm*-widthy);
00119 }
00120 
00121 void report()
00122 {
00123     // ser2usb.printf("-1- status: %d \r\n", status);
00124     // ser2usb.printf("-2- now_x: %d now_y: %d \r\n", now_x,now_y);
00125     //ser2usb.printf("-3- switch_GS: %i \r\n", switch_GS.read());
00126     //ser2usb.printf("-4- switch_pos1: %i switch_pos2: %i switch_pos3: %i \r\n", switch_pos1.read(),switch_pos2.read(),switch_pos3.read());
00127     //ser2usb.printf("-5- en[0]: %i en[1]: %i \r\n", en[0].read(),en[1].read());
00128     //ser2usb.printf("-6- dir[0]: %i dir[1]: %i \r\n", dir[0].read(),dir[1].read());
00129     //ser2usb.printf("-7- step[0]: %i step[1]: %i \r\n", step[0].read(),step[1].read());
00130 }
00131 
00132 void init()
00133 {
00134     status = 0;
00135     now_x = 0;
00136     now_y = 0;
00137     step[0] = 0;
00138     step[1] = 0;
00139     //memset(buf,0,sizeof(buf)/sizeof(char));
00140     //上x下y
00141 
00142 }
00143 void rotate(int id, int pix) //id= 0--x,1--y  pix=3200为一圈
00144 {
00145     if (pix >= 0) {
00146         dir[0] = dir_x;
00147         dir[1] = dir_y;
00148     } else {
00149         pix = -pix;
00150         dir[0] = 1 - dir_x;
00151         dir[1] = 1 - dir_y;
00152     }
00153     for (int i = 0; i < pix; i++) {
00154         step[id] = 1;
00155         wait(step_halfperiod);
00156         step[id] = 0;
00157         wait(step_halfperiod);
00158     }
00159 }
00160 
00161 void moveto(float x, float y)
00162 {
00163     rotate(0, (x - now_x)*unit_xy);
00164     rotate(1, (y - now_y)*unit_xy);
00165     now_x = x;
00166     now_y = y;
00167 }
00168 void fuwei()
00169 {
00170     moveto(0, 0);
00171     status = 1;
00172 }
00173 void markdot(float value)
00174 {
00175     //switch_GS=1;
00176     int Ndot = floor(value * dot_max / H_max);
00177     for (int i = 0; i < Ndot; i++) {
00178         switch_GS = 1;
00179         wait(dot_last);
00180         switch_GS = 0;
00181     }
00182 }
00183 /*
00184 void dealdata(){
00185     ser2usb.printf("getdata: %s \r\n",sdata);
00186     int data_size = 0;//strlen(pch);
00187     char * pch;
00188     pch = strtok (sdata,"\r\n ,.-)(");
00189     while (pch != NULL)
00190     {
00191         thedata[data_size/3][data_size%3]=atof(pch);
00192         pch = strtok (NULL, "\r\n ,.-)(");
00193         data_size++;
00194     }
00195     Nofdata = (data_size)/3;
00196 }
00197 void usbprintdata(){
00198     ser2usb.printf("dealdata:  \r\n   <<<<<<<<<<<<<<<<");
00199     for(int i=0;i<Nofdata;i++){
00200         ser2usb.printf("<<< i: %i  x: %f  y: %f  v: %f \r\n",i,thedata[i][0],thedata[i][1],thedata[i][2]);
00201     }
00202     ser2usb.printf("dealdata done!    \r\n    >>>>>>>>>>>>");
00203 }
00204 */
00205 
00206 void str2numbers(char buf[])
00207 {
00208     pc.printf("%s\r\n",buf);
00209     char *p = buf;
00210     int index = 0;
00211     int x,y,z;
00212     while(true)
00213     {
00214         p = strchr(p, '(');
00215         if(!p) break;
00216         p++;
00217         sscanf(p, "%d%d%d",&x,&y,&z);
00218         pc.printf("%d %d %d \r\n",x,y,z);
00219         numbers[index][0] = x;
00220         numbers[index][1] = y;
00221         numbers[index][2] = z;
00222         index++;
00223     }
00224 }
00225 
00226 void draw()
00227 {
00228     unit_xy=unit_xy*k;
00229     float x, y, v;
00230     fp_drawing = fopen("/sd/write.txt", "r");
00231     pc.printf("reading\r\n");
00232     for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) 
00233     {
00234         pc.printf("(%f,%f,%f)|%d|%d\r\n", x, y, v,i,dot);
00235         moveto(x, y);
00236         markdot(v);
00237         // moveto(thedata[i][0],thedata[i][1]);
00238         // markd??>ot(thedata[i][2]);
00239     }
00240     fclose(fp_drawing);
00241 }
00242 
00243 void on_control_cmd(const char* actuator_name, const char* control_value)
00244 {
00245     //pc.printf("Received [%s] [%s]\r\n", actuator_name, control_value);
00246     if (strcmp(actuator_name, "dat") == 0) {
00247         //接受到的坐标数据写入存储卡
00248         /*
00249         if(dot<10)
00250         {
00251             strcpy(buf,control_value);
00252             dot++;
00253             pc.printf("received   %d|%d|%s\r\n",status,dot,buf);
00254         }
00255         else
00256         {
00257             fprintf(fp_drawing, "%s", buf);
00258             memset(buf,0,sizeof(buf)/sizeof(char));
00259             dot=0;
00260             strcpy(buf,control_value);
00261             dot++;
00262             flag=1;
00263             pc.printf("received.   %d|%d|%s\r\n",status,dot,buf);
00264         }
00265         */
00266         //fprintf(fp_drawing, "%s", control_value);
00267         //str2numbers(control_value);
00268         pc.printf("received   %d|%d\r\n",status,dot);
00269         fprintf(fp_drawing, "%s", control_value);
00270         dot++;
00271         flag=1;
00272         pc.printf("received   %d|%d\r\n",status,dot);
00273         
00274     } 
00275     else if (strcmp(actuator_name, "command") == 0) {
00276         int the_command = atoi(control_value);
00277         //
00278         if(key!=0)
00279             {
00280                 if (key==1) {
00281                     k=the_command*0.1;
00282                     pc.printf("Received k %f\n",k);
00283             
00284                 }
00285                 if (key==2) {
00286                     x=the_command;
00287                     pc.printf("Received x %f\n",x);
00288             
00289                 }
00290                 if (key==3) {
00291                     y=the_command;
00292                     pc.printf("Received y %f\n",y);
00293                 }
00294 
00295                 key=0;
00296             }
00297         else
00298         {
00299                 if (the_command == 1&&status==2&&key==0) {
00300                     Getawork = 1;
00301                 } 
00302                 if (the_command == 2&&status==3&&key==0) {
00303                     Getdata = 1;
00304                     pc.printf("Received Getdata %d\n",Getdata);
00305                 }
00306                 /*else if (the_command == 3&&status==2&&key==0) {//传k
00307                     key=1;
00308                 }*/
00309                 else if (the_command == 4&&status==2&&key==0) {//传x
00310                     key=2;
00311                 }
00312                 else if (the_command == 5&&status==2&&key==0) {//传y
00313                     key=3;
00314                 }
00315         }
00316         //
00317         
00318     }
00319 }
00320 
00321 int main()
00322 {
00323     init();     //初始化
00324 
00325     //建立通信
00326     status = 1;
00327 
00328     pc.printf("starting\r\n");
00329 
00330     MQTTSocket sock;
00331     MClient client(sock);
00332 
00333     //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
00334     const char* sensors[][2] = {
00335         "report", "",
00336         NULL, NULL //最后一行以空指针作为结束标记
00337     };
00338 
00339     //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
00340     const char* actuators[][2] = {
00341         "dat", "",
00342         "command", "",
00343         NULL, NULL //最后一行以空指针作为结束标记
00344     };
00345     pc.printf("connecting...\r\n");
00346 
00347     networking_init(sock, client, "192.168.1.100", sensors, actuators, on_control_cmd);
00348 
00349     pc.printf("Initialization done.\r\n");
00350 
00351     status = 2;
00352 
00353     
00354     pc.printf("laser printer send ready.haha\r\n");
00355     
00356     Getawork = 0;
00357     Getdata = 0;
00358 
00359     //pc.printf("yes%f|%f|%f\r\n",y,k,y*k);
00360     //pc.printf("ready.\r\n");
00361     
00362     
00363     pc.printf("biginning\r\n");
00364     biginning();
00365     pc.printf("finish\r\n");
00366     
00367     //draw();
00368     
00369     while (true) 
00370     {
00371         if(x==0||y==0&&status==2)
00372         {
00373             while(x==0&&status==2)
00374             {   
00375                 pc.printf("waiting x\r\n");
00376                 publish_value(client, "report", "input x:");
00377                 client.yield(5000);
00378             }
00379             while(y==0&&status==2)
00380             {
00381                 pc.printf("waiting y\r\n");
00382                 publish_value(client, "report", "input y:");
00383                 client.yield(5000);
00384             }
00385         }
00386         while(y*k>widthy&&status==2)
00387         {
00388             k-=0.01;
00389             pc.printf("no%f|%f|%f\r\n",y,k,y*k);
00390             wait(0.001);
00391         }
00392         if (status == 2) 
00393         {
00394             
00395             if (Getawork) 
00396             { //收到新任务
00397                 Getawork = 0;
00398                 pc.printf("Begin receiving...  %d\r\n",status);
00399                 // 打开文件,准备接受坐标数据
00400                 fp_drawing = fopen("/sd/write.txt", "w");
00401                 if (fp_drawing) 
00402                 {
00403                     pc.printf("File opened\r\n");
00404                     status = 3;
00405                     publish_value(client, "report", "wait data.");
00406                     pc.printf("wait data.%d\r\n",status);
00407                 }
00408             }
00409             else
00410             {
00411                 publish_value(client, "report", "ready.");
00412             }
00413              
00414         }
00415 
00416          
00417         if (status == 3) 
00418         {
00419             if(flag==1)
00420             {
00421                 //wait(15);
00422                 
00423                 publish_value(client, "report", "received.");
00424                 pc.printf("received.  %d\r\n",status);
00425                 flag=0;
00426             }
00427             if (Getdata) 
00428             { //数据接收完
00429                 Getdata = 0;
00430                 /*
00431                 fprintf(fp_drawing, "%s", buf);
00432                 memset(buf,0,sizeof(buf)/sizeof(char));
00433                 dot=0;
00434                 */
00435                 pc.printf("End receiving...  %d\r\n",status);
00436                 fclose(fp_drawing);
00437                 publish_value(client, "report", "starting");
00438                 status = 4;
00439             }
00440         } 
00441         if (status == 4) 
00442         {
00443             // pc.printf("debug4 \r\n");
00444             // dealdata();
00445             pc.printf("Drawing..  %d |%d\r\n",status,dot);
00446             // usbprintdata();
00447             draw();
00448             publish_value(client, "report", "done.");
00449             pc.printf("done.  %d \r\n",status);
00450             
00451             status = 2;
00452             x=0;
00453             y=0;
00454             Getawork = 0;
00455             Getdata = 0;
00456             unit_xy=200;
00457             k=1.0;
00458             flag=0;
00459             dot=0;
00460             //memset(buf,0,sizeof(buf)/sizeof(char));
00461         }
00462         client.yield(1000);
00463     }
00464     
00465 }