a library for laser printer based on https://os.mbed.com/users/xmwmx/code/wmx_laser_copy/
Dependents: Example_for_learning_easy
Revision 2:35089485bc89, committed 2018-10-16
- Comitter:
- Dennis_Yu
- Date:
- Tue Oct 16 07:40:46 2018 +0000
- Parent:
- 1:9cfdf3c372b5
- Commit message:
- all function added
Changed in this revision
laser.cpp | Show annotated file Show diff for this revision Revisions of this file |
laser.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/laser.cpp Tue Oct 16 06:42:48 2018 +0000 +++ b/laser.cpp Tue Oct 16 07:40:46 2018 +0000 @@ -1,7 +1,81 @@ #include "laser.h" -laser::laser(Esp8266 & iClient) - :client(iClient) +laser::laser(Esp8266 & iClient, SDFileSystem & iSD) + :client(iClient), sd(iSD), + switch_GS(PC_15), + switch_pos1(PC_8), //P25 + switch_pos2(PA_6), //P26 + switch_pos3(PA_11), //P27 + fp_drawing(NULL), + status(0), Endoffile(0) +{ + Lenoflattice = 1 ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100 + unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step + unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step + unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step + ///Ticker ticker_step; + step_halfperiod = 0.0001;//0.0002; + dir_x = 1;///调试时调整 + dir_y = 1;///调试时调整 + dot_max = 30; //灰度最大值对应多少次激光点击 + dot_last = 0.005; //每次激光点击持续多久 + H_max = 255; //灰度值最大值 + k = 1.0; //缩放比例 + y=0;//图片y边长 + x=0;//图片x边长 + key=0;//判断变量 + dot=0;//点计数 + flag=0; +} + +void laser::moveto(float x, float y) +{ + rotate(0, (x - now_x)*unit_xy); + rotate(1, (y - now_y)*unit_xy); + now_x = x; + now_y = y; +} + +void laser::rotate(int id, int pix) //id= 0--x,1--y pix=3200为一圈 { - ; + if (pix >= 0) { + dir[0] = dir_x; + dir[1] = dir_y; + } else { + pix = -pix; + dir[0] = 1 - dir_x; + dir[1] = 1 - dir_y; + } + for (int i = 0; i < pix; i++) { + step[id] = 1; + wait(step_halfperiod); + step[id] = 0; + wait(step_halfperiod); + } } + +void laser::markdot(float value) +{ + //switch_GS=1; + int Ndot = floor(value * dot_max / H_max); + for (int i = 0; i < Ndot; i++) { + switch_GS = 1; + wait(dot_last); + switch_GS = 0; + } +} + +void laser::draw() +{ + unit_xy=unit_xy*k; + float x, y, v; + fp_drawing = fopen("/sd/write.txt", "r"); + ser2usb.printf("reading\r\n"); + for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) + { + ser2usb.printf("(%f,%f,%f)|%d|%d\r\n", x, y, v,i,dot); + moveto(x, y); + markdot(v); + } + fclose(fp_drawing); +}
--- a/laser.h Tue Oct 16 06:42:48 2018 +0000 +++ b/laser.h Tue Oct 16 07:40:46 2018 +0000 @@ -1,3 +1,4 @@ +#pragma once #ifndef LASER_H #define LASER_H @@ -10,12 +11,65 @@ extern Serial ser2usb; +//步进电机 +DigitalOut step[2] = {DigitalOut(PC_5), DigitalOut(PA_5)}; //0--x P9,1--y P17 +DigitalOut dir[2] = {DigitalOut(PC_4), DigitalOut(PA_4)}; //0--x,1--y +DigitalOut en[2] = {DigitalOut(PD_2), DigitalOut(PA_2)}; //0--x,1--y + class laser { public: - laser(Esp8266 & iClient); + laser(Esp8266 & iClient, SDFileSystem & iSD); + void draw(); private: Esp8266 & client; + SDFileSystem & sd; + + void moveto(float x, float y); + void rotate(int id ,int pix); + void markdot(float value); + + //电子开关,激光开关 + DigitalOut switch_GS; + //行程开关 + DigitalIn switch_pos1; //P25 + DigitalIn switch_pos2; //P26 + DigitalIn switch_pos3; //P27 + + //运行中的全局变量 + volatile bool Working, Getawork, Isend, Dataused, Getdata; + FILE *fp_drawing; // 存储图案的文件 + int status; //0: 初始化; 1:建立通信; 2:等候任务; 3:等待数据 4:正在执行一个任务 + int now_x, now_y; + int Endoffile; + + // float thedata[50][3]; + // char sdata[1024]; + int Nofdata; + bool ifreceive; + + //与实际有关参数 + float Lenoflattice ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100 + int unit_xy; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step + int unit_x; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step + int unit_y; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step + ///Ticker ticker_step; + float step_halfperiod;//0.0002; + int max_x, max_y; + int dir_x;///调试时调整 + int dir_y;///调试时调整 + int dot_max; //灰度最大值对应多少次激光点击 + float dot_last; //每次激光点击持续多久 + int H_max; //灰度值最大值 + double k; //缩放比例 + double y;//图片y边长 + double x;//图片x边长 + int key;//判断变量 + int dot;//点计数 + int flag; + int numbers[100][3]; + //char buf[100]; }; + #endif