Esempio n. 1
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)
Esempio n. 2
0
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
    if Button.LEFT_PLUS in pressed:
Esempio n. 3
0
# 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)
lock_motor.stop()
locked = True
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)
Esempio n. 5
0
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)

# Run at 500 deg/s back to the 0 angle
print("Demo of run_target to 0")
example_motor.run_target(500, 0)
wait(1500)

# Run at 500 deg/s back to the -90 angle
print("Demo of run_target to -90")
example_motor.run_target(500, -90)
wait(1500)

# Run at 500 deg/s until the motor stalls
print("Demo of run_until_stalled")
example_motor.run_until_stalled(500)
print("Done")
wait(1500)
Esempio n. 6
0
# Hand-Controlled Grabber:
# press the Force Sensor to grab objects,
# and release the Force Sensor to let go.

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import ForceSensor, Motor
from pybricks.parameters import Port

# Configure the Hub, the Force Sensor and the Motor
hub = PrimeHub()
force_sensor = ForceSensor(Port.E)
motor = Motor(Port.A)

while True:
    # Grab when the Force Sensor is pressed
    if force_sensor.pressed():
        motor.run(speed=-1000)

    # else let go
    else:
        motor.run_until_stalled(speed=1000)
Esempio n. 7
0
from pybricks.hubs import MoveHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Stop, Color
from pybricks.tools import wait

# Initialize the arm motor
arm = Motor(Port.D)
arm.run_until_stalled(-100, duty_limit=30)
arm.reset_angle(0)

# Initialize the belt motor
belt = Motor(Port.B)
belt.run_until_stalled(-100, duty_limit=30)
belt.reset_angle(0)

# Component positions
FEET = 0
BELLY = 365
HEAD = 128
ARMS = 244
NECK = 521
BASE = 697

# Place all the elements
for element in (FEET, BELLY, ARMS, NECK, HEAD):

    # Go to the element
    belt.run_target(speed=200, target_angle=element)

    # Grab the element
    arm.run_time(speed=300, time=2000)
Esempio n. 8
0
class TrickyPlayingSoccer:

    WHEEL_DIAMETER = 44
    AXLE_TRACK = 88
    KICK_SPEED = 1000

    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 reset_kicker_motor(self):
        # Prepare the kicker motor for kicking.
        self.hub.light.off()
        self.kicker_motor.run_until_stalled(-self.KICK_SPEED, Stop.HOLD)
        self.kicker_motor.run_angle(self.KICK_SPEED, 325)

    def kick(self):
        # Kick the ball!
        self.kicker_motor.track_target(360)

    def run_to_and_kick_ball(self):
        # Drive until we see the red ball.
        self.drive_base.drive(speed=1000, turn_rate=0)
        while self.color_sensor.color() != Color.RED:
            wait(10)
        self.hub.light.on(Color.RED)
        self.hub.speaker.beep(frequency=100, duration=100)
        self.kick()
        self.drive_base.stop()
        self.hub.light.off()

    def celebrate(self):
        # Celebrate with light and sound.
        self.hub.display.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.speaker.beep(frequency=1000, duration=1000)
        self.hub.light.animate([Color.CYAN, Color.GREEN, Color.MAGENTA],
                               interval=100)

        # Celebrate by dancing around.
        self.drive_base.drive(speed=0, turn_rate=360)
        self.kicker_motor.run_angle(self.KICK_SPEED, 5 * 360)

        # Party's over! Let's stop everything.
        self.hub.display.off()
        self.hub.light.off()
        self.drive_base.stop()
Esempio n. 9
0
from pybricks.pupdevices import Motor
from pybricks.parameters import Port

# Initialize a motor on port A.
example_motor = Motor(Port.A)

# We'll use a speed of 200 deg/s in all our commands.
speed = 200

# Run the motor in reverse until it hits a mechanical stop.
# The duty_limit=30 setting means that it will apply only 30%
# of the maximum torque against the mechanical stop. This way,
# you don't push against it with too much force.
example_motor.run_until_stalled(-speed, duty_limit=30)

# Reset the angle to 0. Now whenever the angle is 0, you know
# that it has reached the mechanical endpoint.
example_motor.reset_angle(0)

# Now make the motor go back and forth in a loop.
# This will now work the same regardless of the
# initial motor angle, because we always start
# from the mechanical endpoint.
for count in range(10):
    example_motor.run_target(speed, 180)
    example_motor.run_target(speed, 90)