Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env pybricks-micropython
- from pybricks.hubs import EV3Brick
- from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
- InfraredSensor, UltrasonicSensor, GyroSensor)
- from pybricks.nxtdevices import (LightSensor)
- from pybricks.parameters import Port, Stop, Direction, Button, Color
- from pybricks.tools import wait, StopWatch, DataLog
- from pybricks.robotics import DriveBase
- from pybricks.media.ev3dev import SoundFile, ImageFile
- # This program requires LEGO EV3 MicroPython v2.0 or higher.
- # Click "Open user guide" on the EV3 extension tab for more information.
- # Create your objects here.
- ev3 = EV3Brick()
- right_motor = Motor(Port.B)
- left_motor = Motor(Port.C)
- lift_motor = Motor(Port.A)
- claw = Motor(Port.D)
- light_sensor = LightSensor(Port.S3)
- #color_sensor_one = ColorSensor(Port.S2)
- #color_sensor_two = ColorSensor(Port.S1)
- wheel_diameter = 43.3
- axle_track = 130
- # Configure drivebase and set speed/acceleration options
- driver = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)
- driver.settings(800, 200, 30, 1200)
- # Calculate the light threshold. Choose values based on your measurements.
- BLACK = 15
- WHITE = 30
- threshold = (BLACK + WHITE) / 2
- # Set the drive speed at 100 millimeters per second.
- DRIVE_SPEED = 100
- # Set the gain of the proportional line controller. This means that for every
- # percentage point of light deviating from the threshold, we set the turn
- # rate of the drivebase to 1.2 degrees per second.
- # For example, if the light value deviates from the threshold by 10, the robot
- # steers at 10*1.2 = 12 degrees per second.
- PROPORTIONAL_GAIN = 1.2
- # Write your program here.
- ev3.speaker.beep()
- wait(300)
- # Start following the line endlessly.
- while True:
- # Calculate the deviation from the threshold.
- deviation = light_sensor.reflection() - threshold
- # Calculate the turn rate.
- turn_rate = PROPORTIONAL_GAIN * deviation
- # Set the drive base speed and turn rate.
- driver.drive(DRIVE_SPEED, turn_rate)
- # You can wait for a short time or do other things in this loop.
- wait(10)
Add Comment
Please, Sign In to add comment