Gamepad - StrayCatNZ/lego GitHub Wiki
Driving a robot base with an xbox controller
Building Instructions: github.com//StrayCatNZ/lego/blob/main/bot1.pdf
Just copy and paste this source code into your pybricks file, no need to type everything by hand :)
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.iodevices import XboxController
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
hub = TechnicHub()
hub.light.on(Color.RED)
left_motor = Motor(Port.A)
right_motor = Motor(Port.B, Direction.COUNTERCLOCKWISE)
gp = XboxController()
drive = 0
steer = 0
while True:
drive = -gp.joystick_left()[1]
steer = gp.joystick_left()[0]
left_motor.dc(drive - steer)
right_motor.dc(drive + steer)
Page address: github.com/StrayCatNZ/lego/wiki/Gamepad