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