Beispiel #1
0
def process_lidar(lidar_data):
    """ process the lidar image """
    if not SCHEDULER.is_lidar_enable():
        return

    set_lidar_values(lidar_data)

    state = SCHEDULER.get_state()
    if state is "construction":
        process_construction()
    elif state is "parking":
        process_parking()
    elif state is "tunnel":
        process_tunnel()
Beispiel #2
0
def test_frontcam(image):
    """ process the frontcam image """
    # print("image is subscribed")
    if not SCHEDULER.is_frontcam_enable():
        return

    if not EYE.is_front_occupied():

        STATE = SCHEDULER.get_state()

        if STATE == "traffic_light":
            signal = is_light_green(image)
            rospy.logdebug("[PROC] is_light_green: {}"
                           .format(signal))
            if signal:
                SCHEDULER.set_state("to_intersection")
                TURTLE.enable()
                TURTLE.set_speed("fast")
                TURTLE.set_speed_smooth("normal")

        elif STATE == "to_intersection":
            signal = check_left_right_sign(image)
            rospy.logdebug("[PROC] left or right: {}"
                           .format(signal))
            if signal == "right":
                SCHEDULER.set_state("intersection_right")
            elif signal == "left":
                SCHEDULER.set_state("intersection_left")
        if SCHEDULER.debug_option["show_center_slope"]:
            SCHEDULER.check_time("frontcam", min=0.5)
        info = EYE.see_front(image)
        if info is None:
            return

        # rospy.logdebug("[PROC] test_frontcam: state {}  {}".format(
        #     info["state"], info["horizon_position"]))
        # if (info["horizon_position"] < 150):
        #     TURTLE.set_speed("fast")
        #     TURTLE.set_speed_smooth("normal")

        if (info["state"] == "turning") and (info["turning_to"] is not "None"):
            rospy.logdebug(
                "[PROC] turn off for 1.5s")
            rospy.Timer(rospy.Duration(1.5),
                        EYE.release_front_occupied, oneshot=True)
        else:
            rospy.Timer(rospy.Duration(0.1),
                        EYE.release_front_occupied, oneshot=True)
Beispiel #3
0
def view_frontcam(image):
    """ process the frontcam """
    # rospy.loginfo("[VIEW] frontcam image received.")
    if not EYE.is_front_occupied():
        if SCHEDULER.debug_option["show_center_slope"]:
            SCHEDULER.check_time("frontcam", min=0.4)
        info = EYE.see_front(image)
        if info is None:
            return
        # rospy.logdebug("info: {:s}".format(str(info)))
        if info["center"] is "-1000" or info["center"] is "1000" or info["center"] is "0":
            pass
        else:
            EYE.reset_state()
        rospy.Timer(rospy.Duration(0.1),
                    EYE.release_front_occupied, oneshot=True)
Beispiel #4
0
def process_lidar(lidar_data):
    """ process the lidar image """
    if not SCHEDULER.is_lidar_enable():
        return
    if SCHEDULER.is_lidar_occupied() or TURTLE.is_occupied():
        return

    state = SCHEDULER.get_state()
    if SCHEDULER.debug_option["show_timer"] and (state != "construction"):
        SCHEDULER.check_time("lidar", min=0.4, stop_when_delay=False)

    set_lidar_values(lidar_data)
    state = SCHEDULER.get_state()

    front = get_object_distance("front")

    if (front < 0.15) and (front > 0):
        TURTLE.stop()
        return

    if state is "default":
        leftside = get_object_distance("leftside")
        print(leftside)
        if (leftside < 0.35) and (leftside > 0):
            rospy.Timer(rospy.Duration(5),
                        SCHEDULER.release_lidar_occupied,
                        oneshot=True)
            SCHEDULER.set_state("to_construction")
            return
    elif state is "to_construction":
        leftside = get_object_distance("leftside")
        print("to_construction: " + str(leftside))
        if (leftside < 0.35) and (leftside > 0):
            SCHEDULER.set_state("construction")
            return
        # rospy.Timer(
        #     rospy.Duration(0.15), SCHEDULER.release_lidar_occupied, oneshot=True
        # )
        # process_construction()
    elif state is "construction":
        process_construction()
    elif state is "parking":
        process_parking()
Beispiel #5
0
    def move(self):
        """ move the bot """
        if not self._enable_running:
            return

        diff_angular = abs(self._angular - self._angular_smooth)
        if diff_angular > 0.1:
            if self._angular < self._angular_smooth:
                self._angular += 0.1
            elif self._angular > self._angular_smooth:
                self._angular -= 0.1
        elif diff_angular is not 0:
            self._angular = self._angular_smooth
        else:
            SCHEDULER.check_time("speed_smooth", min=0.2)

        diff_speed = abs(self._speed - self._speed_smooth)
        if diff_speed > 0.0002:
            if self._speed < self._speed_smooth:
                self._speed += 0.0002
            elif self._speed > self._speed_smooth:
                self._speed -= 0.0002
        elif diff_speed is not 0:
            self._speed = self._speed_smooth

        if SCHEDULER.debug_option["show_speed_angular"]:
            rospy.logdebug("speed: {:0.2f}  angular: {:0.2f}".format(
                self._speed, self._angular))

        twist = Twist()
        twist.linear.x = self._speed
        twist.linear.y = 0
        twist.linear.z = 0
        twist.angular.x = 0
        twist.angular.y = 0
        twist.angular.z = self._angular
        self._publisher_velocity.publish(twist)
Beispiel #6
0
def process_lidar(lidar_data):
    """ process the lidar image """
    if not SCHEDULER.is_lidar_enable():
        return
    if SCHEDULER.is_lidar_occupied() or TURTLE.is_occupied():
        return

    state = SCHEDULER.get_state()
    if SCHEDULER.debug_option["show_timer"] and (state != "construction"):
        SCHEDULER.check_time("lidar", min=0.4, stop_when_delay=False)

    set_lidar_values(lidar_data)
    state = SCHEDULER.get_state()

    front = get_object_distance("front")
    if (front < 0.15) and (front > 0):
        TURTLE.stop()
        return

    # if STEP >= 10:
    #     process_parking()
    # else:
    #     process_construction()
    # return

    if state is "intersection_left":
        left = get_object_distance("left")
        if (left > 0) and (left < 0.50):
            TURTLE.set_speed("slow")
            SCHEDULER.set_state("to_construction")
    elif state is "to_construction":
        rospy.Timer(rospy.Duration(0.15),
                    SCHEDULER.release_lidar_occupied,
                    oneshot=True)
        process_construction_new()
    elif state is "construction":
        process_construction_new()
    elif state is "parking":
        process_parking()
    elif state is "tunnel":
        process_tunnel()
Beispiel #7
0
    def move(self):
        """ move the bot """
        if not self._enable_running:
            return
        if SCHEDULER.should_pause_motor() > 0:
            print("should_pause_motor: ", SCHEDULER.request_stopping_motor)
            self.fix_delay()
            return

        diff_angular = abs(self._angular - self._angular_smooth)
        if diff_angular > 0.1:
            if self._angular < self._angular_smooth:
                self._angular += 0.1
            elif self._angular > self._angular_smooth:
                self._angular -= 0.1
        elif diff_angular is not 0:
            self._angular = self._angular_smooth
        # else:
        #     SCHEDULER.check_time("speed_smooth", min=0.2)

        diff_speed = abs(self._speed - self._speed_smooth)
        if diff_speed > 0.0005:
            if self._speed < self._speed_smooth:
                self._speed += 0.0005
            elif self._speed > self._speed_smooth:
                self._speed -= 0.0005
        elif diff_speed is not 0:
            self._speed = self._speed_smooth

        if SCHEDULER.debug_option["show_speed_angular"]:
            rospy.logdebug("speed: {:0.2f}  angular: {:0.2f}".format(
                self._speed, self._angular))

        twist = Twist()
        twist.linear.x = self._speed
        twist.linear.y = 0
        twist.linear.z = 0
        twist.angular.x = 0
        twist.angular.y = 0
        twist.angular.z = self._angular
        self._publisher_velocity.publish(twist)
Beispiel #8
0
import rospy
# from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage, LaserScan
import processor
from constants import PATH_RASPICAM, PATH_USBCAM, PATH_LIDAR, PATH_GALAPAGOS_STATE
from turtlebot import TURTLE
from scheduler import SCHEDULER  # ! not used in contest

TURTLE.set_speed("normal")
# TURTLE.disable()
# TURTLE.set_speed("fast")
# TURTLE.set_speed_smooth("normal")

# SCHEDULER.set_state("traffic_light")
SCHEDULER.set_state("default")
# SCHEDULER.set_state("to_intersection")
# SCHEDULER.set_state("construction")
# SCHEDULER.set_state("parking")

rospy.Subscriber(PATH_USBCAM,
                 CompressedImage,
                 processor.process_subcam,
                 queue_size=1)
# rospy.Subscriber(PATH_LIDAR, LaserScan,
#  processor.test_block_escaping, queue_size=1)
# rospy.Subscriber(PATH_LIDAR, LaserScan,
#  processor.process_lidar, queue_size=1)
# rospy.Subscriber(PATH_RASPICAM, CompressedImage,
#                  processor.process_frontcam, queue_size=1)
Beispiel #9
0
#!/usr/bin/env python3

import rospy
# from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage, LaserScan
from geometry_msgs.msg import Twist
import viewer
import processor
from turtlebot import TURTLE
from scheduler import SCHEDULER  # ! not used in contest

from constants import PATH_RASPICAM, PATH_USBCAM, PATH_LIDAR, PATH_GALAPAGOS_STATE

TURTLE.disable()
SCHEDULER.enable_lidar()
SCHEDULER.set_state("default")
# SCHEDULER.set_state("to_intersection")
# SCHEDULER.set_state("traffic_light")

rospy.Subscriber(PATH_USBCAM,
                 CompressedImage,
                 processor.process_subcam,
                 queue_size=1)
rospy.Subscriber(PATH_RASPICAM,
                 CompressedImage,
                 processor.process_frontcam,
                 queue_size=1)
# rospy.Subscriber(PATH_LIDAR, LaserScan,
#                  processor.process_lidar, queue_size=1)
# rospy.Subscriber('/cmd_vel', Twist, viewer.view_speed, queue_size=1)
Beispiel #10
0
def view_subcam(image):
    """ process the subcam image """
    global DIRECTION
    global BUF_ANGULAR

    if not SCHEDULER.is_enable["subcam"]:
        return
    if SCHEDULER.is_subcam_occupied():
        return

    if SCHEDULER.debug_option["show_timer"]:
        SCHEDULER.check_time("subcam", min=0.3)

    info = EYE.see_sub(image)
    # print(EYE.get_front_state() + "  " + str(EYE.get_front_state()))

    if info is None:
        rospy.logwarn("[PROC] No Information!")
        return
    elif False:
        # elif EYE.get_front_state() is "turning":
        # rospy.logdebug("[PROC] turning...")
        center = info["center"]
        slope = info["slope"]

        # left: + right: -
        # if slope > 0:
        #     weight_slope = pow(abs(slope) / 1.8, 0.9) * 2.6
        # else:
        #     weight_slope = - pow(abs(slope) / 1.8, 0.9) * 2.6
        #     # weight_slope = - pow(abs(slope) / 1.8, 0.5) * 2.5

        if slope > 0:
            value = pow(abs(slope) / 1.8, 1.2) * 3.2
        else:
            value = -pow(abs(slope) / 1.8, 1.2) * 3.2

        # if slope > 0:
        #     weight_center = pow(abs(center) / 250, 0.9) * 5.5
        # elif slope < -0:
        #     weight_center = - pow(abs(center) / 250, 0.9) * 5.5
        # else:
        #     weight_center = 0
        #     # weight_center = slope * 1

        if value > 2.6:
            value = 2.6
        elif value < -2.6:
            value = -2.6

        degree = value
        # BUF_ANGULAR.append(value)

        # past_val = BUF_ANGULAR.pop(0) * 0.7
        # if info["has_line"]:
        #     if (value * past_val >= 0) and (abs(value) > abs(past_val)):
        #         degree = value
        #     else:
        #         degree = 0
        #         BUF_ANGULAR = [0] * BUF_SIZE
        # else:
        #     if value > 0:
        #         degree = 2.7
        #     elif value < 0:
        #         degree = -2.7
        #     else:
        #         degree = 0

        # if not info["has_line"]:
        #     if (slope < 0):
        #         degree = 4.12
        #     else:
        #         degree = -4.12

        if SCHEDULER.debug_option["show_center_slope"]:
            rospy.logdebug(
                "[PROC] slope: {:.2f}  w_slope: {:.2f}  degree: {:.2f}  {}".
                format(slope, value, degree, info["has_line"]))
    elif EYE.get_front_state() is "straight":
        # rospy.logdebug("[PROC] going straight...")
        center = info["center"]
        if center < 0:
            value = pow(abs(center) / 150, 0.9) * 2
        elif center > 0:
            value = -pow(abs(center) / 150, 0.9) * 2
        else:
            value = 0

        if value > 1.5:
            value = 1.5
        elif value < -1.5:
            value = -1.5

        degree = value
        if SCHEDULER.debug_option["show_center_slope"]:
            rospy.logdebug(
                "[PROC] center: {:.2f}  w_center: {:.2f}  {}".format(
                    center, degree, info["has_line"]))

    rospy.Timer(rospy.Duration(0.14),
                SCHEDULER.release_subcam_occupied,
                oneshot=True)
    # TURTLE.turn("", 0.13, degree)
    SCHEDULER.release_subcam_occupied()
Beispiel #11
0
def process_frontcam(image):
    """ process the frontcam image """
    if not SCHEDULER.is_frontcam_enable():
        return

    STATE = SCHEDULER.get_state()
    info = EYE.see_front(image)

    if SCHEDULER.debug_option["show_front_info"]:
        rospy.logdebug(info)

    if STATE == "default":
        # if EYE.is_boostable(image):
        process_acceleration(info)
        # signal = is_construction(image)
        # rospy.logdebug(signal)

    if STATE == "traffic_light":
        if is_light_green(image):
            TURTLE.enable()
            SCHEDULER.set_state("to_intersection")
        return

    if STATE == "to_intersection":
        signal = check_left_right_sign(image)
        if signal == "right":
            SCHEDULER.set_state("intersection_right")
        elif signal == "left":
            SCHEDULER.set_state("intersection_left")
        return

    if STATE == "intersection_right":
        # TODO: make algorithms for right
        if EYE.is_boostable(image):
            TURTLE.boost()
            SCHEDULER.set_state("to_construction")
        return

    if STATE == "intersection_left":
        if EYE.is_boostable(image):
            TURTLE.boost()
            SCHEDULER.set_state("to_construction")
        return

    if STATE == "to_construction":
        if EYE.is_boostable(image):
            TURTLE.boost()

        if is_construction(image):
            SCHEDULER.set_state("construction_searching")

    if STATE == "construction_searching":
        pass
Beispiel #12
0
print("  Konkuk University")
print("  Electronics Engineering")
print("  CLUB Boot4Dim x XDeca")
print("  Author: Kim Ji Hyeong")
print("          ([email protected])")
print("          Song Min Soo")
print("          ([email protected])")
print("          Ji Kunyoung")
print("          ([email protected])")
print("====================================")
print()

from scheduler import SCHEDULER

# * init codes
SCHEDULER.import_modules()

if sys.argv[2] == "True":
    rospy.init_node('GA_' + sys.argv[1],
                    anonymous=False,
                    log_level=rospy.DEBUG)
    SCHEDULER.debug_mode = True
    print()
    print("====================================")
    print("  DEBUG MODE Enabled.")
else:
    rospy.init_node('GA_' + sys.argv[1], anonymous=False)
    print()
    print("====================================")
    print("  Node initialized")
Beispiel #13
0
def process_construction():
    """ process construction state """
    global STEP
    global NUM_OBSTRUCTION
    global LANE_TO

    if TURTLE.is_occupied():
        return

    if STEP == 0:
        leftside = get_object_distance("leftside")
        left = get_object_distance("left")
        if leftside > 0:
            rospy.logdebug("[PROC] LIDAR LEFTSIDE: {}".format(
                leftside))
        if (leftside > 0) and (leftside < 0.40):
            STEP = 1
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))

            SCHEDULER.disable_cams()
            rospy.loginfo("[PROC] construction state started.")
            TURTLE.go_forward(3.5)
            rospy.sleep(rospy.Duration(0.5))
            return
        else:
            return
        # elif (left > 0) and (left < 1.5):
        #     STEP = 2
        #     rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        #     return

        # rospy.sleep(rospy.Duration(2))
    elif STEP == 1:
        TURTLE.set_speed("normal")
        TURTLE.set_speed_smooth("slow")
        left = get_object_distance("left")
        if left > 0:
            rospy.logdebug("[PROC] LIDAR LEFT: {}".format(
                left))
        if (left < 0.50) or (left > 1.5):
            return
        else:
            STEP = 3
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
            return
    elif STEP == 2:
        # TODO: write code for first left lane
        pass
    elif STEP == 3:
        TURTLE.go_turn("left", speed=0.11)
        LANE_TO = "left"
    elif STEP == 4:
        TURTLE.set_speed("normal")
        reverse_lane()
        biased = get_object_distance(LANE_TO + "_biased")
        if biased > 0:
            rospy.logdebug("[PROC] LIDAR {:s}_BIASED: {}"
                           .format(LANE_TO, biased))
        reverse_lane()

        if (biased == 0) or (biased > 0.30):
            return

        TURTLE.go_turn(LANE_TO, duration=0.5, angular=4.2)
        TURTLE.set_speed("normal")
        # TURTLE.set_speed("fast")
        # if LANE_TO is "left":
        #     TURTLE.turn(0.13, 1.3, consuming_time=1.5)
        # else:
        #     TURTLE.turn(0.13, -1.3, consuming_time=1.5)
        rospy.sleep(rospy.Duration(2.2))
        reverse_lane()

    elif STEP == 5:
        TURTLE.go_turn(LANE_TO, duration=0.7, angular=4.2)
        TURTLE.set_speed("normal")
        # TURTLE.set_speed("fast")
        # if LANE_TO is "left":
        #     TURTLE.turn(0.13, 1.3, consuming_time=1.5)
        # else:
        #     TURTLE.turn(0.13, -1.3, consuming_time=1.5)

        NUM_OBSTRUCTION += 1
        if NUM_OBSTRUCTION < 2:
            STEP = 4
            return

    elif STEP == 6:
        TURTLE.go_forward(1)
        rospy.sleep(rospy.Duration(0.6))
    elif STEP == 7:
        TURTLE.go_turn("left", duration=0.8, angular=3)
    elif STEP == 8:
        TURTLE.set_speed("fast")
        TURTLE.set_speed_smooth("normal")
        TURTLE.go_forward(5)
        rospy.sleep(rospy.Duration(0.5))
    elif STEP == 9:
        # NOTE: turn to parking step
        STEP = 10
        SCHEDULER.set_state("parking")
    else:
        return
    STEP += 1
    rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
Beispiel #14
0
def process_parking():
    """ process parking state """
    global STEP

    # if TURTLE.is_occupied():
    #     return

    if STEP == 10:
        frontleft = get_object_distance("frontleft")
        frontright = get_object_distance("frontright")
        left = get_object_distance("left")
        right = get_object_distance("right")

        if SCHEDULER.debug_option["show_parking_lidar"]:
            rospy.logdebug("frontright: {:.2f}  frontleft: {:.2f}".format(
                frontright, frontleft))

        if (frontleft > 0) and (frontleft < 1.0):
            STEP = 11
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        elif (frontright > 0) and (frontright < 1.0):
            STEP = 12
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        if (left > 0) and (left < 0.5):
            STEP = 13
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        elif (right > 0) and (right < 0.5):
            STEP = 14
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))

        # NOTE: return is needed to prevent executing STEP += 1
        return

    elif STEP == 11:
        SCHEDULER.disable_cams()
        TURTLE.set_speed("normal")
        TURTLE.go_forward(2.5)
        TURTLE.go_turn("right", 2)
        STEP = 15
        rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        return

    elif STEP == 12:
        # Edit HERE
        return

    elif STEP == 13:
        # Edit HERE
        return

    elif STEP == 14:
        SCHEDULER.disable_cams()
        TURTLE.set_speed("normal")
        TURTLE.go_turn("left", angular=1.8, duration=1.2)
        STEP = 15
        rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        return

    elif STEP == 15:
        # Edit HERE
        return

    elif STEP == 16:
        # Edit HERE
        return

    elif STEP == 17:
        # Edit HERE
        return

    elif STEP == 18:
        # Edit HERE
        return

    elif STEP == 19:
        TURTLE.set_speed("fast")
        SCHEDULER.set_state("zigzag")
    else:
        return
    STEP += 1
Beispiel #15
0
def process_subcam(image):
    """ process the subcam image """

    if not SCHEDULER.is_subcam_enable():
        return
    if SCHEDULER.is_subcam_occupied() or TURTLE.is_occupied():
        return

    info = EYE.see_sub(image)

    if SCHEDULER.debug_option["show_timer"]:
        # Check delay only if has line
        # SCHEDULER.check_time("subcam", min=0.28, stop_when_delay=info["has_line"])
        SCHEDULER.check_time("subcam", min=0.28, stop_when_delay=False)

    if info is None:
        rospy.logwarn("[PROC] No Information!")
        return

    center = info["center"]
    slope = info["slope"]

    if slope < -0.5:
        limit = 1.6
        amplitude = 1.0
    else:
        limit = 1.2
        amplitude = 0.8

    limit /= 1.9
    # amplitude /= 2

    state = SCHEDULER.get_state()
    if (EYE.get_front_state() == "straight") and (state is not "zigzag"):
        if (abs(center) < 30) and slope < -0.4:
            degree = pow(abs(slope) / 1.8, 1.1) * amplitude
        elif center < 0:
            degree = pow(abs(center) / 100, 2.0) * amplitude / 2
        elif center > 0:
            degree = -pow(abs(center) / 100, 2.0) * amplitude
        else:
            degree = 0
    else:
        if slope < 0:
            degree = -pow(abs(slope) / 1.8, 2.9) * amplitude * 28.0
        else:
            degree = pow(abs(slope) / 1.0, 1.2) * amplitude * 4.0

        # degree = pow(abs(slope) / 1.8, 0.9) * amplitude
        # elif center < 0:
        #     degree = pow(abs(center) / 100, 1.9) * amplitude
        # elif center > 0:
        #     degree = - pow(abs(center) / 100, 1.9) * amplitude
        # else:
        #     degree = 0

    buf_sum = sum(BUF_ANGULAR)
    if EYE.get_front_state() == "straight":
        adjust_angular = BUF_ANGULAR.pop(0) * 0.9
        BUF_ANGULAR.append(degree)
        degree -= adjust_angular
        # if abs(buf_sum) > 1:
    else:
        reset_buffer()
        adjust_angular = 0

    degree = max(min(degree, limit), -limit)

    if not info["has_line"]:
        if center > 200:
            # degree = -1.2
            degree = -1.1  # For enhancing frequency
        # elif EYE.get_front_state() == "straight":
        #     degree = 0.6
        else:
            # degree = 1.4
            degree = 1.3  # For enhancing frequency
    # if not info["has_line"]:
    #     if center < -55:
    #         # degree = 1.6
    #         degree = 1.4  # For slow speed
    #     elif center > 50:
    #         degree = -1.2
    #         # degree = -1.2  # For slow speed
    #     # elif center > 19:
    #     #     degree = -1.2
    #     else:
    #         degree = 1.4

    if SCHEDULER.debug_option["show_center_slope"]:
        rospy.logdebug(
            "[PROC] center: {:.2f}  slope: {:.2f}  degree: {:.2f}  adj: {:.2f}  buf_sum: {:.2f}  {}  {}"
            .format(center, slope, degree, adjust_angular, buf_sum,
                    EYE.get_front_state(), info["has_line"]))

    rospy.Timer(rospy.Duration(0.15),
                SCHEDULER.release_subcam_occupied,
                oneshot=True)
    TURTLE.turn(0.13, degree)
Beispiel #16
0
def process_parking():
    """ process parking state """
    global STEP

    # if TURTLE.is_occupied():
    #     return

    if STEP == 10:
        frontleft = get_object_distance("frontleft")
        frontright = get_object_distance("frontright")

        if SCHEDULER.debug_option["show_parking_lidar"]:
            rospy.logdebug("front: {:.2f}  frontleft: {:.2f}".format(
                get_object_distance("front"), get_object_distance("frontleft")
            ))

        if (frontleft > 0) and (frontleft < 0.5):
            STEP = 11
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        elif (frontright > 0) and (frontright < 0.5):
            STEP = 12
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        # NOTE: return is needed to prevent executing STEP += 1
        return

    elif STEP == 11:
        SCHEDULER.disable_cams()
        TURTLE.set_speed("normal")
        TURTLE.go_turn("right", 2)
        STEP = 13
        rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        return

    elif STEP == 12:
        SCHEDULER.disable_cams()
        TURTLE.set_speed("normal")
        TURTLE.go_turn("left", 2)
        STEP = 13
        rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
        return

    elif STEP == 13:
        TURTLE.go_forward(0.7)
        # for stopping for a while
        rospy.sleep(rospy.Duration(0.5))

    elif STEP == 14:
        TURTLE.go_backward(1.0)
    elif STEP == 15:
        TURTLE.go_turn_backward(1.1)
    elif STEP == 16:
        TURTLE.set_speed("normal")
        TURTLE.go_forward(1)
    elif STEP == 17:
        # TURTLE.set_speed("fast")
        # TURTLE.set_speed_smooth("normal")
        TURTLE.set_speed("normal")
        SCHEDULER.set_state("zigzag")
    else:
        return
    STEP += 1
Beispiel #17
0
def process_subcam(image):
    """ process the subcam image """

    if not SCHEDULER.is_subcam_enable():
        return
    if EYE.is_sub_occupied() or TURTLE.is_occupied():
        return

    if SCHEDULER.debug_option["show_timer"]:
        SCHEDULER.check_time("subcam", min=0.25)

    info = EYE.see_sub(image)

    if info is None:
        # rospy.logwarn("[PROC] No Information!")
        return

    center = info["center"]
    slope = info["slope"]

    if slope < -0.5:
        # limit = 1.6
        limit = 1.5  # For slow speed
        amplitude = 1.6
    # elif slope > 0.5:
    #     limit = 1.6
    #     amplitude = 1.2
    else:
        # limit = 1.2
        # amplitude = 1.2
        limit = 1.2  # For slow speed
        amplitude = 1.2  # For slow speed

    if EYE.get_front_state() == "straight":
        if (abs(center) < 30) and slope < -0.4:
            degree = pow(abs(slope) / 1.8, 1.1) * amplitude
        elif center < 0:
            degree = pow(abs(center) / 100, 2.0) * amplitude
        elif center > 0:
            degree = - pow(abs(center) / 100, 2.0) * amplitude
        else:
            degree = 0
    else:
        if (abs(center) < 30) and slope < -0.4:
            degree = pow(abs(slope) / 1.8, 0.9) * amplitude
        elif center < 0:
            degree = pow(abs(center) / 100, 1.9) * amplitude
        elif center > 0:
            degree = - pow(abs(center) / 100, 1.9) * amplitude
        else:
            degree = 0

    buf_sum = sum(BUF_ANGULAR)
    if EYE.get_front_state() == "straight":
        adjust_angular = BUF_ANGULAR.pop(0) * 0.9
        BUF_ANGULAR.append(degree)
        degree -= adjust_angular
        # if abs(buf_sum) > 1:
    else:
        reset_buffer()
        adjust_angular = 0

    degree = max(min(degree, limit), -limit)

    if not info["has_line"]:
        if center < -55:
            # degree = 1.6
            degree = 1.4  # For slow speed
        elif center > 50:
            degree = -1.2
            # degree = -1.2  # For slow speed
        elif center > 10:  # editing...
            degree = -0.9  # editing...
        else:
            degree = 1.4

    # if SCHEDULER.debug_option["show_center_slope"]:
    #     rospy.logdebug(
    #         "[PROC] center: {:.2f}  slope: {:.2f}  degree: {:.2f}  adj: {:.2f}  buf_sum: {:.2f}  {}  {}".format(
    #             center, slope, degree, adjust_angular, buf_sum, EYE.get_front_state(), info["has_line"])
    #     )

    rospy.Timer(
        rospy.Duration(0.15), EYE.release_sub_occupied, oneshot=True
    )
    TURTLE.turn(0.13, degree)
Beispiel #18
0
def process_construction_new():
    """ process construction state """
    global STEP
    global NUM_OBSTRUCTION
    global LANE_TO
    global BUF_ANGULAR
    global BUF_SIZE

    if TURTLE.is_occupied():
        return

    if STEP == 0:
        # TURTLE.set_speed("normal")
        leftside = get_object_distance("leftside")
        left = get_object_distance("left")

        if leftside > 0:
            rospy.logdebug("[PROC] LIDAR LEFTSIDE: {}".format(leftside))

        if (leftside > 0) and (leftside < 0.50) and (left > 1.00):
            EYE.check_yellow = False
            SCHEDULER.set_state("construction")
            rospy.loginfo("[PROC] construction state started.")

            STEP = 1
            rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
            TURTLE.go_forward(3.5)
            return
        else:
            return

    elif STEP == 1:
        TURTLE.set_speed("normal")
        TURTLE.set_speed_smooth("slow")
        TURTLE.turn(0.13, 0)

        left = get_object_distance("left")
        leftback = get_object_distance("leftback")
        rospy.logdebug("[PROC] LIDAR LEFT: {:.2f} LEFTBACK: {:.2f}}".format(
            left, leftback))
        if (left > 0) and (left < 0.50):
            return
        else:
            TURTLE.set_speed("slow")
            if (leftback > 0.5):
                # TURTLE.go_forward(2.5)
                STEP = 3
                rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
            return

    elif STEP == 2:
        # TODO: write code for first left lane
        pass

    elif STEP == 3:
        TURTLE.set_speed("normal")
        TURTLE.set_speed_smooth("stop")
        front = get_object_distance("front")
        right_biased = get_object_distance("right_biased")

        if (front > 1.0) and (right_biased < 1.0) and (right_biased > 0.0):
            print("passed", front, right_biased)
            print("BUF_SIZE: ", BUF_SIZE)
            BUF_SIZE = 15
            reset_buffer()
            TURTLE.set_speed_smooth("slow")
            pass
        elif front * right_biased == 0:
            return
        else:
            TURTLE.set_speed("stop")
            print("turning...", front, right_biased)
            TURTLE.turn(0.13, 1.0)
            # rospy.sleep(rospy.Duration(5.0))
            return
        # TURTLE.go_turn("left")

    elif STEP == 4:
        right_biased = get_object_distance("right_biased")
        left_biased = get_object_distance("left_biased")
        front = get_object_distance("front")

        if right_biased == 0.0:
            right_biased = 3.0
        if left_biased == 0.0:
            left_biased = 3.0
        if front == 0.0:
            front = 3.0
        elif front < 0.2:
            TURTLE.set_speed_smooth("stop")
        else:
            TURTLE.set_speed_smooth("slow")

        min_distance = min(right_biased, left_biased)

        degree = 0
        if (front < 1.0):
            degree += max(pow(1.0 - front, 2), 0)
        else:
            degree += max(0.5 - min_distance, 0) * 3
        # if min_distance < 0.5:
        # degree += max((0.5 - min_distance), 0) * 1.5
        # elif (min_distance > 1.0) and (min_distance < 3.0):
        # degree = 0.2

        if (left_biased == min_distance) and (min_distance < 0.5):
            degree *= -1

        # max_distance = max(right_biased, left_biased)
        # if (left_biased == max_distance):
        #     degree *= -1

        # degree = 0
        # if min_distance > 0 and min_distance < 0.5:
        #     if right_biased > left_biased:
        #         degree = (0.5 - min_distance) * (-7)
        #         LANE_TO = "right"
        #     elif right_biased < left_biased:
        #         degree = (0.5 - min_distance) * (7)
        #         LANE_TO = "left"

        # if is_left_crashable():
        #     degree = -1.7
        # elif is_right_crashable():
        #     degree = 1.7

        degree *= 3
        degree = max(min(degree, 2.0), -2.0)
        BUF_ANGULAR.append(degree)
        degree -= BUF_ANGULAR.pop(0)
        print("BUF_ANGULAR:", BUF_ANGULAR)
        # if degree != 0:
        #     BUF_ANGULAR.append(degree)

        # elif len(BUF_ANGULAR) > 9:
        #     STEP = 5

        if SCHEDULER.debug_option["show_construction_lidar"]:
            rospy.logdebug(
                "[PROC] r_based: {:.2f}  l_based: {:.2f}  min: {:.2f}  front: {:.2f}  deg: {:.2f}"
                .format(right_biased, left_biased, min_distance, front,
                        degree))

        TURTLE.turn(0.13, degree)
        return

    elif STEP == 5:
        print("[StEP 5]")
        if len(BUF_ANGULAR) > 0:
            TURTLE.turn(0.13, -BUF_ANGULAR.pop(0))
            return
        else:
            front = get_object_distance("front")
            print(front)
            # if (front > 0) and (front < 1.0):
            #     if LANE_TO == "right":
            #         TURTLE.turn(0.13, 0.8)
            #     else:
            #         TURTLE.turn(0.13, -0.8)
            #     return
            # else:
            if NUM_OBSTRUCTION < 1:
                NUM_OBSTRUCTION += 1
                STEP = 4
                return

    elif STEP == 6:
        TURTLE.go_forward(1)
        TURTLE.set_speed("normal")
        TURTLE.go_turn("left")
        TURTLE.set_speed("normal")
        TURTLE.set_speed("fast")
        TURTLE.set_speed_smooth("normal")
        TURTLE.go_forward(5)

        STEP = 10
        SCHEDULER.set_state("parking")
        BUF_SIZE = 3
        reset_buffer()
        return

    STEP += 1
    rospy.logdebug("[PROC] STEP changed to {}".format(STEP))
Beispiel #19
0
def process_frontcam(image):
    """ process the frontcam image """
    if not SCHEDULER.is_frontcam_enable():
        return

    state = SCHEDULER.get_state()
    info = EYE.see_front(image)

    if SCHEDULER.debug_option["show_front_info"]:
        rospy.logdebug(info)

    if state == "default":
        # if EYE.is_boostable(image):
        process_acceleration(info)
        # SCHEDULER.set_state("to_intersection")
        # signal = is_construction(image)
        # rospy.logdebug(signal)
        # if is_construction(image):
        #     TURTLE.boost()
        #     SCHEDULER.set_state("construction")

    if state == "traffic_light":
        if is_light_green(image):
            TURTLE.enable()
            SCHEDULER.set_state("to_intersection")
        return

    # NOTE: temporary settings:
    if state == "to_intersection":
        # rospy.Timer(rospy.Duration(35), SCHEDULER.enable_lidar, oneshot=True)
        SCHEDULER.set_state("intersection_left")

    # if state == "to_intersection":
    #     signal = check_left_right_sign(image)
    #     if signal == "right":
    #         SCHEDULER.set_state("intersection_right")
    #     elif signal == "left":
    #         SCHEDULER.set_state("intersection_left")
    #     return

    if state == "intersection_right":
        # TODO: make algorithms for right
        if EYE.is_boostable(image):
            TURTLE.boost()
            SCHEDULER.set_state("to_construction")
        return

    # NOTE: temporary settings:
    if state == "intersection_left":
        # if EYE.is_boostable(image):
        #     TURTLE.boost()
        #     SCHEDULER.set_state("to_construction")
        return

    if state == "to_construction":
        # if EYE.is_boostable(image):
        # TURTLE.boost()
        EYE.check_yellow = True
        # if is_construction(image):
        # SCHEDULER.set_state("construction_searching")

    if state == "construction_searching":
        pass
Beispiel #20
0
#!/usr/bin/env python3

import rospy
# from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage, LaserScan
from geometry_msgs.msg import Twist
import viewer
import processor
from turtlebot import TURTLE
from scheduler import SCHEDULER  # ! not used in contest

from constants import PATH_RASPICAM, PATH_USBCAM, PATH_LIDAR, PATH_GALAPAGOS_STATE

TURTLE.disable()
SCHEDULER.set_state("to_construction")
# SCHEDULER.set_state("parking")
# SCHEDULER.set_state("to_intersection")
# SCHEDULER.set_state("traffic_light")

rospy.Subscriber(PATH_USBCAM, CompressedImage,
                 processor.process_subcam, queue_size=1)
rospy.Subscriber(PATH_RASPICAM, CompressedImage,
                 processor.process_frontcam, queue_size=1)
rospy.Subscriber(PATH_LIDAR, LaserScan,
                 processor.process_lidar, queue_size=1)
# rospy.Subscriber('/cmd_vel', Twist, viewer.view_speed, queue_size=1)
from std_msgs.msg import String
from time import sleep

import processor
from constants import PATH_RASPICAM, PATH_USBCAM, PATH_LIDAR, PATH_GALAPAGOS_STATE
from turtlebot import TURTLE
from scheduler import SCHEDULER  # ! not used in contest

# TURTLE.disable()
# TURTLE.set_speed("little")
TURTLE.set_speed("normal")
# TURTLE.set_speed("slower")
TURTLE.set_speed_smooth("normal")

# SCHEDULER.set_state("traffic_light")
SCHEDULER.set_state("to_intersection")
# SCHEDULER.set_state("zigzag")
# SCHEDULER.set_state("to_construction")
# SCHEDULER.set_state("construction")
# SCHEDULER.set_state("parking")

rospy.Subscriber(PATH_USBCAM,
                 CompressedImage,
                 processor.process_subcam,
                 queue_size=1)
rospy.Subscriber(PATH_LIDAR, LaserScan, processor.process_lidar, queue_size=1)
rospy.Subscriber(PATH_RASPICAM,
                 CompressedImage,
                 processor.process_frontcam,
                 queue_size=1)