@@ -22,75 +22,74 @@ from sr.robot3 import *
22
22
### Standard Initialisation
23
23
24
24
~~~~~ python
25
- R = Robot()
25
+ robot = Robot()
26
26
~~~~~
27
27
28
28
### Initialisation without waiting for the start button
29
29
30
30
~~~~~ python
31
- R = Robot(auto_start = True )
31
+ robot = Robot(wait_for_start = False )
32
32
33
33
# Code here runs before the start button is pressed
34
34
35
- R .wait_start()
35
+ robot .wait_start() # wait for the start button
36
36
~~~~~
37
37
38
38
### Initialisation with extra logging
39
39
40
40
You can also tell the robot to print extra logging information, although this is quite noisy.
41
41
42
42
~~~~~ python
43
- R = Robot(verbose = True )
43
+ robot = Robot(debug = True )
44
44
~~~~~
45
45
46
46
## Selecting which board to control
47
47
48
48
If you only have one board of a given type plugged into your robot, then you can use its singular name:
49
49
50
50
~~~~~ python
51
- R .power_board.something
52
- R .motor_board.something
53
- R .servo_board.something
54
- R.ruggeduino.something
51
+ robot .power_board
52
+ robot .motor_board
53
+ robot .servo_board
54
+ robot.arduino
55
55
~~~~~
56
56
57
57
If you have multiple boards of a given type plugged into your robot, you must index them by serial number:
58
58
59
59
~~~~~ python
60
- R.motor_boards[" srABC1" ].something
61
- R.servo_boards[" srXYZ2" ].something
62
- R.ruggeduinos[" 1234567890" ].something
60
+ robot.motor_boards[" srABC1" ]
61
+ robot.arduinos[" 1234567890" ]
63
62
~~~~~
64
63
65
64
## Power Board
66
65
67
- The outputs on the power board will turn on when you initialise your robot.
66
+ The outputs on the power board will turn on when you initialise your robot and turn off when your code ends .
68
67
69
68
### Turn on and off the power outputs
70
69
71
70
~~~~~ python
72
71
# Turn all of the outputs on
73
- R .power_board.outputs.power_on()
72
+ robot .power_board.outputs.power_on()
74
73
75
74
# Turn all of the outputs off
76
- R .power_board.outputs.power_off()
75
+ robot .power_board.outputs.power_off()
77
76
78
77
# Turn a single output on
79
- R .power_board.outputs[OUT_H0 ].is_enabled = True
78
+ robot .power_board.outputs[OUT_H0 ].is_enabled = True
80
79
81
80
# Turn a single output off
82
- R .power_board.outputs[OUT_H0 ].is_enabled = False
81
+ robot .power_board.outputs[OUT_H0 ].is_enabled = False
83
82
~~~~~
84
83
85
84
### Reading voltage and current
86
85
87
86
~~~~~ python
88
87
# Read the current of an individual output
89
- current = R .power_board.outputs[OUT_H0 ].current
88
+ current = robot .power_board.outputs[OUT_H0 ].current
90
89
91
90
# Read the current and voltage from the LiPo battery
92
- voltage = R .power_board.battery_sensor.voltage
93
- current = R .power_board.battery_sensor.current
91
+ voltage = robot .power_board.battery_sensor.voltage
92
+ current = robot .power_board.battery_sensor.current
94
93
~~~~~
95
94
96
95
### Buzzer
@@ -99,10 +98,13 @@ The power board has an on-board piezoelectric buzzer.
99
98
100
99
~~~~~ python
101
100
# Play a standard note C6 -> C8 included for 0.5s
102
- R .power_board.piezo.buzz(0.5 , Note.C6 )
101
+ robot .power_board.piezo.buzz(Note.C6, 0.5 )
103
102
104
103
# Play a tone at 1047Hz for 1 second
105
- R.power_board.piezo.buzz(1 , 1047 )
104
+ robot.power_board.piezo.buzz(1047 , 1 )
105
+
106
+ # Play a tone at 500Hz tone for 2 seconds and wait for it to finish
107
+ robot.power_board.piezo.buzz(500 , 2 , blocking = True )
106
108
~~~~~
107
109
108
110
## Motors
@@ -114,34 +116,33 @@ You can set the power of each motor on the board between -1 and 1.
114
116
If you change the power of your motor too rapidly, the overcurrent protection may be triggered.
115
117
116
118
~~~~~ python
117
- R .motor_board.motors[0 ].power = 1
118
- R .motor_board.motors[1 ].power = - 1
119
+ robot .motor_board.motors[0 ].power = 1
120
+ robot .motor_board.motors[1 ].power = - 1
119
121
~~~~~
120
122
121
- Setting a motor to ` COAST ` is equivalent to power level ` 0 ` .
123
+ ### Special motor values
124
+
125
+ Setting a motor to ` BRAKE ` is equivalent to power level ` 0 ` .
122
126
123
127
~~~~~ python
124
128
# This is the same operation
125
- R .motor_board.motors[0 ].power = COAST
126
- R .motor_board.motors[0 ].power = 0
129
+ robot .motor_board.motors[0 ].power = BRAKE
130
+ robot .motor_board.motors[0 ].power = 0
127
131
~~~~~
128
132
129
- ### Braking Motors
130
-
131
- You can also brake a motor, which will quickly slow the motor.
133
+ ` COAST ` will stop applying power to the motors. This will mean they continue moving under the momentum they had before and slowly come to a stop.
132
134
133
135
~~~~~ python
134
- R.motor_board.motors[0 ].power = BRAKE
135
- R.motor_board.motors[1 ].power = - 1
136
+ robot.motor_board.motors[0 ].power = COAST
136
137
~~~~~
137
138
138
139
## Servos
139
140
140
141
You can set the position of each servo output on the board between -1 and 1.
141
142
142
143
~~~~~ python
143
- R .servo_board.servos[0 ].position = - 1
144
- R .servo_board.servos[1 ].position = 1
144
+ robot .servo_board.servos[0 ].position = - 1
145
+ robot .servo_board.servos[1 ].position = 1
145
146
~~~~~
146
147
147
148
You can also set the position to ` 0 ` , which is the approximate centre.
@@ -153,15 +154,23 @@ You can also set the position to `0`, which is the approximate centre.
153
154
It can sometimes be useful to save a photo of what markers the robot can see:
154
155
155
156
~~~~~ python
156
- R.camera.save(" my-photo.png" ) # Save my-photo.png to the USB drive
157
+ robot.camera.save(" my-photo.png" ) # Save my-photo.png to the USB drive
158
+ ~~~~~
159
+
160
+ ### Capturing an openCV array
161
+
162
+ Take a photo using the webcam, and return the image data as an OpenCV array:
163
+
164
+ ~~~~~ python
165
+ frame = robot.camera.capture()
157
166
~~~~~
158
167
159
168
### Looking for markers
160
169
161
170
You can take a photo with the camera and search for markers:
162
171
163
172
~~~~~ python
164
- markers = R .camera.see()
173
+ markers = robot .camera.see()
165
174
~~~~~
166
175
167
176
There are various bits of information available about visible markers:
@@ -172,62 +181,59 @@ for marker in markers:
172
181
marker.id # The ID of the marker
173
182
marker.size # Physical size of the marker in mm.
174
183
175
- marker.distance # Distance away from the camera in mm
184
+ marker.pixel_centre # The co-ordinates of the centre of the marker
185
+ marker.pixel_corners # A list of corners of the marker
176
186
177
- # Cartesian coords of the marker
178
- marker.cartesian.x
179
- marker.cartesian.y
180
- marker.cartesian.z
181
-
182
- # Spherical coords of the marker
183
- marker.spherical.rot_x
184
- marker.spherical.rot_y
185
- marker.spherical.dist
187
+ # Position of the marker
188
+ marker.position.distance # Distance away from the camera in mm
189
+ marker.position.horizontal_angle # angle to the marker in radians
190
+ marker.position.vertical_angle # angle to the marker in radians
186
191
187
192
# Orientation of the marker
188
- marker.orientation.rot_x
189
- marker.orientation.rot_y
190
- marker.orientation.rot_z
191
- marker.orientation.rotation_matrix
193
+ marker.orientation.yaw
194
+ marker.orientation.pitch
195
+ marker.orientation.roll
192
196
~~~~~
193
197
194
- ## Ruggeduino
198
+ ## Arduino
195
199
196
200
### Setting the mode of a pin
197
201
198
202
~~~~~ python
199
- R .ruggeduino.pins[4 ].mode = OUTPUT
200
- R .ruggeduino.pins[4 ].mode = INPUT
201
- R .ruggeduino.pins[4 ].mode = INPUT_PULLUP
203
+ robot .ruggeduino.pins[4 ].mode = OUTPUT
204
+ robot .ruggeduino.pins[4 ].mode = INPUT
205
+ robot .ruggeduino.pins[4 ].mode = INPUT_PULLUP
202
206
~~~~~
203
207
204
208
### Digital Write
205
209
206
- You can set the output for a pin of the Ruggeduino :
210
+ You can set the output for a pin of the Arduino :
207
211
208
212
~~~~~ python
209
- R .ruggeduino.pins[4 ].mode = OUTPUT
213
+ robot .ruggeduino.pins[4 ].mode = OUTPUT
210
214
211
- R .ruggeduino.pins[2 ].digital_write(True )
212
- R .ruggeduino.pins[2 ].digital_write(False )
215
+ robot .ruggeduino.pins[2 ].digital_write(True )
216
+ robot .ruggeduino.pins[2 ].digital_write(False )
213
217
~~~~~
214
218
215
219
### Digital Read
216
220
217
221
You can read a digital value from the pins of the Ruggeduino:
218
222
219
223
~~~~~ python
220
- R .ruggeduino.pins[3 ].mode = INPUT
224
+ robot .ruggeduino.pins[3 ].mode = INPUT
221
225
222
- value = R .ruggeduino.pins[3 ].digital_read()
226
+ value = robot .ruggeduino.pins[3 ].digital_read()
223
227
~~~~~
224
228
225
229
### Analogue Read
226
230
227
231
You can read an analogue value from the analogue pins of the Ruggeduino:
228
232
229
233
~~~~~ python
230
- value = R.ruggeduino.pins[A0].analogue_read()
234
+ robot.ruggeduino.pins[A0].mode = INPUT
235
+
236
+ value = robot.ruggeduino.pins[A0].analogue_read()
231
237
~~~~~
232
238
233
239
## Metadata
@@ -237,29 +243,32 @@ The API also makes some information about where your code is running
237
243
### Starting Zone for a match
238
244
239
245
~~~~~ python
240
- zone = R.zone # -> 0, 1, 2, or 3
241
- ~~~~~
242
-
243
- ### Arena Information
244
-
245
- ~~~~~ python
246
- arena = R.arena # -> 'A'
246
+ zone = robot.zone # -> 0, 1, 2, or 3
247
247
~~~~~
248
248
249
249
### Robot Mode
250
250
251
251
This is set to ` COMP ` when your robot is in a match.
252
252
253
253
~~~~~ python
254
- robot_mode = R .mode # -> DEV or COMP
254
+ robot_mode = robot .mode # -> DEV or COMP
255
255
~~~~~
256
256
257
- ### USB Key Path
257
+ ### USB stick Path
258
258
259
- This is the path to where your USB key is mounted.
259
+ This is the path to where your USB stick is mounted.
260
260
261
261
You can use this to save files and information to the drive.
262
262
263
263
~~~~~ python
264
- usb_key_path = R.usbkey # -> pathlib.Path
264
+ usb_key_path = robot.usbkey # -> pathlib.Path
265
+ ~~~~~
266
+
267
+ ### Is simulated
268
+
269
+ A boolean value indicating whether or not the code is running in the simulator.
270
+ This value is True when in the simulator and False when on the robot.
271
+
272
+ ~~~~~ python
273
+ value = robot.is_simulated
265
274
~~~~~
0 commit comments