-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcmotor.py
38 lines (32 loc) · 1.06 KB
/
dcmotor.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
# Created by https://RandomNerdTutorials.com
#This file includes a class to control DC motors
class DCMotor:
#the min_duty and max_duty are defined for 15000Hz frequency
#you can pass as arguments
def __init__(self, pin1, pin2, enable_pin, min_duty=750, max_duty=1023):
self.pin1 = pin1
self.pin2= pin2
self.enable_pin = enable_pin
self.min_duty = min_duty
self.max_duty = max_duty
#speed value can be between 0 and 100
def forward(self, speed):
self.speed = speed
self.enable_pin.duty(self.duty_cycle(self.speed))
self.pin1.value(1)
self.pin2.value(0)
def backwards(self, speed):
self.speed = speed
self.enable_pin.duty(self.duty_cycle(self.speed))
self.pin1.value(0)
self.pin2.value(1)
def stop(self):
self.enable_pin.duty(0)
self.pin1.value(0)
self.pin2.value(0)
def duty_cycle(self, speed):
if self.speed <= 0 or self.speed > 100:
duty_cycle = 0
else:
duty_cycle = int (self.min_duty + (self.max_duty - self.min_duty)*((self.speed - 1)/(100-1)))
return duty_cycle