class MVP: def __init__(self): self.steer_motor = Motor(Port.A) self.drive_motor = Motor(Port.B, positive_direction=Direction.COUNTERCLOCKWISE) def calibrate(self): self.steer_motor.run_target(speed=1000, target_angle=0)
class Blast: WHEEL_DIAMETER = 44 AXLE_TRACK = 100 def __init__(self): self.hub = InventorHub() # Configure the drive base self.left_motor = Motor(Port.C, Direction.COUNTERCLOCKWISE) self.right_motor = Motor(Port.A) self.drive_base = DriveBase(self.left_motor, self.right_motor, wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK) # Configure other motors and sensors self.arm_movement_motor = Motor(Port.D) self.action_motor = Motor(Port.B) self.color_sensor = ColorSensor(Port.E) self.distance_sensor = UltrasonicSensor(Port.F) def activate_display(self): self.hub.display.orientation(up=Side.RIGHT) for _ in range(10): self.hub.display.image( image=[[00, 11, 33, 11, 00], [11, 33, 66, 33, 11], [33, 66, 99, 66, 33], [11, 33, 66, 33, 11], [00, 11, 33, 11, 00]]) self.hub.light.on(color=Color.RED) wait(100) self.hub.display.off() self.hub.light.off() wait(100) def calibrate_arms(self): self.arm_movement_motor.run_until_stalled(speed=1000, then=Stop.HOLD) for _ in range(10): self.distance_sensor.lights.on(100) wait(100) self.distance_sensor.lights.off() wait(100) self.arm_movement_motor.run_angle(speed=1000, rotation_angle=-850, then=Stop.HOLD, wait=True)
def __init__(self): self.hub = InventorHub() # Configure the drive base self.left_motor = Motor(Port.C, Direction.COUNTERCLOCKWISE) self.right_motor = Motor(Port.A) self.drive_base = DriveBase(self.left_motor, self.right_motor, wheel_diameter=self.WHEEL_DIAMETER, axle_track=self.AXLE_TRACK) # Configure other motors and sensors self.arm_movement_motor = Motor(Port.D) self.action_motor = Motor(Port.B) self.color_sensor = ColorSensor(Port.E) self.distance_sensor = UltrasonicSensor(Port.F)
def __init__(self): # Configure the hub, the kicker motor, and the sensors. self.hub = InventorHub() self.kicker_motor = Motor(Port.C) self.distance_sensor = UltrasonicSensor(Port.D) self.color_sensor = ColorSensor(Port.E) # Configure the drive base. left_motor = Motor(Port.B, Direction.COUNTERCLOCKWISE) right_motor = Motor(Port.A) self.drive_base = DriveBase(left_motor, right_motor, self.WHEEL_DIAMETER, self.AXLE_TRACK) # Prepare tricky to start kicking! self.distance_sensor.lights.off() self.reset_kicker_motor() self.distance_sensor.lights.on(100)
def get_motor(port): """Returns a ``Motor`` object if a motor is connected to ``port`` or ``None`` if no motor is connected.""" try: return Motor(port) except OSError as ex: if ex.args[0] != uerrno.ENODEV: raise return None
class Gelo: def __init__(self): self.hub = InventorHub(top_side=Axis.Z, front_side=-Axis.X) # Configure the leg motors self.front_left_leg_motor = Motor(Port.D) self.front_right_leg_motor = \ Motor(Port.C, Direction.COUNTERCLOCKWISE) self.rear_left_leg_motor = Motor(Port.B) self.rear_right_leg_motor = \ Motor(Port.A, Direction.COUNTERCLOCKWISE) self.leg_motors = [ self.front_left_leg_motor, self.front_right_leg_motor, self.rear_left_leg_motor, self.rear_right_leg_motor ] # Configure the sensors self.color_sensor = ColorSensor(Port.F) self.distance_sensor = UltrasonicSensor(Port.E) def activate_display(self): self.hub.display.orientation(up=Side.TOP) for _ in range(10): self.hub.display.image([[00, 11, 33, 11, 00], [11, 33, 33, 33, 11], [33, 66, 99, 66, 33], [11, 33, 66, 33, 11], [00, 11, 33, 11, 00]]) wait(50) self.hub.display.off() wait(50) def straighten_legs(self): for leg_motor in self.leg_motors: leg_motor.run_target(speed=1000, target_angle=0, wait=False) def walk(self, speed: int, seconds: int): for _ in range(seconds): self.front_left_leg_motor.run_time(speed=speed, time=500, wait=False) self.rear_right_leg_motor.run_time(speed=speed, time=500) self.front_right_leg_motor.run_time(speed=speed, time=500, wait=False) self.rear_left_leg_motor.run_time(speed=speed, time=500)
def __init__(self): # Initialize each motor with the correct direction and gearing. self.left_rear_wheels = Motor(Port.E, Direction.CLOCKWISE, self.REAR_GEARS) self.right_rear_wheels = Motor(Port.F, Direction.COUNTERCLOCKWISE, self.REAR_GEARS) self.left_front_wheel = Motor(Port.C, Direction.COUNTERCLOCKWISE, None) self.right_front_wheel = Motor(Port.D, Direction.CLOCKWISE, None) # Initialize sensors, named after the Perseverance Mars Rover instruments. self.mast_cam = UltrasonicSensor(Port.B) self.sherloc = ColorSensor(Port.A) # Initialize the hub and start the animation self.hub = InventorHub() self.hub.display.animate(self.HEART_BEAT, 30)
def __init__(self): self.hub = InventorHub(top_side=Axis.Z, front_side=-Axis.X) # Configure the leg motors self.front_left_leg_motor = Motor(Port.D) self.front_right_leg_motor = \ Motor(Port.C, Direction.COUNTERCLOCKWISE) self.rear_left_leg_motor = Motor(Port.B) self.rear_right_leg_motor = \ Motor(Port.A, Direction.COUNTERCLOCKWISE) self.leg_motors = [ self.front_left_leg_motor, self.front_right_leg_motor, self.rear_left_leg_motor, self.rear_right_leg_motor ] # Configure the sensors self.color_sensor = ColorSensor(Port.F) self.distance_sensor = UltrasonicSensor(Port.E)
from pybricks.pupdevices import Motor, ColorDistanceSensor from pybricks.parameters import Port, Stop from pybricks.tools import wait # The number of degrees the motor must rotate to move the test rig by 1m DEGREES_PER_METRE = 4822 TEST_DISTANCE_MM = 100 TEST_INCREMENT_MM = 5 DEGREES_PER_INCREMENT = round(DEGREES_PER_METRE * TEST_INCREMENT_MM / 1000) motor = Motor(Port.D) sensor = ColorDistanceSensor(Port.B) results = {} distanceFromTarget = 0 while distanceFromTarget <= TEST_DISTANCE_MM: wait(500) newReading = sensor.hsv() results[distanceFromTarget] = newReading motor.run_angle(100, -DEGREES_PER_INCREMENT, Stop.HOLD) distanceFromTarget += TEST_INCREMENT_MM motor.run_angle(150, 0, Stop.COAST) sortedResultKeys = sorted(results) for key in sortedResultKeys: print("%s" % (results[key])) print("Last reading was at %smm" % (sortedResultKeys[-1]))
from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait # Initialize a motor on port A. example_motor = Motor(Port.A) # Please have a look at the previous example first. This example # finds two endspoints and then makes the middle the zero point. # The run_until_stalled gives us the angle at which it stalled. # We want to know this value for both endpoints. left_end = example_motor.run_until_stalled(-200, duty_limit=30) right_end = example_motor.run_until_stalled(200, duty_limit=30) # We have just moved to the rightmost endstop. So, we can reset # this angle to be half the distance between the two endpoints. # That way, the middle corresponds to 0 degrees. example_motor.reset_angle((right_end - left_end) / 2) # From now on we can simply run towards zero to reach the middle. example_motor.run_target(200, 0) wait(1000)
from pybricks.pupdevices import Motor, Remote from pybricks.parameters import Port, Direction, Stop, Button from pybricks.tools import wait # Initialize the motors. steer = Motor(Port.B) front = Motor(Port.A, Direction.COUNTERCLOCKWISE) # Lower the acceleration so the car starts and stops realistically. front.control.limits(acceleration=1000) # Connect to the remote. remote = Remote() # Find the steering endpoint on the left and right. # The middle is in between. left_end = steer.run_until_stalled(-200, then=Stop.HOLD) right_end = steer.run_until_stalled(200, then=Stop.HOLD) # We are now at the right. Reset this angle to be half the difference. # That puts zero in the middle. steer.reset_angle((right_end - left_end) / 2) steer.run_target(speed=200, target_angle=0, wait=False) # Now we can start driving! while True: # Check which buttons are pressed. pressed = remote.buttons.pressed() # Choose the steer angle based on the left controls. steer_angle = 0
""" This program is for Tricky's Intro dance. Follow the corresponding building instructions in the LEGO® MINDSTORMS® Robot Inventor App. Trigger Tricky to dance by placing something near its Distance Sensor. """ from pybricks.pupdevices import Motor, UltrasonicSensor from pybricks.parameters import Direction, Port from pybricks.robotics import DriveBase from pybricks.tools import wait # Configure the Drive Base and the Distance Sensor. drive_base = DriveBase(left_motor=Motor(Port.A, Direction.COUNTERCLOCKWISE), right_motor=Motor(Port.B), wheel_diameter=44, axle_track=88) distance_sensor = UltrasonicSensor(Port.D) # Turn the Distance Sensor lights off and on. distance_sensor.lights.off() wait(1000) distance_sensor.lights.on(100) # Tricky begins dancing/turning whenever the Distance Sensor detects # something closer than 10 cm (100 mm). while True: if distance_sensor.distance() < 100:
# SPDX-License-Identifier: MIT # Copyright (c) 2020 The Pybricks Authors """ Hardware Module: 1 Description: Verifies color sensing and calibration capability. """ from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor from pybricks.parameters import Port, Color # Initialize devices. motor = Motor(Port.A) color_sensor = ColorSensor(Port.B) ultrasonic_sensor = UltrasonicSensor(Port.C) SPEED = 500 # Color angle targets angles = { "GREEN": 20, "BLUE": 110, "RED": 200, "YELLOW": 290, "BLACK": 250, "WHITE": 75, "NONE": 162, } # Verify saturated colors without calibration. for name in ("GREEN", "BLUE", "RED", "YELLOW"): motor.run_target(SPEED, angles[name])
from pybricks.pupdevices import Motor, DCMotor, Remote from pybricks.parameters import Port, Direction, Button, Color from pybricks.tools import wait # Connect to the remote. remote = Remote() # Initialize the drive motors. drive_motor1 = Motor(Port.A) drive_motor2 = Motor(Port.B) # Lower the acceleration so it starts and stops realistically. drive_motor1.control.limits(acceleration=1000) drive_motor2.control.limits(acceleration=1000) # Find the steering endpoint on the left and right. # The middle is in between. steer_motor = Motor(Port.D, Direction.COUNTERCLOCKWISE) left_end = steer_motor.run_until_stalled(-400) right_end = steer_motor.run_until_stalled(400) # Return to the center. max_steering = (right_end - left_end) / 2 steer_motor.reset_angle(max_steering) steer_motor.run_target(speed=200, target_angle=0) # Initialize the differential as unlocked. lock_motor = DCMotor(Port.C) LOCK_POWER = 60 lock_motor.dc(-LOCK_POWER) wait(600)
# SPDX-License-Identifier: MIT # Copyright (c) 2020 The Pybricks Authors """ Hardware Module: 1 Description: Verifies motor readings such as angle, absolute angle, and speed. """ from pybricks.pupdevices import Motor, UltrasonicSensor from pybricks.parameters import Port, Direction from pybricks.tools import wait, StopWatch # Initialize devices. motor = Motor(Port.A) motor = Motor(port=Port.A, positive_direction=Direction.CLOCKWISE, gears=[]) ultrasonic_sensor = UltrasonicSensor(Port.C) # Assert initial values. assert motor.speed() == 0, "Unexpected initial motor speed." assert -180 <= motor.angle() <= 179, "Unexpected initial motor angle." # Verify driving to zero. for target in (-360, 0, 360): motor.run_target(500, target) assert ultrasonic_sensor.distance() < 100, "Unexpected distance result." # Test angle reset. for reset_value in (-98304, -360, -180, -1.234, 0, 0.1, 1, 178, 180, 13245687): motor.reset_angle(reset_value) assert motor.angle() == int(reset_value), "Incorrect angle reset"
# SPDX-License-Identifier: MIT # Copyright (c) 2020 The Pybricks Authors """ Hardware Module: 1 Description: Verifies that sensors do not give stale data after a mode change. """ from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port, Color from pybricks.tools import wait # Initialize devices. motor = Motor(Port.A) color_sensor = ColorSensor(Port.B) SPEED = 500 # Initialize sensor to make sure we're in the expected mode. color_sensor.color() wait(500) # Color angle targets angles = { "GREEN": 20, "BLUE": 110, "RED": 200, "YELLOW": 290, "BLACK": 250, "WHITE": 75, "NONE": 162,
from pybricks.pupdevices import Motor from pybricks.parameters import Port, Direction from pybricks.tools import wait # Initialize a motor on port A with the positive direction as counterclockwise. example_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE) # When we choose a positive speed value, the motor now goes counterclockwise. example_motor.run(500) # This is useful when your motor is mounted in reverse or upside down. # By changing the positive direction, your script will be easier to read, # because a positive value now makes your robot/mechanism go forward. # Wait for three seconds. wait(3000)
def _lightOff(): hub.light.off() def _lightBlink(colour): hub.light.blink(colour, [200, 200]) print(pybricks.version) hub = TechnicHub() sensor = ColorDistanceSensor(Port.A) colour.initColoursForSensor(sensor) controller = ColorDistanceSensor(Port.B) leftMotor = Motor(Port.C, Direction.COUNTERCLOCKWISE, gears=[12, 20]) rightMotor = Motor(Port.D, gears=[12, 20]) driveBase = DriveBase(leftMotor, rightMotor, 49.7, 98.1) motion = motion.Motion(sensor, driveBase) terrain = navigation.Terrain() try: print("Battery is %smV" % hub.battery.voltage()) _lightBlink(PBColor.WHITE) _waitForController() _lightOff() home = motion.position.copy() while True: try: motion.seek()
# SPDX-License-Identifier: MIT # Copyright (c) 2021 The Pybricks Authors """ Hardware Module: 1 Description: Verifies motor trajectory generation. """ from pybricks.pupdevices import Motor from pybricks.parameters import Port # Initialize devices. motor = Motor(Port.A) # Basic trajectories starting from zero. It would be nice to compute them # here in the loop so we can also include random tests, vary acceleration, etc. trajectories = [ ((500, 90), (0, 244, 244, 488, 0, 45, 45, 90, 0, 367, 1500, -1500)), ((1000, 720), (0, 666, 720, 1386, 0, 333, 387, 720, 0, 1000, 1500, -1500)), ((2000, 720), (0, 666, 720, 1386, 0, 333, 387, 720, 0, 1000, 1500, -1500)), ((10, 720), (0, 6, 72006, 72012, 0, 0, 720, 720, 0, 10, 1500, -1500)), ((-500, 360), (0, 333, 721, 1054, 0, -83, -277, -360, 0, -500, -1500, 1500)), ((500, -360), (0, 333, 721, 1054, 0, -83, -277, -360, 0, -500, -1500, 1500)), ((-500, -360), (0, 333, 721, 1054, 0, 83, 277, 360, 0, 500, 1500, -1500)), ((500, 1), (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1500, -1500)), ((500, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), ] # Assert that all trajectories are correct.
from pybricks.hubs import InventorHub from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port, Direction from pybricks.tools import wait, StopWatch from pybricks.geometry import Axis # Initialize motors. left = Motor(Port.C, Direction.COUNTERCLOCKWISE) right = Motor(Port.D) left.reset_angle(0) right.reset_angle(0) # Initialize hub and color sensor. # You can also use the TechnicHub and/or the ColorDistanceSensor # instead. hub = InventorHub() sensor = ColorSensor(Port.A) # Initialize position buffer for speed calculation. DT = 5 window = int(300 / DT) buf = [0] * window idx = 0 angle = 0 DRIVE_SPEED = 300 PAUSE = 5000 # Timer to generate reference position. watch = StopWatch() while True:
test_args = [ None, [1, 3], (1, 3), [1, 2, 3], [1, 28], [12, 36], [36, 12], [[1, 24], [12, 36]], [(1, 24), (12, 36)], [(1, 24), [12, 36]], [[57, 24], [12, 123456789, 1]], ] # Initialize device to known position. motor = Motor(Port.A, gears=None) motor.run_target(500, 170) wait(500) motor.dc(0) real_angle = motor.angle() # Test expected result. for gears in test_args: # Initialize motors with new settings. motor = Motor(Port.A, gears=gears) # Get expected and measured value. expected = real_angle / get_gear_ratio(gears) measured = motor.angle() # Allow at most one degree of error. We could be a bit more precise
from pybricks.hubs import TechnicHub from pybricks.pupdevices import Motor from pybricks.parameters import Port, Stop from pybricks.tools import wait from pybricks.experimental import getchar hub = TechnicHub() mDrive = Motor(Port.D) mSteer = Motor(Port.B) mSteer.reset_angle() SPEED_DRIVE = 100 TIME_DRIVE = 30 STEP_STEER = 15 SPEED_STEER = 720 MAX_STEER = 75 mSteer.run_target(SPEED_STEER, 0, then=Stop.BRAKE) while True: c = getchar() if c == ord('q'): mDrive.dc(SPEED_DRIVE) wait(TIME_DRIVE) elif c == ord('a'): mDrive.dc(-SPEED_DRIVE) wait(TIME_DRIVE) elif c == ord('o'): if mSteer.angle() > -MAX_STEER: mSteer.run_angle(SPEED_STEER, -STEP_STEER, then=Stop.BRAKE)
from pybricks.pupdevices import Motor from pybricks.parameters import Port, Stop from pybricks.tools import wait # Initialize a motor on port A. example_motor = Motor(Port.A) # By default, the motor holds the position. It keeps # correcting the angle if you move it. example_motor.run_angle(500, 360) wait(1000) # This does exactly the same as above. example_motor.run_angle(500, 360, then=Stop.HOLD) wait(1000) # You can also brake. This applies some resistance # but the motor does not move back if you move it. example_motor.run_angle(500, 360, then=Stop.BRAKE) wait(1000) # This makes the motor coast freely after it stops. example_motor.run_angle(500, 360, then=Stop.COAST) wait(1000)
from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait # Initialize a motor on port A. example_motor = Motor(Port.A) # Run at 500 deg/s and then stop by coasting. print("Demo of run") example_motor.run(500) wait(1500) example_motor.stop() wait(1500) # Run at 70% duty cycle ("power") and then stop by coasting. print("Demo of dc") example_motor.dc(50) wait(1500) example_motor.stop() wait(1500) # Run at 500 deg/s for two seconds. print("Demo of run_time") example_motor.run_time(500, 2000) wait(1500) # Run at 500 deg/s for 90 degrees. print("Demo of run_angle") example_motor.run_angle(500, 180) wait(1500)
from pybricks.pupdevices import Motor, ColorDistanceSensor from pybricks.parameters import Port, Direction, Stop from pybricks.tools import wait # Initialize the motors and sensor. steer = Motor(Port.C) front = Motor(Port.A, Direction.COUNTERCLOCKWISE) rear = Motor(Port.B, Direction.COUNTERCLOCKWISE) sensor = ColorDistanceSensor(Port.D) # Lower the acceleration so the car starts and stops realistically. front.control.limits(acceleration=1000) rear.control.limits(acceleration=1000) # Find the steering endpoint on the left and right. The difference # between them is the total angle it takes to go from left to right. # The middle is in between. left_end = steer.run_until_stalled(-200, then=Stop.HOLD) right_end = steer.run_until_stalled(200, then=Stop.HOLD) # We are now at the right limit. We reset the motor angle to the # limit value, so that the angle is 0 when the steering mechanism is # centered. limit = (right_end - left_end) // 2 steer.reset_angle(limit) steer.run_target(speed=200, target_angle=0, then=Stop.COAST) # Given a motor speed (deg/s) and a steering motor angle (deg), this # function makes the car move at the desired speed and turn angle. # The car keeps moving until you give another drive command.
from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait # Initialize a motor on port A. example_motor = Motor(Port.A) # Run at 500 deg/s and then stop by coasting. example_motor.run(500) wait(1500) example_motor.stop() wait(1500) # Run at 500 deg/s and then stop by braking. example_motor.run(500) wait(1500) example_motor.brake() wait(1500) # Run at 500 deg/s and then stop by holding. example_motor.run(500) wait(1500) example_motor.hold() wait(1500) # Run at 500 deg/s and then stop by running at 0 speed. example_motor.run(500) wait(1500) example_motor.run(0) wait(1500)
from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait # Initialize a motor on port A. example_motor = Motor(Port.A) # Make the motor run clockwise at 500 degrees per second. example_motor.run(500) # Wait for three seconds. wait(3000) # Make the motor run counterclockwise at 500 degrees per second. example_motor.run(-500) # Wait for three seconds. wait(3000)
from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait # Initialize a motor on port A. example_motor = Motor(Port.A) # Start moving at 300 degrees per second. example_motor.run(300) # Display the angle and speed 50 times. for i in range(100): # Read the angle (degrees) and speed (degrees per second). angle = example_motor.angle() speed = example_motor.speed() # Print the values. print(angle, speed) # Wait some time so we can read what is displayed. wait(200)
from pybricks.hubs import InventorHub from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port, Button, Stop from pybricks.tools import wait # Tuning parameters SPEED_MAX = 1000 SPEED_TURN = 500 SPEED_OFFLINE = 400 # Initialize the hub, motors, and sensor hub = InventorHub() steer_motor = Motor(Port.A) drive_motor = Motor(Port.B) sensor = ColorSensor(Port.C) def wait_for_button(b): # Wait for press while b not in hub.buttons.pressed(): wait(10) # and release while b in hub.buttons.pressed(): wait(10) # Use the color saturation value to track line def get_light(): return sensor.hsv().s
from pybricks.pupdevices import Motor from pybricks.parameters import Port # Initialize motors on port A and B. track_motor = Motor(Port.A) gripper_motor = Motor(Port.B) # Make the track motor start moving, # but don't wait for it to finish. track_motor.run_angle(500, 360, wait=False) # Now make the gripper motor rotate. This # means they move at the same time. gripper_motor.run_angle(200, 720)