Advertisement
AntonioVillanueva

Futuro control desde PC de la CNC .....NO ACABADO !!!!

Mar 7th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <termios.h>
  9. #include <string.h> // needed for memset
  10.  
  11. #include <fstream>  //  fstream::open / fstream::close
  12.  
  13. #include <sstream>
  14.  
  15. using namespace std;
  16. #define BPS B9600 //velocidad puerto
  17. #define PORTNAME "/dev/ttyACM0" //0 o 1
  18. #define SPEED 0.001
  19.  
  20. #define LEFT '4'
  21. #define RIGHT '6'
  22. #define UP '8'
  23. #define DOWN '2'
  24. #define VK_PAGE_UP '7'
  25. #define VK_PAGE_DOWN '1'
  26.  
  27.  
  28. /***********************************************************************/
  29. class control{
  30.     public:
  31.     control (string port);
  32.     ~control ();
  33.     void Run();
  34.        
  35.     private:
  36.  
  37.     unsigned char c;//Buffer lectura /escritura tty
  38.     int tty_fd;
  39.    
  40.     string portname;//ttyACM0
  41.     float speed;
  42.     string gcode;
  43.     bool streaming;//false
  44.     size_t i;//posicion en gcode
  45.     fstream fs;
  46.     string fichero;
  47.    
  48.     void configurePort(string port);
  49.     void draw();//Menu
  50.     void keyPressed(char key);
  51.     void portwrite(string gcode);
  52.     void stream();
  53.  
  54.     char ReadTTY ();//Lee ttyACM0
  55.     bool WriteTTY();//Escribe ttyACM0
  56.     string trim(string s);//Elimina espacios delante y detras
  57.     void serialEvent(string p);
  58.  
  59. };
  60. /***********************************************************************/
  61.     char control::ReadTTY (){//Lee ttyACM0
  62.         if (read(tty_fd,&c,1)>0)
  63.         write(STDOUT_FILENO,&c,1);
  64.         return c;
  65.     }
  66. /***********************************************************************/  
  67.     bool control::WriteTTY(){//Escribe ttyACM0
  68.         if (read(STDIN_FILENO,&c,1)>0) {write(tty_fd,&c,1);return true;}
  69.         return false;
  70.     }
  71. /***********************************************************************/
  72. /***********************************************************************/
  73.     control::control(string port):portname(PORTNAME),speed(SPEED),streaming(false){}
  74. /***********************************************************************/
  75.     control::~control (){
  76.         if (tty_fd!=0) {close(tty_fd);}
  77.     }
  78. /***********************************************************************/
  79.     void control::configurePort(string port){
  80.     //Para configurar puerto serie
  81.         struct termios tio;
  82.         struct termios stdio;
  83.  
  84.         memset(&stdio,0,sizeof(stdio));
  85.         stdio.c_iflag=0;
  86.         stdio.c_oflag=0;
  87.         stdio.c_cflag=0;
  88.         stdio.c_lflag=0;
  89.         stdio.c_cc[VMIN]=1;
  90.         stdio.c_cc[VTIME]=0;
  91.         tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
  92.         tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
  93.         fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);   //READ non-blocking
  94.  
  95.         memset(&tio,0,sizeof(tio));
  96.         tio.c_iflag=0;
  97.         tio.c_oflag=0;
  98.         tio.c_cflag=CS8|CREAD|CLOCAL;           // 8N1
  99.         tio.c_lflag=0;
  100.         tio.c_cc[VMIN]=1;
  101.         tio.c_cc[VTIME]=5;
  102.  
  103.         tty_fd=open(port.c_str(), O_RDWR | O_NONBLOCK);        // O_NONBLOCK might override VMIN and VTIME, so read() may return immediately.
  104.         cfsetospeed(&tio,BPS);            // 9600
  105.         cfsetispeed(&tio,BPS);      
  106.  
  107.         tcsetattr(tty_fd,TCSANOW,&tio);
  108.     }
  109.  
  110. /***********************************************************************/
  111.     void control::Run(){
  112.         draw () ;//MENU
  113.         configurePort (PORTNAME);//dev/ttyACM0 para empezar
  114.         while (true){//bucle principal
  115.             ReadTTY ();
  116.             WriteTTY();        
  117.         }      
  118.     }
  119. /***********************************************************************/
  120.     void control::draw(){//MENU
  121.         cout <<"INSTRUCTIONS"<<endl;
  122.         cout <<"p: select serial port"<<endl;
  123.         cout <<"1: set speed to 0.001 inches (1 mil) per jog"<<endl;
  124.         cout <<"2: set speed to 0.010 inches (10 mil) per jog"<<endl;
  125.         cout <<"3: set speed to 0.100 inches (100 mil) per jog"<<endl;
  126.         cout <<"arrow keys: jog in x-y plane"<<endl;
  127.         cout <<"page up & page down: jog in z axis"<<endl;
  128.         cout <<"$: display grbl settings"<<endl;
  129.         cout <<"h: go home"<<endl;
  130.         cout <<"0: zero machine (set home to the current location)"<<endl;
  131.         cout <<"g: stream a g-code file"<<endl;
  132.         cout <<"x: stop streaming g-code (this is NOT immediate)"<<endl;
  133.         cout <<"current jog speed: " << speed << " inches per step"<<endl;
  134.         cout <<"current serial port: "<< portname<<endl;
  135. }
  136. /***********************************************************************/
  137.     void control::portwrite(string gcode){
  138.         write(tty_fd,&gcode,gcode.length());
  139.     }
  140. /***********************************************************************/
  141.     void control::keyPressed(char key){
  142.   if (key == '1') speed = 0.001;
  143.   if (key == '2') speed = 0.01;
  144.   if (key == '3') speed = 0.1;
  145.  
  146.   if (!streaming) {//Lectura comandos directos
  147.     if (key == LEFT) portwrite("G91\nG20\nG00 X-"+to_string(speed)+" Y0.000 Z0.000\n");
  148.  
  149.    
  150.     if (key == RIGHT) portwrite("G91\nG20\nG00 X" + to_string(speed) + " Y0.000 Z0.000\n");
  151.     if (key == UP) portwrite("G91\nG20\nG00 X0.000 Y" + to_string(speed) + " Z0.000\n");
  152.     if (key == DOWN) portwrite("G91\nG20\nG00 X0.000 Y-" + to_string(speed )+ " Z0.000\n");
  153.     if (key == VK_PAGE_UP) portwrite("G91\nG20\nG00 X0.000 Y0.000 Z" + to_string(speed) + "\n");
  154.     if (key== VK_PAGE_DOWN) portwrite("G91\nG20\nG00 X0.000 Y0.000 Z-" + to_string(speed )+ "\n");
  155.     if (key == 'h') portwrite("G90\nG20\nG00 X0.000 Y0.000 Z0.000\n");
  156.     if (key == 'v') portwrite("$0=75\n$1=74\n$2=75\n");
  157.     if (key == 'v') portwrite("$0=100\n$1=74\n$2=75\n");
  158.     if (key == 's') portwrite("$3=10\n");
  159.     if (key == 'e') portwrite("$16=1\n");
  160.     if (key == 'd') portwrite("$16=0\n");
  161.     //if (key == '0') openSerialPort();
  162.     //if (key == 'p') selectSerialPort();
  163.     if (key == '$') portwrite("$$\n");
  164.   }
  165.  
  166.   if (!streaming && key == 'g') {
  167.     gcode.clear(); i = 0;
  168.     //file = NULL;
  169.     cout <<"Loading file..."<<endl<<"Select a file to process:";
  170.     cin >>fichero;
  171.     fs.open (fichero, std::fstream::in);// | std::fstream::out | std::fstream::app
  172.    
  173.    
  174. //Procesar fichero .......
  175.  
  176.     fs.close();
  177.    
  178.   }
  179.  
  180.   if (key == 'x') streaming = false;
  181. }
  182.  
  183. /***********************************************************************/
  184.     void control::stream(){
  185.         stringstream ss;
  186.         string s;
  187.  
  188.         ss >> s;
  189.        
  190.         if (!streaming) return;
  191.  
  192.         while (true) {
  193.             if (i == gcode.length()) {
  194.                 streaming = false;
  195.                 return;
  196.             }
  197.    
  198.             //if (gcode[i].trim().length() == 0) i++;//salta espacios en blanco ?????
  199.             if (gcode[i]==' '){i++;}
  200.             else break;
  201.         }
  202.  
  203.         //println(gcode[i]);
  204.         cout <<gcode[i];//Muestra el gcode
  205.        
  206.         //port.write(gcode[i] + '\n');//envia al terminal ttyACM0 el caracter
  207.         ss<<gcode[i]+'\n';
  208.         ss>>s;     
  209.         portwrite (s);     
  210.         i++;
  211. }
  212. /***********************************************************************/
  213.     string control::trim(string s){//Elimina espacios delante y detras
  214.         string::iterator it;
  215.         for (it=s.begin() ;*it== ' ' && it!=s.end();it++){}    
  216.         s.erase(s.begin(),it);
  217.        
  218.         it =s.end();it--;
  219.         while (*it--==' '){s.pop_back();}
  220.         return s;
  221.     }
  222. /***********************************************************************/
  223.     void control::serialEvent(string p){
  224.         string s;
  225.         size_t found(0);
  226.         char c;
  227.        
  228.         while ( ( c=ReadTTY() )!='\n' ){//Lee ttyACM0 hasta salto de linea
  229.             s+=ReadTTY();//Crea string s con caracteres ...
  230.         }
  231.        
  232.         s=trim(s); //procesa el string elimina espacios
  233.         cout <<s<<endl;
  234.        
  235.         found=s.find("ok");
  236.        
  237.         if (found==0 && found!=string::npos ){ stream() ;}
  238.        
  239.         found=s.find("error"); 
  240.         if (found==0 && found!=string::npos ){ stream() ;}
  241.        
  242.        
  243.         /*
  244.         string s = p.readStringUntil('\n');
  245.         println(s.trim());
  246.         if (s.trim().startsWith("ok")) stream();
  247.         if (s.trim().startsWith("error")) stream(); // XXX: really?
  248.         */
  249.        
  250.     }
  251. /***********************************************************************/
  252. /***********************************************************************/
  253. /***********************************************************************/
  254. int main (){
  255.     control miniCNC(PORTNAME);
  256.     miniCNC.Run(); 
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement