Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid wildcard imports and mention enums #625

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .spelling
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DTSE9H
Ebuyer
esque
finessing
GPIO
iMAX
IntelliJ
Nikitin
Expand Down
4 changes: 2 additions & 2 deletions programming/arduino/custom_firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The Arduino serial number is a string of numbers and letters, and is output in t
You'll need the serial number later, so it's best to save it into a variable:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot

# Replace this with the actual serial number of your board
ARDUINO_SN = "752303138333517171B1"
Expand All @@ -40,7 +40,7 @@ If you need to communicate with a device, you will need to open its serial port.
If you want the `robot` object to do this and provide a serial port for your use, you will need to do the following.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot

# Replace this with the actual serial number of your board
ARDUINO_SN = "752303138333517171B1"
Expand Down
12 changes: 9 additions & 3 deletions programming/arduino/sr_firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The kit can control multiple Arduinos at once, however we only provide one in th
If there is exactly one Arduino connected to your robot, it can be accessed using the `arduino` property of the `Robot` object.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

my_arduino = robot.arduino
Expand Down Expand Up @@ -57,10 +57,13 @@ The possible modes for a pin are:
`INPUT_PULLUP`
: set the pin to input mode with a [pull-up resistor](#pull-up-resistors)

These are available from `sr.robot3`, or the `GPIOPinMode` enum.
RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved

An example of how to use this is below:

~~~~~ python
from sr.robot3 import INPUT, INPUT_PULLUP, OUTPUT

# set Arduino pin 2 to output
robot.arduino.pins[2].mode = OUTPUT

Expand Down Expand Up @@ -96,8 +99,11 @@ value = robot.arduino.pins[A0].analog_read()
value = robot.arduino.pins[A4].analog_read()
~~~~~

The analog pin numbers are available as `A0`, `A1`, `A2`, `A3`, `A4`, and `A5` respectively.
The analog pin numbers are available as `A0`, `A1`, `A2`, `A3`, `A4`, and `A5` respectively, either imported directly or using the `AnalogPins` enum.

~~~~ python
from sr.robot3 import A0, A1, A2, A3, A4, A5, AnalogPins
~~~~
RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved

## Output

Expand Down Expand Up @@ -145,6 +151,6 @@ distance_mm = robot.arduino.ultrasound_measure(4, 5)

<div class="warning">
The ultrasound sensor can measure distances up to around 4 metres.
If the ultrasound signal has to travel further, the echo may not be detected.
If the ultrasound signal has to travel further, the echo may not be detected.
This will cause the sensor to timeout and return 0.
</div>
2 changes: 1 addition & 1 deletion programming/cheat_sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For more information, make sure you check the rest of the documentation.
In order to use the `sr.robot3` API you first need to import it into your code:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved
~~~~~

## Initialising your robot
Expand Down
8 changes: 7 additions & 1 deletion programming/leds.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ robot.kch.leds[LED_B].colour = Colour.CYAN
robot.kch.leds[LED_C].colour = Colour.OFF
~~~~~

The available colours are:
The available colours are defined on the `Colour` enum:

~~~~~ python
Colour.OFF
Expand All @@ -63,3 +63,9 @@ robot.kch.leds[LED_B].r = False
# Turn on the green segment of LED B
robot.kch.leds[LED_B].g = True
~~~~~

These values are all available from the `sr.robot3` module:

~~~~ python
from sr.robot3 import Colour, LED_A, LED_B, LED_C
~~~~
7 changes: 5 additions & 2 deletions programming/motors.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Accessing the Motor Board
If there is exactly one Motor Board connected to your robot, it can be accessed using the `motor_board` property of the `Robot` object.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

my_motor_board = robot.motor_board
Expand All @@ -39,7 +39,7 @@ sr.robot3.robot - INFO - Found MotorBoard, serial: srXYZ1
You can then access the boards like this:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

my_motor_board = robot.motor_boards["srABC1"]
Expand Down Expand Up @@ -125,6 +125,7 @@ Special Values
--------------

In addition to the numeric values, there are two special constants that can be used:

- `BRAKE`
- `COAST`

Expand All @@ -135,6 +136,8 @@ In addition to the numeric values, there are two special constants that can be u
</div>

~~~~~ python
from sr.robot3 import BRAKE, COAST

# Stop the motor as quick as possible
robot.motor_boards["srABC1"].motors[0].power = BRAKE
~~~~~
Expand Down
8 changes: 6 additions & 2 deletions programming/power.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Accessing the Power Board
The power board can be accessed using the `power_board` property of the `Robot` object.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved
robot = Robot()

my_power_board = robot.power_board
Expand All @@ -28,7 +28,7 @@ my_power_board = robot.power_board
Power Outputs
-------------

Each of the power board's controllable outputs has a constant whose name closely matches the name of the output:
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`:

* H0 : `OUT_H0`
* H1 : `OUT_H1`
Expand All @@ -38,6 +38,8 @@ Each of the power board's controllable outputs has a constant whose name closely
* L3 : `OUT_L3`
* 5V : `OUT_FIVE_VOLT`

These are also available on the `PowerOutputPosition` enum.

RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved
Both of the 5V outputs are controlled together.

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:
Expand Down Expand Up @@ -108,6 +110,8 @@ The frequency on the buzzer is limited from 8Hz to 10,000Hz
</div>

~~~~~ python
from sr.robot3 import Note

# Beep for 0.5s in D.
robot.power_board.piezo.buzz(Note.D6, 0.5)

Expand Down
13 changes: 3 additions & 10 deletions programming/robot_api/comp_mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ This will instruct your robot that it is in competition mode and will have a num
Certain robot attributes will change when in comp mode, these can be used to change your robot's behaviour.
The affected attributes are:

mode
: Whether the robot is running in competition mode.
When in a competition match, this will be `COMP`, and at all other times this will be `DEV`.
- `robot.mode`
- `robot.zone`

zone
: The number of the scoring zone that the robot is associated with.
Between `0` and `3`.

The zone you are in defines which arena markers are near your scoring zone.
You can use the knowledge of your zone to compensate for this, so your robot behaves correctly in all starting positions.
See the [rules]({{ site.baseurl }}/rules) for where the scoring zones and arena markers are positioned.
See [other robot attributes]({{ site.baseurl }}/programming/robot_api/#other-robot-attributes) for further details of these attributes.
50 changes: 44 additions & 6 deletions programming/robot_api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ See the [Motor Board]({{ site.baseurl }}/programming/motors) page for more detai
To gain access to all of this functionality, all you need to do at the top of your code is the following:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()
~~~~~

This imports the Student Robotics module that we've written to interface with our hardware.
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.

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:
RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved

~~~~ python
from sr.robot3 import Robot, OUT_H0, AnalogPins
~~~~

If you don't need a value, it's best not to import it, to avoid accidentally overriding it.

<div class="info" markdown="1">
Most examples in the documentation will assume you have created a `robot` object from the `Robot` class.
If you see `robot` in a code example, it is assumed you have run the two lines above.
Expand All @@ -46,7 +54,6 @@ Then, within your `robot` you can use the following attributes to access to the

The functions of each board are described on their respective pages.


## Other Robot Attributes

As well as the attributes listed above, the `Robot` class also has the following attributes, which you may find useful:
Expand All @@ -56,6 +63,8 @@ zone
Between `0` and `3`.

This attribute is only available after the start button is pressed and will throw an error if accessed before.
The zone you are in defines which arena markers are near your scoring zone.
You can use the knowledge of your zone to compensate for this, so your robot behaves correctly in all starting positions.
See the [competition mode](./comp_mode) page for more information about this attribute.

mode
Expand All @@ -66,7 +75,7 @@ mode
See the [competition mode](./comp_mode) page for more information about this attribute.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot, COMP, DEV

robot = Robot()

Expand All @@ -76,14 +85,16 @@ mode
print("This is development")
~~~~~

`COMP` and `DEV` are aliases for `RobotMode.COMP` and `RobotMode.DEV`, which can also be imported from `sr.robot3`.

RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved
usbkey
: A [`Path`](https://docs.python.org/3/library/pathlib.html#basic-use) object containing the path to the USB stick.
You can use this to easily read and write files on the USB stick itself.

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:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
RealOrangeOne marked this conversation as resolved.
Show resolved Hide resolved
import os

robot = Robot()
Expand All @@ -103,15 +114,15 @@ is_simulated
Normally the Robot object is initialised with the following:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()
~~~~~

By default your robot will pause on this line waiting for the start button to be pressed.
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.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot(wait_for_start=False)

# Initialisation phase.
Expand All @@ -121,3 +132,30 @@ robot.wait_start()
~~~~~

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.

## Enums

Many values from the robot API are "enums". Enums are sets of values with names.

Enums compare equal to each other:

~~~~~ python
from sr.robot3 import Colour

pump_output = PowerOutputPosition.H0

pump_output == PowerOutputPosition.H0 # True
pump_output == PowerOutputPosition.H1 # False
~~~~~

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


~~~~~ python
from sr.robot3 import RobotMode

RobotMode.COMP == "COMP" # False
RobotMode.COMP.value == "COMP" # True
~~~~~

In general, the enum should be used and compared directly, rather than using its inner value.
2 changes: 1 addition & 1 deletion programming/servos.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Accessing the Servo Board
The servo board can be accessed using the `servo_board` property of the `Robot` object.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

my_servo_board = robot.servo_board
Expand Down
8 changes: 4 additions & 4 deletions programming/vision/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ see
Here's an example that will repeatedly print out the distance, in meters, to each marker that the robot can see:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

while True:
Expand All @@ -52,7 +52,7 @@ capture

~~~~~ python
import cv2
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

frame = robot.camera.capture()
Expand All @@ -68,7 +68,7 @@ You can use the output of the `capture` method with the other vision commands to
This may be useful if you wish to use both your own vision algorithms and our marker detection on the same frames.

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot
robot = Robot()

# Capture an OpenCV frame
Expand Down Expand Up @@ -101,7 +101,7 @@ Snapshots are saved to your USB drive, and can be viewed on another computer.
</div>

```python
from sr.robot3 import *
from sr.robot3 import Robot

robot = Robot()

Expand Down
4 changes: 2 additions & 2 deletions tutorials/basic_motor_control.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ If the motor appears to be going in the wrong direction, just swap the motor's p
As a first example:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot

robot = Robot()

Expand Down Expand Up @@ -131,7 +131,7 @@ Our aim is to do the forwards and backwards bit (as above), but, instead of sett
Here's the code:

~~~~~ python
from sr.robot3 import *
from sr.robot3 import Robot

robot = Robot()

Expand Down
Loading