예제 #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)
예제 #2
0
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)
예제 #3
0
# 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.
for (speed, angle), trajectory in trajectories:
    # Start from 0.
    motor.reset_angle(0)

    # Initiate run_target command, stopping immediately.
    motor.run_angle(speed, angle, wait=False)
    result = motor.control.trajectory()
    motor.stop()

    # Compare generated trajectory to saved trajectory.
    assert trajectory == result, "Bad trajectory: {0} for {1}".format(
        result, (speed, angle))
예제 #4
0
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)
예제 #5
0
# 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)

# 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)
예제 #6
0
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)
    elif c == ord('p'):
        if mSteer.angle() < MAX_STEER:
            mSteer.run_angle(SPEED_STEER, STEP_STEER, then=Stop.BRAKE)
    elif c == ord('r') or c == ord('0'):
        mSteer.run_target(SPEED_STEER, 0, then=Stop.BRAKE)
    else:
        mDrive.stop()
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]))
예제 #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()
"""
This program is for CNC Machine
(in the "Invention Squad: Broken" lesson unit).

Follow the corresponding building instructions in the LEGO® SPIKE™ Prime App.

This program will draw a rectangle.
"""

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

# Configure the Hub, the Force Sensor and the Motor
hub = PrimeHub()
horizontal_motor = Motor(Port.A)
vertical_motor = Motor(Port.C)

# Draw a rectangle
horizontal_motor.run_angle(speed=1000, rotation_angle=400)  # go right
vertical_motor.run_angle(speed=1000, rotation_angle=100)  # go down
horizontal_motor.run_angle(speed=1000, rotation_angle=-400)  # go left
vertical_motor.run_angle(speed=1000, rotation_angle=-100)  # go up