Additional motor with Pybricks - 4X4 X-treme Off-roader #2010
-
Hello to all! I have a model #42099 4X4 X-treme Off-roader. It's hub has 3 ports occupied, 1 is empty. I replaced the default firmware with the Pybricks one for this model. This let me use Lego remote (88010) to control my off-roader. Here is my code: from pybricks.pupdevices import *
from pybricks.parameters import *
from pybricks.tools import wait
front = Motor(Port.A)
rear = Motor(Port.B)
steering = Motor(Port.C)
hook = Motor(Port.D)
remoteControl = Remote()
#steering calibration sequence
steering.run_until_stalled(500)
steering.reset_angle(0)
steering.run_until_stalled(-500)
maxAngle = steering.angle()
steering.run_target(500, (maxAngle/2)+10)
steering.reset_angle(0)
#main loop
while True:
#update pressed buttons information
pressed = remoteControl.buttons.pressed()
#commands to drive forwards or backwards
if Button.LEFT_PLUS in pressed:
rear.dc(100)
front.dc(100)
elif Button.LEFT_MINUS in pressed:
rear.dc(-100)
front.dc(-100)
else:
rear.brake()
front.brake()
#commands to steer
if Button.RIGHT_PLUS in pressed:
steering.run_target(1200, (maxAngle/2)-5, Stop.HOLD,False)
elif Button.RIGHT_MINUS in pressed:
steering.run_target(1200, -((maxAngle/2)-5), Stop.HOLD,False)
else:
steering.run_target(1200, 0, Stop.HOLD, False)
#4th motor for hook, red buttons
if Button.LEFT in pressed:
hook.dc(-100)
else:
hook.brake()
if Button.RIGHT in pressed:
hook.dc(100)
else:
hook.brake()
wait(100) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
That is a nice extension 😄
Have a look at this piece of your code. #4th motor for hook, red buttons
if Button.LEFT in pressed:
hook.dc(-100)
else:
hook.brake()
if Button.RIGHT in pressed:
hook.dc(100)
else:
hook.brake() What will it do if one button is pressed, or when both are pressed? By contrast, what will the following adaptation do? #4th motor for hook, red buttons
if Button.LEFT in pressed:
hook.dc(-100)
elif Button.RIGHT in pressed:
hook.dc(100)
else:
hook.brake() |
Beta Was this translation helpful? Give feedback.
That is a nice extension 😄
Have a look at this piece of your code.
What will it do if one button is pressed, or when both are pressed?
By contrast, what will the following adaptation do?