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
+47-48Lines changed: 47 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,72 +6,72 @@ 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, 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 detected as "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
-
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:
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 -1 and 1 inclusive. Fractional values (such as 0.42) can be used to specify less than 100% power. Negative values run the motor in the opposite direction.
43
+
44
+
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
45
46
46
~~~~~python
47
-
from sr.robotimport*
47
+
from sr.robot3import*
48
48
import time
49
49
50
50
R = Robot()
51
51
52
-
# motor board 0, channel 0 to full power forward
53
-
R.motors[0].m0.power =100
52
+
# motor board srABC1, channel 0 to full power forward
53
+
R.motor_boards["srABC1"].motors[0].power =1
54
54
55
-
# motor board 1, channel 0 to full power reverse
56
-
R.motors[1].m0.power =-100
55
+
# motor board srXYZ1, channel 0 to full power reverse
56
+
R.motor_boards["srXYZ1"].motors[0].power =-1
57
57
58
-
# motor board 0, channel 1 to half power forward
59
-
R.motors[0].m1.power =50
58
+
# motor board srABC1, channel 1 to half power forward
59
+
R.motor_boards["srABC1"].motors[1].power =0.5
60
60
61
-
# motor board 1, channel 0 stopped
62
-
R.motors[1].m0.power =0
61
+
# motor board srXYZ1, channel 0 stopped
62
+
R.motor_boards["srXYZ1"].motors[0].power =0
63
63
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
64
+
# the following will put motor board srABC1, channel 1 at 25% power (forwards) for 2.5 seconds:
65
+
R.motor_boards["srABC1"].motors[1].power =0.25
66
66
time.sleep(2.5) # wait for 2.5 seconds
67
-
R.motors[0].m1.power =0
67
+
R.motor_boards["srABC1"].motors[1].power =0
68
68
~~~~~
69
69
70
70
You can read the current power value for a motor using the same field:
71
71
72
72
~~~~~python
73
-
# get the current output power of Motor Board 0, channel 0
74
-
currentTarget = R.motors[0].m0.power
73
+
# get the current output power of Motor Board srABC1, channel 0
0 commit comments