Exemplo n.º 1
0
    def __init__(
        self,
        left_motor_port: str = OUTPUT_B,
        right_motor_port: str = OUTPUT_C,
        motor_class=LargeMotor,
        polarity: str = Motor.POLARITY_NORMAL,
        ir_sensor_port: str = INPUT_4,
        # sites.google.com/site/ev3devpython/learn_ev3_python/using-sensors
        ir_beacon_channel: int = 1):
        self.left_motor = LargeMotor(address=left_motor_port)
        self.right_motor = LargeMotor(address=right_motor_port)

        self.tank_driver = \
            MoveTank(
                left_motor_port=left_motor_port,
                right_motor_port=right_motor_port,
                motor_class=motor_class)

        self.steer_driver = \
            MoveSteering(
                left_motor_port=left_motor_port,
                right_motor_port=right_motor_port,
                motor_class=motor_class)

        self.left_motor.polarity = self.right_motor.polarity = \
            self.tank_driver.left_motor.polarity = \
            self.tank_driver.right_motor.polarity = \
            self.steer_driver.left_motor.polarity = \
            self.steer_driver.right_motor.polarity = polarity

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.tank_drive_ir_beacon_channel = ir_beacon_channel
Exemplo n.º 2
0
    def __init__(self,
                 jaw_motor_port: str = OUTPUT_A,
                 left_motor_port: str = OUTPUT_B,
                 right_motor_port: str = OUTPUT_C,
                 touch_sensor_port: str = INPUT_1,
                 color_sensor_port: str = INPUT_3,
                 ir_sensor_port: str = INPUT_4,
                 ir_beacon_channel: int = 1):
        self.jaw_motor = MediumMotor(address=jaw_motor_port)

        self.left_motor = LargeMotor(address=left_motor_port)
        self.right_motor = LargeMotor(address=right_motor_port)
        self.tank_driver = MoveTank(left_motor_port=left_motor_port,
                                    right_motor_port=right_motor_port,
                                    motor_class=LargeMotor)
        self.steer_driver = MoveSteering(left_motor_port=left_motor_port,
                                         right_motor_port=right_motor_port,
                                         motor_class=LargeMotor)

        self.touch_sensor = TouchSensor(address=touch_sensor_port)
        self.color_sensor = ColorSensor(address=color_sensor_port)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel

        self.speaker = Sound()

        self.roaring = False
        self.walk_speed = self.NORMAL_WALK_SPEED
Exemplo n.º 3
0
def run(power, target, kp, kd, ki, direction, minRef, maxRef):
    lastError = error = integral = 0
    left_motor.run_direct()
    right_motor.run_direct()

    free_count = 0
    isFree = False

    while not btn.any() and not isFree:
        # if ts.value():
        # 	print ('Breaking loop') # User pressed touch sensor
        # 	break
        refRead = col.value()
        error = target - (100 * (refRead - minRef) / (maxRef - minRef))
        derivative = error - lastError
        lastError = error
        integral = float(0.5) * integral + error
        course = (kp * error + kd * derivative + ki * integral) * direction
        for (motor, pow) in zip((left_motor, right_motor),
                                steering2(course, power)):
            motor.duty_cycle_sp = pow
        free_count, isFree = freeSpot(free_count, isFree)
        sleep(0.01)  # Aprox 100Hz

    left_motor.stop()
    right_motor.stop()

    sleep(2)
    steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
    steer_pair.on_for_degrees(steering=0,
                              speed=15,
                              degrees=200,
                              brake=False,
                              block=True)
    parking.back_parl_park(steer_pair)
Exemplo n.º 4
0
def acceleration(
    degrees,
    finalSpeed,
    steering=0,
    robot=MoveSteering(OUTPUT_A, OUTPUT_B),
    motorA=LargeMotor(OUTPUT_A)
):  # Function to accelerate while moving so the robot can get traction before moving fast
    """Function to accelerate the robot and drive a specific distance"""

    motorA.reset()  #reseting how many degrees the robot has moved
    motorA.position = 0  # setting how many degrees the robot has moved

    accelerateDegree = degrees * 0.8
    # declerationDegree = degrees * 0.2'
    speed = 0  #starting speed
    while motorA.position < degrees and False == Constants.STOP:  #while the robot hasen't moved the target amount of degree's(distance)
        if motorA.position < accelerateDegree and False == Constants.STOP:
            if speed < finalSpeed:  # while the robot hasen't accelerated to the target speed
                speed += 5  #speed up
                robot.on(steering=steering, speed=speed)  # Start Moving
                sleep(0.1)  #wait so that it doesn't accelerate immidiatly
            else:
                robot.on(steering=steering,
                         speed=finalSpeed)  # otherwise just keep moving
                sleep(0.01)
        elif False == Constants.STOP:
            if speed > 10:
                speed -= 5
                robot.on(steering=steering, speed=speed)
                sleep(0.05)
            else:
                robot.on(steering=steering, speed=speed)
                sleep(0.01)

    robot.off()  # Stop Moving
Exemplo n.º 5
0
def lineFollowRightPID(degrees,
                       kp=1.0,
                       ki=0,
                       kd=0,
                       color=ColorSensor(INPUT_1),
                       robot=MoveSteering(OUTPUT_A, OUTPUT_B),
                       motorA=LargeMotor(OUTPUT_A)):
    """Function to follow line using color sensor on right side of line"""

    color.mode = 'COL-REFLECT'
    motorA.reset()
    motorA.position = 0

    lasterror = 0
    while motorA.position < degrees and False == Constants.STOP:
        error = (
            (Constants.WHITE + Constants.BLACK) / 2
        ) - color.reflected_light_intensity  # colorLeft.reflected_light_intensity - colorRight.reflected_light_intensity
        #correction = error * GAIN  # correction = PID(error, lasterror, kp, ki, kd)
        correction = PIDMath(error=error,
                             lasterror=lasterror,
                             kp=kp,
                             ki=ki,
                             kd=kd)
        if correction > 100: correction = 100
        if correction < -100: correction = -100
        robot.on(steering=correction, speed=20)
        lasterror = error
    robot.off()
Exemplo n.º 6
0
def constructor():
    sound = Sound()
    motor_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
    touch_sensor_left = TouchSensor(INPUT_1)
    touch_sensor_right = TouchSensor(INPUT_4)

    return sound, motor_pair, touch_sensor_left, touch_sensor_right
Exemplo n.º 7
0
 def MoveBackward(self, steering=0, speed=40):
     steer_pair = MoveSteering(OUTPUT_A, OUTPUT_B, motor_class=LargeMotor)
     steer_pair.on_for_seconds(steering=0,
                               speed=20,
                               seconds=2,
                               brake=True,
                               block=True)
Exemplo n.º 8
0
def lineFollowTillIntersectionRightPID(kp=1.0,
                                       ki=0,
                                       kd=0,
                                       color=ColorSensor(INPUT_1),
                                       color2=ColorSensor(INPUT_3),
                                       robot=MoveSteering(OUTPUT_A, OUTPUT_B)):
    """Function to follow a line till it encounters intersection"""  # *an intersection is a line that is going through the line that the robot is following

    color.mode = 'COL-REFLECT'  #setting color mode
    color2.mode = 'COL-REFLECT'  #setting color mode
    lasterror = 0
    sound = Sound()
    while color2.reflected_light_intensity <= Constants.WHITE and False == Constants.STOP:
        error = (
            (Constants.WHITE + Constants.BLACK) / 2
        ) - color.reflected_light_intensity  # colorLeft.reflected_light_intensity - colorRight.reflected_light_intensity
        # correction = error * GAIN  # correction = PID(error, lasterror, kp, ki, kd)
        correction = PIDMath(error=error,
                             lasterror=lasterror,
                             kp=kp,
                             ki=ki,
                             kd=kd)
        if correction > 100: correction = 100
        if correction < -100: correction = -100
        robot.on(speed=20, steering=correction)
        lasterror = error
    sound.beep()
    robot.off()
Exemplo n.º 9
0
 def __init__(self, baseSpeed=500, dt=50):
     self.leftMotor = LargeMotor(OUTPUT_C)
     self.rightMotor = LargeMotor(OUTPUT_A)
     self.steeringDrive = MoveSteering(OUTPUT_C, OUTPUT_A)
     self.craneMotor = MediumMotor(OUTPUT_B)
     self.baseSpeed = baseSpeed
     self.dt = dt
Exemplo n.º 10
0
def acceleration(degrees, finalSpeed, steering = 0, robot = MoveSteering(OUTPUT_A, OUTPUT_B), motorA = LargeMotor(OUTPUT_A)):
    """Function to accelerate the robot and drive a specific distance"""

    motorA.reset()
    motorA.position = 0
    accelerateDegree = degrees * 0.8
    # declerationDegree = degrees * 0.2'
    speed = 0
    while motorA.position < degrees and False == Constants.STOP:
        if motorA.position < accelerateDegree and False == Constants.STOP:
            if speed < finalSpeed:
                speed += 5
                robot.on(steering = steering, speed = speed)
                sleep(0.1)
            else:
                robot.on(steering = steering, speed = finalSpeed)
                sleep(0.01)
        elif False == Constants.STOP:
            if speed > 10:
                speed -= 5
                robot.on(steering = steering, speed = speed)
                sleep(0.05)
            else:
                robot.on(steering = steering, speed = speed)
                sleep(0.01)
    
    robot.off()
Exemplo n.º 11
0
    def __init__(self):
        """
        Performs Alexa Gadget initialization routines and ev3dev resource allocation.
        """
        super().__init__()

        # Robot state
        self.patrol_mode = False
        self.follow_mode = False

        # Internal Variables
        self.light_intensity = 0
        self.batt_voltage = 0

        # Connect two large motors on output ports B and C
        #self.drive = MoveTank(OUTPUT_D, OUTPUT_C)
        self.steerdrive = MoveSteering(OUTPUT_C, OUTPUT_D)
        self.leds = Leds()
        self.ir = InfraredSensor()
        self.ir.mode = 'IR-SEEK'
        self.touch = TouchSensor()
        self.light = ColorSensor(address='ev3-ports:in4')
        self.sound = Sound()

        # Start threads
        threading.Thread(target=self._patrol_thread, daemon=True).start()
        threading.Thread(target=self._follow_thread, daemon=True).start()
        threading.Thread(target=self._pat_thread, daemon=True).start()
        threading.Thread(target=self._power_thread, daemon=True).start()
        threading.Thread(target=self._light_sensor_thread, daemon=True).start()
Exemplo n.º 12
0
def lineSquare(
        leftMotor=LargeMotor(OUTPUT_A),
        rightMotor=LargeMotor(OUTPUT_B),
        robot=MoveSteering(OUTPUT_A, OUTPUT_B),
        colorLeft=ColorSensor(INPUT_1),
        colorRight=ColorSensor(INPUT_3)):  # look below for what this is doing
    '''Function to square the robot precisely on a black line'''
    if True == Constants.STOP: return
    colorLeft.mode = 'COL-REFLECT'  #setting colorsenso mode
    colorRight.mode = 'COL-REFLECT'  #setting colorsensor mode

    counter = 0  # setting that we've linesquared 0 times so far
    while counter < 2 and False == Constants.STOP:  # linesquare 2 times
        left = Thread(target=MoveLeftMotor)
        right = Thread(
            target=MoveRightMotor
        )  # Linesquare left and right motor at the same time using thread
        left.start()  #starting the Thread
        right.start()  # starting the Thread
        left.join()  #join so we wait for this thread to finish
        right.join()  #join so we wait for this thread to finish
        accelerationMoveBackward(
            steering=0, finalSpeed=20, degrees=DistanceToDegree(
                1))  # move backward so we can do it again for extra precision
        counter += 1
Exemplo n.º 13
0
 def __init__(self,
              distance_sensors: EV3DistanceDetectors,
              gyro: Gyro,
              logger=None,
              **kwargs):
     self._logger = logger or logging.getLogger(__name__)
     self._distance_sensors = distance_sensors
     self._gyro = gyro
     self._motor_pair = MoveSteering(OUTPUT_A, OUTPUT_B)
     self._position_corrector = PositionCorrector(self._motor_pair,
                                                  self._gyro)
     self._maze_square_length_mm = KwArgsUtil.kwarg_or_default(
         180, 'maze_square_length_mm', **kwargs)
     self._move_forward_speed_rpm = KwArgsUtil.kwarg_or_default(
         60, 'move_forward_speed_rpm', **kwargs)
     self._motor_pair_polarity_factor = KwArgsUtil.kwarg_or_default(
         1, 'motor_pair_polarity_factor', **kwargs)
     self._turn_speed_rpm = KwArgsUtil.kwarg_or_default(
         50, 'turn_speed_rpm', **kwargs)
     self._wheel_diameter_mm = KwArgsUtil.kwarg_or_default(
         56, 'wheel_diameter_mm', **kwargs)
     self._wheel_circumference_mm = math.pi * self._wheel_diameter_mm
     self._wheelbase_width_at_centers_mm = KwArgsUtil.kwarg_or_default(
         97.5, 'wheelbase_width_at_centers_mm', **kwargs)
     self._turns_until_next_angle_corretion = KwArgsUtil.kwarg_or_default(
         3, 'turns_until_next_angle_corretion', **kwargs)
     self._angle_corretcion_speed = KwArgsUtil.kwarg_or_default(
         25, 'angle_corretcion_speed', **kwargs)
     self._angle_correction_move_backward_mm = KwArgsUtil.kwarg_or_default(
         80.0, 'angle_correction_move_backward_mm', **kwargs)
     self._angle_correction_move_forward_mm = KwArgsUtil.kwarg_or_default(
         20.0, 'angle_correction_move_forward_mm', **kwargs)
     self._wait_for_motors_and_gyro_after_move_sec = KwArgsUtil.kwarg_or_default(
         0.1, 'wait_for_motors_and_gyro_after_move_sec', **kwargs)
Exemplo n.º 14
0
 def __init__(self,
              ms=MoveSteering(OUTPUT_A, OUTPUT_B),
              cs=ColorSensor(address=INPUT_1),
              isensor=InfraredSensor(INPUT_2)):
     self.steering_drive = ms
     self.color_sensor = cs
     self.infrared_sensor = isensor
Exemplo n.º 15
0
 def __init__(self,
              movesteering=MoveSteering(OUTPUT_A, OUTPUT_B),
              colorsensor):
     self.steering_drive = movesteering
     self.color_sensor = colorsensor
     self.last_search_direction = 1
     self.last_brightness = 1000
Exemplo n.º 16
0
 def __init__(self,
              left_address=OUTPUT_A,
              right_address=OUTPUT_B,
              touch_address=INPUT_3):
     self._wheels = MoveSteering(left_address, right_address)
     self._left = self._wheels.left_motor
     self._right = self._wheels.right_motor
     self._touch = TouchSensor(touch_address)
Exemplo n.º 17
0
 def __init__(self):
     self.server_address = (str(sys.argv[1]), 5000)
     self.sensor = InfraredSensor()
     self.motor = MediumMotor(OUTPUT_D)
     self.drive_motor = MoveTank(OUTPUT_B, OUTPUT_C)
     self.turn_motor = MoveSteering(OUTPUT_B, OUTPUT_C)
     self.moved = 0
     self.turned = 0
Exemplo n.º 18
0
 def __init__(self):
     """Constructor for the internal state of the robot, e.g. in which direction he is
     currently looking"""
     self.direction = Directions.up
     self.gy = GyroSensor()
     self.steer_pair = MoveSteering(OUTPUT_A, OUTPUT_D, motor_class= LargeMotor)
     self.zero_point = ColorSensor().reflected_light_intensity
     self.s = Sensors()
Exemplo n.º 19
0
def MoveForwardBlack(distanceInCm, colorLeft = ColorSensor(INPUT_1), robot = MoveSteering(OUTPUT_A, OUTPUT_B), motorA = LargeMotor(OUTPUT_A)):
    deg = DistanceToDegree(distanceInCm)
    motorA.reset()
    motorA.position = 0
    while colorLeft.reflected_light_intensity > Constants.BLACK and motorA.position < deg and False == Constants.STOP:
        #print("stop=" + str(Constants.STOP), file=stderr)
        robot.on(speed=20, steering = 0)
    robot.off()
Exemplo n.º 20
0
 def __init__(self, left_motor_port, right_motor_port, infra_sensor_mode,
              color_sensor_mode):
     self.__movesteering = MoveSteering(left_motor_port, right_motor_port)
     self.__mediummotor = MediumMotor()
     self.__cs = ColorSensor()
     self.__cs.mode = color_sensor_mode
     self.__ir = InfraredSensor()
     self.__ir.mode = infra_sensor_mode
Exemplo n.º 21
0
 def __init__(self, left_motor_port, right_motor_port, infra_mode,
              ultra_mode):
     self.__moveSteering = MoveSteering(left_motor_port, right_motor_port)
     self.__mediumMotor = MediumMotor()
     self.__ir = InfraredSensor()
     self.__ir.mode = infra_mode
     self.__us = UltrasonicSensor()
     self.__us.mode = ultra_mode
Exemplo n.º 22
0
def square():
    motor_pair = MoveSteering(OUTPUT_B, OUTPUT_C)

    for i in range(4):
        # Move robot forward for 3 seconds
        motor_pair.on_for_seconds(steering=0, speed=50, seconds=3)
        # Turn robot left 90 degrees (adjust rotations for your particular robot)
        motor_pair.on_for_rotations(steering=-100, speed=15, rotations=0.5)
Exemplo n.º 23
0
def constructor():
    # color_sensor = ColorSensor()
    sound = Sound()
    motor_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
    # touch_sensor = TouchSensor()
    ultrasonic_sensor = UltrasonicSensor()

    return sound, motor_pair, ultrasonic_sensor
Exemplo n.º 24
0
async def on_connect(socket, path):

    try:
        movesteering = MoveSteering(OUTPUT_A, OUTPUT_B)
        colorsensor = ColorSensor(INPUT_1)
        fork = MediumMotor(OUTPUT_C)
        remote_control = RemoteControl(socket, movesteering, fork)
        fixed_mode = FixedMode(socket, movesteering)
        line_follower = LineFollower(movesteering, colorsensor)

        mode = remote_control
        #mode = fixed_mode
#        while True:
#            raw_cmd = ""

        while True:
            try:
                raw_cmd = await asyncio.wait_for(socket.recv(), timeout = 500)
                #await mode.run(raw_cmd)
                mode.run(raw_cmd)
            except TimeoutError:
                pass

            # if raw_cmd != "":
            #     await mode.run(raw_cmd)
            #else:
                # print("CHANGING MODE")
                # print(raw_cmd)
                #
                # command = json.loads(raw_cmd)
                # command_type = command['type']
                # if command_type == 'MODE':
                #     old_number = mode_number
                #     mode_number = command['mode']
                #
                #     print(mode_number)
                #     print(old_number)
                #
                #     if mode_number != old_number:
                #         mode.stop()
                #         if mode_number == 1:
                #             mode = line_follower
                #             mode.start()
                #         elif mode_number == 2:
                #             print("2")
                #         elif mode_number == 3:
                #             print("3")
                #         elif mode_number == 4:
                #             print("4")
                #         elif mode_number == 5:
                #             print("5")
                #         elif mode_number == 6:
                #             mode = remote_control
                                # start repl
                #             mode.start()

    except ConnectionClosed:
        pass
Exemplo n.º 25
0
 def __init__(self):
     self.tank_pair = MoveTank(OUTPUT_A, OUTPUT_B)
     self.steer_pair = MoveSteering(OUTPUT_A, OUTPUT_B)
     self.line_sensor = ColorSensor(INPUT_2)
     self.line_counter = ColorSensor(INPUT_3)
     self.sound = Sound()
     self.gyro = GyroSensor(INPUT_1)
     self.gyro.reset()
     self.dir_state = "S"
Exemplo n.º 26
0
    def __init__(self):
        self.pid = PIDController(kP=1.0, kI=0.0, kD=0.1)

        # motors
        try:
            self.steerPair = MoveSteering(OUTPUT_B, OUTPUT_C)
        except ev3dev2.DeviceNotFound as e:
            Debug.print("Error:", e)

        self.speed = Config.data['pid']['fast']['speed_max']
Exemplo n.º 27
0
    def __init__(self):
        self.button = Button()

        self.tank = MoveSteering(OUTPUT_C, OUTPUT_B)

        self.cs = ColorSensor()
        self.cs.mode = ColorSensor.MODE_COL_REFLECT

        self.gs = GyroSensor()
        self.gs.reset()
        self.before_direction = self.gyro()
Exemplo n.º 28
0
def drive():
    tank = MoveTank(OUTPUT_C, OUTPUT_B)
    tank_steering = MoveSteering(OUTPUT_C, OUTPUT_B)

    def signal_handler(sig, frame):
        stop(tank)
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)

    random_walk(tank_steering, 10)
    stop(tank)
Exemplo n.º 29
0
    def start(self):
        # move over start line
        steeringDrive = MoveSteering(OUTPUT_B, OUTPUT_C)
        steeringDrive.on_for_seconds(0, SpeedPercent(-40), 4)

        # find and follow line
        self.mvFollowLine.lookForLine(-40)
        self.mvFollowLine.followLine()

        steeringDrive.on_for_seconds(0, SpeedPercent(-40), 2)

        print("First over")
Exemplo n.º 30
0
 def __init__(self, wheel1 = OUTPUT_B, wheel2 = OUTPUT_C):
     self.steer_pair = MoveSteering(wheel1, wheel2)
     self.gyro = GyroSensor()
     self.tank_pair = MoveTank(wheel1, wheel2)
     self.motor1 = LargeMotor(wheel1)
     self.motor2 = LargeMotor(wheel2)
     self.color1 = ColorSensor(INPUT_1)
     self.color4 = ColorSensor(INPUT_4)
     self._black1 = 0
     self._black4 = 0 
     self._white1 = 100
     self._white4 = 100