test-ui.py
1 #!/usr/bin/env python 2 3 import linuxcnc 4 import hal 5 6 import time 7 import sys 8 import os 9 import math 10 11 12 # this is how long we wait for linuxcnc to do our bidding 13 timeout = 1.0 14 15 16 def introspect(): 17 os.system("halcmd show pin python-ui") 18 19 20 def wait_for_pin_value(pin_name, value, timeout=1): 21 print "waiting for %s to go to %f (timeout=%f)" % (pin_name, value, timeout) 22 23 start = time.time() 24 while (h[pin_name] != value) and ((time.time() - start) < timeout): 25 time.sleep(0.1) 26 27 if h[pin_name] != value: 28 print "timeout! pin %s didn't get to %f" % (pin_name, value) 29 introspect() 30 sys.exit(1) 31 32 print "pin %s went to %f!" % (pin_name, value) 33 34 35 def verify_pin_value(pin_name, value): 36 if (h[pin_name] != value): 37 print "pin %s is %f, expected %f" % (pin_name, h[pin_name], value) 38 sys.exit(1); 39 40 print "pin %s is %f" % (pin_name, value) 41 42 43 def get_interp_param(param_number): 44 c.mdi("(debug, #%d)" % param_number) 45 while c.wait_complete() == -1: 46 pass 47 48 # wait up to 2 seconds for a reply 49 start = time.time() 50 while (time.time() - start) < 2: 51 error = e.poll() 52 if error == None: 53 time.sleep(0.010) 54 continue 55 56 kind, text = error 57 if kind == linuxcnc.OPERATOR_DISPLAY: 58 return float(text) 59 60 print text 61 62 print "error getting parameter %d" % param_number 63 return None 64 65 66 def verify_interp_param(param_number, expected_value): 67 param_value = get_interp_param(param_number) 68 print "interp param #%d = %f (expecting %f)" % (param_number, param_value, expected_value) 69 if math.fabs(param_value - expected_value) > 0.000001: 70 print "ERROR: interp param #%d = %f, expected %f" % (param_number, param_value, expected_value) 71 sys.exit(1) 72 73 74 def verify_stable_pin_values(pins, duration=1): 75 start = time.time() 76 while (time.time() - start) < duration: 77 for pin_name in pins.keys(): 78 val = h[pin_name] 79 if val != pins[pin_name]: 80 print "ERROR: pin %s = %f (expected %f)" % (pin_name, val, pin[pin_name]) 81 sys.exit(1) 82 time.sleep(0.010) 83 84 85 def verify_tool(tool, x, y, z, a, b, c, u, v, w, diameter, front_angle, back_angle): 86 wait_for_pin_value('tool-number', tool) 87 verify_interp_param(5400, tool) 88 89 # verify the current location, as offset by TLO 90 verify_interp_param(5420, x) 91 verify_interp_param(5421, y) 92 verify_interp_param(5422, z) 93 verify_interp_param(5423, a) 94 verify_interp_param(5424, b) 95 verify_interp_param(5425, c) 96 verify_interp_param(5426, u) 97 verify_interp_param(5427, v) 98 verify_interp_param(5428, w) 99 100 # verify tool diameter & angles 101 verify_interp_param(5410, diameter) 102 verify_interp_param(5411, front_angle) 103 verify_interp_param(5412, back_angle) 104 105 return True 106 107 108 109 110 # 111 # connect to HAL 112 # shell out to halcmd to net our pins to where they need to go 113 # 114 115 h = hal.component("python-ui") 116 117 h.newpin("tool-number", hal.HAL_S32, hal.HAL_IN) 118 119 h.ready() # mark the component as 'ready' 120 121 os.system("halcmd source ./postgui.hal") 122 123 124 # 125 # connect to LinuxCNC 126 # 127 128 c = linuxcnc.command() 129 s = linuxcnc.stat() 130 e = linuxcnc.error_channel() 131 132 c.state(linuxcnc.STATE_ESTOP_RESET) 133 c.state(linuxcnc.STATE_ON) 134 c.mode(linuxcnc.MODE_MDI) 135 c.wait_complete() 136 137 138 # 139 # this is a non-random toolchanger, so it starts with no tool in the spindle 140 # make sure the startup condition is sane 141 # 142 143 print "*** starting up, expecting T0 in spindle & no TLO" 144 145 verify_tool( 146 tool=0, 147 x=0, y=0, z=0, 148 a=0, b=0, c=0, 149 u=0, v=0, w=0, 150 diameter=0, 151 front_angle=0, 152 back_angle=0 153 ) 154 155 156 print "*** load T100 but dont apply TLO" 157 158 c.mdi('t100 m6') 159 c.wait_complete() 160 161 verify_tool( 162 tool=100, 163 x=0, y=0, z=0, 164 a=0, b=0, c=0, 165 u=0, v=0, w=0, 166 diameter=0.125, 167 front_angle=0, 168 back_angle=0 169 ) 170 171 172 print "*** apply TLO of loaded tool (T100)" 173 174 c.mdi('g43') 175 c.wait_complete() 176 177 verify_tool( 178 tool=100, 179 x=-2, y=0, z=-1, 180 a=0, b=0, c=0, 181 u=0, v=0, w=0, 182 diameter=0.125, 183 front_angle=0, 184 back_angle=0 185 ) 186 187 188 print "*** apply TLO of T200 instead" 189 190 c.mdi('g43 h200') 191 c.wait_complete() 192 193 verify_tool( 194 tool=100, 195 x=-0.2, y=0, z=-0.1, 196 a=0, b=0, c=0, 197 u=0, v=0, w=0, 198 diameter=0.125, 199 front_angle=0, 200 back_angle=0 201 ) 202 203 204 print "*** try to add in TLO with no H-word, should fail" 205 206 # first drain the error queue 207 start = time.time() 208 while (time.time() - start) < 2: 209 error = e.poll() 210 if error == None: 211 # no more queued errors, continue with test 212 break 213 214 c.mdi('g43.2') 215 c.wait_complete() 216 error = e.poll() 217 if error[1] != "G43.2: H-word missing": 218 print "G43.2 with missing H-word did not produce expected error" 219 print "got [%s]" % e.error[1] 220 sys.exit(1) 221 222 223 print "*** add in TLO of T100" 224 225 c.mdi('g43.2 h100') 226 c.wait_complete() 227 228 verify_tool( 229 tool=100, 230 x=-2.2, y=0, z=-1.1, 231 a=0, b=0, c=0, 232 u=0, v=0, w=0, 233 diameter=0.125, 234 front_angle=0, 235 back_angle=0 236 ) 237 238 239 print "*** add in TLO of T300" 240 241 c.mdi('g43.2 h300') 242 c.wait_complete() 243 244 verify_tool( 245 tool=100, 246 x=-2.22, y=0, z=-1.11, 247 a=0, b=0, c=0, 248 u=0, v=0, w=0, 249 diameter=0.125, 250 front_angle=0, 251 back_angle=0 252 ) 253 254 255 print "*** add in TLO of T400" 256 257 c.mdi('g43.2 h400') 258 c.wait_complete() 259 260 verify_tool( 261 tool=100, 262 x=-2.222, y=0, z=-1.111, 263 a=0, b=0, c=0, 264 u=0, v=0, w=0, 265 diameter=0.125, 266 front_angle=0, 267 back_angle=0 268 ) 269 270 271 print "*** add in TLO of T400 again" 272 273 c.mdi('g43.2 h400') 274 c.wait_complete() 275 276 verify_tool( 277 tool=100, 278 x=-2.224, y=0, z=-1.112, 279 a=0, b=0, c=0, 280 u=0, v=0, w=0, 281 diameter=0.125, 282 front_angle=0, 283 back_angle=0 284 ) 285 286 print "*** now let's try it rotated. first, just rotate but don't change the tlo" 287 288 c.mdi('g10 l2 p1 r33') 289 c.wait_complete() 290 291 verify_tool( 292 tool=100, 293 x=-2.224 * math.cos(math.radians(33)), y=2.224 * math.sin(math.radians(33)), z=-1.112, 294 a=0, b=0, c=0, 295 u=0, v=0, w=0, 296 diameter=0.125, 297 front_angle=0, 298 back_angle=0 299 ) 300 301 print "*** clear tlo" 302 303 c.mdi('g49') 304 c.wait_complete() 305 306 verify_tool( 307 tool=100, 308 x=-0, y=0, z=0, 309 a=0, b=0, c=0, 310 u=0, v=0, w=0, 311 diameter=0.125, 312 front_angle=0, 313 back_angle=0 314 ) 315 316 print "*** apply t100" 317 318 c.mdi('g43 h100') 319 c.wait_complete() 320 321 verify_tool( 322 tool=100, 323 x=-2*math.cos(math.radians(33)), y=2*math.sin(math.radians(33)), z=-1, 324 a=0, b=0, c=0, 325 u=0, v=0, w=0, 326 diameter=0.125, 327 front_angle=0, 328 back_angle=0 329 ) 330 331 print "*** add in t200" 332 333 c.mdi('g43.2 h200') 334 c.wait_complete() 335 336 verify_tool( 337 tool=100, 338 x=-2.2*math.cos(math.radians(33)), y=2.2*math.sin(math.radians(33)), z=-1.1, 339 a=0, b=0, c=0, 340 u=0, v=0, w=0, 341 diameter=0.125, 342 front_angle=0, 343 back_angle=0 344 ) 345 346 347 348 # if we get here it all worked! 349 sys.exit(0) 350