Skip to content

Commit 1c50c8a

Browse files
committed
Add robots 101 page about motors
1 parent 4983104 commit 1c50c8a

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

_data/sidebar_tree.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ tree:
2121
title: Tech Days
2222
- url: /robots_101/team_supervisor
2323
title: Running a team
24+
- url: /robots_101/motors
25+
title: Motors
2426
- url: /tutorials/
2527
title: Tutorials
2628
tree:

robots_101/motors.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
layout: page
3+
title: Robots 101 - Motors
4+
---
5+
6+
# Robots 101 - Motors
7+
8+
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.
9+
10+
The motor board gives you two axis of control. Not only can you control the speed the motor turns, but also the direction.
11+
12+
## Preparation
13+
14+
To start using your motor board, you'll need to [connect]({{ site.baseurl }}/tutorials/assembly) it to your robot.
15+
16+
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.
17+
18+
Your code will need to initialise a `Robot` object, which allows you to control the connected motor boards:
19+
20+
~~~~~ python
21+
from sr.robot3 import Robot
22+
23+
robot = Robot()
24+
~~~~~
25+
26+
In this tutorial, we'll assume your robot has 2 drive motors, one on each side.
27+
28+
## Driving forwards
29+
30+
For your robot to drive forwards, both motors need to turn the same direction.
31+
32+
~~~~~ python
33+
from sr.robot3 import Robot
34+
35+
robot = Robot()
36+
37+
robot.motor_board.motors[0].power = 0.5
38+
robot.motor_board.motors[1].power = 0.5
39+
40+
# Drive for 10 seconds.
41+
robot.sleep(10)
42+
~~~~~
43+
44+
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.
45+
46+
<div class="info">
47+
If your motors turn in opposite directions, it's often easier to swap the wires rather than adjusting your code.
48+
</div>
49+
50+
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.
51+
52+
### Inaccuracies
53+
54+
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.
55+
56+
To account for this, you'll need to change your code slightly. If one motor spins slower, give it a little more power:
57+
58+
~~~~~ python
59+
MOTOR_0_MULTIPLIER = 1
60+
MOTOR_1_MULTIPLIER = 0.9
61+
62+
robot.motor_board.motors[0].power = 0.5 * MOTOR_0_MULTIPLIER
63+
robot.motor_board.motors[1].power = 0.5 * MOTOR_1_MULTIPLIER
64+
~~~~~
65+
66+
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:
67+
68+
~~~~~ python
69+
def drive_straight(speed):
70+
robot.motor_board.motors[0].power = speed * MOTOR_0_MULTIPLIER
71+
robot.motor_board.motors[1].power = speed * MOTOR_1_MULTIPLIER
72+
73+
74+
drive_straight(0.5)
75+
robot.sleep(1)
76+
drive_straight(0)
77+
~~~~~
78+
79+
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.
80+
81+
### Driving backwards
82+
83+
Your robot can drive backwards by both motors turning in reverse. Motors can spin backwards by setting them to a negative power:
84+
85+
~~~~~ python
86+
robot.motor_board.motors[0].power = -0.5 * MOTOR_0_MULTIPLIER
87+
robot.motor_board.motors[1].power = -0.5 * MOTOR_1_MULTIPLIER
88+
~~~~~
89+
90+
The above code sets both motors to half speed backwards (`-0.5`), taking into account the multipliers.
91+
92+
## Turning
93+
94+
For your robot to turn, each motor needs to turn in a different direction.
95+
96+
If the left wheel drives forwards, and the right wheel drives backwards, your robot will turn clockwise about its centre.
97+
98+
~~~~~ python
99+
robot.motor_board.motors[0].power = 0.5 * MOTOR_0_MULTIPLIER
100+
robot.motor_board.motors[1].power = -0.5 * MOTOR_1_MULTIPLIER
101+
~~~~~
102+
103+
If the left wheel drives forwards, and the right wheel remains stationary, your robot will turn about the right wheel.
104+
105+
~~~~~ python
106+
robot.motor_board.motors[0].power = 0.5 * MOTOR_0_MULTIPLIER
107+
robot.motor_board.motors[1].power = 0
108+
~~~~~

0 commit comments

Comments
 (0)