Motor function question #487
-
|
I have the 42114 Volvo Hauler and I am trying to make a control program for it. I am going through the Pybricks beta coder and also using a 88010 remote. I have the drive and steering working great, but the gearbox has me scratching my head. I want it to turn +90 degrees incrementally with the red left button, and -90 degrees incrementally with the right red button. I have tried: if Button.RIGHT in pressed:
gearbox.run_angle(150, -90, Stop.HOLD, False)
elif Button.LEFT in pressed:
gearbox.run_angle(150, 90, Stop.HOLD, False)and this works, but the motor coasts a little bit farther than +/-90*. I have also tried: if Button.RIGHT in pressed:
gearbox.run_target(150, -90, Stop.HOLD, False)
elif Button.LEFT in pressed:
gearbox.run_target(150, 90, Stop.HOLD, False)and this works, but will only go back and fourth between +90* and -90* with button presses. I'm quite new to Pybricks so Im not sure how to code it properly. Thanks for your help! (Full program is listed below if interested) from pybricks.pupdevices import Motor, Remote
from pybricks.parameters import Port, Direction, Stop, Button
from pybricks.tools import wait
# Initialize the motors.
front = Motor(Port.A, Direction.COUNTERCLOCKWISE)
gearbox = Motor(Port.B, Direction.COUNTERCLOCKWISE)
steer = Motor(Port.D)
# Connect to the remote.
remote = Remote()
# Read the current settings
old_kp, old_ki, old_kd, _, _ = steer.control.pid()
# Set new values
steer.control.pid(kp=old_kp*4, kd=old_kd*0.4)
# Find the steering endpoint on the left and right.
# The middle is in between.
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
right_end = steer.run_until_stalled(200, then=Stop.HOLD)
# We are now at the right. Reset this angle to be half the difference.
# That puts zero in the middle.
steer.reset_angle((right_end - left_end)/2)
steer.run_target(speed=200, target_angle=0, wait=False)
# Set steering angle.
steer_angle = (((right_end - left_end)/2)-5)
print('steer angle:',steer_angle)
# Now we can start driving!
while True:
# Check which buttons are pressed.
pressed = remote.buttons.pressed()
# Choose the steer angle based on the right controls.
if Button.LEFT_PLUS in pressed:
steer.run_target(1400, -steer_angle, Stop.HOLD, False)
elif Button.LEFT_MINUS in pressed:
steer.run_target(1400, steer_angle, Stop.HOLD, False)
else:
steer.track_target(0)
# Choose the drive speed based on the left controls.
drive_speed = 0
if Button.RIGHT_PLUS in pressed:
drive_speed += 100
if Button.RIGHT_MINUS in pressed:
drive_speed -= 100
# Apply the selected speed.
front.dc(drive_speed)
# Select a gear based on the right and left red controls.
if Button.RIGHT in pressed:
gearbox.run_angle(500, -90, Stop.HOLD, False)
elif Button.LEFT in pressed:
gearbox.run_angle(500, 90, Stop.HOLD, False)
# Wait.
wait(10) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Both
But in your case, you use So perhaps you could add a step in between to achieve what you want. Instead of controlling the motors directly with the remote, you could use the buttons of the remote to increment/decrement a variable called |
Beta Was this translation helpful? Give feedback.
Both
run_angleandrun_targetcan be used, each in their own way.run_angleis usually great for incremental movement, but you'll want to make sure you start from a known position at the very beginning. So you could userun_targetto move it to0at the start of your program, and then userun_angleafter that.But in your case, you use
wait=False, and you might be giving it a newrun_anglecommand when the previous one isn't done yet. That's fine in some applications, but perhaps not in your case. For example, if you are at 45 and you callrun_angleto add 90 degrees, you're going to 135.So perhaps you could add a step in between to achieve what you want. Instead of controlling the motor…