actuators
- Working with Actuators¶
The actuators
module provides a collection of classes to interact with actuators, such as servos, relays, and buttons.
Servo¶
-
class
actuators.
Servo
(port[=2], position[=0])¶ Allows control of a Grove Servo. Default
port
for the servo is2
. Theposition
parameter sets the initial position of the servo.-
Servo.
set_position
(degree)¶ Sets the
degree
position of the servo (between0
and180
, which is half a circle). Ifdegree
is greater than 180, the servo will be set at the180
degree position. Likewise, ifdegree
is less than0
, the servo will rotate to the 0 degree position.
-
Servo.
get_position
()¶ Returns the current position of the servo in degrees.
-
Example:¶
from actuators import Servo
s = Servo(1, init_degree = 180) # defines a servo connected to port 1 with initial position at 180 degrees.
s.set_position(90) # rotate the servo by 90 degrees.
Relay¶
-
class
actuators.
Relay
(port[=1])¶ Allows control of a Grove Relay. The relay by default is normally open (NO) triggered by a
high
signal.-
Relay.
on
()¶ Activate the relay, close the circuit, and turn on whatever appliance that’s connected to the relay.
-
Relay.
off
()¶ Deactivate the relay, open the circuit, and turn off whatever appliance that’s connected to the relay.
-
Relay.
is_on
()¶ Returns
True
if the relay is on, orFalse
if the relay is off.
-
Button¶
-
class
actuators.
Button
(port[=2], pullup[=True])¶ Allows control of a Grove Button. There are two ways to use a button. First, you can access
Button.is_pressed
property orButton.is_pressed()
method to determine if the button is pressed. Alternatively, you can also set a callback function withButton.on_release()
method use the interrupt mechanism. Please see example of how to use the callback.-
Button.
is_pressed
()¶ Returns
True
if the button is pressed, orFalse
if it is not.
-
Button.
on_press
(callback)¶ Executes the
callback
function provided to the method when the button is pressed.
-
Button.
on_release
(callback)¶ Executes the
callback
function provided to the method when the button is released.
-
Example¶
Controlling the LED with the Button
from actuators import Led, Button
led = Led(1) # Specifies an LED at Port 1
button = Button(2) # Specifies a button at Port 2
## Turns on the LED when the button is pressed
while True:
if button.is_pressed():
led.on()
else:
led.off()
Turns the LED on/off with a callback function
from actuators import Button
from displays import Led
led = Led(1, on=False) # Specifies an LED at Port 1
button = Button(2) # Specifies a button at Port 2
## Define a callback function
def turn_on_led(pin):
global led # Need this line to refer to the led object outside the function.
if led.is_on():
led.off()
else:
led.on()
## Set the callback function to Button.on_release method.
button.on_release(callback=turn_on_led) # Note that no () are needed.
Buzzer¶
-
class
actuators.
Buzzer
(port[=2])¶ Allows control of a Grove Buzzer. You can control the buzzer to play a note or a piece of music. The notes are written in strings. Available notes are
["c", "d", "e", "f", "g", "a", "b", "C", " "]
. White space means skip.-
Buzzer.
play_note
(note, duration[=0.5])¶ Plays a note.
duration
controls how long the note gets played.
-
Buzzer.
play_music
(notes, rhythms[=None], tempo[=1])¶ Plays a string of
notes
. You may supply a list of numbers the same length asnotes
asrhythms
, how long each note gets played. You may also specify an overalltempo
. The largertempo
is, the slower the music.
-
Example¶
Using the buzzer
from actuators import Buzzer
buzzer = Buzzer(port=2)
# Plays one note
buzzer.play_note("c", duration=1)
# Plays a piece of music
buzzer.play_music("cdefgabC", rhythms=[32, 16, 8, 4, 2, 1, 0.5, 1], tempo = 0.8)