Open a new Python file in your editor, and start by importing the necessary libraries to control the motors.
Add this to the very top of the code.py file:
from setup import Forward, Stop, Wait, M1, M2, M3, M4, RightWheel, LeftWheel
Based on the setup.py configuration, initialize your motors. This example assumes you have two motors controlling the wheels (left and right) and that you have a function newMotor to create motor instances.
# Assuming newMotor is a function to initialize motors as per your setup.py
RightWheel = M3 #Change to M1, M2, M3 or M4 depending on where it's connected
LeftWheel = M4 #Change to M1, M2, M3 or M4 depending on where it's connected
Now, call the move_forward function to make the robot start moving. You can adjust the speed parameter to control how fast the robot moves forward.
Forward(1, speed=0.5) #This will move your robot forward at half speed for 1 second
If you want the robot to keep moving forwards until something else happens set the time to 0:
Forward(0, speed=0.5) #Keeps the robot moving forwards until you tell it to do something else
The forward function you call looks like this:
def Forward(delay=1, speed=0.6):
speed = max(min(speed, 1), 0) #Make sure the speed is between 0 and 1
RightWheel.throttle = speed
LeftWheel.throttle = speed
if delay > 0: #If delay is bigger than 0
Wait(delay) #Wait for the duration of the delay
RightWheel.throttle = 0 #Stop the right wheel
LeftWheel.throttle = 0 #Stop the left wheel
Can you see how it works?
After moving forward for a while, you'll want to stop the robot. You can do this by setting the throttle of both motors to 0.
Forward(0, speed=0.5)
Wait(2)
Stop() # This stops the robot
The Stop function you call looks like this:
def Stop():
RightWheel.throttle = 0
LeftWheel.throttle = 0
Can you see how it works?
Save your Python script and run it. Your robot should move forward and then stop. You can adjust the duration of the forward movement by using the Wait() function from the setup library before calling the Stop() function.
Forward(0.5)
Wait(2) # Adjust the time as needed
Stop()
This is a basic guide to get your robot moving forward. Depending on your robot's specific design and requirements, you may need to adjust the motor labels, speed values, and durations. Experiment with different settings to see what works best for your robot!
Code is case and spelling sensitive, if an error occurs first check the spelling and check you've capitalised the function you're calling e.g. Forward(2) will work, but forward(2) wont!
If your robot drifts off to one side you can slow down one motor to compensate, to do this add the following near the top of the code.py file:
If it moves to the left add this to the top of the file below the line from setup import.. :
RightWheel.modifier = 0.85 #adjust the number (between 0.7 and 1) by trial and error to get it to run straight
if it moves to the right:
LeftWheel.modifier = 0.9 #adjust the number (between 0.7 and 1) by trial and error to get it to run straight
RightWheel.swapDirection() #Swap the direction of the motor
and/or:
LeftWheel.swapDirection() #Swap the direction of the motor