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/sr/motors/index.md
+44-47Lines changed: 44 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,72 +6,70 @@ title: Motors
6
6
Motors
7
7
======
8
8
9
-
The `motors` object is used to control a collection of Motor Boards.
10
-
Similar to `ruggeduinos` and `servos`, `motors` can be used like a list.
11
-
To do something with the **first Motor Board**, you would use:
9
+
The `motor_board` object is used to control a collection of Motor Boards.
10
+
11
+
When a single Motor Board is connected to your robot, you can control it
12
+
using the `motor_board` object.
12
13
13
14
~~~~~python
14
-
R.motors[0].something...
15
+
R.motor_board.something...
15
16
~~~~~
16
17
17
-
...because indexes are 0-based (counting starts from 0, not 1).
18
-
19
-
When you have more than one Motor Board connected to your kit they will be ordered based upon their SR Part Code, as written on the underside of the board.
20
-
21
-
The SR Part Code of each detected Motor Board is also printed to the log when your robot starts.
18
+
The serial number of each detected Motor Board is printed to the log when your robot starts.
22
19
It will look something like this:
23
20
24
21
~~~~~not-code
25
-
Found the following devices:
26
-
- Motors:
27
-
0: Motor( serialnum = "SR0XJ1F" )
28
-
1: Motor( serialnum = "SR0A123" )
22
+
sr.robot3.robot INFO - Found Student Robotics Motor Board v4 - srABC1
29
23
~~~~~
30
24
31
-
32
-
However, like `ruggeduinos` and `servos`, `motors` is actually a dictionary.
33
-
As a result, in `motors` you can also use the SR Part Code of the Motor Board as a key.
34
-
For example, if you had a board that was labelled "SR0A123",
25
+
If you have more than one Motor Board attached, the `motor_boards` object can be used to control a collection of Motor Board. It is a dictionary access via serial number, which is usually written on the board. For example, if you had a board whose serial number was "srABC1",
35
26
you could do this instead:
36
27
37
28
~~~~~python
38
-
R.motors["SR0A123"].something...
29
+
R.motor_boards["srABC1"].something...
39
30
~~~~~
40
31
32
+
<divclass="warning">
33
+
When you have more than one Motor board connected to your kit,
34
+
you must use `R.motor_boards` and index by serial number. This is so
35
+
that the kit knows which Motor Board you want to control.
36
+
</div>
37
+
38
+
41
39
Setting motor power
42
40
-------------------
43
41
44
42
Motor power is controlled using [PWM](https://en.wikipedia.org/wiki/Pulse-width_modulation) with 100% power being a [duty cycle](https://en.wikipedia.org/wiki/Duty_cycle) of 1. You set the power with an integer value between -100 and 100 inclusive (where a negative value puts the motor in reverse). The field to change the output power is `power`. As each Motor Board has two outputs you will need to specify which output you want to control:
45
43
46
44
~~~~~python
47
-
from sr.robotimport*
45
+
from sr.robot3import*
48
46
import time
49
47
50
48
R = Robot()
51
49
52
-
# motor board 0, channel 0 to full power forward
53
-
R.motors[0].m0.power =100
50
+
# motor board srABC1, channel 0 to full power forward
51
+
R.motor_boards["srABC1"].motors[0].power =1
54
52
55
-
# motor board 1, channel 0 to full power reverse
56
-
R.motors[1].m0.power =-100
53
+
# motor board srXYZ1, channel 0 to full power reverse
54
+
R.motor_boards["srXYZ1"].motors[0].power =-1
57
55
58
-
# motor board 0, channel 1 to half power forward
59
-
R.motors[0].m1.power =50
56
+
# motor board srABC1, channel 1 to half power forward
57
+
R.motor_boards["srABC1"].motors[1].power =0.5
60
58
61
-
# motor board 1, channel 0 stopped
62
-
R.motors[1].m0.power =0
59
+
# motor board srXYZ1, channel 0 stopped
60
+
R.motor_boards["srXYZ1"].motors[0].power =0
63
61
64
-
# the following will put motor board 0, channel 1 at 25% power (forwards) for 2.5 seconds:
65
-
R.motors[0].m1.power =25
62
+
# the following will put motor board srABC1, channel 1 at 25% power (forwards) for 2.5 seconds:
63
+
R.motor_boards["srABC1"].motors[1].power =0.25
66
64
time.sleep(2.5) # wait for 2.5 seconds
67
-
R.motors[0].m1.power =0
65
+
R.motor_boards["srABC1"].motors[1].power =0
68
66
~~~~~
69
67
70
68
You can read the current power value for a motor using the same field:
71
69
72
70
~~~~~python
73
-
# get the current output power of Motor Board 0, channel 0
74
-
currentTarget = R.motors[0].m0.power
71
+
# get the current output power of Motor Board srABC1, channel 0
0 commit comments