/ tests / trajectory-planner / circular-arcs / linuxcnc_control.py
linuxcnc_control.py
  1  #!/usr/bin/env python
  2  '''Copied from m61-test'''
  3  
  4  import linuxcnc
  5  import os
  6  from time import sleep
  7  
  8  
  9  # this is how long we wait for linuxcnc to do our bidding
 10  
 11  class LinuxcncError(Exception):
 12      pass
 13  #    def __init__(self, value):
 14  #        self.value = value
 15  #    def __str__(self):
 16  #        return repr(self.value)
 17  
 18  class LinuxcncControl:
 19      '''
 20      issue G-Code commands
 21      make sure important modes are saved and restored
 22      mode is saved only once, and can be restored only once
 23  
 24      usage example:
 25          e = emc_control()
 26          e.prepare_for_mdi()
 27              any internal sub using e.g("G0.....")
 28          e.finish_mdi()
 29  
 30      '''
 31  
 32      def __init__(self,timeout=2):
 33          self.c = linuxcnc.command()
 34          self.e = linuxcnc.error_channel()
 35          self.s = linuxcnc.stat()
 36          self.timeout = timeout
 37  
 38      def running(self, do_poll=True):
 39          '''
 40          check wether interpreter is running.
 41          If so, cant switch to MDI mode.
 42          '''
 43          if do_poll:
 44              self.s.poll()
 45          return (self.s.task_mode == linuxcnc.MODE_AUTO and
 46                  self.s.interp_state != linuxcnc.INTERP_IDLE)
 47  
 48      def set_mode(self,m):
 49          '''
 50          set EMC mode if possible, else throw LinuxcncError
 51          return current mode
 52          '''
 53          self.s.poll()
 54          if self.s.task_mode == m :
 55              return m
 56          if self.running(do_poll=False):
 57              raise LinuxcncError("interpreter running - cant change mode")
 58          self.c.mode(m)
 59          self.c.wait_complete()
 60  
 61          return m
 62  
 63      def set_state(self,m):
 64          '''
 65          set EMC mode if possible, else throw LinuxcncError
 66          return current mode
 67          '''
 68          self.s.poll()
 69          if self.s.task_mode == m :
 70              return m
 71          self.c.state(m)
 72          self.c.wait_complete(self.timeout)
 73          return m
 74  
 75      def do_home(self,axismask):
 76          self.s.poll()
 77          self.c.home(axismask)
 78          self.c.wait_complete(self.timeout)
 79  
 80  
 81      def ok_for_mdi(self):
 82          '''
 83          check wether ok to run MDI commands.
 84          '''
 85          self.s.poll()
 86          return not self.s.estop and self.s.enabled and self.s.homed
 87  
 88      def prepare_for_mdi(self):
 89          '''
 90          check wether ok to run MDI commands.
 91          throw  LinuxcncError if told so.
 92          return current mode
 93          '''
 94  
 95          self.s.poll()
 96          if self.s.estop:
 97              raise LinuxcncError("machine in ESTOP")
 98  
 99          if not self.s.enabled:
100              raise LinuxcncError("machine not enabled")
101  
102          if not self.s.homed:
103              raise LinuxcncError("machine not homed")
104  
105          if self.running():
106              raise LinuxcncError("interpreter not idle")
107  
108          return self.set_mode(linuxcnc.MODE_MDI)
109  
110      g_raise_except = True
111  
112      def g(self,code,wait=False):
113          '''
114          issue G-Code as MDI command.
115          wait for completion if reqested
116          '''
117  
118          self.c.mdi(code)
119          if wait:
120              try:
121                  while self.c.wait_complete(self.timeout) == -1:
122                      pass
123                  return True
124              except KeyboardInterrupt:
125                  print "interrupted by keyboard in c.wait_complete(self.timeout)"
126                  return False
127  
128          self.error = self.e.poll()
129          if self.error:
130              kind, text = self.error
131              if kind in (linuxcnc.NML_ERROR, linuxcnc.OPERATOR_ERROR):
132                  if self.g_raise_except:
133                      raise LinuxcncError(text)
134                  else:
135                      print ("error " + text)
136              else:
137                  print ("info " + text)
138          return False
139  
140      def get_current_tool(self):
141          self.s.poll()
142          return self.s.tool_in_spindle
143  
144      def active_codes(self):
145          self.e.poll()
146          return self.s.gcodes
147  
148      def get_current_system(self):
149          g = self.active_codes()
150          for i in g:
151                  if i >= 540 and i <= 590:
152                          return i/10 - 53
153                  elif i >= 590 and i <= 593:
154                          return i - 584
155          return 1
156  
157      def open_program(self,filename):
158          '''Open an nc file'''
159          self.s.poll()
160          self.set_mode(linuxcnc.MODE_AUTO)
161          self.c.wait_complete()
162          sleep(.25)
163          self.c.program_open(filename)
164          self.c.wait_complete()
165  
166      def run_full_program(self):
167          '''Start a loaded program'''
168          self.s.poll()
169          self.c.auto(linuxcnc.AUTO_RUN, 0)
170          self.c.wait_complete(self.timeout)
171          return self.check_rcs_error()
172  
173      def set_feed_scale(self,scale):
174          '''Assign a feed scale'''
175  
176          self.s.poll()
177          self.c.feedrate(scale)
178          self.c.wait_complete(self.timeout)
179  
180      def wait_on_program(self):
181          self.s.poll()
182          while self.s.exec_state != linuxcnc.EXEC_DONE or self.s.state != linuxcnc.RCS_DONE and self.s.task_state == linuxcnc.STATE_ON:
183              sleep(.25)
184              self.s.poll()
185              if self.s.task_state != linuxcnc.STATE_ON:
186                  return False
187              if self.check_rcs_error():
188                  print "Found RCS error while waiting, running again"
189                  self.run_full_program()
190  
191          return True
192  
193      def check_rcs_error(self):
194          self.s.poll()
195          if self.s.state == linuxcnc.RCS_ERROR:
196              print "detected RCS error"
197              return True
198          return False
199  
200  def introspect():
201      os.system("halcmd show pin python-ui")