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
Each of the power board's controllable outputs has a constant whose name closely matches the name of the output:
31
+
Each of the power board's controllable outputs has a constant whose name closely matches the name of the output, which can be imported from `sr.robot3`:
32
32
33
33
* H0 : `OUT_H0`
34
34
* H1 : `OUT_H1`
@@ -38,6 +38,8 @@ Each of the power board's controllable outputs has a constant whose name closely
38
38
* L3 : `OUT_L3`
39
39
* 5V : `OUT_FIVE_VOLT`
40
40
41
+
These are also available on the `PowerOutputPosition` enum.
42
+
41
43
Both of the 5V outputs are controlled together.
42
44
43
45
All the ports are turned on when your code starts running, you can then control whether each output is turned on or off like so:
@@ -108,6 +110,8 @@ The frequency on the buzzer is limited from 8Hz to 10,000Hz
Copy file name to clipboardExpand all lines: programming/robot_api/index.md
+44-6Lines changed: 44 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,13 +23,21 @@ See the [Motor Board]({{ site.baseurl }}/programming/motors) page for more detai
23
23
To gain access to all of this functionality, all you need to do at the top of your code is the following:
24
24
25
25
~~~~~python
26
-
from sr.robot3 import*
26
+
from sr.robot3 importRobot
27
27
robot = Robot()
28
28
~~~~~
29
29
30
30
This imports the Student Robotics module that we've written to interface with our hardware.
31
31
We then use the `Robot` class from within the `sr.robot3` module, to create a `robot` object that sets up and gives you access to your robot's hardware.
32
32
33
+
Alongside `Robot`, other values are importable from `sr.robot3` which may be useful, such as `OUT_H0` or `A3`. It's best practice to only import the values you need, rather than `import *`. Most of these are available directly, or can be retrieved from the enums they're defined on (`PowerOutputPosition` and `AnalogPins` in these cases). If you need multiple values, you can import them all on one line:
34
+
35
+
~~~~python
36
+
from sr.robot3 import Robot, OUT_H0, AnalogPins
37
+
~~~~
38
+
39
+
If you don't need a value, it's best not to import it, to avoid accidentally overriding it.
40
+
33
41
<divclass="info"markdown="1">
34
42
Most examples in the documentation will assume you have created a `robot` object from the `Robot` class.
35
43
If you see `robot` in a code example, it is assumed you have run the two lines above.
@@ -46,7 +54,6 @@ Then, within your `robot` you can use the following attributes to access to the
46
54
47
55
The functions of each board are described on their respective pages.
48
56
49
-
50
57
## Other Robot Attributes
51
58
52
59
As well as the attributes listed above, the `Robot` class also has the following attributes, which you may find useful:
@@ -56,6 +63,8 @@ zone
56
63
Between `0` and `3`.
57
64
58
65
This attribute is only available after the start button is pressed and will throw an error if accessed before.
66
+
The zone you are in defines which arena markers are near your scoring zone.
67
+
You can use the knowledge of your zone to compensate for this, so your robot behaves correctly in all starting positions.
59
68
See the [competition mode](./comp_mode) page for more information about this attribute.
60
69
61
70
mode
@@ -66,7 +75,7 @@ mode
66
75
See the [competition mode](./comp_mode) page for more information about this attribute.
67
76
68
77
~~~~~ python
69
-
from sr.robot3 import *
78
+
from sr.robot3 import Robot, COMP, DEV
70
79
71
80
robot = Robot()
72
81
@@ -76,14 +85,16 @@ mode
76
85
print("This is development")
77
86
~~~~~
78
87
88
+
`COMP` and `DEV` are aliases for `RobotMode.COMP` and `RobotMode.DEV`, which can also be imported from `sr.robot3`.
89
+
79
90
usbkey
80
91
: A [`Path`](https://docs.python.org/3/library/pathlib.html#basic-use) object containing the path to the USB stick.
81
92
You can use this to easily read and write files on the USB stick itself.
82
93
83
94
An example of how the `usbkey` attribute might be used to read a file called `my-file.txt` which is stored on the USB stick:
84
95
85
96
~~~~~ python
86
-
from sr.robot3 import *
97
+
from sr.robot3 import Robot
87
98
import os
88
99
89
100
robot = Robot()
@@ -103,15 +114,15 @@ is_simulated
103
114
Normally the Robot object is initialised with the following:
104
115
105
116
~~~~~python
106
-
from sr.robot3 import*
117
+
from sr.robot3 importRobot
107
118
robot = Robot()
108
119
~~~~~
109
120
110
121
By default your robot will pause on this line waiting for the start button to be pressed.
111
122
However if you want to initialise some hardware or software before the start button is pressed then Robot initialisation can be broken up as follows.
112
123
113
124
~~~~~python
114
-
from sr.robot3 import*
125
+
from sr.robot3 importRobot
115
126
robot = Robot(wait_for_start=False)
116
127
117
128
# Initialisation phase.
@@ -121,3 +132,30 @@ robot.wait_start()
121
132
~~~~~
122
133
123
134
This will not pause on the line which creates the `robot` but will instead pause on the `robot.wait_start()` line, until the start button is pressed.
135
+
136
+
## Enums
137
+
138
+
Many values from the robot API are "enums". Enums are sets of values with names.
139
+
140
+
Enums compare equal to each other:
141
+
142
+
~~~~~python
143
+
from sr.robot3 import Colour
144
+
145
+
pump_output = PowerOutputPosition.H0
146
+
147
+
pump_output == PowerOutputPosition.H0 # True
148
+
pump_output == PowerOutputPosition.H1 # False
149
+
~~~~~
150
+
151
+
Enums are special values. They may look like strings or numbers, but they're not. An enum is a name for a special value. For example, the value for `PowerOutputPosition.H0` is `0`, whilst `RobotMode.COMP` is `"COMP"`. The inner value can be retrieved using `.value`.
152
+
153
+
154
+
~~~~~python
155
+
from sr.robot3 import RobotMode
156
+
157
+
RobotMode.COMP=="COMP"# False
158
+
RobotMode.COMP.value =="COMP"# True
159
+
~~~~~
160
+
161
+
In general, the enum should be used and compared directly, rather than using its inner value.
0 commit comments