-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add robots 101 page about motors #644
Open
RealOrangeOne
wants to merge
2
commits into
main
Choose a base branch
from
robots-101-motors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
layout: page | ||
title: Robots 101 - Motors | ||
--- | ||
|
||
# Robots 101 - Motors | ||
|
||
Your robot will need to be able to move to score points. There are many different ways of doing this, but motors are the way to do it. Motors are controlled using the [motor board]({{ site.baseurl }}/kit/motor_board). Each motor board can control up to 2 motors. | ||
|
||
The motor board gives you two axis of control. Not only can you control the speed the motor turns, but also the direction. | ||
|
||
## Preparation | ||
|
||
To start using your motor board, you'll need to [connect]({{ site.baseurl }}/tutorials/assembly) it to your robot. | ||
|
||
When testing your motors, it's best to put your robot on the floor, or raise it above the table, to ensure it doesn't drive off and damage itself or something else. | ||
|
||
Your code will need to initialise a `Robot` object, which allows you to control the connected motor boards: | ||
|
||
~~~~~ python | ||
from sr.robot3 import Robot | ||
|
||
robot = Robot() | ||
~~~~~ | ||
|
||
In this tutorial, we'll assume your robot has 2 drive motors, one on each side. | ||
|
||
## Driving forwards | ||
|
||
For your robot to drive forwards, both motors need to turn the same direction. | ||
|
||
~~~~~ python | ||
from sr.robot3 import Robot | ||
|
||
robot = Robot() | ||
|
||
robot.motor_board.motors[0].power = 0.5 | ||
robot.motor_board.motors[1].power = 0.5 | ||
|
||
# Drive for 10 seconds. | ||
robot.sleep(10) | ||
~~~~~ | ||
|
||
The above code will set both motors (`0` and `1`) to half speed forwards (`0.5`). If you run this code, your robot should drive forwards. If you want your robot to move faster or slower, just increase or decrease the speed. | ||
|
||
<div class="info"> | ||
If your motors turn in opposite directions, it's often easier to swap the wires rather than adjusting your code. | ||
</div> | ||
|
||
When the robot runs out of code to run, the program terminates, and all motors and other components turn off. At the end of the program is `robot.sleep(10)`, which lets the program keep running for 10 seconds. For your actual program, your robot will likely have more to do after it moves. | ||
|
||
### LEDs | ||
|
||
As your motors are turning, you may notice some LEDs lighting up next to the outputs. The LEDs will light up either red or blue based on the direction the robot. | ||
|
||
### Inaccuracies | ||
|
||
Even though both your motors are turning at the same speed, your robot may not drive in a perfectly straight line. This is expected, and is usually down to manufacturing tolerances in the motors or wheels themselves. | ||
|
||
To account for this, you'll need to change your code slightly. If one motor spins slower, give it a little more power: | ||
|
||
~~~~~ python | ||
MOTOR_0_MULTIPLIER = 1 | ||
MOTOR_1_MULTIPLIER = 0.9 | ||
|
||
robot.motor_board.motors[0].power = 0.5 * MOTOR_0_MULTIPLIER | ||
robot.motor_board.motors[1].power = 0.5 * MOTOR_1_MULTIPLIER | ||
~~~~~ | ||
|
||
If motor `1` spins faster than motor `0`, slow it down. The above code uses a multiplier to slow motor `1` down to 90% of the intended power. By tuning the multiplier correctly, you only need to think "move forward at half speed", and the multipliers handle the rest: | ||
|
||
~~~~~ python | ||
def drive_straight(speed): | ||
robot.motor_board.motors[0].power = speed * MOTOR_0_MULTIPLIER | ||
robot.motor_board.motors[1].power = speed * MOTOR_1_MULTIPLIER | ||
|
||
|
||
drive_straight(0.5) | ||
robot.sleep(1) | ||
drive_straight(0) | ||
~~~~~ | ||
|
||
Here, the `drive_straight` function can be used to drive your robot in a straight line. Your robot will drive forwards at half speed for 1 second, then stop. Setting your motor powers to `0` will stop your robot. | ||
|
||
### Driving backwards | ||
|
||
Your robot can drive backwards by both motors turning in reverse. Motors can spin backwards by setting them to a negative power: | ||
|
||
~~~~~ python | ||
robot.motor_board.motors[0].power = -0.5 * MOTOR_0_MULTIPLIER | ||
robot.motor_board.motors[1].power = -0.5 * MOTOR_1_MULTIPLIER | ||
~~~~~ | ||
|
||
The above code sets both motors to half speed backwards (`-0.5`), taking into account the multipliers. | ||
|
||
## Turning | ||
|
||
For your robot to turn, each motor needs to turn in a different direction. | ||
|
||
If the left wheel drives forwards, and the right wheel drives backwards, your robot will turn clockwise about its centre. | ||
|
||
~~~~~ python | ||
robot.motor_board.motors[0].power = 0.5 * MOTOR_0_MULTIPLIER | ||
robot.motor_board.motors[1].power = -0.5 * MOTOR_1_MULTIPLIER | ||
~~~~~ | ||
|
||
If the left wheel drives forwards, and the right wheel remains stationary, your robot will turn about the right wheel. | ||
|
||
~~~~~ python | ||
robot.motor_board.motors[0].power = 0.5 * MOTOR_0_MULTIPLIER | ||
robot.motor_board.motors[1].power = 0 | ||
~~~~~ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought: "Motors" in "Robots 101" could imply there's more here than just how to use the motors API.
Perhaps putting this content under "Robots 101" isn't the right place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely. From the title I'd assumed this was also about motors themselves, perhaps including suggestions on how to pick them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking again this page feels like it has a lot of overlap with https://studentrobotics.org/docs/tutorials/basic_motor_control, is that expected?
There is good/new content in this PR, which would definitely be valuable to have in our docs, so maybe an alternative is to review the existing tutorial and either merge this or break out separate sub-pages?