Created
April 22, 2020 16:27
-
-
Save antonvh/65be1cdd09c7ef35f75209d60afdd509 to your computer and use it in GitHub Desktop.
Pybricks 2.0 code for remote controlled race car with another EV3 brick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env pybricks-micropython | |
# car code | |
from pybricks.hubs import EV3Brick | |
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, | |
InfraredSensor, UltrasonicSensor, GyroSensor) | |
from pybricks.parameters import Port, Stop, Direction, Button, Color | |
from pybricks.tools import wait, StopWatch | |
ev3 = EV3Brick() | |
left_motor = Motor(Port.B) | |
right_motor = Motor(Port.C) | |
steer_motor = Motor(Port.A) | |
# Auto center steering wheels. | |
steer_motor.run_until_stalled(250) | |
steer_motor.reset_angle(80) | |
steer_motor.run_target(300,0) | |
#Still have to integrate this in an automated test. For now it's copy-paste | |
from pybricks.messaging import (BluetoothMailboxServer, | |
BluetoothMailboxClient, Mailbox, TextMailbox, NumericMailbox, LogicMailbox) | |
server = BluetoothMailboxServer() | |
server.wait_for_connection(1) | |
ev3.speaker.beep() | |
steering = NumericMailbox('left', server) | |
throttle = NumericMailbox('fwd', server) | |
while True: | |
steering.wait() | |
steer_tgt = steering.read() | |
if steer_tgt: | |
steer_motor.track_target(steer_tgt) | |
throttle.wait() | |
throttle_tgt = throttle.read() | |
if throttle_tgt: | |
left_motor.dc(throttle_tgt - throttle_tgt * 0.01 * steer_tgt) | |
right_motor.dc(throttle_tgt + throttle_tgt * 0.01 * steer_tgt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env /home/robot/remote_control_bt/pybricks-micropython | |
# Remote control code | |
# Don't pick it up until it beeps, or the Gyro calibration will be off. | |
from pybricks.hubs import EV3Brick | |
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, | |
InfraredSensor, UltrasonicSensor, GyroSensor) | |
from pybricks.parameters import Port, Stop, Direction, Button, Color | |
from pybricks.tools import wait, StopWatch | |
from pybricks.nxtdevices import SoundSensor | |
ev3 = EV3Brick() | |
gyro = GyroSensor(Port.S3) | |
snd = SoundSensor(Port.S2) | |
ts_l = TouchSensor(Port.S1) | |
ts_r = TouchSensor(Port.S4) | |
from pybricks.messaging import BluetoothMailboxServer, BluetoothMailboxClient, Mailbox, TextMailbox, NumericMailbox, LogicMailbox | |
# Replace this by the Bluetooth address of the car | |
# Remember to pair both EV3 bricks first. | |
car_2 = "00:17:E7:AD:B2:8F" | |
client = BluetoothMailboxClient() | |
client.connect(car_2) | |
ev3.speaker.beep() | |
print("Connected to {}".format(car_2)) | |
steering = NumericMailbox('left', client) | |
throttle = NumericMailbox('fwd', client) | |
while True: | |
steering.send(gyro.angle()) | |
if ts_l.pressed(): | |
throttle.send(-snd.intensity()) | |
else: | |
throttle.send(snd.intensity()) | |
if Button.CENTER in ev3.buttons.pressed() or ts_r.pressed(): | |
gyro.reset_angle(0) | |
if Button.DOWN in ev3.buttons.pressed(): | |
client.close() | |
break | |
wait(16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment