def backup_table(q_values, q_values_file=Q_VALUES_FILE): logger = Logger() q_values_file = logger.create(q_values_file) for state in q_values: actions = "" for action in q_values[state]: actions = actions + str(action) + " " logger.append(q_values_file, state + ":" + actions) logger.append(q_values_file, "TOTAL ANALYZED STATES: " + str(len(q_values))) logger.close(q_values_file)
def main(): root_path = sys.path[0] # create folder to save ext history download count report stats_his_folder = os.path.join(root_path, 'stats_ext_history') if not os.path.isdir(stats_his_folder): os.mkdir(stats_his_folder) modeler_his_folder = os.path.join(root_path, 'modeler_ext_history') if not os.path.isdir(modeler_his_folder): os.mkdir(modeler_his_folder) # create folder to save R essential history download count report r_essential_his_folder = os.path.join(root_path, 'r_essential_history') if not os.path.isdir(r_essential_his_folder): os.mkdir(r_essential_his_folder) # create log file log_folder = os.path.join(root_path, LOG_NAME) if not os.path.isdir(log_folder): os.mkdir(log_folder) # name csv file stats_csv_file = 'stats_download_count.csv' modeler_csv_file = 'modeler_download_count.csv' r_essential_file = 'r_essential_download_count.csv' try: # generate logger main_logger = Logger(os.path.join(log_folder, LOG_NAME), 'main_logger') create_csv_file(INDEX_PACKAGE_URL['stats'], stats_csv_file, stats_his_folder, main_logger) create_csv_file(INDEX_PACKAGE_URL['modeler'], modeler_csv_file, modeler_his_folder, main_logger) r_essential_list = {'R_Essentials_Statistics': 'SPSS_Statistics_R_Essentials', 'R_Essentials_Modeler': 'SPSS_Modeler_R_Essentials'} create_r_essentials_dn_count_file(r_essential_file, r_essential_his_folder, r_essential_list, main_logger) exit(0) except Exception: main_logger.error(traceback.print_exc()) exit(1) finally: main_logger.info("Download Count Report execution completed!") main_logger.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument('-sc', type=str, help='score file') parser.add_argument('-percent', type=float, default=5, help='percent (1-100) best scoring to get') parser.add_argument('-filter', type=str, default='score', help='filter or score term to use') parser.add_argument('-num', default=10, type=int, help='use if you want a number of results, not better than percentile') parser.add_argument('-mode', default='%') parser.add_argument('-over_under', type=str, default='under', help='under/over score should be over/under threshold') parser.add_argument('-result', type=str, default=None, help='should the names be written to a file separate from the log file') args = vars(parser.parse_args()) logger = Logger('top_%.1f_%s.log' % (args['percent'], args['filter'])) # read in the score file, determine the threshold for the percentile sc_df = Rf.score_file2df(args['sc']) score = sc_df[args['filter']] if args['mode'] == '%': threshold = np.percentile(score, args['percent']) logger.log('found a threshold for %f for filter %s to be %.2f' % (args['percent'], args['filter'], threshold)) # create a df for lines that pass the threshold, either over or above it... if args['over_under'] == 'over': pass_df = sc_df[sc_df[args['filter']] >= threshold] elif args['over_under'] == 'under': pass_df = sc_df[sc_df[args['filter']] <= threshold] if args['mode'] == 'num': sc_df.sort_values(args['filter'], inplace=True) pass_df = sc_df.head(args['num']) # output the names (description) of models that pass the threshold, either to the logger file, or to a separate file if args['result'] is None: logger.create_header('models passing the threshold:') for name in pass_df['description']: logger.log(name, skip_stamp=True) else: with open(args['result'], 'w+') as fout: for name in pass_df['description']: fout.write(name + '\n') logger.close()
def log(filtered): current_time = time.ctime() for h in filtered: logger.write_log(current_time, h) while (True): # Capture frame-by-frame ret, frame = cap.read() if not ret: break out, visible = image_processor.process_frame(frame) if len(visible) != 0: log(visible) cv2.imwrite(get_file_name(), frame) cv2.imshow('frame', out) writer.write(out) if cv2.waitKey(5) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() writer.release() cv2.destroyAllWindows() logger.close()
def q_learning(gamma=GAMMA, alfa=ALFA, episodes=EPISODES): agent = QLAgent() logger = Logger() stats = logger.create(STATS_FILE) logger.append(stats, "total: " + str(episodes)) logger.append(stats, "episode, timesteps, time_elapsed, wrong_moves") episode = 0 while episode < episodes: episode += 1 timestep = 0 wrong_moves = 0 start = int(round(time.time() * 1000)) # if episode & 1000 == 0 and agent.epsilon > 0.1: # agent.epsilon -= 0.1 agent.puzzle.reset() print("Episode: {} started at {}".format(episode, datetime.now())) # file = logger.create(EPISODE_FILE_NAME.format(episode)) done = False while not done: current_state = copy_matrix( agent.puzzle.state, np.zeros((agent.puzzle.rows, agent.puzzle.columns))) action = agent.next_action(current_state) next_state, reward, done = agent.puzzle.step(action) if reward == -10: wrong_moves += 1 # logger.append(file, "TIMESTEP -> " + str(timestep)) # logger.append(file, "CURRENT STATE ->\n" + str(current_state)) # logger.append(file, "ACTION -> " + str(action)) # logger.append(file, "NEXT_STATE -> \n" + str(next_state)) # logger.append(file, "REWARD -> " + str(reward)) # logger.append(file, "\n") timestep += 1 if not done: current_q_value = agent.get_action_values( current_state)[action] next_q_values = agent.get_action_values(next_state) agent.q_values[key( current_state )][action] = current_q_value + alfa * ( reward + gamma * np.max(next_q_values) - current_q_value) else: agent.q_values[key( current_state)][action] = current_q_value + alfa * ( reward - current_q_value) break agent.visits[key( current_state)] = agent.visits[key(current_state)] + 1 end = int(round(time.time() * 1000)) # logger.append(file, "FINAL STATE: \n" + str(agent.puzzle.state) + "\n") # logger.close(file) logger.append(stats, str(episode)) logger.append(stats, str(timestep)) logger.append(stats, str(end - start)) logger.append(stats, str(wrong_moves)) logger.close(stats) backup_table(agent.q_values) backup_visits(agent.visits)
def backup_visits(visits, visits_file=VISITS_FILE): logger = Logger() visit_file = logger.create(visits_file) for state in visits: logger.append(visit_file, state + ":" + str(visits[state])) logger.close(visit_file)
class Model: """ A generic model. Manages generating data and recording for one scenario replication. """ def __init__(self, experiment_name, scenario_name, run_id, scratch_path): self.id_counter = itertools.count() self.experiment_name = experiment_name self.scenario_name = scenario_name self.run_id = run_id # set up a logger to use - only need one per base_model self.logger = Logger(experiment_name, scenario_name, run_id, scratch_path) self.step_counter = itertools.count() self.current_step = next(self.step_counter) self._total_steps = None self._total_days = None # true if the model has started but not ended self.started = False @property def total_days(self): return self._total_days @total_days.setter def total_days(self, value): # only get bigger if self._total_days is None or value > self._total_days: self._total_days = value self._total_steps = value * 4 @property def total_steps(self): return self._total_steps @total_steps.setter def total_steps(self, value): if self._total_steps is None or value > self._total_steps: self._total_steps = value self._total_days = round(value / 4) def get_new_id(self): """ Get a number that is unique in this base_model. :return: New unique number """ return next(self.id_counter) def run(self, total_replications, total_model_steps): """ Run the base_model. Calls start(), then step() until done, then end() :param total_replications: Total number of replications of this scenario (just for friendly prints) :param total_model_steps: Total number of steps to run the model """ self.total_steps = total_model_steps self.start(total_replications) self.run_to_step() if self.started: self.end() return copy.deepcopy(self.logger.db_filepath), copy.deepcopy(self.logger.tables) def start(self, total_runs): # print("start {}:{}:{} total={} ({})".format(self.experiment_name, self.scenario_name, self.run_id, total_runs, # datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) self.started = True def run_to_step(self, end_step=None): """ Run the model for a set number of steps. :param end_step: Step to end on. Defaults to the total steps. """ info_types = ["count", "resistance", "treatments"] if end_step is None: end_step = self.total_steps while self.current_step < end_step and self.started: # sleep for a random time 50-200 milliseconds sleep_time = random.uniform(.05, .2) time.sleep(sleep_time) # log something to the database self.logger.log_info(random.choice(info_types), "test", self.current_step, random.randint(10, 1000)) # increment the step self.current_step = next(self.step_counter) def end(self): """ Stop the model running. Closes everything safely. """ # print("end {}:{}:{} -- {}/{} steps".format(self.experiment_name, self.scenario_name, self.run_id, # self.current_step, self.total_steps)) # self.current_step = self.total_steps self.logger.end_scenario(self.current_step + 1) self.logger.close() # print("End: {} {} - {} ({})".format(self.experiment_name, self.scenario_name, # self.run_id, # datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) self.started = False
class Driver: def __init__(self): self.state = State(0) self.params = Params() self.status = Status(self.params) self.sleep_time = 1 self.pwm_read = PwmRead(self.params.pin_mode_in, self.params.pin_servo_in, self.params.pin_thruster_in) self.pwm_out = PwmOut(self.params.pin_servo_out, self.params.pin_thruster_out) self.pid = PositionalPID() self.logger = Logger() self.logger.open() def load(self, filename): print('loading', filename) f = open(filename, "r") line = f.readline() line = f.readline() self.state.time_limit = int(line.split()[1]) # Time Limit line = f.readline() self.sleep_time = float(line.split()[1]) # Sleep time line = f.readline() line = f.readline() line = f.readline() p = float(line.split()[1]) # P line = f.readline() i = float(line.split()[1]) # I line = f.readline() d = float(line.split()[1]) # D self.pid.setPID(p, i, d) line = f.readline() line = f.readline() line = f.readline() num = int(line.split()[1]) # Number of waypoints line = f.readline() for i in range(num): line = f.readline() self.status.waypoint.addPoint(float(line.split()[0]), float(line.split()[1])) f.close() return def doOperation(self): while self.state.inTimeLimit(): self.readPWM() self.readGps() mode = self.getMode() if mode == 'RC': self.remoteControl() elif mode == 'AN': self.autoNavigation() self.outPWM() self.printLog() time.sleep(self.sleep_time) return def getMode(self): return self.status.mode def updateMode(self): mode_duty_ratio = self.pwm_read.pulse_width[0] if mode_duty_ratio < 1500: self.status.mode = 'RC' elif mode_duty_ratio >= 1500: self.status.mode = 'AN' return def readGps(self): self.status.readGps() self.updateMode() #if self.status.isGpsError(): #self.status.mode = 'RC' return def updateStatus(self): status = self.status status.calcTargetDirection() status.calcTargetDistance() status.updateTarget() return def readPWM(self): self.pwm_read.measurePulseWidth() self.pwm_out.servo_pulsewidth = self.pwm_read.pulse_width[1] self.pwm_out.thruster_pulsewidth = self.pwm_read.pulse_width[2] return def outPWM(self): self.pwm_out.updatePulsewidth() return def autoNavigation(self): self.updateStatus() boat_direction = self.status.boat_direction target_direction = self.status.target_direction servo_pulsewidth = self.pid.getStepSignal(target_direction, boat_direction) self.pwm_out.servo_pulsewidth = servo_pulsewidth self.pwm_out.thruster_pulsewidth = 1880 return def remoteControl(self): # Do nothing return def printLog(self): timestamp_string = self.status.timestamp_string mode = self.getMode() latitude = self.status.latitude longitude = self.status.longitude speed = self.status.speed direction = self.status.boat_direction servo_pw = self.pwm_out.servo_pulsewidth thruster_pw = self.pwm_out.thruster_pulsewidth t_direction = self.status.target_direction t_distance = self.status.target_distance target = self.status.waypoint.getPoint() t_latitude = target[0] t_longitude = target[1] print(timestamp_string) print( '[%s MODE] LAT=%.7f, LON=%.7f, SPEED=%.2f [km/h], DIRECTION=%lf' % (mode, latitude, longitude, speed, direction)) print('DUTY (SERVO, THRUSTER): (%lf, %lf) [us]' % (servo_pw, thruster_pw)) print('TARGET (LATITUDE, LONGITUDE): (%lf, %lf)' % (t_latitude, t_longitude)) print('TARGET (DIRECTION, DISTANCE): (%lf, %lf [m])' % (t_direction, t_distance)) print('') log_list = [ timestamp_string, mode, latitude, longitude, direction, t_latitude, t_longitude, servo_pw, thruster_pw, t_direction, t_distance ] self.logger.write(log_list) return def finalize(self): self.logger.close() self.pwm_out.finalize() return
class Driver: def __init__(self): self.state = State(0) self.params = Params() self.status = Status(self.params) self.sleep_time = 1 self.pwm_read = PwmRead(self.params.pin_mode_in, self.params.pin_servo_in, self.params.pin_thruster_in) self.pwm_out = PwmOut(self.params.pin_servo_out, self.params.pin_thruster_out) self.pid = PositionalPID() self.logger = Logger() self.logger.open() def load(self, filename): print('loading', filename) f = open(filename, "r") line = f.readline() line = f.readline() self.state.time_limit = int(line.split()[1]) # Time Limit line = f.readline() self.sleep_time = float(line.split()[1]) # Sleep time line = f.readline() line = f.readline() line = f.readline() self.pwm_read.num_cycles = int(line.split()[1]) # NUM_CYCLE line = f.readline() line = f.readline() line = f.readline() self.pwm_out.coefficient = float(line.split()[1]) # Coefficient line = f.readline() line = f.readline() line = f.readline() self.pid.angular_range = int(line.split()[1]) # angular_range line = f.readline() p = float(line.split()[1]) # P line = f.readline() i = float(line.split()[1]) # I line = f.readline() d = float(line.split()[1]) # D self.pid.setPID(p, i, d) line = f.readline() line = f.readline() line = f.readline() self.status.waypoint_radius = float( line.split()[1]) # range of target point line = f.readline() num = int(line.split()[1]) # Number of waypoints line = f.readline() for i in range(num): line = f.readline() self.status.waypoint.addPoint(float(line.split()[0]), float(line.split()[1])) f.close() return def doOperation(self): while self.state.inTimeLimit(): self.readPWM() self.readGps() mode = self.getMode() if mode == 'RC': self.remoteControl() elif mode == 'AN': self.autoNavigation() self.outPWM() self.printLog() time.sleep(self.sleep_time) return def getMode(self): return self.status.mode def updateMode(self): mode_duty_ratio = self.pwm_read.pulse_width[0] if mode_duty_ratio < 1500: self.status.mode = 'RC' elif mode_duty_ratio >= 1500: self.status.mode = 'AN' return def readGps(self): self.status.readGps() self.updateMode() #if self.status.isGpsError(): #self.status.mode = 'RC' return def updateStatus(self): status = self.status status.calcTargetDirection() status.calcTargetDistance() status.updateTarget() return # Read pwm pulsewidth # Set the readout signals as the output signals def readPWM(self): self.pwm_read.measurePulseWidth() self.pwm_out.servo_pulsewidth = self.pwm_read.pulse_width[1] self.pwm_out.thruster_pulsewidth = self.pwm_read.pulse_width[2] return def outPWM(self): self.pwm_out.updatePulsewidth() return def autoNavigation(self): self.updateStatus() if self.status.mode != 'AN_END': boat_direction = self.status.boat_direction target_direction = self.status.target_direction servo_pulsewidth = self.pid.getStepSignal(target_direction, boat_direction) self.pwm_out.servo_pulsewidth = servo_pulsewidth self.pwm_out.thruster_pulsewidth = 1880 #if thruster do not run, change this to a relatively small value(max=1900) return else: # If the boat has passed the last waypoint, # the navigation system get RC mode. return def remoteControl(self): # Do nothing return def printLog(self): timestamp_string = self.status.timestamp_string mode = self.getMode() latitude = self.status.latitude longitude = self.status.longitude speed = self.status.speed direction = self.status.boat_direction servo_pw = self.pwm_out.servo_pulsewidth thruster_pw = self.pwm_out.thruster_pulsewidth t_index = self.status.waypoint.getIndex() t_direction = self.status.target_direction t_distance = self.status.target_distance target = self.status.waypoint.getPoint() t_latitude = target[0] t_longitude = target[1] err = self.pid.ErrBack # To print logdata print(timestamp_string) print( '[%s MODE] LAT=%.7f, LON=%.7f, SPEED=%.2f [km/h], DIRECTION=%lf' % (mode, latitude, longitude, speed, direction)) print('DUTY (SERVO, THRUSTER): (%6.1f, %6.1f) [us]' % (servo_pw, thruster_pw)) print('TARGET No.%2d' % (t_index)) print('TARGET (LATITUDE, LONGITUDE): (%.7f, %.7f)' % (t_latitude, t_longitude)) print('TARGET (DIRECTION, DISTANCE): (%5.2f, %5.2f [m])' % (t_direction, t_distance)) print('') # To write logdata (csv file) log_list = [ timestamp_string, mode, latitude, longitude, direction, speed, t_index, t_latitude, t_longitude, t_direction, err ] self.logger.write(log_list) return def finalize(self): self.logger.close() self.pwm_out.finalize() return
class Demo: __module__ = __name__ __doc__ = 'API Midi Demo \n' def __init__(self, c_instance, stdout): c_instance.show_message("APIMidi Demo") self._Demo__c_instance = c_instance self.logger = Logger(stdout) self.logger.log("API Midi Demo connected") # Dispatcher handles routing incoming requests (note / cc) self.dispatcher = Dispatcher(self,self.logger) def song(self): """returns a reference to the Live song instance that we control""" return self._Demo__c_instance.song() def connect_script_instances(self,scripts): pass def disconnect(self): self.logger.close() self.send_midi(GOODBYE_SYSEX_MESSAGE) def script_handle(self): return self._Demo__c_instance.handle() def update_display(self): pass def application(self): return Live.Application.get_application() def suggest_input_port(self): return '' def suggest_output_port(self): return '' def can_lock_to_devices(self): return True def suggest_map_mode(self,cc_no): return Live.MidiMap.MapMode.absolute def show_message(self, message): self._Demo__c_instance.show_message(message) def send_midi(self, midi_event_bytes): self._Demo__c_instance.send_midi(midi_event_bytes) def refresh_state(self): self.send_midi(WELCOME_SYSEX_MESSAGE) def build_midi_map(self, midi_map_handle): if DEBUG: self.logger.log("Build Midi Map") map_mode = Live.MidiMap.MapMode.absolute self.dispatcher.build_midi_map(self.script_handle(),midi_map_handle) self.send_midi(BUILD_MAP_SYSEX_MESSAGE) def receive_midi(self,midi_bytes): status = midi_bytes[0] & 240 channel = midi_bytes[0] & 15 #self.logger.log("MIDI IN " + str(status) + " : " + str(channel)) if((status == NOTE_ON_STATUS) or (status == NOTE_OFF_STATUS)): note = midi_bytes[1] velocity = midi_bytes[2] self.dispatcher.handle_note(channel,note,velocity) elif (status == CC_STATUS): cc_no = midi_bytes[1] cc_value = midi_bytes[2] self.dispatcher.handle_cc(channel,cc_no,cc_value) def instance_identifier(self): return self._Demo__c_instance.instance_identifier() def sendSysex(self,data): """ Data must be a tuple of bytes, remember only 7-bit data is allowed for sysex """ self.send_midi(SYSEX_BEGIN + data + SYSEX_END)
def main(): """ Main program loop. Sets up the connections to the AVR and the server, then reads key presses and sends them to the server. """ avr = AVRChip() avr.reset_keys() # clear the key buffer on the AVR avr.led_off() logger = Logger() try: with open('/etc/device_key.txt', 'r') as f: for line in f: line = line.strip() DEVICE_KEY = line except IOError: DEVICE_KEY = '12345' try: server = ServerConnection(logger, DEVICE_KEY) server.connect() buf = "" while (True): c = avr.read_key() # read_key() will always return a character. NULL means no new # key presses. if c == '\0': time.sleep(0.1) continue buf += c logger.info('KEY PRESSED - ' + str(c) + '\n') # maximum size to avoid running out of memory if len(buf) > 16: logger.error('Reached maximum buffer size') buf = "" if c == '#': # These are the cases where we should continue reading input. # Otherwise, the # character always terminates the input. if buf in ('*#', '*#*#', '*#*#*#'): continue if buf == '*#*#*#*#': if server.register_device(): logger.info('Registration successful') avr.avr_indicate_success() else: logger.info('Registration unsuccessful') avr.avr_indicate_failure() elif len(buf) == 7: password = buf[:6] if server.open_door(password): logger.info('Door open successful') avr.avr_indicate_success() else: logger.info('Door open unsuccessful') avr.avr_indicate_failure() elif len(buf) == 14: if buf[6] != '*': logger.error('Invalid entry') buf = "" continue current_password = buf[:6] new_password = buf[7:-1] if server.change_password(current_password, new_password): logger.info('Password change successful') avr.avr_indicate_success() else: logger.info('Password change unsuccessful') avr.avr_indicate_failure() elif len(buf) == 16: if buf[8] != '*': logger.error('Invalid entry') buf = "" continue master_password = buf[:8] new_password = buf[9:-1] if server.change_password_master(master_password, new_password): logger.info('Password change successful') avr.avr_indicate_success() else: logger.info('Password change unsuccessful') avr.avr_indicate_failure() else: logger.error('Invalid entry') buf = "" except KeyboardInterrupt: pass finally: logger.close()
class Driver: def __init__(self): self.state = State(0) self.params = Params() self.status = Status(self.params) self.sleep_time = 1 self.pwm_read = PwmRead( self.params.pin_mode_in, self.params.pin_servo_in, self.params.pin_thruster_in, self.params.pin_OR, ) self.pwm_out = PwmOut(self.params.pin_servo_out, self.params.pin_thruster_out) self.pid = PositionalPID() self.logger = Logger() self.logger.open() # Whether experienced OR mode or not self.or_experienced = False # setup for ina226 print("Configuring INA226..") self.iSensor = ina226(INA226_ADDRESS, 1) self.iSensor.configure( avg=ina226_averages_t["INA226_AVERAGES_4"], ) self.iSensor.calibrate(rShuntValue=0.002, iMaxExcepted=1) time.sleep(1) print("Configuration Done") current = self.iSensor.readShuntCurrent() print("Current Value is " + str(current) + "A") print("Mode is " + str(hex(self.iSensor.getMode()))) def load(self, filename): print("loading", filename) f = open(filename, "r") line = f.readline() line = f.readline() self.state.time_limit = int(line.split()[1]) # Time Limit line = f.readline() self.sleep_time = float(line.split()[1]) # Sleep time line = f.readline() line = f.readline() line = f.readline() p = float(line.split()[1]) # P line = f.readline() i = float(line.split()[1]) # I line = f.readline() d = float(line.split()[1]) # D self.pid.setPID(p, i, d) line = f.readline() line = f.readline() line = f.readline() num = int(line.split()[1]) # Number of waypoints line = f.readline() for i in range(num): line = f.readline() self.status.waypoint.addPoint( float(line.split()[0]), float(line.split()[1]) ) f.close() return def doOperation(self): while self.state.inTimeLimit(): self.readPWM() self.readGps() # for test self.pwm_read.printPulseWidth() # ina226 print( "Current: " + str(round(self.iSensor.readShuntCurrent(), 3)) + "A" + ", Voltage: " + str(round(self.iSensor.readBusVoltage(), 3)) + "V" + ", Power:" + str(round(self.iSensor.readBusPower(), 3)) + "W" ) mode = self.getMode() if mode == "RC": self.remoteControl() elif mode == "AN": self.autoNavigation() elif mode == "OR": self.outOfRangeOperation() self.outPWM() self.printLog() time.sleep(self.sleep_time) return def getMode(self): return self.status.mode def updateMode(self): mode_duty_ratio = self.pwm_read.pulse_width[0] or_pulse = self.pwm_read.pulse_width[3] # OR mode if or_pulse < 1300 or (1500 <= mode_duty_ratio and self.or_experienced): if not self.or_experienced: self.status.updateWayPoint() self.status.mode = "OR" self.or_experienced = True # RC mode elif 0 < mode_duty_ratio < 1500: self.status.mode = "RC" # AN mode elif 1500 <= mode_duty_ratio and not self.or_experienced: self.status.mode = "AN" else: print("Error: mode updating failed", file=sys.stderr) return def readGps(self): self.status.readGps() self.updateMode() # if self.status.isGpsError(): # self.status.mode = 'RC' return def updateStatus(self): status = self.status status.calcTargetDirection() status.calcTargetDistance() status.updateTarget() return # Read pwm pulsewidth # Set the readout signals as the output signals def readPWM(self): self.pwm_read.measurePulseWidth() self.pwm_out.servo_pulsewidth = self.pwm_read.pulse_width[1] self.pwm_out.thruster_pulsewidth = self.pwm_read.pulse_width[2] return def outPWM(self): self.pwm_out.updatePulsewidth() return def autoNavigation(self): self.updateStatus() boat_direction = self.status.boat_direction target_direction = self.status.target_direction servo_pulsewidth = self.pid.getStepSignal(target_direction, boat_direction) self.pwm_out.servo_pulsewidth = servo_pulsewidth self.pwm_out.thruster_pulsewidth = 1700 return def remoteControl(self): # Do nothing return def outOfRangeOperation(self): # Be stationary # self.pwm_out.finalize() # update waypoint where the boat was self.autoNavigation() return def printLog(self): timestamp_string = self.status.timestamp_string mode = self.getMode() latitude = self.status.latitude longitude = self.status.longitude speed = self.status.speed direction = self.status.boat_direction servo_pw = self.pwm_out.servo_pulsewidth thruster_pw = self.pwm_out.thruster_pulsewidth t_direction = self.status.target_direction t_distance = self.status.target_distance target = self.status.waypoint.getPoint() t_latitude = target[0] t_longitude = target[1] err = self.pid.ErrBack current = str(round(self.iSensor.readShuntCurrent(), 3)) voltage = str(round(self.iSensor.readBusVoltage(), 3)) power = str(round(self.iSensor.readBusPower(), 3)) # To print logdata print(timestamp_string) print( "[%s MODE] LAT=%.7f, LON=%.7f, SPEED=%.2f [km/h], DIRECTION=%lf" % (mode, latitude, longitude, speed, direction) ) print( "DUTY (SERVO, THRUSTER): (%6.1f, %6.1f) [us]" % (servo_pw, thruster_pw) ) print("TARGET (LATITUDE, LONGITUDE): (%.7f, %.7f)" % (t_latitude, t_longitude)) print( "TARGET (DIRECTION, DISTANCE): (%5.2f, %5.2f [m])" % (t_direction, t_distance) ) print("") # To write logdata (csv file) log_list = [ timestamp_string, mode, latitude, longitude, direction, speed, t_latitude, t_longitude, servo_pw, thruster_pw, t_direction, t_distance, err, current, voltage, power, ] self.logger.write(log_list) return def finalize(self): self.logger.close() self.pwm_out.finalize() return
def main(): parser = argparse.ArgumentParser() parser.add_argument('-sc', type=str, help='score file') parser.add_argument('-percent', type=float, default=5, help='percent (1-100) best scoring to get') parser.add_argument('-filter', type=str, default='score', help='filter or score term to use') parser.add_argument('-num', default=10, type=int, help='use if you want a number of results, not better than percentile') parser.add_argument('-mode', default='%') parser.add_argument('-over_under', type=str, default='under', help='under/over score should be over/under threshold') parser.add_argument('-result', type=str, default=None, help='should the names be written to a file separate from the log file') parser.add_argument('-terms', nargs='+', default=['score', 'a_shape', 'a_pack', 'a_ddg', 'res_solv']) parser.add_argument('-thresholds', nargs='+', type=float) parser.add_argument('-percentile', default=10, type=int) args = vars(parser.parse_args()) logger = Logger('top_%.1f_%s.log' % (args['percent'], args['filter'])) # read in the score file, determine the threshold for the percentile sc_df = Rf.score_file2df(args['sc']) score = sc_df[args['filter']] if args['mode'] == '%': threshold = np.percentile(score, args['percent']) logger.log('found a threshold for %f for filter %s to be %.2f' % (args['percent'], args['filter'], threshold)) # create a df for lines that pass the threshold, either over or above it... if args['over_under'] == 'over': pass_df = sc_df[sc_df[args['filter']] >= threshold] elif args['over_under'] == 'under': pass_df = sc_df[sc_df[args['filter']] <= threshold] if args['mode'] == 'num': sc_df.sort_values(args['filter'], inplace=True) pass_df = sc_df.head(args['num']) if args['mode'] == 'best_of_best': threshold = np.percentile(score, args['percent']) sc_df = sc_df[sc_df[args['filter']] <= threshold] pass_df = Rf.get_best_of_best(sc_df, args['terms'], args['percentile']) if args['mode'] == 'thresholds': for term, thrs in zip(args['terms'], args['thresholds']): if term in ['a_sasa', 'a_pack', 'a_shape', 'a_tms_span_fa', 'a_tms_span', 'a_span_topo']: sc_df = sc_df[sc_df[term] > thrs] elif term in ['a_mars', 'a_ddg', 'score', 'total_score', 'a_res_solv', 'a_span_ins']: sc_df = sc_df[sc_df[term] < thrs] threshold = np.percentile(score, args['percent']) pass_df = sc_df[sc_df[args['filter']] < threshold] # output the names (description) of models that pass the threshold, either to the logger file, or to a separate file if args['result'] is None: logger.create_header('models passing the threshold:') for idx, row in pass_df.iterrows(): logger.log('%s %f' % (row['description'], row['score']), skip_stamp=True) else: with open(args['result'], 'w+') as fout: for name in pass_df['description']: fout.write(name + '\n') logger.close()
infoFile = "../LogFiles/uploadZehrsLoblaws.info" errFile = "../LogFiles/uploadZehrsLoblaws.err" debugFile = "../LogFiles/uploadZehrsLoblaws.dbg" logger = Logger(infoFile, errFile, debugFile) infoFile2 = "../LogFiles/sobeys.info" errFile2 = "../LogFiles/sobeys.err" debugFile2 = "../LogFiles/sobeys.dbg" logger2 = Logger(infoFile2, errFile2, debugFile2) csvWriter1 = CSVWriter("zehrs") csvWriter2 = CSVWriter("loblaws") csvWriter3 = CSVWriter("sobeys") zehrs = ZehrsLoblaws("zehrs", logger, csvWriter1) loblaws = ZehrsLoblaws("loblaws", logger, csvWriter2) sobeys = Sobeys("sobeys", logger2, csvWriter3) #print("ZEHRS\n") #zehrs.parse() #print("\n\n\nLOBLAWS\n") #loblaws.parse() sobeys.parse() logger.close() logger2.close() csvWriter1.close() csvWriter2.close() csvWriter3.close()
class Driver: def __init__(self): self._time_manager = TimeManager() self._params = Params() self._status = Status(self._params) self.log_time = time.time() self._pwm_read = PwmRead( self._params.pin_mode_in, self._params.pin_servo_in, self._params.pin_thruster_in, self._params.pin_or, ) self._pwm_out = PwmOut(self._params.pin_servo_out, self._params.pin_thruster_out) self._pid = PositionalPID() self._logger = Logger() self._logger.open() # Whether experienced OR mode or not self._or_experienced = False # setup for ina226 print("Configuring INA226..") try: self.i_sensor = ina226(INA226_ADDRESS, 1) self.i_sensor.configure( avg=ina226_averages_t["INA226_AVERAGES_4"], ) self.i_sensor.calibrate(rShuntValue=0.002, iMaxExcepted=1) self.i_sensor.log() print("Mode is " + str(hex(self.i_sensor.getMode()))) except: print("Error when configuring INA226") time.sleep(1) print("Configuration Done") def check_mode_change(self): print( "Please set to AN mode and then switch to RC mode to start appropriately." ) self._pwm_read.measure_pulse_width() self._update_mode() if self._status.mode == "AN": print("Next procedure: Set to RC mode to start.") while self._status.mode == "AN": self._pwm_read.measure_pulse_width() self._update_mode() time.sleep(0.1) elif self._status.mode == "RC": print( "Next procedure: set to AN mode and then switch to RC mode to start." ) while self._status.mode == "RC": self._pwm_read.measure_pulse_width() self._update_mode() time.sleep(0.1) print("Next procedure: Set to RC mode to start.") while self._status.mode == "AN": self._pwm_read.measure_pulse_width() self._update_mode() time.sleep(0.1) print("Procedure confirmed.") def load_params(self, filename): print("loading", filename) with open(filename, "r") as f: params = yaml.safe_load(f) time_limit = params["time_limit"] sleep_time = params["sleep_time"] P = params["P"] I = params["I"] D = params["D"] self._time_manager.set_time_limit(time_limit) # Time Limit self._sleep_time = float(sleep_time) # Sleep time self._pid.set_pid(P, I, D) for wp in params["waypoints"]: name = wp["name"] lat = wp["lat"] lon = wp["lon"] print(name, lat, lon) self._status.waypoint.add_point(lat, lon) return def do_operation(self): while self._time_manager.in_time_limit(): # update pwm # Read pwm pulse width self._pwm_read.measure_pulse_width() # Set the readout signals as the output signals # self._pwm_out.mode_pulse_width = self._pwm_read.pins[ # self._pwm_read.pin_mode # ]["pulse_width"] self._pwm_out.servo_pulse_width = self._pwm_read.pins[ self._pwm_read.pin_servo]["pulse_width"] self._pwm_out.thruster_pulse_width = self._pwm_read.pins[ self._pwm_read.pin_thruster]["pulse_width"] # read gps self._status.read_gps() self._update_mode() mode = self._status.mode if mode == "RC": pass elif mode == "AN": self._auto_navigation() elif mode == "OR": self._out_of_range_operation() # update output self._pwm_out.update_pulse_width() if time.time() - self.log_time > 1: self.log_time = time.time() # for test self._pwm_read.print_pulse_width() # ina226 if hasattr(self, "i_sensor"): self.i_sensor.log() self._print_log() time.sleep(self._sleep_time) return def _update_mode(self): mode_duty_ratio = self._pwm_read.pins[ self._pwm_read.pin_mode]["pulse_width"] # RC mode if 0 < mode_duty_ratio < 1500: self._status.mode = "RC" # AN mode elif 1500 <= mode_duty_ratio: self._status.mode = "AN" else: print("Error: mode updating failed", file=sys.stderr) return def _auto_navigation(self): # update status status = self._status status.calc_target_bearing() status.calc_target_distance() status.update_target() boat_heading = math.degrees(self._status.boat_heading) target_bearing = math.degrees(self._status.target_bearing) target_bearing_relative = math.degrees( self._status.target_bearing_relative) target_distance = self._status.target_distance servo_pulse_width = self._pid.get_step_signal(target_bearing_relative, target_distance) self._pwm_out.servo_pulse_width = servo_pulse_width self._pwm_out.thruster_pulse_width = 1700 return def _out_of_range_operation(self): # Be stationary # self.pwm_out.end() # update waypoint where the boat was self._auto_navigation() return def _print_log(self): timestamp = self._status.timestamp_string mode = self._status.mode latitude = self._status.latitude longitude = self._status.longitude speed = self._status.speed heading = math.degrees(self._status.boat_heading) servo_pw = self._pwm_out.servo_pulse_width thruster_pw = self._pwm_out.thruster_pulse_width t_bearing = math.degrees(self._status.target_bearing) t_bearing_rel = math.degrees(self._status.target_bearing_relative) t_distance = self._status.target_distance target = self._status.waypoint.get_point() t_latitude = target[0] t_longitude = target[1] t_idx = self._status.waypoint._index err = self._pid.err_back if hasattr(self, "i_sensor"): current = str(round(self.i_sensor.readShuntCurrent(), 3)) voltage = str(round(self.i_sensor.readBusVoltage(), 3)) power = str(round(self.i_sensor.readBusPower(), 3)) else: current = 0 voltage = 0 power = 0 # To print logdata print(timestamp) print("[%s MODE] LAT=%.7f, LON=%.7f, SPEED=%.2f [km/h], HEADING=%lf" % (mode, latitude, longitude, speed, heading)) print("DUTY (SERVO, THRUSTER): (%6.1f, %6.1f) [us]" % (servo_pw, thruster_pw)) print(f"TARGET INDEX: {t_idx}") print("TARGET (LATITUDE, LONGITUDE): (%.7f, %.7f)" % (t_latitude, t_longitude)) print("TARGET (REL_BEARING, DISTANCE): (%5.2f, %5.2f [m])" % (t_bearing_rel, t_distance)) print("") # To write logdata (csv file) log_list = [ timestamp, mode, latitude, longitude, heading, speed, t_latitude, t_longitude, servo_pw, thruster_pw, t_bearing, t_distance, err, current, voltage, power, ] self._logger.write(log_list) return def end(self): self._logger.close() self._pwm_read.end() self._pwm_out.end() return