SHARE
TWEET

Gcode_executer.py

a guest Jan 2nd, 2015 28 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RPi.GPIO as GPIO
  2. import Motor_control
  3. from Bipolar_Stepper_Motor_Class import Bipolar_Stepper_Motor
  4. import time
  5. from numpy import pi, sin, cos, arccos, arcsin
  6. from math import sqrt
  7.  
  8. filename='yourfile.nc' #file name of the G code commands
  9.  
  10. GPIO.setmode(GPIO.BOARD)
  11.  
  12. MX=Bipolar_Stepper_Motor(13,18,1234,170) #takes step pin,dir pin,limit pin,backlash (optional)
  13.  
  14. MY=Bipolar_Stepper_Motor(7,11,1234,670) # the limit switches are currently unimplemented
  15.  
  16. Laser_switch=15
  17.  
  18. dx=0.00078 #resolution in x direction. Unit: mm
  19. dy=0.00078 #resolution in y direction. Unit: mm
  20.  
  21. Engraving_speed=1 #unit=mm/sec=0.04in/sec
  22.  
  23. GPIO.setup(Laser_switch,GPIO.OUT)
  24.  
  25. GPIO.output(Laser_switch,0)
  26.  
  27. speed=Engraving_speed/min(dx,dy)      #step/sec
  28.  
  29. def XYposition(lines):
  30.     #given a movement command line, return the X Y position
  31.     xchar_loc=lines.index('X')
  32.     i=xchar_loc+1
  33.     while (47<ord(lines[i])<58)|(lines[i]=='.')|(lines[i]=='-'):
  34.         i+=1
  35.     x_pos=float(lines[xchar_loc+1:i])
  36.    
  37.     ychar_loc=lines.index('Y')
  38.     i=ychar_loc+1
  39.     while (47<ord(lines[i])<58)|(lines[i]=='.')|(lines[i]=='-'):
  40.         i+=1
  41.     y_pos=float(lines[ychar_loc+1:i])
  42.  
  43.     return x_pos,y_pos
  44.  
  45. def IJposition(lines):
  46.     #given a G02 or G03 movement command line, return the I J position
  47.     ichar_loc=lines.index('I')
  48.     i=ichar_loc+1
  49.     while (47<ord(lines[i])<58)|(lines[i]=='.')|(lines[i]=='-'):
  50.         i+=1
  51.     i_pos=float(lines[ichar_loc+1:i])
  52.    
  53.     jchar_loc=lines.index('J')
  54.     i=jchar_loc+1
  55.     while (47<ord(lines[i])<58)|(lines[i]=='.')|(lines[i]=='-'):
  56.         i+=1
  57.     j_pos=float(lines[jchar_loc+1:i])
  58.  
  59.     return i_pos,j_pos
  60.  
  61. def moveto(MX,x_pos,dx,MY,y_pos,dy,speed,engraving):
  62. #Move to (x_pos,y_pos) (in real unit)
  63.     stepx=int(round(x_pos/dx))-MX.position
  64.     stepy=int(round(y_pos/dy))-MY.position
  65.  
  66.     Total_step=sqrt((stepx**2+stepy**2))
  67.            
  68.     if Total_step>0:
  69.         if lines[0:3]=='G0 ': #fast movement
  70.             print 'No Laser, fast movement: Dx=', stepx, '  Dy=', stepy
  71.             Motor_control.Motor_Step(MX,stepx,MY,stepy,3000)
  72.         else:
  73. ##            print 'Laser on, movement: Dx=', stepx, '  Dy=', stepy
  74.             Motor_control.Motor_Step(MX,stepx,MY,stepy,speed)
  75.     return 0
  76.  
  77. try:#read and execute G code
  78.     for lines in open(filename,'r'):
  79.         if lines==[]:
  80.             1 #blank lines
  81.         elif lines[0:3]=='G90':
  82.             print 'start'
  83.            
  84.         elif lines[0:3]=='G20':# working in inch;
  85.             dx/=25.4
  86.             dy/=25.4
  87.             print 'Working in inch';
  88.              
  89.         elif lines[0:3]=='G21':# working in mm;
  90.             print 'Working in mm'
  91.            
  92.         elif lines[0:3]=='M05':
  93.             GPIO.output(Laser_switch,0)
  94.             print 'Laser turned off'
  95.            
  96.         elif lines[0:3]=='M03':
  97.             GPIO.output(Laser_switch,1)
  98.             print 'Laser turned on'
  99.  
  100.         elif lines[0:3]=='M02':
  101.             GPIO.output(Laser_switch,0)
  102.             print 'Finished! Shutting down...'
  103.             break
  104.         elif (lines[0:3]=='G1F')|(lines[0:4]=='G1 F'):
  105.             1#do nothing
  106.         elif (lines[0:3]=='G0 ')|(lines[0:3]=='G1 ')|(lines[0:3]=='G01'):#|(lines[0:3]=='G02')|(lines[0:3]=='G03'):
  107.             #linear engraving movement
  108.             if (lines[0:3]=='G0 '):
  109.                 engraving=False
  110.             else:
  111.                 engraving=True
  112.                
  113.             [x_pos,y_pos]=XYposition(lines)
  114.             moveto(MX,x_pos,dx,MY,y_pos,dy,speed,engraving)
  115.            
  116.         elif (lines[0:3]=='G02')|(lines[0:3]=='G03'): #circular interpolation
  117.             old_x_pos=x_pos
  118.             old_y_pos=y_pos
  119.  
  120.             [x_pos,y_pos]=XYposition(lines)
  121.             [i_pos,j_pos]=IJposition(lines)
  122.  
  123.             xcenter=old_x_pos+i_pos   #center of the circle for interpolation
  124.             ycenter=old_y_pos+j_pos
  125.            
  126.            
  127.             Dx=x_pos-xcenter
  128.             Dy=y_pos-ycenter      #vector [Dx,Dy] points from the circle center to the new position
  129.            
  130.             r=sqrt(i_pos**2+j_pos**2)   # radius of the circle
  131.            
  132.             e1=[-i_pos,-j_pos] #pointing from center to current position
  133.             if (lines[0:3]=='G02'): #clockwise
  134.                 e2=[e1[1],-e1[0]]     #perpendicular to e1. e2 and e1 forms x-y system (clockwise)
  135.             else:                   #counterclockwise
  136.                 e2=[-e1[1],e1[0]]      #perpendicular to e1. e1 and e2 forms x-y system (counterclockwise)
  137.  
  138.             #[Dx,Dy]=e1*cos(theta)+e2*sin(theta), theta is the open angle
  139.  
  140.             costheta=(Dx*e1[0]+Dy*e1[1])/r**2;
  141.             sintheta=(Dx*e2[0]+Dy*e2[1])/r**2;        #theta is the angule spanned by the circular interpolation curve
  142.                
  143.             if costheta>1:  # there will always be some numerical errors! Make sure abs(costheta)<=1
  144.                 costheta=1;
  145.             elif costheta<-1:
  146.                 costheta=-1;
  147.  
  148.             theta=arccos(costheta);
  149.             if sintheta<0:
  150.                 theta=2.0*pi-theta;
  151.  
  152.             no_step=int(round(r*theta/dx/5.0));   # number of point for the circular interpolation
  153.            
  154.             for i in range(1,no_step+1):
  155.                 tmp_theta=i*theta/no_step;
  156.                 tmp_x_pos=xcenter+e1[0]*cos(tmp_theta)+e2[0]*sin(tmp_theta);
  157.                 tmp_y_pos=ycenter+e1[1]*cos(tmp_theta)+e2[1]*sin(tmp_theta);
  158.                 moveto(MX,tmp_x_pos,dx,MY, tmp_y_pos,dy,speed,True);
  159.        
  160. except KeyboardInterrupt:
  161.     pass
  162.  
  163. GPIO.output(Laser_switch,0);   # turn off laser
  164. moveto(MX,0,dx,MY,0,dy,3000,False);  # move back to Origin
  165.  
  166. GPIO.cleanup();
RAW Paste Data
Top