Skip to content

Commit 7b28f6c

Browse files
committed
Merge branch 'update-power-board' into sr2022
2 parents e712ee3 + 9ab0d21 commit 7b28f6c

File tree

1 file changed

+71
-23
lines changed

1 file changed

+71
-23
lines changed

programming/sr/power/index.md

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,12 @@ Power
77
=====
88

99
There are a few things that can be done with the power board, namely current and voltage sensing, and beeping.
10-
As there is only one power board, it is not accessed like a list like `motors` for example:
10+
As there is only one power board, it is not accessed like a list like `motors` and is instead accessed directly, for example:
1111

1212
~~~~~ python
13-
R.power.something...
13+
r.power_board.something...
1414
~~~~~
1515

16-
[Battery Status](#battery) {#battery}
17-
-------
18-
19-
The power board can report both the battery voltage, in Volts, and the current being drawn from it, in Amps.
20-
You can access these values like so:
21-
22-
~~~~~ python
23-
# Print the battery voltage and current to the log
24-
print(R.power.battery.voltage, R.power.battery.current)
25-
~~~~~
26-
27-
A fully charged battery will measure 12.6V.
28-
The power board will turn off and signal a low battery at 10.2V.
29-
The discharge curve is roughly linear between 11.4V and 10.4V.
30-
31-
3216
[Power Outputs](#outputs) {#outputs}
3317
-------
3418

@@ -46,29 +30,93 @@ While they are all turned on when your code starts running,
4630
you can control whether each output is turned on or off like so:
4731

4832
~~~~~ python
33+
from sr.robot3 import *
34+
4935
# Turn output H0 off
50-
R.power.output[OUT_H0] = False
36+
r.power_board.outputs[OUT_H0].is_enabled = False
5137

5238
# Turn output L0 on
53-
R.power.output[OUT_L0] = True
39+
r.power_board.outputs[OUT_L0].is_enabled = True
40+
41+
# Find out whether L3 is enabled
42+
print(r.power_board.outputs[OUT_L3].is_enabled)
43+
44+
# Find the current (in Amps) being used by L3
45+
print(r.power_board.outputs[OUT_L3].current)
5446
~~~~~
5547

5648
An exception is raised if you try to set an output index which doesn't exist.
5749

50+
You can also control all the outputs together:
51+
52+
~~~~~ python
53+
r.power_board.outputs.power_off()
54+
r.power_board.outputs.power_on()
55+
~~~~~
56+
57+
<div class="warning">
58+
If you turn off the power output which is powering another of your boards,
59+
then they will appear to be missing and your code will break if you try to
60+
control them.
61+
</div>
62+
63+
64+
[Battery Status](#battery) {#battery}
65+
-------
66+
67+
The power board can report both the battery voltage, in Volts, and the current being drawn from it, in Amps.
68+
You can access these values like so:
69+
70+
~~~~~ python
71+
# Print the battery voltage and current to the log
72+
print(
73+
r.power_board.battery_sensor.voltage,
74+
r.power_board.battery_sensor.current,
75+
)
76+
~~~~~
77+
78+
A fully charged battery will measure 12.6V.
79+
The power board will turn off and signal a low battery at 10.2V.
80+
The discharge curve is roughly linear between 11.4V and 10.4V.
81+
5882

5983
[Beeping](#beeping) {#beeping}
6084
-------
6185

6286
The power board has a piezo buzzer which can beep.
6387

64-
The beep function accepts 1 or 2 parameters, `duration` is compulsory and is measured in milliseconds. `note` is optional, but must be one string of `a-g` or `uc`. `frequency` is also optional, and should be an integer. One of `note` and `frequency`, must be given. If both are given, `note` is used.
88+
The `buzz` function accepts multiple parameters, depending on what you
89+
want to play. The first argument is the duration of the beep, in
90+
seconds. The later arguments are either the note you want to play, or
91+
the frequency of the buzzer (in Hertz). You have to specify which of
92+
note or frequency you're passing using a keyword argument, your code
93+
will fail otherwise.
94+
95+
Theoretically, the piezo buzzer will buzz at any provided frequency, however
96+
humans can only hear between [20Hz and 20000Hz][pitch-range].
97+
98+
The `Note` enum provides notes in [scientific pitch notation][pitch-notation]
99+
between `C6` and `C8`. You can play other tones by providing a frequency.
100+
101+
<div class="info">
102+
Calling `buzz` is non-blocking, which means it doesn't actually wait
103+
for the piezo to stop buzzing before continuing with your code. If you
104+
want to wait for the buzzing to stop, add a `sleep` afterwards!
105+
If you send more than 32 beeps to the robot too quickly, your power board will crash!
106+
</div>
65107

66108
~~~~~ python
109+
from sr.robot3 import Note
110+
67111
# Beep for 0.5s in D.
68-
R.power.beep(500, note='d')
112+
r.power_board.piezo.buzz(0.5, Note.D6)
69113

70114
# Beep for 2s at 400Hz
71-
R.power.beep(2000, frequency=400)
115+
r.power_board.piezo.buzz(2, 400)
72116
~~~~~
73117

74118
`ValueError` is raised if the note is not recognised or the frequency is not an integer.
119+
120+
121+
[pitch-range]: https://en.wikipedia.org/wiki/Hearing_range#Humans
122+
[pitch-notation]: https://en.wikipedia.org/wiki/Scientific_pitch_notation

0 commit comments

Comments
 (0)