Skip to content

Commit 36ab963

Browse files
Merge pull request #625 from srobo/avoid-wildcard-import
Avoid wildcard imports and mention enums
2 parents 596dc13 + 4c05495 commit 36ab963

File tree

12 files changed

+85
-34
lines changed

12 files changed

+85
-34
lines changed

.spelling

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ DTSE9H
1313
Ebuyer
1414
esque
1515
finessing
16+
GPIO
1617
iMAX
1718
IntelliJ
1819
Nikitin

programming/arduino/custom_firmware.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The Arduino serial number is a string of numbers and letters, and is output in t
2323
You'll need the serial number later, so it's best to save it into a variable:
2424

2525
~~~~~ python
26-
from sr.robot3 import *
26+
from sr.robot3 import Robot
2727

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

4242
~~~~~ python
43-
from sr.robot3 import *
43+
from sr.robot3 import Robot
4444

4545
# Replace this with the actual serial number of your board
4646
ARDUINO_SN = "752303138333517171B1"

programming/arduino/sr_firmware.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The kit can control multiple Arduinos at once, however we only provide one in th
1515
If there is exactly one Arduino connected to your robot, it can be accessed using the `arduino` property of the `Robot` object.
1616

1717
~~~~~ python
18-
from sr.robot3 import *
18+
from sr.robot3 import Robot
1919
robot = Robot()
2020

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

60+
These are available from `sr.robot3`, or the `GPIOPinMode` enum.
6061

6162
An example of how to use this is below:
6263

6364
~~~~~ python
65+
from sr.robot3 import INPUT, INPUT_PULLUP, OUTPUT
66+
6467
# set Arduino pin 2 to output
6568
robot.arduino.pins[2].mode = OUTPUT
6669

@@ -96,8 +99,11 @@ value = robot.arduino.pins[A0].analog_read()
9699
value = robot.arduino.pins[A4].analog_read()
97100
~~~~~
98101

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

104+
~~~~ python
105+
from sr.robot3 import A0, A1, A2, A3, A4, A5, AnalogPins
106+
~~~~
101107

102108
## Output
103109

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

146152
<div class="warning">
147153
The ultrasound sensor can measure distances up to around 4 metres.
148-
If the ultrasound signal has to travel further, the echo may not be detected.
154+
If the ultrasound signal has to travel further, the echo may not be detected.
149155
This will cause the sensor to timeout and return 0.
150156
</div>

programming/cheat_sheet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ For more information, make sure you check the rest of the documentation.
1616
In order to use the `sr.robot3` API you first need to import it into your code:
1717

1818
~~~~~ python
19-
from sr.robot3 import *
19+
from sr.robot3 import Robot
2020
~~~~~
2121

2222
## Initialising your robot

programming/leds.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ robot.kch.leds[LED_B].colour = Colour.CYAN
3838
robot.kch.leds[LED_C].colour = Colour.OFF
3939
~~~~~
4040

41-
The available colours are:
41+
The available colours are defined on the `Colour` enum:
4242

4343
~~~~~ python
4444
Colour.OFF
@@ -63,3 +63,9 @@ robot.kch.leds[LED_B].r = False
6363
# Turn on the green segment of LED B
6464
robot.kch.leds[LED_B].g = True
6565
~~~~~
66+
67+
These values are all available from the `sr.robot3` module:
68+
69+
~~~~ python
70+
from sr.robot3 import Colour, LED_A, LED_B, LED_C
71+
~~~~

programming/motors.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Accessing the Motor Board
1919
If there is exactly one Motor Board connected to your robot, it can be accessed using the `motor_board` property of the `Robot` object.
2020

2121
~~~~~ python
22-
from sr.robot3 import *
22+
from sr.robot3 import Robot
2323
robot = Robot()
2424

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

4141
~~~~~ python
42-
from sr.robot3 import *
42+
from sr.robot3 import Robot
4343
robot = Robot()
4444

4545
my_motor_board = robot.motor_boards["srABC1"]
@@ -125,6 +125,7 @@ Special Values
125125
--------------
126126

127127
In addition to the numeric values, there are two special constants that can be used:
128+
128129
- `BRAKE`
129130
- `COAST`
130131

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

137138
~~~~~ python
139+
from sr.robot3 import BRAKE, COAST
140+
138141
# Stop the motor as quick as possible
139142
robot.motor_boards["srABC1"].motors[0].power = BRAKE
140143
~~~~~

programming/power.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Accessing the Power Board
1818
The power board can be accessed using the `power_board` property of the `Robot` object.
1919

2020
~~~~~ python
21-
from sr.robot3 import *
21+
from sr.robot3 import Robot
2222
robot = Robot()
2323

2424
my_power_board = robot.power_board
@@ -28,7 +28,7 @@ my_power_board = robot.power_board
2828
Power Outputs
2929
-------------
3030

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

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

41+
These are also available on the `PowerOutputPosition` enum.
42+
4143
Both of the 5V outputs are controlled together.
4244

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

110112
~~~~~ python
113+
from sr.robot3 import Note
114+
111115
# Beep for 0.5s in D.
112116
robot.power_board.piezo.buzz(Note.D6, 0.5)
113117

programming/robot_api/comp_mode.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ This will instruct your robot that it is in competition mode and will have a num
2828
Certain robot attributes will change when in comp mode, these can be used to change your robot's behaviour.
2929
The affected attributes are:
3030

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

35-
zone
36-
: The number of the scoring zone that the robot is associated with.
37-
Between `0` and `3`.
38-
39-
The zone you are in defines which arena markers are near your scoring zone.
40-
You can use the knowledge of your zone to compensate for this, so your robot behaves correctly in all starting positions.
41-
See the [rules]({{ site.baseurl }}/rules) for where the scoring zones and arena markers are positioned.
34+
See [other robot attributes]({{ site.baseurl }}/programming/robot_api/#other-robot-attributes) for further details of these attributes.

programming/robot_api/index.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ See the [Motor Board]({{ site.baseurl }}/programming/motors) page for more detai
2323
To gain access to all of this functionality, all you need to do at the top of your code is the following:
2424

2525
~~~~~ python
26-
from sr.robot3 import *
26+
from sr.robot3 import Robot
2727
robot = Robot()
2828
~~~~~
2929

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

33+
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:
34+
35+
~~~~ python
36+
from sr.robot3 import Robot, OUT_H0, AnalogPins
37+
~~~~
38+
39+
If you don't need a value, it's best not to import it, to avoid accidentally overriding it.
40+
3341
<div class="info" markdown="1">
3442
Most examples in the documentation will assume you have created a `robot` object from the `Robot` class.
3543
If you see `robot` in a code example, it is assumed you have run the two lines above.
@@ -46,7 +54,6 @@ Then, within your `robot` you can use the following attributes to access to the
4654

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

49-
5057
## Other Robot Attributes
5158

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

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

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

6877
~~~~~ python
69-
from sr.robot3 import *
78+
from sr.robot3 import Robot, COMP, DEV
7079

7180
robot = Robot()
7281

@@ -76,14 +85,16 @@ mode
7685
print("This is development")
7786
~~~~~
7887

88+
`COMP` and `DEV` are aliases for `RobotMode.COMP` and `RobotMode.DEV`, which can also be imported from `sr.robot3`.
89+
7990
usbkey
8091
: A [`Path`](https://docs.python.org/3/library/pathlib.html#basic-use) object containing the path to the USB stick.
8192
You can use this to easily read and write files on the USB stick itself.
8293

8394
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:
8495

8596
~~~~~ python
86-
from sr.robot3 import *
97+
from sr.robot3 import Robot
8798
import os
8899

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

105116
~~~~~ python
106-
from sr.robot3 import *
117+
from sr.robot3 import Robot
107118
robot = Robot()
108119
~~~~~
109120

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

113124
~~~~~ python
114-
from sr.robot3 import *
125+
from sr.robot3 import Robot
115126
robot = Robot(wait_for_start=False)
116127

117128
# Initialisation phase.
@@ -121,3 +132,30 @@ robot.wait_start()
121132
~~~~~
122133

123134
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.
135+
136+
## Enums
137+
138+
Many values from the robot API are "enums". Enums are sets of values with names.
139+
140+
Enums compare equal to each other:
141+
142+
~~~~~ python
143+
from sr.robot3 import Colour
144+
145+
pump_output = PowerOutputPosition.H0
146+
147+
pump_output == PowerOutputPosition.H0 # True
148+
pump_output == PowerOutputPosition.H1 # False
149+
~~~~~
150+
151+
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`.
152+
153+
154+
~~~~~ python
155+
from sr.robot3 import RobotMode
156+
157+
RobotMode.COMP == "COMP" # False
158+
RobotMode.COMP.value == "COMP" # True
159+
~~~~~
160+
161+
In general, the enum should be used and compared directly, rather than using its inner value.

programming/servos.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Accessing the Servo Board
1818
The servo board can be accessed using the `servo_board` property of the `Robot` object.
1919

2020
~~~~~ python
21-
from sr.robot3 import *
21+
from sr.robot3 import Robot
2222
robot = Robot()
2323

2424
my_servo_board = robot.servo_board

0 commit comments

Comments
 (0)