Beispiel #1
0
    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
Beispiel #2
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
Beispiel #3
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())
Beispiel #4
0
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
Beispiel #5
0
 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
Beispiel #6
0
# 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)
Beispiel #7
0
 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)
Beispiel #8
0
import keras
import pandas as pd
import numpy as np
import os
from jetracer.nvidia_racecar import NvidiaRacecar
from jetcam.csi_camera import CSICamera
import time
import vae
from vae.controller import VAEController
from stable_baselines import SAC
car = NvidiaRacecar()
throttle = 0.8
#path = "jetcar_weights.pkl"
path = "logs/sac/JetVae-v0_46/JetVae-v0.pkl"
input_array = np.zeros((1, 256))
try:
    i = 0

    v = VAEController()
    v.load("logs/vae-256.pkl")
    #model = keras.models.load_model(path)
    model = SAC.load(path)
    camera = CSICamera(width=112, height=112)
    camera.running = True

    print("Imported Camera! Ready to Start!")
    while True:
        print("Starting to read image")
        image = camera.value.copy()
        print("Image Read!")
        #image = cv2.resize(image, (224//2, 224//2))
Beispiel #9
0
#
#Save the optimized model using the cell below
torch.save(model_trt.state_dict(), 'road_following_model_trt.pth')
#
#Load the optimized model by executing the cell below
import torch
from torch2trt import TRTModule

model_trt = TRTModule()
model_trt.load_state_dict(torch.load('road_following_model_trt.pth'))
#
#Create the racecar class
#
from jetracer.nvidia_racecar import NvidiaRacecar

car = NvidiaRacecar()
#
#Create the camera class.
from jetcam.csi_camera import CSICamera

camera = CSICamera(width=224, height=224, capture_fps=65)
"""
Finally, execute the cell below to make the racecar move forward, steering the racecar based on the x value of the apex.

Here are some tips,

If the car wobbles left and right, lower the steering gain
If the car misses turns, raise the steering gain
If the car tends right, make the steering bias more negative (in small increments like -0.05)
If the car tends left, make the steering bias more postive (in small increments +0.05)
"""
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)
    
Beispiel #11
0
# parameter setting
# 長さの単位はcm
isLoop = True

centerLine = 30
thresWidth = 15
facingAngleThres = 15
betweenSensors = 20

timeStamp = []
timeStamp.append(time.time())

print("carmachi")
# steering setting
car = NvidiaRacecar()
car.steering_gain = 1.0
car.steering_offset = 0.0

rightMax = 1.0
leftMax = -1.0

steerParam = 1.0  # steerParam * facingAngle で facingAngle[rad]曲がる

# throttle setting
car.throttle_gain = 0.5

speedHigh = 10
speedLow = 0.5

# *Distance are the acquired distance to the wall from each sensor
Beispiel #12
0
# file
file = "data"

# Parent Directory path
parent_dir = "/home/petbot/jetracer/notebooks/"

# Path
path = os.path.join(parent_dir, file)
# os.mkdir(path)

print("Directory '% s' created" % file)
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
print("successful")

car = NvidiaRacecar()
car.throttle = 0
car.steering = 0.5

i = 0
print("press keys")
while True:
    while True:
        ret_val, image = cap.read()
        #   cv2.imshow("CSI Camera", image)
        cv2.imwrite(path + '/' + str(i) + '.jpeg', image)
        i += 1

    if keyboard.is_pressed("w"):
        car.throttle += 0.3
    elif keyboard.is_pressed("s"):
#!/usr/bin/env python3

import rospy
from jetracer.nvidia_racecar import NvidiaRacecar
from std_msgs.msg import Float32

#Initialize car variable and tune settings
car = NvidiaRacecar()
car.steering_gain = 0.65
car.steering_offset = -0.16
car.throttle_gain = 1
car.steering = 0.0
car.throttle = 0.0

#Throttle
def callback_throttle(throt):
    car.throttle = throt.data
    rospy.loginfo("Throttle: %s", str(throt.data))

#Steering
def callback_steering(steer):
    car.steering = steer.data
    rospy.loginfo("Steering: %s", str(steer.data))

#Setup node and topics subscription
def racecar():
    rospy.init_node('racecar', anonymous=True)
    rospy.Subscriber("throttle", Float32, callback_throttle)
    rospy.Subscriber("steering", Float32, callback_steering)

    rospy.spin()
Beispiel #14
0
from jetracer.nvidia_racecar import NvidiaRacecar

car = NvidiaRacecar()
car.steering = 0.3
print("car.steer gain {}".format(car.steering_gain))
print("car.steering_offset {}".format(car.steering_offset))
car.throttle = 0.0
print("car.throttle_gain {}".format(car.throttle_gain))
car.throttle_gain = 0.5