Ejemplo n.º 1
0
    def __init__(self, port=None):
        """
        The  port  must be 1, 2, 3, 4, or None, where  None  means to attempt
        to autodetect a port into which a physical Touch Sensor is plugged.
        ---
        :param port:  The port (1, 2, 3, or 4) into which a Touch Sensor is
                      plugged, or None to attempt to autodetect such a port.
        :type port: int | None
        """
        if port not in (1, 2, 3, 4, None):
            message = "The port for a TouchSensor must be\n"
            message += "1, 2, 3, or 4, or None to attempt\n"
            message += "to autodetect a physical Touch Sensor."
            raise ValueError(message)

        if port is not None:
            self._touch_sensor = ev3.TouchSensor('in' + str(port))
        else:
            self._touch_sensor = ev3.TouchSensor()  # Attempt to autodetect
Ejemplo n.º 2
0
 def __init__(self):
     self.running = True
     self.touch_sensor = ev3.TouchSensor()
     self.seeker = ev3.BeaconSeeker(channel=1)
     self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
     self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
     assert self.left_motor.connected
     assert self.right_motor.connected
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     self.mode = "Follow"
Ejemplo n.º 3
0
    def arm_down(self):
        arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        assert arm_motor.connected

        touch_sensor = ev3.TouchSensor()
        assert touch_sensor
        arm_motor.run_to_abs_pos(position_sp=0, speed_sp=900)
        arm_motor.wait_while(
            ev3.Motor.STATE_STALLED)  # Blocks until the motor finishes running
        ev3.Sound.beep().wait()
Ejemplo n.º 4
0
def touch_sensor_as_state():
    """Example of using the TouchSensor with a state."""
    touch_sensor = ev3.TouchSensor()
    assert touch_sensor
    for _ in range(10):
        if touch_sensor.is_pressed:
            print("Touch sensor is pressed")
        else:
            print("Touch sensor is not pressed")
        time.sleep(1.0)
 def __init__(self):
     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(drive_name="pixy-lego")
     self.mqtt_client = com.MqttClient()
     self.mqtt_client.connect_to_pc()
Ejemplo n.º 6
0
 def __init__(self):
     self.speed = 400
     self.turnSpeed = 100
     self.ts = ev3.TouchSensor()
     self.lm = ev3.LargeMotor('outA')
     self.rm = ev3.LargeMotor('outB')
     self.lm.reset()
     self.rm.reset()
     self.cpr = self.lm.count_per_rot
     self.prevLmPosition = self.lm.position
     self.prevRmPosition = self.rm.position
Ejemplo n.º 7
0
 def __init__(self):
     self.color_sensor = ev3.ColorSensor()
     assert self.color_sensor
     self.touch_sensor = ev3.TouchSensor()
     assert self.touch_sensor
     self.ir_sensor = ev3.InfraredSensor()
     assert self.ir_sensor
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     assert self.pixy
     self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
     self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C)
Ejemplo n.º 8
0
    def arm_up(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()
        ev3.Sound.beep().wait()
Ejemplo n.º 9
0
 def arm_up(self):
     arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
     assert arm_motor.connected
     touch_sensor = ev3.TouchSensor()
     assert touch_sensor.connected
     arm_motor.run_forever(speed_sp=900)
     while not touch_sensor.is_pressed:
         time.sleep(0.01)
     arm_motor.stop(stop_action="brake")
     arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
     ev3.Sound.beep().wait()
Ejemplo n.º 10
0
 def __init__(self, port):
     """
     Constructs a TouchSensor at the given port,
     where  port  should be one of:
       ev3.INPUT_1    ev3.INPUT_2    ev3.INPUT_3    ev3.INPUT_4
     """
     self.port = port
     self.sensor = ev3.TouchSensor(port)
     assert self.sensor.connected,\
         ("The touch sensor appears to not be connected.\n"
          + "  Check the jack labelled 1.  Is the plug connected securely?")
Ejemplo n.º 11
0
def run():
# the execution of all code shall be started from within this function
    print('Hello World')
    planet = Planet()
    odometry = Odometry()
    movement_functions = Movement(motor_list = (ev3.LargeMotor('outB'), ev3.LargeMotor('outC')), odometry = odometry)
    line_follower = follow.LineFollowing(colour_sensor = ev3.ColorSensor('in2'),
                         ts_list = (ev3.TouchSensor('in1'), ev3.TouchSensor('in4')), movement=movement_functions, odometry = odometry)
    communication = Communication(planet = planet, odometry = odometry)
    communication.connect()
    line_follower.colour_calibration()
    line_follower.line_following()
    communication.first_communication()
    sleep(2)
    line_follower.path_recognising()

    while True:
        #odometry.odometry_calculations()
        line_follower.line_following()
        line_follower.path_recognising()
Ejemplo n.º 12
0
    def arm_up(self):
        MAX_SPEED = 900
        arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        assert arm_motor.connected

        touch_sensor = ev3.TouchSensor()
        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')
Ejemplo n.º 13
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.º 14
0
 def __init__(self):
     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.beacon_seeker = ev3.BeaconSeeker(channel=1)
     self.pixy = ev3.Sensor(driver_name='pixy-lego')
     assert self.color_sensor
     assert self.ir_sensor
     assert self.pixy
Ejemplo n.º 15
0
 def __init__(self):
     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.beacon = ev3.BeaconSeeker(channel=2)
     self.ir = ev3.InfraredSensor()
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     self.items_in_cart = []
     self.max_speed = 900
Ejemplo n.º 16
0
def main():
    print("--------------------------------------------")
    print(" Say the Color")
    print(" Press the touch sensor to exit")
    print("--------------------------------------------")
    ev3.Sound.speak("Say the Color").wait()

    color_sensor = ev3.ColorSensor()
    assert color_sensor

    # Potential values of the color_sensor.color property
    # 0: No color
    # 1: Black
    # 2: Blue
    # 3: Green
    # 4: Yellow
    # 5: Red
    # 6: White
    # 7: Brown
    # From http://python-ev3dev.readthedocs.io/en/latest/sensors.html#special-sensor-classes

    last_color_spoken = 0  # No color name has been spoken.

    touch_sensor = ev3.TouchSensor()
    assert touch_sensor

    while not touch_sensor.is_pressed:
        current_color = color_sensor.color
        if current_color == 0:
            last_color_spoken = current_color  # Clear the saved value
        else:
            if current_color != last_color_spoken:
                last_color_spoken = current_color  # Avoid saying the name again immediately.
                if current_color == 1:
                    ev3.Sound.speak("Black").wait()
                elif current_color == 2:
                    ev3.Sound.speak("Blue").wait()
                elif current_color == 3:
                    ev3.Sound.speak("Green").wait()
                elif current_color == 4:
                    ev3.Sound.speak("Yellow").wait()
                elif current_color == 5:
                    ev3.Sound.speak("Red").wait()
                elif current_color == 6:
                    ev3.Sound.speak("White").wait()
                elif current_color == 7:
                    ev3.Sound.speak("Brown").wait()

        # Hit Ctrl-c to exit as there is no other way out!
        time.sleep(0.05)

    ev3.Sound.speak("Goodbye").wait()
Ejemplo n.º 17
0
    def arm_up(self):

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

        touch_sensor = ev3.TouchSensor()
        assert touch_sensor

        arm_motor.run_to_rel_pos(position_sp=14.2 * 360, 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()
Ejemplo n.º 18
0
def publish_data(run_event):
    while run_event.is_set():
        ts = ev3.TouchSensor()
        if ts.connected and ts.value() == 1:
            try:
                ev3.Leds.set_color(ev3.Leds.LEFT, (ev3.Leds.GREEN, ev3.Leds.RED)[ts.value()])
                payload = ("{\"touchSensorValue\": \"%d\"}" % ts.value())
                client.publish(mindBlue.publisher_topic, payload)
                print("Data sent")
            except:
                print("Err")
        else:
            ev3.Leds.set_color(ev3.Leds.LEFT, (ev3.Leds.GREEN, ev3.Leds.RED)[ts.value()])
Ejemplo n.º 19
0
def main():
    print("--------------------------------------------")
    print(" Say the Color")
    print("--------------------------------------------")
    ev3.Sound.speak("Say the Color").wait()
    print(" Press the touch sensor to exit")

    color_sensor = ev3.ColorSensor()
    assert color_sensor

    # Potential values of the color_sensor.color property
    # 0: No color
    # 1: Black
    # 2: Blue
    # 3: Green
    # 4: Yellow
    # 5: Red
    # 6: White
    # 7: Brown
    # From http://python-ev3dev.readthedocs.io/en/latest/sensors.html#special-sensor-classes

    last_color_spoken = 0  # No color name has been spoken.

    touch_sensor = ev3.TouchSensor()
    assert touch_sensor

    while not touch_sensor.is_pressed:
        current_color = color_sensor.color
        if current_color == ev3.ColorSensor.COLOR_NOCOLOR:
            last_color_spoken = current_color  # Clear the saved value
        else:
            if current_color != last_color_spoken:
                last_color_spoken = current_color  # Avoid saying the name again immediately.
                if current_color == ev3.ColorSensor.COLOR_BLACK:
                    ev3.Sound.speak("Black").wait()
                elif current_color == ev3.ColorSensor.COLOR_BLUE:
                    ev3.Sound.speak("Blue").wait()
                elif current_color == ev3.ColorSensor.COLOR_GREEN:
                    ev3.Sound.speak("Green").wait()
                elif current_color == ev3.ColorSensor.COLOR_YELLOW:
                    ev3.Sound.speak("Yellow").wait()
                elif current_color == ev3.ColorSensor.COLOR_RED:
                    ev3.Sound.speak("Red").wait()
                elif current_color == ev3.ColorSensor.COLOR_WHITE:
                    ev3.Sound.speak("White").wait()
                elif current_color == ev3.ColorSensor.COLOR_BROWN:
                    ev3.Sound.speak("Brown").wait()
        time.sleep(0.05)

    print("Goodbye!")
    ev3.Sound.speak("Goodbye").wait()
Ejemplo n.º 20
0
    def arm_calibration(self):
        """Moves the arm up and then back down to recalibrate it."""
        touch_sensor = ev3.TouchSensor()
        self.arm_motor.run_forever(speed_sp=900)
        while not touch_sensor.is_pressed:
            time.sleep(0.01)
        self.arm_motor.stop(stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        ev3.Sound.beep().wait()

        self.arm_motor.run_to_rel_pos(position_sp=-5112, speed_sp=900)
        self.arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
        ev3.Sound.beep().wait()

        self.arm_motor.position = 0
Ejemplo n.º 21
0
 def setupSensorsMotors(self, configs):
     """Takes in a dictionary where the key is a string that identifies a motor or sensor
     and the value is the port for that motor or sensor. It sets up all the specified motors
     and sensors accordingly."""
     for item in configs:
         port = configs[item]
         if item == self.LEFT_MOTOR:
             self.leftMotor = ev3.LargeMotor(port)
         elif item == self.RIGHT_MOTOR:
             self.rightMotor = ev3.LargeMotor(port)
         elif item == self.SERVO_MOTOR:
             self.servoMotor = 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("in1")
         else:
             print("Unknown configuration item:", item)
def leds_example():
    """Basic LED example"""
    touch_sensor = ev3.TouchSensor()
    assert touch_sensor

    for _ in range(100):
        time.sleep(0.1)
        if touch_sensor.is_pressed:
            ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED)
            ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.ORANGE)
        else:
            ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.GREEN)
            ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.ORANGE)

    ev3.Leds.all_off()  # The lines below can be used to turn off only 1 LED if you need that.
Ejemplo n.º 23
0
 def __init__(self):
     self.mr = ev3.LargeMotor("outC")  #Initializing sensors and motors
     self.ml = ev3.LargeMotor("outB")
     self.cs = ev3.ColorSensor()
     self.ts = ev3.TouchSensor()
     self.d_wheel = 5.6
     self.d_axis = 12
     self.oldposition = (0, 0)
     self.position = (0, 0)
     self.oldview = Direction.NORTH
     self.view = Direction.NORTH
     self.status = "free"
     self.speedListL = []
     self.speedListR = []
     self.timeList = []
     self.directionList = []
Ejemplo n.º 24
0
 def __int__(self):
     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.touch_sensor = ev3.TouchSensor()
     self.color_sensor = ev3.ColorSensor()
     self.ir_sensor = ev3.InfraredSensor()
     self.BeaconSeeker = ev3.BeaconSeeker()
     self.pixy = ev3.Sensor(driver_name="pixy-lego")
     assert self.pixy.connected
     assert self.ir_sensor.connected
     assert self.color_sensor.connected
     assert self.arm_motor.connected
     assert self.left_motor.connected
     assert self.right_motor.connected
     assert self.touch_sensor.connected
Ejemplo n.º 25
0
def color_sensor():
    #WORKING TO COLOR SENSOR:

    ts = ev3.TouchSensor()

    cl = ev3.ColorSensor()
    cl.mode = 'COL-COLOR'  #returns an integer [0,7]
    #cl.mode='COL-REFLECT' #measures reflected light intensity and returns an integer [0,100]
    #cl.mode='COL-AMBIENT' #measures ambient light intensity and returns an integer [0,100]
    #cl.mode='RGB-RAW' #returns RGB tuple ([0,1020], [0,1020], [0,1020])

    colors = ('unknown black blue green yellow red white brown'.split())

    while not ts.value():
        ev3.Sound.speak(colors[cl.value()]).wait()
        time.sleep(1)
Ejemplo n.º 26
0
 def __init__(self):
     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.running = None
     assert self.right_motor
     assert self.left_motor
     assert self.touch_sensor
     assert self.arm_motor
     assert self.color_sensor
     assert self.ir_sensor
     assert self.pixy
Ejemplo n.º 27
0
def makeLightSwitch():
    print ("turn on LED w switch")
    print ("for 10 seconds")

    t_start = util.timestamp_now()
    ts = ev3.TouchSensor(ev3.INPUT_1)
    while True:
        ev3.Leds.set_color(ev3.Leds.LEFT, (ev3.Leds.GREEN, ev3.Leds.RED)[ts.value()])
        t_now = util.timestamp_now()
        if (t_now - t_start > 10E3):
            print ("finishing")
            break

    print ("turning off light")
    print (" ")
    ev3.Leds.set_color(ev3.Leds.LEFT, (ev3.Leds.GREEN, ev3.Leds.RED)[0])
Ejemplo n.º 28
0
    def __init__(self):
        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.rc1 = ev3.RemoteControl(channel=1)
        self.rc2 = ev3.RemoteControl(channel=2)
        self.rc3 = ev3.RemoteControl(channel=3)
        self.rc4 = ev3.RemoteControl(channel=4)
        self.color_sensor = ev3.ColorSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.pixy = ev3.Sensor(driver_name="pixy-lego")

        assert ev3.ColorSensor()
        assert ev3.InfraredSensor()
        assert ev3.Sensor(driver_name="pixy-lego")
Ejemplo n.º 29
0
    def __init__(self, sensor_list):
        self.sensors = {}
        # self.sensors_and_names_dict = {} # actuator_name: actuator for actuator_name, actuator in zip(self.actuator_names, self.sensors)
        self.main_motors_running = False

        for sensor in sensor_list:
            if sensor["type"] == "TOUCH_SENSOR":
                logger.debug("Adding TOUCH_SENSOR:%s", sensor["port"])
                self.sensors[sensor["name"]] = ev3.TouchSensor(sensor["port"])
            elif sensor["type"] == "INFRARED_SENSOR":
                logger.debug("Adding MEDIUM_MOTOR:%s", sensor["port"])
                self.sensors[sensor["name"]] = ev3.InfraredSensor(
                    sensor["port"])
            elif sensor["type"] == "COLOR_SENSOR":
                logger.debug("Adding MEDIUM_MOTOR:%s", sensor["port"])
                self.sensors[sensor["name"]] = ev3.ColorSensor(sensor["port"])
Ejemplo n.º 30
0
    def arm_calibration(self):
        arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        assert arm_motor.connected
        touch_sensor = ev3.TouchSensor()
        assert touch_sensor.connected
        arm_motor.run_forever(speed_sp=900)
        while not touch_sensor.is_pressed:
            time.sleep(0.01)
        arm_motor.stop(stop_action="brake")
        arm_revolutions_for_full_range = 14.2
        arm_motor.run_to_rel_pos(position_sp=-arm_revolutions_for_full_range *
                                 440,
                                 speed_sp=900)
        arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
        ev3.Sound.beep().wait()

        arm_motor.position = 0