Skip to content

Commit 5b4b72e

Browse files
committed
Update cheat sheet
1 parent 95a659f commit 5b4b72e

File tree

1 file changed

+80
-71
lines changed

1 file changed

+80
-71
lines changed

programming/cheat_sheet.md

Lines changed: 80 additions & 71 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

4040
You can also tell the robot to print extra logging information, although this is quite noisy.
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.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()
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,62 +181,59 @@ 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
184+
marker.pixel_centre # The co-ordinates of the centre of the marker
185+
marker.pixel_corners # A list of corners of the marker
176186

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
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[4].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

217221
You can read a digital value from the pins of the Ruggeduino:
218222

219223
~~~~~ python
220-
R.ruggeduino.pins[3].mode = INPUT
224+
robot.ruggeduino.pins[3].mode = INPUT
221225

222-
value = R.ruggeduino.pins[3].digital_read()
226+
value = robot.ruggeduino.pins[3].digital_read()
223227
~~~~~
224228

225229
### Analogue Read
226230

227231
You can read an analogue value from the analogue pins of the Ruggeduino:
228232

229233
~~~~~ 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()
231237
~~~~~
232238

233239
## Metadata
@@ -237,29 +243,32 @@ The API also makes some information about where your code is running
237243
### Starting Zone for a match
238244

239245
~~~~~ 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
247247
~~~~~
248248

249249
### Robot Mode
250250

251251
This is set to `COMP` when your robot is in a match.
252252

253253
~~~~~ python
254-
robot_mode = R.mode # -> DEV or COMP
254+
robot_mode = robot.mode # -> DEV or COMP
255255
~~~~~
256256

257-
### USB Key Path
257+
### USB stick Path
258258

259-
This is the path to where your USB key is mounted.
259+
This is the path to where your USB stick is mounted.
260260

261261
You can use this to save files and information to the drive.
262262

263263
~~~~~ 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
265274
~~~~~

0 commit comments

Comments
 (0)