Ejemplo n.º 1
0
def main():
    print("--------------------------------------------")
    print(" Touch sensor arm movements")
    print("--------------------------------------------")
    ev3.Sound.speak("Touch sensor arm movements").wait()

    arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
    assert arm_motor.connected

    touch_sensor = ev3.TouchSensor()
    assert touch_sensor

    while True:
        command_to_run = input(
            "Enter c (for calibrate), u (for up), d (for down), or q (for quit): "
        )
        if command_to_run == 'c':
            print("Calibrate the arm")
            print(
                "TODO: 3 is to delete this print statement, uncomment the line below, and implement that function."
            )
            # arm_calibration(arm_motor, touch_sensor)
        elif command_to_run == 'u':
            print("Move the arm to the up position")
            print(
                "TODO: 4 is to delete this print statement, uncomment the line below, and implement that function."
            )
            # arm_up(arm_motor, touch_sensor)
        elif command_to_run == 'd':
            print("Move the arm to the down position")
            print(
                "TODO: 5 is to delete this print statement, uncomment the line below, and implement that function."
            )
            # arm_down(arm_motor)
        elif command_to_run == 'q':
            break
        else:
            print(command_to_run,
                  "is not a known command. Please enter a valid choice.")

    print("Goodbye!")
    ev3.Sound.speak("Goodbye").wait()
Ejemplo n.º 2
0
    def arm_calibration(self):
        arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        assert arm_motor.connected

        touch_sensor = ev3.TouchSensor()
        assert touch_sensor

        arm_motor.run_forever(speed_sp=900)
        while not touch_sensor.is_pressed:
            time.sleep(0.01)
        arm_motor.stop_action = ev3.Motor.STOP_ACTION_BRAKE
        ev3.Sound.beep()

        arm_revolutions_for_full_range = 14.2 * 360
        arm_motor.run_to_rel_pos(position_sp=-arm_revolutions_for_full_range,
                                 speed_sp=900)
        arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
        ev3.Sound.beep()

        arm_motor.position = 0
Ejemplo n.º 3
0
    def __init__(self, port, quite):
        self.host = socket.gethostbyname(socket.getfqdn())
        self.port = port
        self.quite = quite
        self.__socket = None
        self.__started = False
        self.__peer_sock = None
        self.__peer_addr = None

        self.__leftMotor = ev3.LargeMotor('outD')
        self.__rightMotor = ev3.LargeMotor('outA')
        self.__smallMotor = ev3.MediumMotor('outC')
        self.__ir = ev3.InfraredSensor()
        self.__power = ev3.PowerSupply()

        self.__last_ping = 0
        self.__last_ir = 0
        self.__last_pwr = 0

        self.__gear = 1
Ejemplo n.º 4
0
    def __init__(self, port, motor_type='large'):
        """
        Constructs a Motor based on the input parameters.  Valid ports:
          'A', 'B', 'C', or 'D'
          Our "large" motors are in ports "B" and "C"
          Our "medium" motor is in port "A"
        Valid motor_types: "medium" or "large"
        ---
        :param port: Letter on EV3 brick where motor connects
        :type port: str
        :param motor_type: Motor types are either "medium" or "large"
        :type motor_type: str
        """
        if motor_type == 'large':
            self._motor = ev3.LargeMotor('out' + port)
        else:
            self._motor = ev3.MediumMotor('out' + port)

        # Check that the motor is actually connected (crash now if not connected)
        assert self._motor.connected
Ejemplo n.º 5
0
    def __init__(self):
        self.robot = robo.Snatch3r()
        self.mqtt_client = None
        self.running = True
        self.touch_sensor = ev3.TouchSensor()
        self.seeker = ev3.BeaconSeeker(channel=1)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)

        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()

        self.code=[]

        assert self.color_sensor
        assert self.ir_sensor
        assert self.left_motor.connected
        assert self.right_motor.connected
        assert self.arm_motor.connected
Ejemplo n.º 6
0
 def __init__(self):
     self.main_cell = 'comp'
     try:
         try:
             self.ser = serial.Serial('/dev/ttyACM0', 9600,
                                      serial.EIGHTBITS, serial.PARITY_NONE,
                                      serial.STOPBITS_ONE)
         except:
             self.ser = serial.Serial('/dev/ttyACM1', 9600,
                                      serial.EIGHTBITS, serial.PARITY_NONE,
                                      serial.STOPBITS_ONE)
         self.cell_offset = 96
         self.main_cell = 'arduino'
     except:
         self.motor = ev3.MediumMotor('outA')
         if not self.motor.connected:
             self.motor = ev3.LargeMotor('outA')
         if self.motor.connected:
             self.button = ev3.TouchSensor('in1')
             self.main_cell = 'ev3'
Ejemplo n.º 7
0
 def __init__(self):
     """Creates and stores left and right motor"""
     self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
     self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
     self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
     self.touch_sensor = ev3.TouchSensor()
     self.color_sensor = ev3.ColorSensor()
     self.ir_sensor = ev3.InfraredSensor()
     self.MAX_SPEED = 900
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     self.mqtt_client = com.MqttClient()
     self.mqtt_client.connect_to_pc()
     self.run = True
     assert self.left_motor.connected
     assert self.right_motor.connected
     assert self.arm_motor.connected
     assert self.touch_sensor
     assert self.color_sensor
     assert self.ir_sensor
     assert self.pixy
Ejemplo n.º 8
0
def get_motor(io_type, port, settings):
    try:
        if io_type == 'large motor':
            i = ev3.LargeMotor(port)
        elif io_type == 'medium motor':
            i = ev3.MediumMotor(port)

        if settings[
                'motor_mode'] == 'position':  # position is the motors current distance from its starting point (when plugged in)
            if settings['units'] == 'rotations':
                return i.position / i.count_per_rot
            elif settings['units'] == 'degrees':
                return i.position
        # duty cycle is the tachomotor motor's percentage of power; can be varied to generate constant speed; value range = -100 to 100
        elif settings['motor_mode'] == 'duty_cycle':
            return i.duty_cycle
        elif settings['motor_mode'] == 'speed':  # value range = -1000 to 1000
            return i.speed
    except ValueError:
        return "Not found"
Ejemplo n.º 9
0
def main():
    claw = ev3.MediumMotor('outA')
    arm = ev3.LargeMotor('outB')
    swing = ev3.LargeMotor('outC')

    open(claw)
    time.sleep(1)
    arm.run_timed(time_sp=500, speed_sp=300)
    time.sleep(1)
    close(claw)
    time.sleep(1)
    arm.run_timed(time_sp=500, speed_sp=-400)
    time.sleep(1)
    swing.run_timed(time_sp=500, speed_sp=1000)
    time.sleep(1)
    arm.run_timed(time_sp=500, speed_sp=300)
    time.sleep(1)
    claw.run_timed(time_sp=500, speed_sp=-250)
    time.sleep(1)
    arm.run_timed(time_sp=500, speed_sp=-400)
Ejemplo n.º 10
0
    def __init__(self):
        # Connect two large motors on output ports B and C
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        self.running = 0
        self.ir_sensor = ev3.InfraredSensor()
        self.color_sensor = ev3.ColorSensor()
        self.pixy = ev3.Sensor(driver_name="pixy-lego")
        self.beacon_seeker = ev3.BeaconSeeker(channel=1)

        # Check that the motors are actually connected
        assert self.left_motor.connected
        assert self.right_motor.connected
        assert self.arm_motor.connected
        assert self.touch_sensor
        assert self.ir_sensor
        assert self.color_sensor
        assert self.pixy
Ejemplo n.º 11
0
    def Straight_PD_forever(self):
        #Initialise motors

        motor2 = ev3.MediumMotor('outD')
        motor0 = ev3.LargeMotor('outB')
        assert motor0.connected
        motor1 = ev3.LargeMotor('outC')
        assert motor1.connected

        # Connect EV3 color sensor.
        cl = ev3.ColorSensor()

        # Put the color sensor into COL-REFLECT mode
        # to measure reflected light intensity.
        cl.mode = 'COL-REFLECT'
        #t_end = time.time() + 7

        Kp = 120  # scaled by 10 to cater for small value integer arithmetic
        Ki = 5
        Kd = 100  # scaled by 10 to cater for small value integer arithmetic
        offset = 85  # Initialize the variables
        Tp = 25
        integral = 0
        lastError = 0
        derivative = 0

        while True:
            LightValue = cl.value()  # current light reading?
            error = LightValue - offset  # subtract the offset
            integral = integral + error  # calculate the integral
            derivative = error - lastError  # calculate the derivative
            Turn = Kp * error + Ki * integral + Kd * derivative  #the "P term" the "I term" and the "D term"
            Turn = Turn / 100  # undo the affect of the factor of 100 in Kp, Ki and Kd!
            powerA = Tp + Turn  # power level for A motor
            powerC = Tp - Turn  # power level for C motor
            motor0.run_forever(speed_sp=powerA, duty_cycle_sp=25)
            motor1.run_forever(speed_sp=powerC, duty_cycle_sp=25)
            lastError = error  # save error for next time around loop
            if error >= 15:  # Stops ramp condition and servos the right wheel backwards to correct error on tight curves.
                error = 15
                motor0.run_timed(speed_sp=-25, duty_cycle_sp=25)
Ejemplo n.º 12
0
    def arm_calibration(self):
        touch_sensor = ev3.TouchSensor()
        MAX_SPEED = 900
        arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        assert arm_motor.connected
        assert touch_sensor
        arm_motor.run_forever(speed_sp=MAX_SPEED)
        while not touch_sensor.is_pressed:
            time.sleep(0.01)
        arm_motor.stop()
        print('arm is up')
        ev3.Sound.beep().wait()

        arm_revolutions_for_full_range = 14.2 * 360
        arm_motor.run_to_rel_pos(position_sp=-arm_revolutions_for_full_range,
                                 speed_sp=MAX_SPEED)
        arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
        ev3.Sound.beep().wait()

        arm_motor.position = 0
        print('arm calibrated')
Ejemplo n.º 13
0
    def __init__(self):
        # Connect the required equipement
        self.lm = ev3.LargeMotor('outB')
        self.rm = ev3.LargeMotor('outC')
        self.mm = ev3.MediumMotor()

        self.ir = ev3.InfraredSensor()
        self.ts = ev3.TouchSensor()
        self.cs = ev3.ColorSensor()

        self.screen = ev3.Screen()

        # Reset the motors
        for m in (self.lm, self.rm, self.mm):
            m.reset()
            m.position = 0
            m.stop_action = 'brake'

        self.draw_face()

        quote('initiating')
    def __init__(self):

        # The code below connects all of the robots sensors and motors to
        # specific variables
        self.mqtt_client = None
        self.running = True
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.pixy = ev3.Sensor(driver_name="pixy-lego")

        # Then is assures that all sensors and motors are connected properly
        assert self.color_sensor
        assert self.left_motor.connected
        assert self.right_motor.connected
        assert self.arm_motor.connected
        assert self.ir_sensor.connected
        assert self.pixy
Ejemplo n.º 15
0
    def __init__(self):
        self.mqtt_client = None
        self.running = True
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        # self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()
        # self.pixy = ev3.Sensor(driver_name="pixy-lego")

        # assert self.color_sensor
        assert self.left_motor.connected
        assert self.right_motor.connected
        assert self.arm_motor.connected
        assert self.ir_sensor.connected
        # assert self.pixy

        self.led_colors = [ev3.Leds.BLACK, ev3.Leds.GREEN, ev3.Leds.RED]

        self.current_color_index = 0
Ejemplo n.º 16
0
    def __init__(self):
        self.running = True
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_D)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.beacon_seeker = ev3.BeaconSeeker(channel=4)
        self.color_sensor = ev3.ColorSensor()
        self.pixy = ev3.Sensor(driver_name='pixy-lego')
        self.mqtt_client = com.MqttClient(self)
        self.mqtt_client.connect_to_pc()

        assert self.ir_sensor.connected
        assert self.color_sensor.connected
        assert self.pixy.connected
        assert self.touch_sensor
        assert self.arm_motor.connected
        assert self.left_motor.connected
        assert self.right_motor.connected
        self.MAX_SPEED = 900
Ejemplo n.º 17
0
    def __init__(self):
        """Constructs and connects two large motors on output ports B and C."""
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.pixy = ev3.Sensor(driver_name="pixy-lego")
        self.color_key = None

        self.MAX_SPEED = 900
        self.running = True
        """Check that the motors are actually connected."""
        assert self.left_motor.connected
        assert self.right_motor.connected
        assert self.arm_motor.connected
        assert self.touch_sensor
        assert self.color_sensor
        assert self.ir_sensor
        assert self.pixy
Ejemplo n.º 18
0
    def __init__(self):

        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.touch_sensor = ev3.TouchSensor()

        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.beacon_seeker = ev3.BeaconSeeker(channel=1)
        self.pixy = ev3.Sensor(driver_name="pixy-lego")

        assert self.ir_sensor
        assert self.arm_motor
        assert self.touch_sensor
        assert self.left_motor
        assert self.right_motor
        assert self.color_sensor
        assert self.beacon_seeker
        assert self.pixy
        self.pixy.mode = "SIG1"
Ejemplo n.º 19
0
 def __init__(self):
     self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
     assert self.arm_motor.connected
     self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
     assert self.left_motor.connected
     self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
     assert self.right_motor.connected
     self.touch_sensor = ev3.TouchSensor()
     assert self.touch_sensor
     self.MAX_SPEED = 900
     self.SLOW_SPEED = 100
     self.running = True
     self.color_sensor = ev3.ColorSensor()
     assert self.color_sensor
     self.ir_sensor = ev3.InfraredSensor()
     assert self.ir_sensor
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     assert self.pixy
     self.active = False
     self.sides = 0
     self.degrees_turned = 0
Ejemplo n.º 20
0
def run(self):
# sensors
        cs = ev3.ColorSensor();      assert cs.connected  # measures light intensity
        us = ev3.UltrasonicSensor(); assert us.connected  # measures distance
        cs.mode = 'COL-REFLECT'  # measure light intensity
        us.mode = 'US-DIST-CM'   # measure distance in cm
# motors
        lm = ev3.LargeMotor('outB');  assert lm.connected  # left motor
        rm = ev3.LargeMotor('outC');  assert rm.connected  # right motor
        mm = ev3.MediumMotor('outD'); assert mm.connected  # medium motor
        speed = 360/4  # deg/sec, [-1000, 1000]
        dt = 500       # milliseconds
        stop_action = "coast"
# PID tuning
        Kp = 1  # proportional gain
        Ki = 0  # integral gain
        Kd = 0  # derivative gain
        integral = 0
        previous_error = 0
# initial measurment
        target_value = cs.value()
Ejemplo n.º 21
0
    def __init__(self):
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        assert self.left_motor
        assert self.right_motor

        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        assert self.arm_motor
        assert self.touch_sensor

        self.running = None

        self.color_sensor = ev3.ColorSensor()
        assert self.color_sensor

        self.ir_sensor = ev3.InfraredSensor()
        assert self.ir_sensor

        self.pixy = ev3.Sensor(driver_name="pixy-lego")
        assert self.pixy
Ejemplo n.º 22
0
def USscanForward_timed2(self):
    #This function scans for objects and implements avoidance measures
    #if necessary

    motor2 = ev3.MediumMotor('outD')
    motor2.connected

    #motor2.run_timed(time_sp=2000, speed_sp=-90)
    us = ev3.UltrasonicSensor()
    #t_end = time.time() + 5
    #while time.time() <  t_end:
    print us.value()
    if us.value() < 100:
        ev3.Sound.speak('object still in range').wait()
        OD = OC.OdometryControl()
        #OD.turn_left_ObjectAvoid()
        #OD.objectAvoidForward()
        OD.turn_right_ObjectAvoid()
        OD.objectAvoidForward()
        OD.turn_right_ObjectAvoid()
        OD.objectAvoidForward2()
Ejemplo n.º 23
0
 def __init__(self):
     """
     Creates and stores the instance variables for the Snatch3r class.
     Asserts the motors and sensors, so that the program will crash if
     they are not connected.
     """
     self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
     self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
     self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
     self.touch_sensor = ev3.TouchSensor()
     self.color_sensor = ev3.ColorSensor()
     self.ir_sensor = ev3.InfraredSensor()
     self.MAX_SPEED = 900
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     assert self.left_motor.connected
     assert self.right_motor.connected
     assert self.arm_motor.connected
     assert self.touch_sensor
     assert self.color_sensor
     assert self.ir_sensor
     assert self.pixy
Ejemplo n.º 24
0
    def __init__(self,
                 robot=None,
                 bttn=None,
                 leftM='outD',
                 rightM='outA',
                 armM='outC',
                 medM='outB',
                 cs='in3',
                 us='in1',
                 cs2='in4'):
        # self.name = name

        self.bttn = ev3.Button()
        self.leftM = ev3.LargeMotor(leftM)
        self.rightM = ev3.LargeMotor(rightM)
        self.armM = ev3.LargeMotor(armM)
        self.medM = ev3.MediumMotor(medM)
        self.cs = ev3.ColorSensor('in3')
        self.us = ev3.UltrasonicSensor('in1')
        self.cs2 = ev3.ColorSensor('in4')
        self.state = 'advance'
Ejemplo n.º 25
0
 def setupSensorsMotors(self, configs):
     for item in configs:
         port = configs[item]
         if item == self.LEFT_MOTOR:
             self.lmot = ev3.LargeMotor(port)
         elif item == self.RIGHT_MOTOR:
             self.rmot = ev3.LargeMotor(port)
         elif item == self.SERVO_MOTOR:
             self.mmot = ev3.MediumMotor(port)
         elif item == self.LEFT_TOUCH:
             self.leftTouch = ev3.TouchSensor(port)
         elif item == self.RIGHT_TOUCH:
             self.rightTouch = ev3.TouchSensor(port)
         elif item == self.ULTRA_SENSOR:
             self.ultraSensor = ev3.UltrasonicSensor(port)
         elif item == self.COLOR_SENSOR:
             self.colorSensor = ev3.ColorSensor(port)
         elif item == self.GYRO_SENSOR:
             self.gyroSensor = ev3.GyroSensor(port)
         else:
             print("Unknown configuration item:", item)
Ejemplo n.º 26
0
    def setup_hardware(self):
        # setup I/O connecting to EV3
        self.motorR = ev3.LargeMotor('outA')
        self.motorL = ev3.LargeMotor('outD')
        self.csR = ev3.ColorSensor('in2')
        self.csM = ev3.ColorSensor('in3')
        self.csL = ev3.ColorSensor('in4')

        #TODO: implement using the DC motor if need
        #self.grabberArms = ev3.MediumMotor('outC')
        self.port = ev3.LegoPort('outB')
        assert self.port.connected
        self.port.mode = 'dc-motor'
        time.sleep(5)
        self.grabberArms = ev3.DcMotor("outB")
        print("DcMotor connected")

        self.grabberLift = ev3.MediumMotor('outC')
        self.gy = ev3.GyroSensor('in1')
        self.reset_gyro()
        self.base_pos = self.grabberLift.position
Ejemplo n.º 27
0
    def test_medium_motor(self):
        def dummy(self):
            pass

        # Do not write motor.command on exit (so that fake tree stays intact)
        ev3.MediumMotor.__del__ = dummy

        m = ev3.MediumMotor()

        self.assertTrue(m.connected)

        self.assertEqual(m.device_index, 0)

        # Check that reading twice works:
        self.assertEqual(m.driver_name, 'lego-ev3-m-motor')
        self.assertEqual(m.driver_name, 'lego-ev3-m-motor')

        self.assertEqual(m.count_per_rot, 360)
        self.assertEqual(m.commands, [
            'run-forever', 'run-to-abs-pos', 'run-to-rel-pos', 'run-timed',
            'run-direct', 'stop', 'reset'
        ])
        self.assertEqual(m.duty_cycle, 0)
        self.assertEqual(m.duty_cycle_sp, 42)
        self.assertEqual(m.encoder_polarity, 'normal')
        self.assertEqual(m.polarity, 'normal')
        self.assertEqual(m.address, 'outA')
        self.assertEqual(m.position, 42)
        self.assertEqual(m.position_sp, 42)
        self.assertEqual(m.ramp_down_sp, 0)
        self.assertEqual(m.ramp_up_sp, 0)
        self.assertEqual(m.speed, 0)
        self.assertEqual(m.speed_regulation_enabled, 'off')
        self.assertEqual(m.speed_sp, 0)
        self.assertEqual(m.state, ['running'])
        self.assertEqual(m.stop_command, 'coast')
        self.assertEqual(m.time_sp, 1000)

        with self.assertRaises(Exception):
            c = m.command
Ejemplo n.º 28
0
 def __init__(self, robotName, configDict=None):
     """Takes in a string, the name of the robot, and an optional dictionary
     giving motor and sensor ports for the robot."""
     self.name = robotName
     self.leftMotor = ev3.LargeMotor('outC')
     self.rightMotor = ev3.LargeMotor('outB')
     self.servoMotor = ev3.MediumMotor('outD')
     self.servoMotor.reset()
     self.leftTouch = None
     self.rightTouch = None
     self.ultraSensor = ev3.UltrasonicSensor('in4')
     self.colorSensor = None
     self.gyroSensor = ev3.GyroSensor('in2')
     self.pixyCam = PixyCam()  #input_1
     self.button = ev3.Button()
     ev3.Sound.set_volume(100)
     if configDict is not None:
         self.setupSensorsMotors(configDict)
     if self.leftMotor is None:
         self.leftMotor = ev3.LargeMotor('outC')
     if self.rightMotor is None:
         self.rightMotor = ev3.LargeMotor('outB')
Ejemplo n.º 29
0
    def __init__(self):
        """"The constructor function that stores the components of the robot as
        well as asserts them to make sure that they are connected"""
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.pixy = ev3.Sensor(driver_name="pixy-lego")
        self.w = 1

        assert self.left_motor.connected
        assert self.right_motor.connected
        assert self.arm_motor.connected
        assert self.touch_sensor.connected
        assert self.color_sensor.connected
        assert self.ir_sensor.connected
        assert self.pixy.connected

        self.MAX_SPEED = 900
        self.running = True
def arm_calibration(arm_motor, touch_sensor):
    """
    Runs the arm up until the touch sensor is hit then back to the bottom again, beeping at both locations.
    Once back at in the bottom position, gripper open, set the absolute encoder position to 0.  You are calibrated!
    The Snatch3r arm needs to move 14.2 revolutions to travel from the touch sensor to the open position.

    Type hints:
      :type arm_motor: ev3.MediumMotor
      :type touch_sensor: ev3.TouchSensor
    """
    # DONE: 3. Implement the arm calibration movement by fixing the code below
    # (it has many bugs).  It should to this:
    #   Command the arm_motor to run forever in the positive direction at max speed.
    #   Create an infinite while loop that will block code execution until the touch sensor's is_pressed value is True.
    #     Within that loop sleep for 0.01 to avoid running code too fast.
    #   Once past the loop the touch sensor must be pressed. So stop the arm motor quickly using the brake stop action.
    #   Make a beep sound
    #   Now move the arm_motor 14.2 revolutions in the negative direction relative to the current location
    #     Note the stop action and speed are already set correctly so we don't need to specify them again
    #   Block code execution by waiting for the arm to finish running
    #   Make a beep sound
    #   Set the arm encoder position to 0 (the last line below is correct to do that, it's new so no bug there)

    # Code that attempts to do this task but has MANY bugs (nearly 1 on every line).  Fix them!
    arm_motor.run_forever(speed_sp=MAX_SPEED)
    while True:
        time.sleep(0.01)
        if touch_sensor.is_pressed:
            break

    arm_motor.stop_action = ev3.MediumMotor(ev3.OUTPUT_A).STOP_ACTION_BRAKE
    ev3.Sound.beep().wait()

    arm_revolutions_for_full_range = 14.2 * 360
    arm_motor.run_to_rel_pos(position_sp=-arm_revolutions_for_full_range)
    arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
    ev3.Sound.beep().wait()

    arm_motor.position = 0  # Calibrate the down position as 0 (this line is correct as is).