You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: programming/servos.md
+62-44Lines changed: 62 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,74 +6,92 @@ title: Servos Board API
6
6
Servos Board API
7
7
================
8
8
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.
10
11
11
-
When a single Servo Board is connected to your robot, you can control it
12
-
using the `servo_board` object.
13
12
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
+
-------------------------
24
15
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.
26
17
27
18
~~~~~python
28
-
R.servo_boards["srABC1"].something...
19
+
from sr.robot3 import*
20
+
robot = Robot()
21
+
22
+
my_servo_board = robot.servo_board
29
23
~~~~~
30
24
31
-
<divclass="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>
36
25
37
26
Setting servo positions
38
27
-----------------------
39
28
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
+
<divclass="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>
43
33
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.
44
38
The position of servos can range from `-1` to `1` inclusive:
# 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
54
46
~~~~~
55
47
56
48
You can read the last value a servo was set to using similar code:
57
49
58
50
~~~~~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)
61
53
~~~~~
62
54
63
-
<divclass="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>
67
55
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 µs|2000 µs|
82
+
|Pulse width limit |500 µs |4000 µ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
# Then move the position of the servo to the upper position
91
+
# (pulse width = 2500us)
92
+
robot.servo_board.servos[7].position =1
93
+
~~~~~
70
94
71
95
<divclass="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.
74
97
</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