-
Notifications
You must be signed in to change notification settings - Fork 1
/
breaking.py
46 lines (37 loc) · 1.44 KB
/
breaking.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
from Adafruit_MotorHAT import Adafruit_MotorHAT
from RPi.GPIO import setmode, output, setup, OUT, BCM
import RPi.GPIO as GPIO
from time import sleep
setmode(BCM)
class braking_motor:
def __init__(self, controller, pwm_pin, dir_pin):
self.MC = controller
if (pwm_pin < 0) or (pwm_pin > 15):
raise NameError('PWM pin must be between 0 and 15 inclusive')
self.pwm_pin = pwm_pin
self.dir_pin = dir_pin
GPIO.setwarnings(False)
setup(dir_pin, OUT)
def pull_brake(self, speed=1000, time_on=1):
output(self.dir_pin, 0) #decides whether braking or unbraking
self.MC._pwm.setPWM(self.pwm_pin, 0, speed)
sleep(time_on)
self.MC._pwm.setPWM(self.pwm_pin, 0, 0)
def release_brake(self, speed=1000, time_on=1):
output(self.dir_pin, 1) #decides whether brakiing or unbraking
self.MC._pwm.setPWM(self.pwm_pin, 1, speed)
sleep(time_on)
self.MC._pwm.setPWM(self.pwm_pin, 0, 0)
def kill(self):
self.MC._pwm.setPWM(self.pwm_pin, 0, 0)
mh = Adafruit_MotorHAT()
brake_motor = braking_motor(mh, 14, 19) #create break motor. pwm pin 14,
output(brake_motor.dir_pin, 0)
def dobrake(speed = 1000, time_on=1):
brake_motor.pull_brake(speed, time_on)
def unbrake(speed = 1000, time_on=1):
brake_motor.release_brake(speed, time_on)
if __name__ == "__main__":
#testing the motor
brake_motor.pull_brake()
brake_motor.release_brake()