Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <string.h> // needed for memset
- #include <fstream> // fstream::open / fstream::close
- #include <sstream>
- using namespace std;
- #define BPS B9600 //velocidad puerto
- #define PORTNAME "/dev/ttyACM0" //0 o 1
- #define SPEED 0.001
- #define LEFT '4'
- #define RIGHT '6'
- #define UP '8'
- #define DOWN '2'
- #define VK_PAGE_UP '7'
- #define VK_PAGE_DOWN '1'
- /***********************************************************************/
- class control{
- public:
- control (string port);
- ~control ();
- void Run();
- private:
- unsigned char c;//Buffer lectura /escritura tty
- int tty_fd;
- string portname;//ttyACM0
- float speed;
- string gcode;
- bool streaming;//false
- size_t i;//posicion en gcode
- fstream fs;
- string fichero;
- void configurePort(string port);
- void draw();//Menu
- void keyPressed(char key);
- void portwrite(string gcode);
- void stream();
- char ReadTTY ();//Lee ttyACM0
- bool WriteTTY();//Escribe ttyACM0
- string trim(string s);//Elimina espacios delante y detras
- void serialEvent(string p);
- };
- /***********************************************************************/
- char control::ReadTTY (){//Lee ttyACM0
- if (read(tty_fd,&c,1)>0)
- write(STDOUT_FILENO,&c,1);
- return c;
- }
- /***********************************************************************/
- bool control::WriteTTY(){//Escribe ttyACM0
- if (read(STDIN_FILENO,&c,1)>0) {write(tty_fd,&c,1);return true;}
- return false;
- }
- /***********************************************************************/
- /***********************************************************************/
- control::control(string port):portname(PORTNAME),speed(SPEED),streaming(false){}
- /***********************************************************************/
- control::~control (){
- if (tty_fd!=0) {close(tty_fd);}
- }
- /***********************************************************************/
- void control::configurePort(string port){
- //Para configurar puerto serie
- struct termios tio;
- struct termios stdio;
- memset(&stdio,0,sizeof(stdio));
- stdio.c_iflag=0;
- stdio.c_oflag=0;
- stdio.c_cflag=0;
- stdio.c_lflag=0;
- stdio.c_cc[VMIN]=1;
- stdio.c_cc[VTIME]=0;
- tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
- tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); //READ non-blocking
- memset(&tio,0,sizeof(tio));
- tio.c_iflag=0;
- tio.c_oflag=0;
- tio.c_cflag=CS8|CREAD|CLOCAL; // 8N1
- tio.c_lflag=0;
- tio.c_cc[VMIN]=1;
- tio.c_cc[VTIME]=5;
- tty_fd=open(port.c_str(), O_RDWR | O_NONBLOCK); // O_NONBLOCK might override VMIN and VTIME, so read() may return immediately.
- cfsetospeed(&tio,BPS); // 9600
- cfsetispeed(&tio,BPS);
- tcsetattr(tty_fd,TCSANOW,&tio);
- }
- /***********************************************************************/
- void control::Run(){
- draw () ;//MENU
- configurePort (PORTNAME);//dev/ttyACM0 para empezar
- while (true){//bucle principal
- ReadTTY ();
- WriteTTY();
- }
- }
- /***********************************************************************/
- void control::draw(){//MENU
- cout <<"INSTRUCTIONS"<<endl;
- cout <<"p: select serial port"<<endl;
- cout <<"1: set speed to 0.001 inches (1 mil) per jog"<<endl;
- cout <<"2: set speed to 0.010 inches (10 mil) per jog"<<endl;
- cout <<"3: set speed to 0.100 inches (100 mil) per jog"<<endl;
- cout <<"arrow keys: jog in x-y plane"<<endl;
- cout <<"page up & page down: jog in z axis"<<endl;
- cout <<"$: display grbl settings"<<endl;
- cout <<"h: go home"<<endl;
- cout <<"0: zero machine (set home to the current location)"<<endl;
- cout <<"g: stream a g-code file"<<endl;
- cout <<"x: stop streaming g-code (this is NOT immediate)"<<endl;
- cout <<"current jog speed: " << speed << " inches per step"<<endl;
- cout <<"current serial port: "<< portname<<endl;
- }
- /***********************************************************************/
- void control::portwrite(string gcode){
- write(tty_fd,&gcode,gcode.length());
- }
- /***********************************************************************/
- void control::keyPressed(char key){
- if (key == '1') speed = 0.001;
- if (key == '2') speed = 0.01;
- if (key == '3') speed = 0.1;
- if (!streaming) {//Lectura comandos directos
- if (key == LEFT) portwrite("G91\nG20\nG00 X-"+to_string(speed)+" Y0.000 Z0.000\n");
- if (key == RIGHT) portwrite("G91\nG20\nG00 X" + to_string(speed) + " Y0.000 Z0.000\n");
- if (key == UP) portwrite("G91\nG20\nG00 X0.000 Y" + to_string(speed) + " Z0.000\n");
- if (key == DOWN) portwrite("G91\nG20\nG00 X0.000 Y-" + to_string(speed )+ " Z0.000\n");
- if (key == VK_PAGE_UP) portwrite("G91\nG20\nG00 X0.000 Y0.000 Z" + to_string(speed) + "\n");
- if (key== VK_PAGE_DOWN) portwrite("G91\nG20\nG00 X0.000 Y0.000 Z-" + to_string(speed )+ "\n");
- if (key == 'h') portwrite("G90\nG20\nG00 X0.000 Y0.000 Z0.000\n");
- if (key == 'v') portwrite("$0=75\n$1=74\n$2=75\n");
- if (key == 'v') portwrite("$0=100\n$1=74\n$2=75\n");
- if (key == 's') portwrite("$3=10\n");
- if (key == 'e') portwrite("$16=1\n");
- if (key == 'd') portwrite("$16=0\n");
- //if (key == '0') openSerialPort();
- //if (key == 'p') selectSerialPort();
- if (key == '$') portwrite("$$\n");
- }
- if (!streaming && key == 'g') {
- gcode.clear(); i = 0;
- //file = NULL;
- cout <<"Loading file..."<<endl<<"Select a file to process:";
- cin >>fichero;
- fs.open (fichero, std::fstream::in);// | std::fstream::out | std::fstream::app
- //Procesar fichero .......
- fs.close();
- }
- if (key == 'x') streaming = false;
- }
- /***********************************************************************/
- void control::stream(){
- stringstream ss;
- string s;
- ss >> s;
- if (!streaming) return;
- while (true) {
- if (i == gcode.length()) {
- streaming = false;
- return;
- }
- //if (gcode[i].trim().length() == 0) i++;//salta espacios en blanco ?????
- if (gcode[i]==' '){i++;}
- else break;
- }
- //println(gcode[i]);
- cout <<gcode[i];//Muestra el gcode
- //port.write(gcode[i] + '\n');//envia al terminal ttyACM0 el caracter
- ss<<gcode[i]+'\n';
- ss>>s;
- portwrite (s);
- i++;
- }
- /***********************************************************************/
- string control::trim(string s){//Elimina espacios delante y detras
- string::iterator it;
- for (it=s.begin() ;*it== ' ' && it!=s.end();it++){}
- s.erase(s.begin(),it);
- it =s.end();it--;
- while (*it--==' '){s.pop_back();}
- return s;
- }
- /***********************************************************************/
- void control::serialEvent(string p){
- string s;
- size_t found(0);
- char c;
- while ( ( c=ReadTTY() )!='\n' ){//Lee ttyACM0 hasta salto de linea
- s+=ReadTTY();//Crea string s con caracteres ...
- }
- s=trim(s); //procesa el string elimina espacios
- cout <<s<<endl;
- found=s.find("ok");
- if (found==0 && found!=string::npos ){ stream() ;}
- found=s.find("error");
- if (found==0 && found!=string::npos ){ stream() ;}
- /*
- string s = p.readStringUntil('\n');
- println(s.trim());
- if (s.trim().startsWith("ok")) stream();
- if (s.trim().startsWith("error")) stream(); // XXX: really?
- */
- }
- /***********************************************************************/
- /***********************************************************************/
- /***********************************************************************/
- int main (){
- control miniCNC(PORTNAME);
- miniCNC.Run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement