コード例 #1
0
 if overtakingSide is not None:
     # check if overtaking should be aborted
     if overtakingSide == 'right' and sensors["left"].getValue(
     ) < 0.8 * sensors["left"].getMaxValue():
         overtakingSide = None
         currentLane -= 1
     elif overtakingSide == 'left' and sensors["right"].getValue(
     ) < 0.8 * sensors["right"].getMaxValue():
         overtakingSide = None
         currentLane += 1
     else:  # reduce the speed if the vehicle from previous lane is still in front
         speed2 = reduce_speed_if_vehicle_on_side(speed, overtakingSide)
         if speed2 < speed:
             speed = speed2
 speed = get_filtered_speed(speed)
 driver.setCruisingSpeed(speed)
 # brake if needed
 speedDiff = driver.getCurrentSpeed() - speed
 if speedDiff > 0:
     driver.setBrakeIntensity(min(speedDiff / speed, 1))
 else:
     driver.setBrakeIntensity(0)
 # car in front, try to overtake
 if frontDistance < 0.8 * frontRange and overtakingSide is None:
     if (is_vehicle_on_side("left")
             and (not safeOvertake or sensors["rear left"].getValue() >
                  0.8 * sensors["rear left"].getMaxValue())
             and sensors["left"].getValue() >
             0.8 * sensors["left"].getMaxValue() and currentLane < 2):
         currentLane += 1
         overtakingSide = 'right'
コード例 #2
0
#!/usr/bin/env python
"""vehicle_driver controller."""
import rospy
import math
from nav_msgs.msg import Odometry
from vehicle import Driver
#from cars_stage1.msg import ControlPoints, VehicleInfo, VehicleInfoArray,Location,CarDimensions,CarVelocity
from geometry_msgs.msg import Point, Pose, Quaternion, Twist, Vector3

rospy.init_node('vehicle_driver', anonymous=True)
pub = rospy.Publisher('/ego_vehicle', Odometry, queue_size=20)
rate = rospy.Rate(10)
odom = Odometry()

driver = Driver()
driver.setSteeringAngle(0)
driver.setCruisingSpeed(20)
gps = driver.getGPS("gps")
gps.enable(1)

while driver.step() != -1:
    driver.setSteeringAngle(0)
    print(gps.getValues()[0], gps.getValues()[2])
    x = gps.getValues()[0]
    y = gps.getValues()[2]
    odom.pose.pose = Pose(Point(x, y, 0), Quaternion(0, 0, 0, 0))
    print(gps.getSpeed())
    vel = gps.getSpeed()
    odom.twist.twist = Twist(Vector3(vel, 0, 0), Vector3(0, 0, 0))
    pub.publish(odom)
def auto_drive_call(m_order_queue, respond_dict):
    """
        Runs drive instance in the simulation.
            Defining and enabling sensors.
            :param respond_dict: dictionary to send value VA process
            :param m_order_queue:
            :type  m_order_queue: multiprocessing.Queue To provide communication between voice assistant process.
            :rtype None

    """
    # Driver initialize
    auto_drive = Driver()
    auto_drive.setAntifogLights(True)
    auto_drive.setDippedBeams(True)
    TIME_STEP = int(auto_drive.getBasicTimeStep())

    # distance sensors
    dist_sensor_names = [
        "front", "front right 0", "front right 1", "front right 2",
        "front right 3", "front left 0", "front left 1", "front left 2",
        "front left 3", "rear", "rear left", "rear right", "right", "left"
    ]

    dist_sensors = {}
    for name in dist_sensor_names:
        dist_sensors[name] = auto_drive.getDistanceSensor("distance sensor " +
                                                          name)
        dist_sensors[name].enable(TIME_STEP)

    # GPS
    gps = auto_drive.getGPS("gps")
    gps.enable(TIME_STEP)

    # Compass
    compass = auto_drive.getCompass("compass")
    compass.enable(TIME_STEP)

    # get and enable front camera
    front_camera1 = auto_drive.getCamera("front camera 1")
    front_camera1.enable(TIME_STEP)

    front_camera2 = auto_drive.getCamera("front camera 2")
    front_camera2.enable(TIME_STEP)
    front_camera3 = auto_drive.getCamera("front camera 3")
    front_camera3.enable(TIME_STEP)

    front_cams = {
        "right": front_camera2,
        "left": front_camera1,
        "center": front_camera3
    }

    # get and enable back camera
    back_camera = auto_drive.getCamera("camera2")
    back_camera.enable(TIME_STEP * 10)
    # back_camera.recognitionEnable(TIME_STEP * 10)

    # Get the display devices.
    # The display can be used to visually show the tracked position.
    # For showing lane detection
    display_front = auto_drive.getDisplay('display')
    display_front.setColor(0xFF00FF)
    # To establish communication between Emergency Vehicle
    receiver = auto_drive.getReceiver("receiver")
    receiver.enable(TIME_STEP)

    # To establish communication between other vehicles
    emitter = auto_drive.getEmitter("emitter")

    # lidar devices
    lidars = []

    Log = list()
    error_Log = list()

    for i in range(auto_drive.getNumberOfDevices()):
        device = auto_drive.getDeviceByIndex(i)
        if device.getNodeType() == Node.LIDAR:
            lidars.append(device)
            device.enable(TIME_STEP * 10)
            device.enablePointCloud()
    if not lidars:
        error_Log.append(" [ DRIVER CALL] This vehicle has no 'Lidar' node.")

    # Set first values
    auto_drive.setCruisingSpeed(40)
    auto_drive.setSteeringAngle(0)
    VA_order, emergency_message, prev_gps, gps_val = None, None, None, None

    # Main Loop
    while auto_drive.step() != -1:
        start_time = time.time()
        # for lidar in lidars:
        #     lidar.getPointCloud()
        if m_order_queue.qsize() > 0:
            VA_order = m_order_queue.get()
        else:
            VA_order = None
        """ If an Emergency Vehicle in the emergency state closer than 4 metre it sends emergency message
             to cars in front of it and other cars has sends messages as a chain to clear the way """
        if receiver.getQueueLength() > 0:
            message = receiver.getData()
            #  for sending emergency message to AutoCars front of our AutoCar
            # emitter.send(message)
            emergency_message = struct.unpack("?", message)
            emergency_message = emergency_message[0]
            receiver.nextPacket()
        else:
            emergency_message = False

        gps_val = round(sqrt(gps.getValues()[0]**2 + gps.getValues()[2]**2), 2)
        if gps_val is None:
            error_Log.append("[DRIVER CALL] couldn't get gps value..")
        else:
            prev_gps = gps_val

        if prev_gps is not None and gps_val is not None:
            gps_val = prev_gps
            if gps_val is not None:
                # To calculate direction of the car
                cmp_val = compass.getValues()
                angle = ((atan2(cmp_val[0], cmp_val[2])) * (180 / pi)) + 180
                # goes on Z axis
                if 335 <= angle <= 360 or 0 <= angle <= 45 or 135 <= angle <= 225:
                    axis = 1
                # goes on X axis
                elif 225 <= angle < 335 or 45 <= angle < 135:
                    axis = 0
                obj_data, LIDAR_data = Obj_Recognition.main(
                    dist_sensor_names, lidars, dist_sensors, front_cams,
                    back_camera)
                DataFusion.main(auto_drive, gps_val, obj_data, LIDAR_data,
                                emergency_message, display_front, front_cams,
                                dist_sensors, VA_order, respond_dict, gps,
                                axis)
            else:
                error_Log.append("[DRIVER CALL] couldn't get gps value..")
        Log.append(str(time.time() - start_time))
        with open("Logs\Driver_Log.csv", 'a') as file:
            wr = writer(file, quoting=QUOTE_ALL)
            wr.writerow(Log)
        if len(error_Log):
            with open("Logs\error_Log.csv", 'a', newline="") as file:
                wr = writer(file, quoting=QUOTE_ALL)
                wr.writerow(error_Log)
コード例 #4
0
while robot.step() != -1:
    # Read the sensors:
    # Enter here functions to read sensor data, like:
    #  val = ds.getValue()
    img_f = front_camera.getImage()
    img_cv = np.frombuffer(img_f, np.uint8).reshape((front_camera.getHeight(), front_camera.getWidth(), 4))
    #front_camera.saveImage("stop.png",100)
    
    nav_con = process_front(img_cv,shadow_state)

    weight = nav_con[0]
    shadow_state = nav_con[1]

    steer = kp*(weight/25000)
    if(abs(weight)>500):
        robot.setCruisingSpeed(35)
    else:
        robot.setCruisingSpeed(50)
    if(shadow_state==1):
        robot.setCruisingSpeed(25)

    print(weight)
    print(steer)


    robot.setSteeringAngle(steer)
    # for plot
    if MAKEPLOT is True:
        rand_val = np.random.randn(1)
        y_vec[-1] = float(robot.getCurrentSpeed())
        change = live_plotter(x_vec,y_vec,change, "Webots Tesla Racecourse Simulation Speed")
コード例 #5
0
import numpy as np
import cv2
# create the Robot instance.
robot = Driver()

# get the time step of the current world.
timestep = int(robot.getBasicTimeStep())
steering_angle = 0
# enable cameras
front_camera = robot.getCamera("front_camera")
rear_camera = robot.getCamera("rear_camera")
front_camera.enable(30)
rear_camera.enable(30)

# start engine and set cruising speed
robot.setCruisingSpeed(50)


# camera processing stuff
def process_camera_image(image):
    cam_height = front_camera.getHeight()
    cam_width = front_camera.getWidth()
    cam_fov = front_camera.getFov()
    # print("h: ", cam_height, " w: ", cam_width)
    num_pixels = cam_height * cam_width
    image = np.frombuffer(image, np.uint8).reshape((cam_height, cam_width, 4))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    print(gray)
    # cv2.imshow(gray)
    # cv2.WaitKey(0)
コード例 #6
0
        elif toTheRight < 70 and obstacles[-1] == 1:
            distSinceObs = 0
            obstacles.append(1)
        else:
            distSinceObs += (curSpeed) * timestep
            if distSinceObs > bigGap:
                bigGap = distSinceObs
            obstacles.append(0)
    elif not spotFound:
        obstacles = []
        obstacles.append(1)

    if bigGap > 7 and obstacles[-1] == 1:
        if (mode == 'Looking' or mode == 'Stopping') and curSpeed > 0:
            accumDist += curSpeed * timestep
            robot.setCruisingSpeed(0)
            robot.setBrakeIntensity(1)
            mode = 'Stopping'
        elif curSpeed == 0 and mode == 'Stopping':
            print(accumDist)
            print(toTheRight)
            mode = 'Parking'
            errors = 0.25
            spotwidth = 2.75
            planleng = distSinceObs - 2 * errors
            goal = [accumDist + planleng / 2, toTheRight + spotwidth / 2]
            path = reeds_shepp.path_sample(
                (carleng / 2, 0, 0), (goal[0], goal[1], 0), rho, step_size)
            xs = np.array([q[0] for q in path])
            ys = np.array([q[1] for q in path])
            thetas = np.array([q[2] for q in path])
コード例 #7
0
Camera.enable(cameraRGB, TIME_STEP)

lms291 = driver.getLidar('Sick LMS 291')
print(lms291)
Lidar.enable(lms291, TIME_STEP)
lms291_width = Lidar.getHorizontalResolution(lms291)
print(lms291_width)

fig = plt.figure(figsize=(3, 3))

while driver.step() != -1:

    if cont < 1000:
        driver.setDippedBeams(True)  # farol ligado
        driver.setIndicator(0)  # 0 -> OFF  1 -> Right   2 -> Left
        driver.setCruisingSpeed(speedFoward)  # acelerador (velocidade)
        driver.setSteeringAngle(0.0)  # volante (giro)
    elif cont > 1000 and cont < 1500:
        driver.setCruisingSpeed(speedBrake)
        driver.setBrakeIntensity(1.0)  # intensidade (0.0 a 1.0)
    elif cont > 1500 and cont < 2500:
        driver.setCruisingSpeed(-speedFoward)  # acelerador (velocidade)
        driver.setSteeringAngle(0.0)  # volante (giro)
    elif cont > 2500:
        cont = 0

    # print('speed (km/h) %0.2f' % driver.getCurrentSpeed())

    cont += 1

    # ler a camera
コード例 #8
0
        elif toTheRight < 70 and obstacles[-1] == 1:
            distSinceObs = 0
            obstacles.append(1)
        else:
            distSinceObs += (curSpeed) * timestep
            if distSinceObs > bigGap:
                bigGap = distSinceObs
            obstacles.append(0)
    elif not spotFound:
        obstacles = []
        obstacles.append(1)

    if bigGap > 5.8 and obstacles[-1] == 1:
        if (mode == 'Looking' or mode == 'Stopping') and curSpeed > 0:
            accumDist += curSpeed * timestep
            robot.setCruisingSpeed(0)
            robot.setBrakeIntensity(1)
            mode = 'Stopping'
        elif curSpeed == 0 and mode == 'Stopping':
            print(accumDist)
            print(toTheRight)
            mode = 'Parking'
            errors = 0.25
            state = State(x=3 * bigGap / 4 + accumDist - 3.37,
                          y=toTheRight + spotWidth / 1.75,
                          yaw=0,
                          v=0.0)
            if abs(state.x - state.y) / state.x < 0.1:
                constraint = np.min([state.x, state.y]) / 2
                constInd = np.argmin([state.x, state.y])
                turnAng = np.arctan(L / constraint)
コード例 #9
0
from vehicle import Driver
import os
import pickle

curr_dir = os.getcwd()
par_dir = curr_dir[:curr_dir.rfind('/')]

driver = Driver()

driver.setSteeringAngle(0.0)
driver.setGear(1)
driver.setThrottle(1)
driver.setCruisingSpeed(100)

camera = driver.getCamera('camera')
camera.enable(1)

brake_coeff = 9.0
counter = 0
braking = False
braking_threshold = 0.3
friction_acc = 0.5

ignore_smart_intersection = True  # Set this to True if you want to ignore smart intersection
while driver.step() != -1:
    counter = counter + 1

    speed = driver.getCurrentSpeed() * (5. / 18.)

    while True:
        try:
コード例 #10
0
forward_velocity = 20
brake = 0

camera = driver.getCamera("camera")
Camera.enable(camera, timestep)

# lms291 = driver.getLidar("Sick LMS 291")
# Lidar.enable(lms291, timestep)
# lms291_yatay = Lidar.getHorizontalResolution(lms291)

degree = 0
driver.setSteeringAngle(degree)
counter = 0

while driver.step() != -1:
    driver.setCruisingSpeed(forward_velocity)

    if counter % 30 == 0:
        Camera.getImage(camera)
        Camera.saveImage(camera, "camera.png", 1)
        img1 = cv2.imread("camera.png")

        if UNET_ACTIVATE:  #USING FOR ROI
            # w = UNET_SHAPE[1]  # frame.shape[1]
            # h = UNET_SHAPE[0]  # frame.shape[0]
            # car_h, car_w = h, int(h / 2)

            prediction, frame_resize = run_unet(img1)
            road, mask = color_ranging(prediction, frame_resize)

            # reference_height = mask.shape[0] - 50
コード例 #11
0
    # cv.namedWindow("main", cv.WINDOW_NORMAL)
    # cv.imshow('main', img_cv)
    # cv.waitKey()

    # live plotting
    if MAKEPLOT is True:
        rand_val = np.random.randn(1)
        y_vec[-1] = float(robot.getCurrentSpeed())
        change = live_plotter(x_vec, y_vec, change, "Live Tesla Speed")
        y_vec = np.append(y_vec[1:], 0.0)

    nav_con = process_front(img_cv)
    #TODO: implement PID. I was too lazy last night
    if (nav_con != -1):
        if (abs(nav_con[0]) > 10):
            steer = nav_con[0] * (math.pi / 180) * .15
            steer = steer * (1 - (abs(nav_con[1]) / 50))
        else:
            steer = 0
    print(steer)
    robot.setSteeringAngle(steer)

    front_camera.saveImage("testimg.png", 100)
    robot.setCruisingSpeed(25)
    #robot.setSteeringAngle(0)
    # Process sensor data here.

    # Enter here functions to send actuator commands, like:
    #  motor.setPosition(10.0)
    pass
コード例 #12
0
    qtdVermelho0 = nparrayc.shape[1]

    qtdVermelho1 = nparrayc1.shape[1]

    print("Quantos pixes tem no combo 0: ", qtdVermelho0)
    print("Quantos pixes tem no combo 1: ", qtdVermelho1)

    #erro = qtdVermelho0 - qtdVermelho1
    # --- Se negativo virar para a X se positivo para Y
    # --- Analisar trativa por causa do numero

    #print(erro)

    k = keyboard.getKey()
    if k == ord('W'):
        print("forward")
        car.setSteeringAngle(0)
        car.setCruisingSpeed(10)
    elif k == ord('D'):
        print("turn right")
        car.setSteeringAngle(0.5)
        car.setCruisingSpeed(10)
    elif k == ord('A'):
        print("turn left")
        car.setSteeringAngle(-0.5)
        car.setCruisingSpeed(10)
    elif k == ord('S'):
        print("stop")
        car.setSteeringAngle(0)
        car.setCruisingSpeed(0)
コード例 #13
0
camera = driver.getCamera("camera")
Camera.enable(camera, timestep)

lms291 = driver.getLidar("Sick LMS 291")
Lidar.enable(lms291, timestep)

lms291_yatay = Lidar.getHorizontalResolution(lms291)

fig = plt.figure(figsize=(3, 3))

# Main loop:
# - perform simulation steps until Webots is stopping the controller
while driver.step() != -1:
    if sayici < 1000:
        driver.setCruisingSpeed(ileri_hizi)  # aracın ileri doğru hızını
        #belirler
        driver.setSteeringAngle(0.6)  #aracın + veya - durumuna
        #göre sağa veya sola döndürür
        driver.setDippedBeams(True)  #ışığı yakma true false
        driver.setIndicator(2)  # aracın sinyal lambalarının sağa döndüğü 1 ile
        #sola döndüğünü 2 ile kapalı tutmak için 0
    elif sayici > 1000 and sayici < 1100:
        driver.setCruisingSpeed(fren)  #frenlerken
        driver.setBrakeIntensity(1.0)  # % de kaç
        # frene basılsın ?
        driver.setDippedBeams(False)  #ışığı kapatıyoruz
    elif sayici > 1100 and sayici < 1500:
        driver.setCruisingSpeed(-ileri_hizi)
        driver.setSteeringAngle(-0.6)
    elif sayici > 1500 and sayici < 1900:
コード例 #14
0
class Altino:
    def __init__(self):
        # initialize devices
        self.initializeDevices()

        # initial speed and steering angle values
        self.speed = 0.0
        self.angle = 0.0

        # initial wheel length used to calculate how many m the wheels have traveled
        self.leftWheelDistance = 0.0
        self.rightWheelDistance = 0.0
        self.updateDistanceTraveled()

        # line forwared
        self.lineFollower = LineFollower(self.camera)

        # navigation
        self.nav = PathPlanner()
        self.nav.setRobotPosition(Position(4, 1))
        self.nav.setGoalPosition(Position(14, 22))
        self.nav.setRobotOrientation(SOUTH)
        self.nav.printStatus()

        # compute fastest route from robot to goal
        self.turns = self.nav.getFastestRoute()
        logger.debug("turns: " + str(self.turns))

        # odometry
        self.odometry = Odometry(self.positionSensors, self.compass,
                                 self.nav.getRobotPosition(),
                                 self.nav.getRobotOrientation())

    # running
    def run(self):
        logger.info("Running..")
        leftIsEmpty = False  # flag used to detect left parking lot
        rightIsEmpty = False  # flag used to detect right parking lot
        leftStartingPosition = 0.0  # length of the left parking lot
        rightStartingPosition = 0.0  # length of the right parking lot
        sideOfParkingLot = 0  # indicates the side of the parking lot found: -1 left, 1 right, 0 not found yet
        actualTurn = 0

        while self.driver.step() != -1:

            ## here goes code that should be executed each step ##

            # get actual compass value
            #compassValues = self.compass.getValues()
            #logger.debug("COMPASS: "******"INIT status")

                # set wheel angle
                self.setAngle(0.0)

                # set starting speed
                self.setSpeed(0.5)

                # set new status
                self.setStatus(Status.TURN)

                # skip to next cycle to ensure everything is working fine
                continue

            # FOLLOW LINE STATUS
            if self.status == Status.FOLLOW_LINE:

                # set cruise speed
                self.setSpeed(0.5)

                # compute new angle
                newAngle = self.lineFollower.getNewSteeringAngle()

                # logger.debug("new steering angle: " + str(newAngle))

                # set new steering angle
                if newAngle != UNKNOWN:
                    self.setAngle(newAngle)
                else:
                    self.setStatus(Status.TURN)

            # TURN STATUS
            if self.status == Status.TURN:
                if actualTurn < len(self.turns):
                    turn = self.turns[actualTurn]
                    self.setAngle(0.5 * turn * MAX_ANGLE)
                else:
                    self.setStatus(Status.STOP)

                # compute new angle
                newAngle = self.lineFollower.getNewSteeringAngle()

                # if line is found
                if newAngle != UNKNOWN:
                    self.setStatus(Status.FOLLOW_LINE)
                    actualTurn += 1
                    continue

            # FORWARD STATUS
            if self.status == Status.FORWARD:
                # logger.debug("FORWARD status")

                # set cruise speed
                self.setSpeed(0.2)

                # avoing obstacles
                ds = self.distanceSensors

                # get distance sensors values
                frontLeftSensor = ds.frontLeft.getValue()
                frontRightSensor = ds.frontRight.getValue()
                sideLeftSensor = ds.sideLeft.getValue()
                sideRightSensor = ds.sideRight.getValue()

                # set values of thresholds
                tolerance = 10
                sideThreshold = 950

                # check if front left obstacle, turn right
                if frontLeftSensor > frontRightSensor + tolerance:
                    self.setAngle(RIGHT * frontLeftSensor / 500.0 * MAX_ANGLE)
                    # logger.debug("Steering angle: " + str(RIGHT * frontLeftSensor / 1000.0 * MAX_ANGLE))
                    logger.debug("Steering angle: " + str(self.angle))

                # check if front right obstacle, turn left
                elif frontRightSensor > frontLeftSensor + tolerance:
                    self.setAngle(LEFT * frontRightSensor / 500.0 * MAX_ANGLE)
                    # logger.debug("Steering angle: " + str(LEFT * frontRightSensor / 1000.0 * MAX_ANGLE))
                    logger.debug("Steering angle: " + str(self.angle))

                # check if side left obstacle, turn slight right
                elif sideLeftSensor > sideThreshold:
                    self.setAngle(RIGHT * sideLeftSensor / 4000.0 * MAX_ANGLE)

                # check if side right obstacle, turn slight left
                elif sideRightSensor > sideThreshold:
                    self.setAngle(LEFT * sideRightSensor / 4000.0 * MAX_ANGLE)

                # if no obstacle go straight
                else:
                    self.setAngle(self.angle / 1.5)

            # SEARCHING_PARK STATUS
            if self.status == Status.SEARCHING_PARK:

                # set slow speed
                self.setSpeed(0.2)

                # set straight wheels
                self.setAngle(0.0)

                # get distance and position sensors
                ds = self.distanceSensors
                ps = self.positionSensors

                #log info for debug
                logger.debug("Left Distance Sensor: " +
                             str(ds.sideLeft.getValue()))
                logger.debug("Left Position Sensor: " +
                             str(ps.frontLeft.getValue()) + " rad")
                logger.debug("Left Wheel Length: " +
                             str(self.leftWheelDistance) + " m")
                logger.debug("Starting position: " +
                             str(leftStartingPosition) + " m")
                logger.debug("Parking Lot Length: " +
                             str(self.leftWheelDistance -
                                 leftStartingPosition) + " m")

                sideThreshold = 650
                leftSensorValue = ds.sideLeft.getValue()
                rightSensorValue = ds.sideRight.getValue()

                # checking parking lot on the LEFT side
                if leftSensorValue < sideThreshold and not leftIsEmpty:
                    leftIsEmpty = True
                    leftStartingPosition = self.leftWheelDistance  # 100

                elif leftSensorValue > sideThreshold and leftIsEmpty:
                    leftIsEmpty = False

                elif leftIsEmpty and self.leftWheelDistance - leftStartingPosition > LENGTH + LENGTH / 3:
                    leftStartingPosition = self.leftWheelDistance  # 200 - 100
                    sideOfParkingLot = LEFT
                    self.setStatus(Status.FORWARD2)

                # checking parking lot on the RIGHT side
                if rightSensorValue < sideThreshold and not rightIsEmpty:
                    rightIsEmpty = True
                    rightStartingPosition = self.rightWheelDistance

                elif rightSensorValue > sideThreshold and rightIsEmpty:
                    rightIsEmpty = False

                elif rightIsEmpty and self.rightWheelDistance - rightStartingPosition > LENGTH + LENGTH / 3:
                    rightStartingPosition = self.rightWheelDistance
                    sideOfParkingLot = RIGHT
                    self.setStatus(Status.FORWARD2)

            # this ensure that the parking manoeuvre starts after going forward and not as soon as the parking lot is detected
            if self.status == Status.FORWARD2:
                distance = 0.19
                if sideOfParkingLot == LEFT:
                    if self.leftWheelDistance - leftStartingPosition > distance:
                        self.status = Status.PARKING
                elif sideOfParkingLot == RIGHT:
                    if self.rightWheelDistance - rightStartingPosition > distance:
                        self.status = Status.PARKING
                else:
                    logger.warning(
                        "Parking lot not found! I don't know if right or left."
                    )
                    self.setStatus(Status.SEARCHING_PARK)

            # starting the parking manoeuvre
            if self.status == Status.PARKING:
                if sideOfParkingLot != LEFT and sideOfParkingLot != RIGHT:
                    logger.error("side of parking lot unknown.")
                    exit(1)

                # stop the vehicle, turn the wheels and go back
                self.setSpeed(0.0)
                self.setAngle(sideOfParkingLot * MAX_ANGLE)
                self.setSpeed(-0.1)

                # when should it turn the other way
                backThreshold = 400
                ds = self.distanceSensors
                rear = ds.back.getValue()

                logger.debug("Back sensor: " + str(rear))

                if rear > backThreshold:
                    self.status = Status.PARKING2

            if self.status == Status.PARKING2:
                self.setAngle(-1 * sideOfParkingLot * MAX_ANGLE)

                threshold = 945
                rear = self.distanceSensors.back.getValue()
                if rear > threshold:
                    self.setStatus(Status.CENTER)

            if self.status == Status.CENTER:

                self.setAngle(0.0)
                self.setSpeed(0.2)

                rear = self.distanceSensors.back.getValue()
                front = self.distanceSensors.frontCenter.getValue()

                if rear - front < 20:
                    self.setStatus(Status.STOP)

            if self.status == Status.STOP:
                self.setSpeed(0.0)
                self.setAngle(0.0)

                # if obstacle is cleared go forward
                if not self.avoidObstacle(
                ) and self.prevStatus == Status.PARKING2:
                    self.setStatus(Status.FORWARD)

            if self.status == Status.MANUAL:
                # get current state
                speed = self.speed
                angle = self.angle
                # logger.debug("Current Key: " + str(currentKey))

                # keyboard controlls
                # accelerate
                if currentKey == self.keyboard.UP:
                    if speed < 0:
                        speed += 0.02
                    else:
                        speed += 0.008

                # break
                elif currentKey == self.keyboard.DOWN:
                    if speed > 0:
                        speed -= 0.02
                    else:
                        speed -= 0.008

                # turn left
                elif currentKey == self.keyboard.LEFT:
                    angle -= 0.01

                # turn right
                elif currentKey == self.keyboard.RIGHT:
                    angle += 0.01

                # handbreak
                elif currentKey == ord(' '):
                    speed /= 4

                # update state
                self.setSpeed(speed)
                self.setAngle(angle)

    # initialize Altino devices
    def initializeDevices(self):
        # get Driver object from Webots
        self.driver = Driver()

        # set vehicle status
        self.status = Status.INIT
        self.prevStatus = Status.INIT

        # get timestep
        self.timestep = int(self.driver.getBasicTimeStep())

        # set sensor timestep
        self.sensorTimestep = 2 * self.timestep

        # get lights
        self.headLights = self.driver.getDevice("headlights")
        self.backLights = self.driver.getDevice("backlights")

        # turn on headLights
        # headLights.set(1)

        # get and enable camera
        self.camera = self.driver.getDevice("camera")
        self.camera.enable(self.sensorTimestep)

        # get distance sensors
        self.distanceSensors = DistanceSensors()
        self.distanceSensors.frontLeft = self.driver.getDevice(
            "front_left_sensor")
        self.distanceSensors.frontCenter = self.driver.getDevice(
            'front_center_sensor')
        self.distanceSensors.frontRight = self.driver.getDevice(
            'front_right_sensor')

        self.distanceSensors.sideLeft = self.driver.getDevice(
            'side_left_sensor')
        self.distanceSensors.sideRight = self.driver.getDevice(
            'side_right_sensor')
        self.distanceSensors.back = self.driver.getDevice('back_sensor')

        # enable distance sensors
        self.distanceSensors.frontLeft.enable(self.sensorTimestep)
        self.distanceSensors.frontCenter.enable(self.sensorTimestep)
        self.distanceSensors.frontRight.enable(self.sensorTimestep)

        self.distanceSensors.sideLeft.enable(self.sensorTimestep)
        self.distanceSensors.sideRight.enable(self.sensorTimestep)
        self.distanceSensors.back.enable(self.sensorTimestep)

        # get position sensors
        self.positionSensors = PositionSensors()
        self.positionSensors.frontLeft = self.driver.getDevice(
            'left_front_sensor')
        self.positionSensors.frontRight = self.driver.getDevice(
            'right_front_sensor')
        self.positionSensors.rearLeft = self.driver.getDevice(
            'left_rear_sensor')
        self.positionSensors.rearRight = self.driver.getDevice(
            'right_rear_sensor')

        # enable position sensors
        self.positionSensors.frontLeft.enable(self.sensorTimestep)
        self.positionSensors.frontRight.enable(self.sensorTimestep)
        self.positionSensors.rearLeft.enable(self.sensorTimestep)
        self.positionSensors.rearRight.enable(self.sensorTimestep)

        # get and enable compass
        self.compass = self.driver.getDevice('compass')
        self.compass.enable(self.sensorTimestep)

        # get and enable keyboard controll
        self.keyboard = self.driver.getKeyboard()
        self.keyboard.enable(self.sensorTimestep)

        # this ensure sensors are correctly initialized
        for i in range(int(self.sensorTimestep / self.timestep) + 1):
            self.driver.step()

    # update cruising speed
    def setSpeed(self, speed):
        if (speed >= -1 * MAX_SPEED and speed <= MAX_SPEED):
            self.speed = speed
        elif (speed > MAX_SPEED):
            self.speed = MAX_SPEED
        elif (speed > -1 * MAX_SPEED):
            self.speed = -1 * MAX_SPEED
        self.driver.setCruisingSpeed(self.speed)

    # update steering angle
    def setAngle(self, angle):
        # ensure angle stays between -MAX_ANGLE and MAX_ANGLE
        if (angle >= -1 * MAX_ANGLE and angle <= MAX_ANGLE):
            self.angle = angle
        elif (angle > MAX_ANGLE):
            self.angle = MAX_ANGLE
        elif (angle < -1 * MAX_ANGLE):
            self.angle = -1 * MAX_ANGLE
        self.driver.setSteeringAngle(self.angle)

    def setStatus(self, status):
        # check if new status is a valid state.
        if status not in list(map(int, Status)):
            logger.warning("Status: " + str(status) +
                           " is invalid, setting STOP status.")
            self.status = Status.STOP
            return

        # backup last status
        self.prevStatus = self.status

        # set new status
        self.status = status

    # check if vehicle is too close to an obstacle
    def avoidObstacle(self):
        threshold = 950
        ds = self.distanceSensors
        fl = ds.frontLeft.getValue()
        fc = ds.frontCenter.getValue()
        fr = ds.frontRight.getValue()

        rear = ds.back.getValue()

        if fl > threshold or fc > threshold or fr > threshold:
            return True

        if rear > threshold:
            return True

    # compute distance traveled by wheels in meters
    def updateDistanceTraveled(self):
        # get position sensors
        ps = self.positionSensors

        # get radiants from wheel
        radFLW = ps.frontLeft.getValue()
        radFRW = ps.frontRight.getValue()

        # compute distance traveled
        self.leftWheelDistance = radFLW * WHEEL_RADIUS
        self.rightWheelDistance = radFRW * WHEEL_RADIUS

        # logger.debug("Distance Updated - Left Wheel Length: " + str(self.leftWheelLength) + " m")

    # check keyboard pressed keys and change status
    def keyboardCommands(self):

        # get current key
        currentKey = self.keyboard.getKey()

        if DEBUG and (currentKey == ord('s') or currentKey == ord('S')):
            logger.debug("Current status: " + str(self.status))

        # press P to find parking lot
        if currentKey == ord('p') or currentKey == ord('P'):
            logger.info("Looking for a parking lot")
            self.setStatus(Status.SEARCHING_PARK)

        # press M to manual control
        elif currentKey == ord('m') or currentKey == ord('M'):
            logger.info("Manual")
            self.setStatus(Status.MANUAL)

        # press A to auto control
        elif currentKey == ord('a') or currentKey == ord('A'):
            logger.info("Auto")
            self.setStatus(Status.FORWARD)

        # return current key to allow other controls
        return currentKey
コード例 #15
0
    # Read the sensors:
    # Enter here functions to read sensor data, like:
    #  val = ds.getValue()
    img_f = front_camera.getImage()
    img_cv = np.frombuffer(img_f, np.uint8).reshape(
        (front_camera.getHeight(), front_camera.getWidth(), 4))

    nav_con = process_front(img_cv, angle_last)

    if (nav_con != -1):
        if (abs(nav_con[0] - angle_buffer[counter]) > 2):
            nav_con = -1
        else:
            angle_buffer[counter] = nav_con[0]
            xmid = nav_con[1]
            robot.setCruisingSpeed(30)
            #offset = nav_con[1]
            counter += 1
            #n_aq+=1
            #aq_l = 1
            if (counter == b_max):
                counter = 0
    else:
        xmid = 0
        robot.setCruisingSpeed(30)
        #aq_l = 0

    angle = np.average(angle_buffer)
    da = angle - angle_last
    if (abs(angle) < .05):
        robot.setCruisingSpeed(55)