Ejemplo n.º 1
0
from pybricks.pupdevices import DCMotor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize a motor without rotation sensors on port A.
example_motor = DCMotor(Port.A)

# Start and stop 10 times.
for count in range(10):
    print("Counter:", count)

    example_motor.dc(70)
    wait(1000)

    example_motor.stop()
    wait(1000)
Ejemplo n.º 2
0
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

# 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:
        steer_angle -= max_steering
Ejemplo n.º 3
0
from pybricks.hubs import CityHub
from pybricks.pupdevices import ColorDistanceSensor, DCMotor
from pybricks.parameters import Port, Color
from pybricks.tools import wait

# Initialize the CityHub
cityHub = CityHub()
print("Battery voltage: " + str(hub.battery.voltage()))

# Initialize the sensor.
train = DCMotor(Port.A)
sensor = ColorDistanceSensor(Port.B)

# Now we use the function we just created above.
while True:

    # Here you can make your train/vehicle stop.
    if (sensor.color() == Color.RED):
        cityHub.on(Color.RED)
        train.dc(0)

    # Here you can make your train/vehicle go forward.
    if (sensor.color() == Color.GREEN):
        cityHub.on(Color.GREEN)
        train.dc(30)

        # Here you can make your train/vehicle go backward.
    if (sensor.color() == Color.WHITE):
        cityHub.on(Color.WHITE)
        train.dc(-1 * train.dc())
Ejemplo n.º 4
0
from pybricks.pupdevices import TiltSensor, DCMotor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize the train motor.
# If you have a motor with encoders, use the Motor class instead.
train = DCMotor(Port.A)

# Initialize the tilt sensor.
sensor = TiltSensor(Port.B)

# Measure the tilt while the train is on a flat surface.
pitch_start, roll_start = sensor.tilt()

# Start driving.
train.dc(-50)

# We need to reach a constant speed before we can check tilt again,
# because acceleration affects tilt. So we wait a second.
wait(1000)

# Wait for the train to sense the hill.
while True:
    pitch_now, roll_now = sensor.tilt()

    wait(10)

    # If we reached an extra 3 degrees, exit/break the loop.
    if pitch_now >= pitch_start + 8:
        break
Ejemplo n.º 5
0
from pybricks.pupdevices import DCMotor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize a motor without rotation sensors on port A.
example_motor = DCMotor(Port.A)

# Make the motor go clockwise (forward) at 70% duty cycle ("70% power").
example_motor.dc(70)

# Wait for three seconds.
wait(3000)

# Make the motor go counterclockwise (backward) at 70% duty cycle.
example_motor.dc(-70)

# Wait for three seconds.
wait(3000)
Ejemplo n.º 6
0
from pybricks.pupdevices import DCMotor, ColorDistanceSensor
from pybricks.tools import StopWatch
from pybricks.parameters import Port

# Initialize the motor and the sensor.
motor = DCMotor(Port.A)
sensor = ColorDistanceSensor(Port.B)

# These are the sensor reflection values in this setup.
# Adapt them to match your ambient light conditions.
LIGHT = 57
DARK = 16

# Threshold values. We add a bit of hysteresis to make
# sure we skip extra changes on the edge of each track.
hysteresis = (LIGHT - DARK) / 4
threshold_up = (LIGHT + DARK) / 2 + hysteresis
threshold_down = (LIGHT + DARK) / 2 - hysteresis

# Initial position state.
on_track = True
position = 0

# Desired drive speed in mm per second.
SPEED = 300

# It's two studs (16 mm) for each position increase.
MM_PER_COUNT = 16

# Start a timer.
watch = StopWatch()
Ejemplo n.º 7
0
from pybricks.pupdevices import DCMotor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize the motor.
train_motor = DCMotor(Port.A)

# Choose the "power" level for your train. Negative means reverse.
train_motor.dc(50)

# Keep doing nothing. The train just keeps going.
while True:
    wait(1000)
Ejemplo n.º 8
0
from pybricks.pupdevices import DCMotor
from pybricks.hubs import CityHub
from pybricks.parameters import Port

hub = CityHub()
mOwl = DCMotor(Port.B)

OWL_SPEED = 40

while True:
    i = input("?")
    if i == "l":
        mOwl.dc(-OWL_SPEED)
    elif i == 'r':
        mOwl.dc(OWL_SPEED)
    elif i == 's':
        mOwl.stop()
    else:
        print("!")
from pybricks.pupdevices import DCMotor
from pybricks.parameters import Port, Direction
from pybricks.tools import wait

# Initialize a motor without rotation sensors on port A,
# with the positive direction as counterclockwise.
example_motor = DCMotor(Port.A, Direction.COUNTERCLOCKWISE)

# When we choose a positive duty cycle, the motor now goes counterclockwise.
example_motor.dc(70)

# This is useful when your (train) 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 train/robot go forward.

# Wait for three seconds.
wait(3000)