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") # Check that the motors and sensors are actually connected 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
def __init__(self): """Establishes a left, right, and arm motor. Establishes the sensors (touch, ir, and color) sensors. Establishes the pixy camera. Sets inches moved to zero. Sets the state of running to be true. This all only occurs when the sensors are connected.""" self.inches_moved = 0 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 = True self.ir_sensor = ev3.InfraredSensor() self.color_sensor = ev3.ColorSensor() assert self.color_sensor assert self.ir_sensor assert self.touch_sensor self.arm_motor.position = 0 self.pixy = ev3.Sensor(driver_name="pixy-lego") assert self.pixy
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") 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
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") 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 __init__(self): 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.pixy = ev3.Sensor(driver_name="pixy-lego") assert self.pixy assert self.color_sensor assert self.ir_sensor assert self.left_motor.connected assert self.right_motor.connected assert self.arm_motor.connected
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
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
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.color_sensor = ev3.ColorSensor() self.ir_sensor = ev3.InfraredSensor() self.beacon_seeker = ev3.BeaconSeeker() self.pixy = ev3.Sensor(driver_name="pixy-lego") assert self.pixy assert self.beacon_seeker assert self.ir_sensor assert self.color_sensor assert self.touch_sensor assert self.arm_motor.connected assert self.left_motor.connected assert self.right_motor.connected
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
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
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
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"
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
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
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 = True 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) self.btn = ev3.Button() # 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
def main(): print("--------------------------------------------") print(" Color tracking") print("--------------------------------------------") ev3.Sound.speak("Color tracking").wait() print("Press the touch sensor to exit this program.") # This code assumes you have setup the pixy object on the Snatch3r class. # Add the pixy property to that class if you have not done so already. robot = robo.Snatch3r() pixy = ev3.Sensor(driver_name="pixy-lego") pixy.mode = "SIG1" assert pixy turn_speed = 100 while not robot.touch_sensor.is_pressed: # DONE: 2. Read the Pixy values for x and y # Print the values for x and y x = pixy.value(1) y = pixy.value(2) print("(X, Y) = ({}, {})".format(x, y)) # DONE: 3. Use the x value to turn the robot # If the Pixy x value is less than 150 turn left (-turn_speed, turn_speed) # If the Pixy x value is greater than 170 turn right (turn_speed, -turn_speed) # If the Pixy x value is between 150 and 170 stop the robot # Continuously track the color until the touch sensor is pressed to end the program. if x < 150: robot.drive_until_otherwise(turn_speed, -turn_speed) elif x > 170: robot.drive_until_otherwise(-turn_speed, turn_speed) else: robot.stop() time.sleep(0.25) print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Pixy display") print(" Press the touch sensor to exit") print("--------------------------------------------") ev3.Sound.speak("Pixy display").wait() print("Press the touch sensor to exit this program.") # Done: 2. Create an MqttClient (no delegate needed since EV3 will only send data, so an empty constructor is fine) # Then connect to the pc using the connect_to_pc method. mqtt_client = com.MqttClient() mqtt_client.connect_to_pc() robot = robo.Snatch3r() pixy = ev3.Sensor(driver_name="pixy-lego") assert pixy.connected touch_sensor = ev3.TouchSensor() assert touch_sensor.connected pixy.mode = "SIG1" while not touch_sensor.is_pressed: # Done: 3. Read the Pixy values for x, y, width, and height # Print the values (much like the print_pixy_readings example) print("value1: X", pixy.value(1)) print("value2: Y", pixy.value(2)) print("value3: Width", pixy.value(3)) print("value4: Height", pixy.value(4)) # Done: 4. Send the Pixy values to the PC by calling the on_rectangle_update method # If you open m2_pc_pixy_display you can see the parameters for that method [x, y, width, height] mqtt_client.send_message( "on_rectangle_update", [pixy.value(1), pixy.value(2), pixy.value(3), pixy.value(4)]) time.sleep(0.25) print("Goodbye!") ev3.Sound.speak("Goodbye").wait() mqtt_client.close()
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 main(): print("--------------------------------------------") print(" Color tracking") print("--------------------------------------------") ev3.Sound.speak("Color tracking").wait() print("Press the touch sensor to exit this program.") # This code assumes you have setup the pixy object on the Snatch3r class. # Add the pixy property to that class if you have not done so already. robot = robo.Snatch3r() pixy = ev3.Sensor(driver_name="pixy-lego") assert pixy.connected touch_sensor = ev3.TouchSensor() assert touch_sensor.connected pixy.mode = "SIG1" turn_speed = 100 while not touch_sensor.is_pressed: # Done: 2. Read the Pixy values for x and y # Print the values for x and y print("value1: X", pixy.value(1)) print("value2: Y", pixy.value(2)) # Done: 3. Use the x value to turn the robot # If the Pixy x value is less than 150 turn left (-turn_speed, turn_speed) # If the Pixy x value is greater than 170 turn right (turn_speed, -turn_speed) # If the Pixy x value is between 150 and 170 stop the robot # Continuously track the color until the touch sensor is pressed to end the program. if pixy.value(1) == 0: robot.drive_both_stop() elif pixy.value(1) > 170: robot.turn_forever(turn_speed) elif pixy.value(1) < 150: robot.turn_forever(-turn_speed) else: robot.drive_both_stop() time.sleep(0.25) robot.drive_both_stop() print("Goodbye!") ev3.Sound.speak("Goodbye").wait()
def main(): print("--------------------------------------------") print(" Print Pixy readings") print("--------------------------------------------") ev3.Sound.speak("Print Pixy readings").wait() print(" Press the touch sensor to exit") pixy = ev3.Sensor(driver_name="pixy-lego") assert pixy.connected touch_sensor = ev3.TouchSensor() assert touch_sensor pixy.mode = "SIG1" while not touch_sensor.is_pressed: print("(X, Y) = ({}, {}) Width = {} Height = {}".format( pixy.value(1), pixy.value(2), pixy.value(3), pixy.value(4))) time.sleep(0.5) print("Goodbye") ev3.Sound.speak("Goodbye").wait()
def __init__(self): # Motors self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) # Sensors self.touch_sensor = ev3.TouchSensor() self.beacon_seeker = ev3.BeaconSeeker(channel=4) self.pixy = ev3.Sensor(driver_name="pixy-lego") self.color_sensor = ev3.ColorSensor() self.ir_sensor = ev3.InfraredSensor() # Defined Constants self.MAX_SPEED = 900 self.running = True assert self.pixy assert self.color_sensor assert self.ir_sensor assert self.touch_sensor assert self.left_motor assert self.right_motor assert self.arm_motor
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.MAX_SPEED = 900 self.Leds = ev3.Leds self.Sounds = ev3.Sound self.left_speed = 600 self.right_speed = 600 self.color_sensor = ev3.ColorSensor() self.ir_sensor = ev3.InfraredSensor() self.pixy = ev3.Sensor(driver_name="pixy-lego") self.man_up_value = 0 self.follower = 0 self.color = 0 self.calibrate = 0 self.time_s = 0 self.user = 1 self.SIG = "SIG1" self.finder = 0 assert self.pixy
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) # Check that the motors are actually connected assert self.left_motor.connected assert self.right_motor.connected # Connect and assert connection of medium motor to output port A self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) assert self.arm_motor.connected # Define and check sensors are connected self.touch_sensor = ev3.TouchSensor() assert self.touch_sensor self.color_sensor = ev3.ColorSensor() assert self.color_sensor self.ir_sensor = ev3.InfraredSensor() assert self.ir_sensor self.remote_control = ev3.RemoteControl() # don't assert remote_control since it is external from the robot self.beacon_seeker = ev3.BeaconSeeker(channel=1) assert self.beacon_seeker self.beacon_seeker_2 = ev3.BeaconSeeker(channel=1) assert self.beacon_seeker_2 self.pixy = ev3.Sensor(driver_name="pixy-lego") assert self.pixy # Definition for Brickman buttons self.btn = ev3.Button() # Define recurring variables self.current_color = 0 self.reflected_light_intensity = 0 self.running = True
def __init__(self): """ Creates motors and sensors for the Snatcher and makes sure they are connected. """ 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.running = True 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") self.mqtt_client = None self.light_level = 90 self.light_calibrated = False self.dark_calibrated = False self.dark_level = 10 self.obstructed = False self.is_stopped = True self.origin_x = 240 self.origin_y = 240 self.current_x = 240 self.current_y = 240 self.last_x = 240 self.last_y = 240 assert self.arm_motor.connected assert self.left_motor.connected assert self.right_motor.connected assert self.touch_sensor.connected assert self.color_sensor.connected assert self.ir_sensor.connected
def __init__(self): 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.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A) assert self.arm_motor.connected self.touch_sensor = ev3.TouchSensor() assert self.touch_sensor.connected self.color_sensor = ev3.ColorSensor() assert self.color_sensor self.ir_sensor = ev3.InfraredSensor() assert self.ir_sensor self.pixy = ev3.Sensor() assert self.pixy self.x = 0 self.y = 0 self.angle = 0 """ Moves the robot forward the requested number of inches at a speed in degrees / second.""" print("abc") degrees_per_inch = 90 degrees = inches * degrees_per_inch self.left_motor.run_to_rel_pos(position_sp=degrees, speed_sp=speed, stop_action=ev3.Motor.STOP_ACTION_BRAKE) self.right_motor.run_to_rel_pos( position_sp=degrees, speed_sp=speed, stop_action=ev3.Motor.STOP_ACTION_BRAKE)
def main(): print("--------------------------------------------") print(" Color tracking") print("--------------------------------------------") ev3.Sound.speak("Color tracking").wait() print("Press the touch sensor to exit this program.") pixy = ev3.Sensor(driver_name="pixy-lego") # This code assumes you have setup the pixy object on the Snatch3r class. # Add the pixy property to that class if you have not done so already. robot = robo.Snatch3r() robot.pixy.mode = "SIG1" turn_speed = 100 while not robot.touch_sensor.is_pressed: # TODO: 2. Read the Pixy values for x and y # Print the values for x and y print("value1: X", pixy.value(1)) print("value1: Y", pixy.value(2)) # TODO: 3. Use the x value to turn the robot # If the Pixy x value is less than 150 turn left (-turn_speed, turn_speed) # If the Pixy x value is greater than 170 turn right (turn_speed, -turn_speed) # If the Pixy x value is between 150 and 170 stop the robot # Continuously track the color until the touch sensor is pressed to end the program. if 0 < robot.pixy.value(1) < 150: robot.turn_degrees(90, -turn_speed) elif robot.pixy.value(1) > 170: robot.turn_degrees((90, turn_speed)) elif 150 <= robot.pixy.value(1) <= 170: robot.stop() time.sleep(0.25) print("Goodbye!") ev3.Sound.speak("Goodbye").wait( ) # TODO: 4. Call over a TA or instructor to sign your team's checkoff sheet.
motorDir = ev3.LargeMotor('outB') assert motorDir.connected motorGarra = ev3.MediumMotor('outA') assert motorGarra.connected motorCatapulta = ev3.MediumMotor('outD') assert motorCatapulta.connected corEsq = ev3.ColorSensor('in4') assert corEsq.connected corEsq.mode = 'COL-COLOR' corDir = ev3.ColorSensor('in1') assert corDir.connected corDir.mode = 'COL-COLOR' corCheck = ev3.Sensor(address='in2', driver_name='ht-nxt-color-v2') corCheck.mode = 'ALL' ultrassom = ev3.UltrasonicSensor('in3') assert ultrassom.connected ultrassom.mode = 'US-DIST-CM' velocidade = -500 v_ajeita = -100 deltaDir = -200 # delta de velocidade ao ajeitar caminho deltaEsq = -200 v_curva = -300 # velocidade em curvas pos_dir = 413 # as curvas pos_esq = 411
#!/usr/bin/env python3 import time import sys import ev3dev.ev3 as ev3 def eprint(*args, **kwargs): # https://stackoverflow.com/a/14981125 print(*args, file=sys.stderr, **kwargs) eprint("Hello, world!") gyro = ev3.Sensor('in3', driver_name='ms-absolute-imu') eprint(gyro) assert gyro.connected gyro.mode = 'TILT' eprint(gyro.value(0), gyro.value(1), gyro.value(2))
def camera(): conn = rpyc.classic.connect('ev3dev.local') # host name or IP address of the EV3 ev3 = conn.modules['ev3dev.ev3'] # import ev3dev.ev3 remotely def limit_speed(speed): conn = rpyc.classic.connect('ev3dev.local') # host name or IP address of the EV3 ev3 = conn.modules['ev3dev.ev3'] # import ev3dev.ev3 remotely """ Limit speed in range [-900,900] """ if speed > 900: speed = 900 elif speed < -900: speed = -900 return speed # Connect Pixy camera and set mode pixy = ev3.Sensor(address = INPUT_3) assert pixy.connected, "Error while connecting Pixy camera" pixy.mode = 'SIG1' # Connect TouchSensor (to stop script) ts = ev3.TouchSensor(INPUT_1) assert ts.connected, "Error while connecting TouchSensor" # Connect LargeMotors rmotor = ev3.LargeMotor(OUTPUT_C) assert rmotor.connected, "Error while connecting right motor" lmotor = ev3.LargeMotor(OUTPUT_B) assert lmotor.connected, "Error while connecting left motor" # Defining constants X_REF = 128 # X-coordinate of referencepoint Y_REF = 150 # Y-coordinate of referencepoint KP = 0.1 # Proportional constant PID-controller KI = 0.01 # Integral constant PID-controller KD = 0.005 # Derivative constant PID-controller GAIN = 10 # Gain for motorspeed # Initializing PID variables integral_x = 0 derivative_x = 0 last_dx = 0 integral_y = 0 derivative_y = 0 last_dy = 0 while not ts.value(): if pixy.value(0) > 0: ev3.Sound.speak('I see it, I am following !') x = pixy.value(1) # X-centroid of largest SIG1-object y = pixy.value(2) # Y-centroid of largest SIG1-object dx = X_REF - x # Error in reference to X_REF integral_x = integral_x + dx # Calculate integral for PID derivative_x = dx - last_dx # Calculate derivative for PID speed_x = KP*dx + KI*integral_x + KD*derivative_x # Speed in X-direction dy = Y_REF - y # Error in reference to Y_REF integral_y = integral_y + dy # Calculate integral for PID derivative_y = dy - last_dy # Calculate derivative for PID speed_y = KP*dy + KI*integral_y + KD*derivative_y # Speed in Y-direction # Calculate motorspeed out of speed_x and speed_y lmotor.run_forever(speed_sp = limit_speed(GAIN*(speed_y - speed_x))) rmotor.run_forever(speed_sp = limit_speed(GAIN*(speed_y + speed_x))) last_dx = dx # Set last error for x last_dy = dy # Set last error for y else: rmotor.stop() # SIG1 not detected, stop motors lmotor.stop() # End of script, stop motors rmotor.stop() lmotor.stop()
#!/usr/bin/env python3 import ev3dev.ev3 as ev3 import time lcd = ev3.Screen() # The EV3 display rightMotor = ev3.LargeMotor('outB') # The motor connected to the right wheel leftMotor = ev3.LargeMotor('outC') # The motor connected to the left wheel button = ev3.Button() # Any button camera = ev3.Sensor(address=ev3.INPUT_1) # The camera assert camera.connected, "Error while connecting Pixy camera to port 2" lcd = ev3.Screen() ts = ev3.TouchSensor('in2') assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor() us.mode = 'US-DIST-CM' units = us.units # reports 'cm' even though the sensor measures 'mm' CAMERA_WIDTH_PIXELS = 255 CAMERA_HEIGHT_PIXELS = 255 leftMotor = ev3.LargeMotor('outC') rightMotor = ev3.LargeMotor('outB') lcd = ev3.Screen() ts = ev3.TouchSensor('in2') assert ts.connected, "Connect a touch sensor to any port" us = ev3.UltrasonicSensor()