def __init__(self): # 센서 객체 생성 self.__pcf8591 = Pcf8591(0x48) self.__distance = Distance(self.__pcf8591) self.cm = 0 self.pre = 0 # 모터 제어 self.__motor_control = NvidiaRacecar() self.stop_flag = False # Oled 표시, 스레딩 self.__oled = Oled() self.__oled_flag = True self.__oled_thread = threading.Thread(target=self.__oled_setting, daemon=True) self.__oled_thread.start() # =================motor values======================== self.__handle_angle = 0 self.__dcMotor_speed = 0.55 self.__max_speed = self.__dcMotor_speed self.__motor_direction = "stop" # =================car status========================== self.__working = False self.__position = None self.__dst = None # =================pid controller====================== self.pid_controller = PIDController(round(datetime.utcnow().timestamp() * 1000)) # self.pid_controller.set_gain(0.63, -0.001, 0.23) # self.pid_controller.set_gain(0.61, 0, 0.22) self.pid_controller.set_gain(0.55, 0, 0.22) # ==================line detect variable=============== self.road_half_width_list = deque(maxlen=10) self.road_half_width_list.append(100) self.prev_road_center_x = deque(maxlen=5) self.prev_road_center_x.append(120) # 플래그 들 self.crosswalk_flag = False self.which_side = None self.change_road_flag = False self.init_flag = None self.stop_and_forward_flag = False self.L_lines = [] self.R_lines = [] self.L_x = 0 self.R_x = 240 # =================mode 1:auto 2:manual================ self.__mode = 0 # =======================count============================ self.count = 0 self.stop_count = 0
def __init__(self, config): self.car = NvidiaRacecar( steering_channel=config.jetracer_steering_channel(), throttle_channel=config.jetracer_throttle_channel(), steering_gain=config.jetracer_steering_gain(), steering_offset=config.jetracer_steering_offset(), throttle_gain=config.jetracer_throttle_gain(), throttle_offset=config.jetracer_throttle_offset())
def __init__(self, camera_width=112, camera_height=112, fps=30): self.car = NvidiaRacecar() print("====== LOADING CAMERA ======") self.camera = CSICamera(width=camera_width, height=camera_height, capture_fps=fps) self.camera.running = True print("====== CAMERA LOADED SUCCESSFULLY ======") self.car.throttle = 0 self.car.steering = 0
def doexecute(): print("start") CATEGORIES = ['apex'] device = torch.device('cuda') model = torchvision.models.resnet18(pretrained=False) model.fc = torch.nn.Linear(512, 2 * len(CATEGORIES)) model = model.cuda().eval().half() # model.load_state_dict(torch.load('model.pth')) model.load_state_dict(torch.load('data/model.pth')) print("1") data = torch.zeros((1, 3, 224, 224)).cuda().half() model_trt = torch2trt(model, [data], fp16_mode=True) torch.save(model_trt.state_dict(), 'road_following_model_trt.pth') print("2") model_trt = TRTModule() model_trt.load_state_dict(torch.load('road_following_model_trt.pth')) print("model load end") car = NvidiaRacecar() # Left Camera camera0 = nano.Camera(device_id=0, flip=2, width=224, height=224, fps=60) # Right Camera camera1 = nano.Camera(device_id=1, flip=2, width=224, height=224, fps=60) STEERING_GAIN = 0.75 STEERING_BIAS = 0.00 cnt = 0 while True: image = camera0.read() image = preprocess(image).half() output = model_trt(image).detach().cpu().numpy().flatten() x = float(output[0]) car.steering = x * STEERING_GAIN + STEERING_BIAS car.throttle = 0.5 print(str(cnt) + ":" + str(x) + ":") cnt = cnt + 1
def prepare(): # 走行Button GPIO.setmode(GPIO.BOARD) GPIO.setup(recbtn, GPIO.IN) GPIO.add_event_detect(gobtn, GPIO.FALLING, callback=btn_thrd, bouncetime=200) # Left Camera camera0 = nano.Camera(device_id=0, flip=2, width=224, height=224, fps=60) # Right Camera camera1 = nano.Camera(device_id=1, flip=2, width=224, height=224, fps=60) CATEGORIES = ['apex'] device = torch.device('cuda') model = torchvision.models.resnet18(pretrained=False) model.fc = torch.nn.Linear(512, 2 * len(CATEGORIES)) model = model.cuda().eval().half() # model.load_state_dict(torch.load('model.pth')) model.load_state_dict(torch.load('data/model.pth')) data = torch.zeros((1, 3, 224, 224)).cuda().half() model_trt = torch2trt(model, [data], fp16_mode=True) torch.save(model_trt.state_dict(), 'road_following_model_trt.pth') model_trt = TRTModule() model_trt.load_state_dict(torch.load('road_following_model_trt.pth')) car = NvidiaRacecar() STEERING_GAIN = -0.75 STEERING_BIAS = 0.00 car.throttle = 0.25 cnt = 0 while True: image = camera0.read() image = preprocess(image).half() output = model_trt(image).detach().cpu().numpy().flatten() x = float(output[0]) car.steering = x * STEERING_GAIN + STEERING_BIAS print(str(cnt) + ":" + str(x) + ":") cnt = cnt + 1
# Model if os.path.isfile(MODEL_PATH_TRT): model_trt = TRTModule() model_trt.load_state_dict(torch.load(MODEL_PATH_TRT)) else: model = AutopilotModel(pretrained=False) model.load_from_path(MODEL_PATH) model.eval() x = torch.ones((1, FRAME_CHANNELS, FRAME_SIZE, FRAME_SIZE)).cuda() model_trt = torch2trt(model, [x], fp16_mode=True) torch.save(model_trt.state_dict(), MODEL_PATH_TRT) try: # Car car = NvidiaRacecar() car.throttle_gain = THROTTLE_GAIN car.steering_offset = STEERING_OFFSET # Camera camera = CSICamera(width=CAMERA_WIDTH, height=CAMERA_HEIGHT) # Control Loop while True: if SHOW_LOGS: start_time = time.time() camera_frame = camera.read() cropped_frame = center_crop_square(camera_frame) resized_frame = cv2.resize(cropped_frame, (FRAME_SIZE, FRAME_SIZE)) preprocessed_frame = preprocess_image(resized_frame)
def __init__(self): self.car = NvidiaRacecar(steering_gain=self.STEERING_GAIN, steering_offset=self.STEERING_OFFSET, throttle_gain=self.THROTTLE_GAIN, throttle_offset=self.THROTTLE_OFFSET)
def collect_data(train,throttle,steering,s_gain): # Importing NvidiaRacecar modeule and CSICamera # Note: CSICamera object can be created once at a time camera = CSICamera(width=112, height=112) camera.running = True car = NvidiaRacecar() # initialize parameters: 0 car.throttle = throttle car.steering = steering car.throttle_gain = -0.5 # file name if not os.path.exists("data/"): os.makedirs("data/") file_name=input("File_name: ") try: while(True): # Display steering and Throttle print("Steering: {}, Throttle {}" .format(car.steering,car.throttle)) # Increase throttle on press if keyboard.is_pressed('i'): if car.throttle<=0.04: car.throttle=0.04 if car.throttle>=0.062: car.throttle=car.throttle else: car.throttle=car.throttle+0.001 # Decrease throttle on release if not keyboard.is_pressed('i'): if car.throttle<=0.04: car.throttle=0.04 else: car.throttle=car.throttle-0.01 # turn left if keyboard.is_pressed('j'): if car.steering>=0.6: car.steering=car.steering else: car.steering =car.steering+0.01 ### turn right if keyboard.is_pressed('l'): if car.steering<=-0.6: car.steering=car.steering else: car.steering =car.steering-0.01 # Resease steering back to steering angle of 0 degrees if not keyboard.is_pressed('j') and not keyboard.is_pressed('l'): if not keyboard.is_pressed('j'): if car.steering<=0: car.steering=car.steering else: car.steering =car.steering-0.015 if not keyboard.is_pressed('l'): if car.steering>=0: car.steering=car.steering else: car.steering =car.steering+0.015 # Emergency break if keyboard.is_pressed('space'): car.throttle=-100 time.sleep(0.1) car.throttle=0 # Save the recording labelled data: Press if keyboard.is_pressed('s'): car.throttle=0 car.steering=0 print("saving") np.savez("data/"+str(file_name)+".npz", train=np.array(train)) print("saved") camera.running=False del camera break # Abort if keyboard.is_pressed('a'): car.throttle=0 car.steering=0 camera.running=False del camera break # append recording train.append([camera.value,car.throttle,car.steering]) except: camera.running=False del camera if __name__ == '__main__': # input: train,throttle,steering,s_gain collect_data([],0,0,-0.5)