Skip to content

Commit d02d1ff

Browse files
authored
SR2024 - Update Section - Servo board API
2 parents 28399ee + 4843684 commit d02d1ff

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed

programming/servos.md

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,92 @@ title: Servos Board API
66
Servos Board API
77
================
88

9-
The `servo_board` object is used to control a collection of Servo Boards.
9+
The servo board provided in the kit can control up to 12 servos.
10+
See the [Servo Board]({{ site.baseurl }}/kit/servo_board) hardware page for more details about this board.
1011

11-
When a single Servo Board is connected to your robot, you can control it
12-
using the `servo_board` object.
1312

14-
~~~~~ python
15-
R.servo_board.something...
16-
~~~~~
17-
18-
The serial number of each detected Servo Board is printed to the log when your robot starts.
19-
It will look something like this:
20-
21-
~~~~~ not-code
22-
sr.robot3.robot INFO - Found Student Robotics Servo Board v4 - srABC1
23-
~~~~~
13+
Accessing the Servo Board
14+
-------------------------
2415

25-
If you have more than one Servo Board attached, you need to specify which one you want to control. This is done using the serial number of the board. For example: if you had a board that was labelled "srABC1",
16+
The servo board can be accessed using the `servo_board` property of the `Robot` object.
2617

2718
~~~~~ python
28-
R.servo_boards["srABC1"].something...
19+
from sr.robot3 import *
20+
robot = Robot()
21+
22+
my_servo_board = robot.servo_board
2923
~~~~~
3024

31-
<div class="warning" markdown="1">
32-
When you have more than one servo board connected to your kit,
33-
you must use `R.servo_boards` and index by serial number. This is so
34-
that the kit knows which servo board you want to control.
35-
</div>
3625

3726
Setting servo positions
3827
-----------------------
3928

40-
Each of the twelve servo outputs can be controlled separately. The servo outputs
41-
are numbered 0-11, see the [Servo Board](/docs/kit/servo_board#connectors) docs
42-
for details of which output is which.
29+
<div class="warning" markdown="1">
30+
To use servo outputs 8-11 power must be provided through the auxillary power input at an appropriate voltage for the connected servos.
31+
See the [Servo Board]({{ site.baseurl }}/kit/servo_board#auxiliary-outputs) page for more info.
32+
</div>
4333

34+
Each of the twelve servo outputs can be controlled separately.
35+
The servo outputs are numbered 0-11, see the [Servo Board]({{ site.baseurl }}/kit/servo_board#connectors) docs for details of which output is which.
36+
37+
This board object has an array called `servos` containing the servos connected to it.
4438
The position of servos can range from `-1` to `1` inclusive:
4539

4640
~~~~~ python
47-
# R.servo_board.servos[SERVO_NUMBER].position = POS
48-
49-
# set servo 1's position to 0.2
50-
R.servo_board.servos[1].position = 0.2
41+
# Set servo 0 position to 0.2
42+
robot.servo_board.servos[0].position = 0.2
5143

52-
# Set servo 2's position (on the Servo Board with serial number srABC) to -0.55
53-
R.servo_boards["srABC"].servos[2].position = -0.55
44+
# Set servo 2 position to -0.55
45+
robot.servo_board.servos[2].position = -0.55
5446
~~~~~
5547

5648
You can read the last value a servo was set to using similar code:
5749

5850
~~~~~ python
59-
# get the last setting of the second servo on the first Servo Board
60-
last_setting = R.servo_board.servos[1].position
51+
# Print the last setting of servo number 1
52+
print(robot.servo_board.servos[1].position)
6153
~~~~~
6254

63-
<div class="info" markdown="1">
64-
While it is possible to retrieve the last position a servo was set to,
65-
this does not guarantee that the servo is currently in that position.
66-
</div>
6755

68-
[How the set position relates to the servo angle](#ServoAngle) {#ServoAngle}
69-
-----------------------------------------------
56+
Disabling servo outputs
57+
-----------------------
58+
59+
Setting a position to `None` will disable an output.
60+
This is the state all the servo outputs are in when the board turns on, where no servo pulses are being sent to the outputs.
61+
62+
~~~~~ python
63+
# disable servo output 5
64+
robot.servo_board.servos[5].position = None
65+
~~~~~
66+
67+
68+
Extended servo range
69+
--------------------
70+
71+
For an RC servo the angle of rotation is determined by the width of an electrical pulse on the control wire.
72+
A typical servo expects to see a pulse every 20ms, with a pulse width between 1ms and 2ms.
73+
Our API will take the position provided (between -1 and 1) and map it to the correct pulse width.
74+
75+
However there is no standard for the width of this pulse and there are differences between manufacturers as to what angle the servo will turn to for a given pulse width.
76+
The API can be used to change what the limits of pulse width are for each servo.
77+
You should experiment and find what the actual limit of your servos are but be careful not drive them past that.
78+
79+
| Parameter | Min value | Max value |
80+
|-------------------|-------------|-------------|
81+
|Pulse width default|1000 &micro;s|2000 &micro;s|
82+
|Pulse width limit |500 &micro;s |4000 &micro;s|
83+
84+
This code to set the pulse width limits should be done before setting the position of the servo.
85+
86+
~~~~~ python
87+
# set the range of servo output 7 between 500us and 2500us
88+
robot.servo_board.servos[7].set_duty_limits(500, 2500)
89+
90+
# Then move the position of the servo to the upper position
91+
# (pulse width = 2500us)
92+
robot.servo_board.servos[7].position = 1
93+
~~~~~
7094

7195
<div class="warning">
72-
You should be careful about forcing a servo to drive past its end stops.
73-
Some servos are very strong and it could damage the internal gears.
96+
You should be careful about forcing a servo to drive past its end stops as this could damage the internal gears.
7497
</div>
75-
76-
The angle of an RC servo is controlled by the width of a pulse supplied to it periodically.
77-
There is no standard for the width of this pulse and there are differences between manufacturers as to what angle the servo will turn to for a given pulse width.
78-
To be able to handle the widest range of all servos our hardware outputs a very wide range of pulse widths which in some cases will force the servo to try and turn past its internal end-stops.
79-
You should experiment and find what the actual limit of your servos are (it almost certainly won't be -1 and 1) and not drive them past that.

0 commit comments

Comments
 (0)