-
Notifications
You must be signed in to change notification settings - Fork 49
/
device.py
50 lines (39 loc) · 1.53 KB
/
device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from gpiozero import Device
from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import DigitalOutputDevice, PWMOutputDevice
import time
Device.pin_factory = PiGPIOFactory()
class Motor:
"""
The class takes three pin numbers as the input to control one of the motor connected to TB6612FNG module.
"""
def __init__(self, in1, in2, pwm):
self.in1 = DigitalOutputDevice(in1)
self.in1.off()
self.in2 = DigitalOutputDevice(in2)
self.in2.on()
self.pwm = PWMOutputDevice(pwm, frequency=1000)
def set_throttle(self, val):
"""Control the orientation and the speed of the motor.
Arguments:
val: a number between -1.0 and 1.0. The motor rotates in forward direction if val > 1, otherwise in reverse direction.
Setting val to None will set the motor to stop mode.
"""
# Set the motor to stop mode.
if val is None:
self.in1.off()
self.in2.off()
self.pwm.value = 1.0
else:
# Determine the orientation of the motor.
if val > 0.0:
self.in1.off()
self.in2.on()
else:
self.in1.on()
self.in2.off()
# Clamp the pwm signal (throttle) to [0, 1].
pwm = max(0.0, min(abs(val), 1.0))
# Note that setting PWM to low will brake the motor no matter what
# in1 and in2 input is.
self.pwm.value = pwm