Skip to content

Commit 0e461b3

Browse files
authored
SR2024 - Update Section - API Quick Reference
2 parents bf1e30a + b0b73fa commit 0e461b3

File tree

1 file changed

+86
-75
lines changed

1 file changed

+86
-75
lines changed

programming/cheat_sheet.md

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,75 +22,74 @@ from sr.robot3 import *
2222
### Standard Initialisation
2323

2424
~~~~~ python
25-
R = Robot()
25+
robot = Robot()
2626
~~~~~
2727

2828
### Initialisation without waiting for the start button
2929

3030
~~~~~ python
31-
R = Robot(auto_start=True)
31+
robot = Robot(wait_for_start=False)
3232

3333
# Code here runs before the start button is pressed
3434

35-
R.wait_start()
35+
robot.wait_start() # wait for the start button
3636
~~~~~
3737

3838
### Initialisation with extra logging
3939

40-
You can also tell the robot to print extra logging information, although this is quite noisy.
40+
You can also tell the robot to print extra logging information, although this will create a lot of logs.
4141

4242
~~~~~ python
43-
R = Robot(verbose=True)
43+
robot = Robot(debug=True)
4444
~~~~~
4545

4646
## Selecting which board to control
4747

4848
If you only have one board of a given type plugged into your robot, then you can use its singular name:
4949

5050
~~~~~ 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
5555
~~~~~
5656

5757
If you have multiple boards of a given type plugged into your robot, you must index them by serial number:
5858

5959
~~~~~ 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"]
6362
~~~~~
6463

6564
## Power Board
6665

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.
6867

6968
### Turn on and off the power outputs
7069

7170
~~~~~ python
7271
# Turn all of the outputs on
73-
R.power_board.outputs.power_on()
72+
robot.power_board.outputs.power_on()
7473

7574
# Turn all of the outputs off
76-
R.power_board.outputs.power_off()
75+
robot.power_board.outputs.power_off()
7776

7877
# 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
8079

8180
# 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
8382
~~~~~
8483

8584
### Reading voltage and current
8685

8786
~~~~~ python
8887
# 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
9089

9190
# 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
9493
~~~~~
9594

9695
### Buzzer
@@ -99,10 +98,13 @@ The power board has an on-board piezoelectric buzzer.
9998

10099
~~~~~ python
101100
# 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)
103102

104103
# 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)
106108
~~~~~
107109

108110
## Motors
@@ -114,34 +116,33 @@ You can set the power of each motor on the board between -1 and 1.
114116
If you change the power of your motor too rapidly, the overcurrent protection may be triggered.
115117

116118
~~~~~ 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
119121
~~~~~
120122

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`.
122126

123127
~~~~~ python
124128
# 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
127131
~~~~~
128132

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.
132134

133135
~~~~~ 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
136137
~~~~~
137138

138139
## Servos
139140

140141
You can set the position of each servo output on the board between -1 and 1.
141142

142143
~~~~~ 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
145146
~~~~~
146147

147148
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.
153154
It can sometimes be useful to save a photo of what markers the robot can see:
154155

155156
~~~~~ python
156-
R.camera.save("my-photo.png") # Save my-photo.png to the USB drive
157+
robot.camera.save("my-photo.jpg") # Save my-photo.jpg 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()
157166
~~~~~
158167

159168
### Looking for markers
160169

161170
You can take a photo with the camera and search for markers:
162171

163172
~~~~~ python
164-
markers = R.camera.see()
173+
markers = robot.camera.see()
165174
~~~~~
166175

167176
There are various bits of information available about visible markers:
@@ -172,94 +181,96 @@ for marker in markers:
172181
marker.id # The ID of the marker
173182
marker.size # Physical size of the marker in mm.
174183

175-
marker.distance # Distance away from the camera in mm
176-
177-
# Cartesian coords of the marker
178-
marker.cartesian.x
179-
marker.cartesian.y
180-
marker.cartesian.z
184+
marker.pixel_centre # The co-ordinates of the centre of the marker
185+
marker.pixel_corners # A list of corners of the marker
181186

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
186191

187192
# 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
192196
~~~~~
193197

194-
## Ruggeduino
198+
## Arduino
195199

196200
### Setting the mode of a pin
197201

198202
~~~~~ 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
202206
~~~~~
203207

204208
### Digital Write
205209

206-
You can set the output for a pin of the Ruggeduino:
210+
You can set the output for a pin of the Arduino:
207211

208212
~~~~~ python
209-
R.ruggeduino.pins[4].mode = OUTPUT
213+
robot.ruggeduino.pins[2].mode = OUTPUT
210214

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)
213217
~~~~~
214218

215219
### Digital Read
216220

217-
You can read a digital value from the pins of the Ruggeduino:
221+
You can read a digital value from the pins of the Arduino:
218222

219223
~~~~~ python
220-
R.ruggeduino.pins[3].mode = INPUT
224+
robot.ruggeduino.pins[3].mode = INPUT
225+
robot.ruggeduino.pins[5].mode = INPUT_PULLUP
221226

222-
value = R.ruggeduino.pins[3].digital_read()
227+
value = robot.ruggeduino.pins[3].digital_read()
228+
value = robot.ruggeduino.pins[5].digital_read()
223229
~~~~~
224230

225231
### Analogue Read
226232

227-
You can read an analogue value from the analogue pins of the Ruggeduino:
233+
You can read an analogue value from the analogue pins of the Arduino:
228234

229235
~~~~~ python
230-
value = R.ruggeduino.pins[A0].analogue_read()
236+
robot.ruggeduino.pins[A0].mode = INPUT
237+
238+
value = robot.ruggeduino.pins[A0].analogue_read()
231239
~~~~~
232240

233241
## Metadata
234242

235243
The API also makes some information about where your code is running
236244

237-
### Starting Zone for a match
245+
### Starting zone for a match
238246

239247
~~~~~ python
240-
zone = R.zone # -> 0, 1, 2, or 3
248+
zone = robot.zone # -> 0, 1, 2, or 3
241249
~~~~~
242250

243-
### Arena Information
251+
### Robot mode
252+
253+
This is set to `COMP` when your robot is in a match.
244254

245255
~~~~~ python
246-
arena = R.arena # -> 'A'
256+
robot_mode = robot.mode # -> DEV or COMP
247257
~~~~~
248258

249-
### Robot Mode
259+
### USB stick path
250260

251-
This is set to `COMP` when your robot is in a match.
261+
This is the path to where your USB stick is mounted.
262+
263+
You can use this to save files and information to the drive.
252264

253265
~~~~~ python
254-
robot_mode = R.mode # -> DEV or COMP
266+
usb_key_path = robot.usbkey
255267
~~~~~
256268

257-
### USB Key Path
269+
### Is simulated
258270

259-
This is the path to where your USB key is mounted.
260-
261-
You can use this to save files and information to the drive.
271+
A boolean value indicating whether or not the code is running in the simulator.
272+
This value is True when in the simulator and False when on the robot.
262273

263274
~~~~~ python
264-
usb_key_path = R.usbkey # -> pathlib.Path
275+
value = robot.is_simulated
265276
~~~~~

0 commit comments

Comments
 (0)