diff --git a/sets/technic/42099-off-roader/42099-off-roader.jpg b/sets/technic/42099-off-roader/42099-off-roader.jpg new file mode 100644 index 0000000..a03cc60 Binary files /dev/null and b/sets/technic/42099-off-roader/42099-off-roader.jpg differ diff --git a/sets/technic/42099-off-roader/README.md b/sets/technic/42099-off-roader/README.md new file mode 100644 index 0000000..74ddafb --- /dev/null +++ b/sets/technic/42099-off-roader/README.md @@ -0,0 +1,9 @@ +--- +permalink: /projects/sets/technic/42099-off-roader/ +title: "42099: 4X4 X-treme Off-Roader" +image: + local: "42099-off-roader.jpg" + credit: "LEGO" +layout: set +description: "Add set description" +--- diff --git a/sets/technic/42099-off-roader/driving/README.md b/sets/technic/42099-off-roader/driving/README.md new file mode 100644 index 0000000..1e7c16d --- /dev/null +++ b/sets/technic/42099-off-roader/driving/README.md @@ -0,0 +1,25 @@ +--- +permalink: /projects/sets/technic/42099-off-roader/driving/ +title: "42099: Technic Off-Roader: Driving" +maintainer: + user: "pybricks" + name: "The Pybricks Authors" +image: + local: "../42099-off-roader.jpg" + credit: "LEGO" +description: + "This project shows how to make the car drive and steer. You can also + remote control it with a keyboard." +building_instructions: + external: https://www.lego.com/cdn/product-assets/product.bi.core.pdf/6314518.pdf +--- + +# Basic driving and steering + +This program centers the steering motor, and defines a function called +``drive`` which can be used to control the vehicle. Further details +are given as comments in the program. + +```python +{% include_relative main.py %} +``` diff --git a/sets/technic/42099-off-roader/driving/main.py b/sets/technic/42099-off-roader/driving/main.py new file mode 100644 index 0000000..fdcc34f --- /dev/null +++ b/sets/technic/42099-off-roader/driving/main.py @@ -0,0 +1,55 @@ +from pybricks.pupdevices import Motor, ColorDistanceSensor +from pybricks.parameters import Port, Direction, Stop +from pybricks.tools import wait + +# Initialize the motors +steer = Motor(Port.C) +front = Motor(Port.A, Direction.COUNTERCLOCKWISE) +rear = Motor(Port.B, Direction.COUNTERCLOCKWISE) + +# Lower the acceleration so the car starts and stops realistically. +front.control.limits(acceleration=1000) +rear.control.limits(acceleration=1000) + +# Find the steering endpoint on the left and right. The difference between them +# is the total angle it takes to go from left to 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 limit. We reset the motor angle to the limit value, +# so that the angle is 0 when the steering mechanism is centered. +limit = (right_end - left_end)//2 +steer.reset_angle(limit) +steer.run_target(speed=200, target_angle=0, then=Stop.COAST) + +# Given a motor speed (deg/s) and a steering motor angle (deg), this function +# makes the car move at the desired speed and turn angle. The car keeps moving +# until you give another drive command. +def drive(drive_motor_speed, steer_angle): + # Start running the drive motors + front.run(drive_motor_speed) + rear.run(drive_motor_speed) + + # Limit the steering value for safety, and then start the steer motor. + limited_angle = max(-limit, min(steer_angle, limit)) + steer.run_target(200, limited_angle, wait=False) + +# Drive forward for 5 seconds. +drive(800, 0) +wait(5000) + +# Drive backward for 5 seconds. +drive(-800, 0) +wait(5000) + +# Drive forward to the right for 5 seconds. +drive(800, 90) +wait(5000) + +# Drive backward to the left for 5 seconds. +drive(-800, -90) +wait(5000) + +# Stop smoothly and center the steer. +drive(0, 0) +wait(2000)