/ tests / motion / g0 / checkresult
checkresult
  1  #!/usr/bin/env python
  2  
  3  import sys
  4  import os
  5  from subprocess import *
  6  
  7  
  8  #
  9  # read some stuff out of the ini file
 10  #
 11  
 12  # servo period in nanoseconds
 13  servo_period_ns = float(Popen(
 14      [
 15          "inivar",
 16          "-var",
 17          "SERVO_PERIOD",
 18          "-sec",
 19          "EMCMOT",
 20          "-ini",
 21          "%s/tests/motion/g0/motion-test.ini" % os.getenv("EMC2_HOME")
 22      ],
 23      stdout=PIPE
 24  ).communicate()[0])
 25  
 26  cycles_per_second = 1000000000.0 / servo_period_ns
 27  
 28  # max acceleration in inches per second^2
 29  max_acceleration_ips2 = float(Popen(
 30      [
 31          "inivar",
 32          "-var",
 33          "MAX_ACCELERATION",
 34          "-sec",
 35          "AXIS_0",
 36          "-ini",
 37          "%s/tests/motion/g0/motion-test.ini" % os.getenv("EMC2_HOME")
 38      ],
 39      stdout=PIPE
 40  ).communicate()[0])
 41  
 42  # max acceleration in inches per second per servo-thread cycle
 43  # the factor of half at the end is because emc2 reserves half the acceleration for blending
 44  max_acceleration_ipspc = (max_acceleration_ips2 / cycles_per_second) * 0.5
 45  
 46  # max velocity in inches per second
 47  max_velocity_ips = float(Popen(
 48      [
 49          "inivar",
 50          "-var",
 51          "MAX_VELOCITY",
 52          "-sec",
 53          "AXIS_0",
 54          "-ini",
 55          "%s/tests/motion/g0/motion-test.ini" % os.getenv("EMC2_HOME")
 56      ],
 57      stdout=PIPE
 58  ).communicate()[0])
 59  
 60  max_velocity_ipc = max_velocity_ips / cycles_per_second
 61  
 62  
 63  # read the samples file and turn it into an array, where index i is an
 64  # array of the floats found on line i of the samples file
 65  samples_filename = sys.argv[1] + ".halsamples"
 66  lines = file(samples_filename).readlines()
 67  samples = []
 68  for line in lines:
 69      s = [ float(x) for x in line.split() ]
 70      samples.append(s)
 71  
 72  
 73  #
 74  # here comes the test proper
 75  #
 76  
 77  
 78  # verify that X starts at 0.000000
 79  if samples[0][1] != 0.000000:
 80      print "sample 0: X starts at %.6f, not at 0.000000" % samples[0][1]
 81      sys.exit(1)
 82  
 83  print "line 0: X starts at 0.000000"
 84  
 85  
 86  # find where X starts moving (X is the second column, column 1)
 87  i = 0
 88  while samples[i][1] == samples[i+1][1]:
 89      i += 1
 90  
 91  print "line %d: accel phase starts" % i
 92  
 93  
 94  # now i is the sample before it starts moving
 95  # verify accel does not exceed maxaccel
 96  # verify vel does not exceed maxvel
 97  # verify accel phase stops when vel == maxvel
 98  highest_seen_accel_ipc2 = 0
 99  old_accel_ipc2 = 0
100  old_v_ipc = 0
101  for i in range(i, len(samples)):
102  
103      new_v_ipc = samples[i+1][1] - samples[i][1]
104      accel_ipc2 = new_v_ipc - old_v_ipc
105  
106      highest_seen_accel_ipc2 = max(accel_ipc2, highest_seen_accel_ipc2)
107  
108      #print "line %d: X=%.6f, v=%.6f i/c (%.6f i/s), a=%.6f i/c^2 (%.6f i/s^2)" % (i, samples[i][1], new_v_ipc, (new_v_ipc * cycles_per_second), accel_ipc2, (accel_ipc2 * cycles_per_second * cycles_per_second))
109  
110      # i hate floating point
111      if ((accel_ipc2 * cycles_per_second) - max_acceleration_ipspc) > 0.0000001: 
112          print "line %d: detected accel constraint violation!" % i
113          print "detected accel %.6f i/c^2 (%.6f i/s^2)" % (accel_ipc2, (accel_ipc2 * cycles_per_second * cycles_per_second))
114          print "max accel %.6f i/s^2)" % max_acceleration_ips2
115          sys.exit(1)
116  
117      if (new_v_ipc - max_velocity_ipc) > 0.0000001:
118          print "line %d: detected vel constraint violation!" % i
119          print "detected vel %.6f i/c (%.6f i/s)" % (new_v_ipc, new_v_ipc * cycles_per_second)
120          print "max vel %.6f i/c (%.6f i/s)" % (max_velocity_ipc, max_velocity_ips)
121          sys.exit(1)
122  
123      if accel_ipc2 == 0:
124          print "line %d: accel phase over" % i
125          break
126  
127      old_v_ipc = new_v_ipc
128  
129  # verify highest seen accel is very close to max accel
130  if abs(max_acceleration_ipspc - (highest_seen_accel_ipc2 * cycles_per_second)) > 0.0000001:
131      print "accel only reached %.6f i/c^2 (%.6f i/s^2)" % (highest_seen_accel_ipc2, (highest_seen_accel_ipc2 * cycles_per_second * cycles_per_second))
132      print "max accel is %.6f i/s^2" % max_acceleration_ips2
133      sys.exit(1)
134  
135  print "    accel reached but did not exceed 1/2 of max accel of %.6f i/s^2" % max_acceleration_ips2
136  
137  print "line %d: entering cruise phase, vel=%.6f i/s, max_accel = %.6f i/s^2" % (i, new_v_ipc * cycles_per_second, highest_seen_accel_ipc2 * cycles_per_second * cycles_per_second)
138  
139  
140  # now i is the first sample of the cruise phase
141  # wait for vel to drop again
142  for i in range(i, len(samples)):
143      new_v_ipc = samples[i+1][1] - samples[i][1]
144  
145      if abs(new_v_ipc - old_v_ipc) > 0.0000001:
146          # decel phase starting
147          break
148  
149      old_v_ipc = new_v_ipc
150  
151  print "line %d: decel phase starting, old_v=%.6f i/s, new_v=%.6f i/s" % (i, old_v_ipc * cycles_per_second, new_v_ipc * cycles_per_second)
152  
153  # now i is the sample before it starts decel
154  # verify accel does not exceed maxaccel
155  # verify vel does not exceed maxvel
156  # verify decel phase stops when vel == 0
157  highest_seen_accel_ipc2 = 0
158  old_accel_ipc2 = 0
159  for i in range(i, len(samples)):
160  
161      new_v_ipc = samples[i+1][1] - samples[i][1]
162      accel_ipc2 = new_v_ipc - old_v_ipc
163  
164      highest_seen_accel_ipc2 = min(accel_ipc2, highest_seen_accel_ipc2)
165  
166      #print "line %d: X=%.6f, v=%.6f i/c (%.6f i/s), a=%.6f i/c^2 (%.6f i/s^2)" % (i, samples[i][1], new_v_ipc, (new_v_ipc * cycles_per_second), accel_ipc2, (accel_ipc2 * cycles_per_second * cycles_per_second))
167  
168      # i hate floating point
169      if ((accel_ipc2 * cycles_per_second) - max_acceleration_ipspc) > 0.0000001: 
170          print "line %d: detected accel constraint violation!" % i
171          print "detected accel %.6f i/c^2 (%.6f i/s^2)" % (accel_ipc2, accel_ipc2 * cycles_per_second * cycles_per_second)
172          print "max accel %.6f i/s^2)" % max_acceleration_ips2
173          sys.exit(1)
174  
175      if (new_v_ipc - max_velocity_ipc) > 0.0000001:
176          print "line %d: detected vel constraint violation!" % i
177          print "detected vel %.6f i/c (%.6f i/s)" % (new_v_ipc, new_v_ipc * cycles_per_second)
178          print "max vel %.6f i/c (%.6f i/s)" % (max_velocity_ipc, max_velocity_ips)
179          sys.exit(1)
180  
181      if new_v_ipc < -0.0000001:
182          print "line %d: detected vel undershoot!" % i
183          print "detected vel %.6f i/c (%.6f i/s)" % (new_v_ipc, new_v_ipc * cycles_per_second)
184          sys.exit(1)
185  
186      if new_v_ipc == 0:
187          print "line %d: decel phase over" % i
188          break
189  
190      old_v_ipc = new_v_ipc
191  
192  # verify highest seen accel is very close to max accel
193  if abs(max_acceleration_ipspc + (highest_seen_accel_ipc2 * cycles_per_second)) > 0.0000001:
194      print "accel only reached %.6f i/c^2 (%.6f i/s^2)" % (highest_seen_accel_ipc2, (highest_seen_accel_ipc2 * cycles_per_second * cycles_per_second))
195      print "max accel is %.6f i/s^2" % max_acceleration_ips2
196      sys.exit(1)
197  
198  print "    decel reached but did not exceed 1/2 of max accel of %.6f i/s^2" % max_acceleration_ips2
199  
200  
201  # verify X stopped at 1.000000
202  if samples[i][1] != 1.000000:
203      print "line %d: X stopped at %.6f, not at 1.000000!" % (i, samples[i][1])
204      sys.exit(1)
205  
206  print "    X reached target of 1.0000"
207  
208  
209  # verify X doesn't move from now on
210  for i in range(i, len(samples) - 1):
211      if samples[i][1] != samples[i+1][1]:
212          print "line %d: X moved!" % i
213          sys.exit(1)
214  
215  
216  print "line %d: X did not move after reaching target" % i
217  
218  print "success!\n"
219  sys.exit(0)
220  
221