def run_test(self, uL=None, uR=None): # the trick is I want to turn for less time than I go forward # or backward # # - I need to think abou the cherrypy interface for this # - I think I want to specify N to manage run time if uL is None: uL = self.uL if uR is None: uR = self.uR serial_utils.WriteByte(self.ser, 2) #start new test check_2 = serial_utils.Read_Byte(self.ser) N = len(uL) self.stopn = N nvect = zeros(N, dtype=int) numsensors = self.numsensors sensor_mat = zeros((N, numsensors)) error = zeros_like(nvect) self.nvect = nvect self.uL = uL self.uR = uR self.sensor_mat = sensor_mat self.error = error for i in range(N): serial_utils.WriteByte(self.ser, 1) #new n and voltage are coming serial_utils.WriteInt(self.ser, i) serial_utils.WriteInt(self.ser, uL[i]) serial_utils.WriteInt(self.ser, uR[i]) nvect[i] = serial_utils.Read_Two_Bytes(self.ser) for j in range(numsensors): sensor_mat[i, j] = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) error[i] = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) nl_check = serial_utils.Read_Byte(self.ser) assert nl_check == 10, "newline problem" serial_utils.WriteByte(self.ser, 3) #stop test check_3 = serial_utils.Read_Byte(self.ser) print('check_3 = ' + str(check_3)) self.nvect = nvect self.sensor_mat = sensor_mat self.error = error return nvect, sensor_mat, error
def run_test(self, uL=None, uR=None): if uL is None: uL = self.uL if uR is None: uR = self.uR serial_utils.WriteByte(self.ser, 2) #start new test check_2 = serial_utils.Read_Byte(self.ser) N = len(uL) self.stopn = N nvect = zeros(N, dtype=int) numsensors = self.numsensors sensor_mat = zeros((N, numsensors)) error = zeros_like(nvect) self.nvect = nvect self.uL = uL self.uR = uR self.sensor_mat = sensor_mat self.error = error for i in range(N): serial_utils.WriteByte(self.ser, 1) #new n and voltage are coming serial_utils.WriteInt(self.ser, i) serial_utils.WriteInt(self.ser, uL[i]) serial_utils.WriteInt(self.ser, uR[i]) nvect[i] = serial_utils.Read_Two_Bytes(self.ser) for j in range(numsensors): sensor_mat[i, j] = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) error[i] = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) nl_check = serial_utils.Read_Byte(self.ser) assert nl_check == 10, "newline problem" serial_utils.WriteByte(self.ser, 3) #stop test check_3 = serial_utils.Read_Byte(self.ser) print('check_3 = ' + str(check_3)) self.nvect = nvect self.sensor_mat = sensor_mat self.error = error return nvect, sensor_mat, error
def open_and_check_serial(self): self._import_ser() self.ser_check_count += 1 serial_utils.WriteByte(self.ser, 0) time.sleep(0.1) if self.ser.inWaiting(): debug_line = serial_utils.Read_Line(self.ser) line_str = ''.join(debug_line) return line_str else: return None
def open_serial(self): # check if it is open first from myserial import ser time.sleep(3.0) # <--- will this work under windows? # does this work better with a Z32U4 # if it isn't rebooting? # could I put this in a while loop rather than the hard 3.0 sleep? # - if you don't get a response, the Uno is ready yet # - I could set up another Uno serial case that responds with just one byte # to acknowledge wake up serial_utils.WriteByte(ser, 0) debug_line = serial_utils.Read_Line(ser) line_str = ''.join(debug_line) self.ser = ser return line_str
def get_error(self): serial_utils.WriteByte(self.ser, 5) e_out = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) return e_out
def calibrate(self): serial_utils.WriteByte(self.ser, 4) #start new test check_4 = serial_utils.Read_Byte(self.ser) return check_4
def run_test(self): serial_utils.WriteByte(self.ser, 2) #start new test check_2 = serial_utils.Read_Byte(self.ser) u = self.calc_u() self.ref = u N = len(u) self._init_vectors(N) self.stopn = -1 stopping = False t1 = time.time() t2 = None for i in range(N): self.tracking_error[i] = self.ref[i] - self.error[i - 1] if i > 0: vdiff = self.calc_v(i) #, self.tracking_error) else: vdiff = 0 self.vdiff_vect[i] = vdiff if stopping: self.uL[i] = 0 self.uR[i] = 0 else: self.uL[i] = self.mysat(self.nominal_speed - vdiff) self.uR[i] = self.mysat(self.nominal_speed + vdiff) # do I organize this into sub-methods and actually stop the test # if we are back to the finish line, or do I just sit there # sending 0's for speed and reading the same stopped data? serial_utils.WriteByte(self.ser, 1) #new n and voltage are coming serial_utils.WriteInt(self.ser, i) serial_utils.WriteInt(self.ser, self.uL[i]) serial_utils.WriteInt(self.ser, self.uR[i]) self.nvect[i] = serial_utils.Read_Two_Bytes(self.ser) for j in range(self.numsensors): self.sensor_mat[i, j] = serial_utils.Read_Two_Bytes_Twos_Comp( self.ser) ## if i > 100: ## #check for completed lap ## if sensor_mat[i,0] > 500 and sensor_mat[i,-1] > 500: ## #lap completed ## self.stopn = i ## t2 = time.time() ## stopping = True self.error[i] = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) nl_check = serial_utils.Read_Byte(self.ser) assert nl_check == 10, "newline problem" serial_utils.WriteByte(self.ser, 3) #stop test check_3 = serial_utils.Read_Byte(self.ser) print('check_3 = ' + str(check_3)) self.stopn = N self.laptime = 999.999 e_trunc = self.error[0:self.stopn] self.total_e = abs(e_trunc).sum() return self.nvect, self.sensor_mat, self.error
def _write_one_speed(self, vL, vR, n=2): serial_utils.WriteByte(self.ser, 1) #new n and voltage are coming serial_utils.WriteInt(self.ser, n) serial_utils.WriteInt(self.ser, vL) serial_utils.WriteInt(self.ser, vR)
def run_test(self, N=None): if N is None: if hasattr(self, 'N'): N = int(self.N) else: N = 500 serial_utils.WriteByte(self.ser, 2) #start new test check_2 = serial_utils.Read_Byte(self.ser) self._init_vectors(N) self.stopn = -1 stopping = False post_stop_count = -1 t1 = time.time() t2 = None for i in range(N): if i > 0: vdiff = self.calc_v(i - 1) else: vdiff = 0 if stopping: self.uL[i] = 0 self.uR[i] = 0 post_stop_count += 1 print('post_stop_count = %i' % post_stop_count) if post_stop_count > 10: break else: self.uL[i] = self.mysat(self.nominal_speed + vdiff) self.uR[i] = self.mysat(self.nominal_speed - vdiff) # do I organize this into sub-methods and actually stop the test # if we are back to the finish line, or do I just sit there # sending 0's for speed and reading the same stopped data? serial_utils.WriteByte(self.ser, 1) #new n and voltage are coming serial_utils.WriteInt(self.ser, i) serial_utils.WriteInt(self.ser, self.uL[i]) serial_utils.WriteInt(self.ser, self.uR[i]) self.nvect[i] = serial_utils.Read_Two_Bytes(self.ser) for j in range(self.numsensors): self.sensor_mat[i, j] = serial_utils.Read_Two_Bytes_Twos_Comp( self.ser) if i > 50: #check for completed lap if (not stopping) and (self.sensor_mat[i, 0] > 500 and self.sensor_mat[i, -1] > 500): #lap completed self.stopn = i t2 = time.time() stopping = True post_stop_count = 1 print('stopn = %i' % self.stopn) self.error[i] = serial_utils.Read_Two_Bytes_Twos_Comp(self.ser) nl_check = serial_utils.Read_Byte(self.ser) assert nl_check == 10, "newline problem" serial_utils.WriteByte(self.ser, 3) #stop test check_3 = serial_utils.Read_Byte(self.ser) print('check_3 = ' + str(check_3)) if t2 is not None: self.laptime = t2 - t1 else: self.laptime = 999.999 e_trunc = self.error[0:self.stopn] self.total_e = abs(e_trunc).sum() return self.nvect, self.sensor_mat, self.error