-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motor_Class(pwm).py
127 lines (108 loc) · 3.8 KB
/
Motor_Class(pwm).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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from imports import *
class Motor_Control:
def __init__(self):
GPIO.setmode(GPIO.BOARD)
# LEFT MOTOR
self.motor1A = 13 # GPIO 27
self.motor1B = 15 # GPIO 22
# RIGHT MOTOR
self.motor2B = 16 # GPIO 23
self.motor2A = 18 # GPIO 24
# Set the GPIO pins as outputs
GPIO.setup(self.motor1A, GPIO.OUT)
GPIO.setup(self.motor1B, GPIO.OUT)
GPIO.setup(self.motor2A, GPIO.OUT)
GPIO.setup(self.motor2B, GPIO.OUT)
# Initialize PWM for the motors
self.pwm_motor1 = GPIO.PWM(self.motor1A, 1000) # 1000 Hz frequency
self.pwm_motor2 = GPIO.PWM(self.motor2A, 1000) # 1000 Hz frequency
# Start PWM with 0% duty cycle
self.pwm_motor1.start(0)
self.pwm_motor2.start(0)
def set_motor_speed(self, pwm_motor, speed):
# Convert speed to a duty cycle value between 0 and 100
duty_cycle = max(0, min(100, speed))
pwm_motor.ChangeDutyCycle(duty_cycle)
def go_forward(self, speed=100):
# L +
self.set_motor_speed(self.pwm_motor1, speed)
GPIO.output(self.motor1B, GPIO.HIGH)
# R +
self.set_motor_speed(self.pwm_motor2, speed)
GPIO.output(self.motor2B, GPIO.LOW)
def go_backward(self, speed=100):
# L -
GPIO.output(self.motor1B, GPIO.LOW)
self.set_motor_speed(self.pwm_motor1, speed)
# R -
GPIO.output(self.motor2B, GPIO.HIGH)
self.set_motor_speed(self.pwm_motor2, speed)
def turn_left(self, speed=100):
# L +
self.set_motor_speed(self.pwm_motor1, speed)
GPIO.output(self.motor1B, GPIO.HIGH)
# R -
GPIO.output(self.motor2B, GPIO.HIGH)
self.set_motor_speed(self.pwm_motor2, speed)
def turn_right(self, speed=100):
# L -
GPIO.output(self.motor1B, GPIO.LOW)
self.set_motor_speed(self.pwm_motor1, speed)
# R +
self.set_motor_speed(self.pwm_motor2, speed)
GPIO.output(self.motor2B, GPIO.LOW)
def stop(self):
# Stop both motors
self.set_motor_speed(self.pwm_motor1, 0)
self.set_motor_speed(self.pwm_motor2, 0)
def cleanup(self):
# Stop PWM and clean up GPIO
self.pwm_motor1.stop()
self.pwm_motor2.stop()
GPIO.cleanup()
def forward(fullspeed):
GPIO.output(motor1A, True)
GPIO.output(motor1B, False)
pwm1.ChangeDutyCycle(fullspeed) #left
GPIO.output(motor2A, True)
GPIO.output(motor2B, False)
pwm2.ChangeDutyCycle(fullspeed) #right
def backward():
GPIO.output(motor1A, False)
GPIO.output(motor1B, True)
pwm1.ChangeDutyCycle(50)
GPIO.output(motor2A, False)
GPIO.output(motor2B, True)
pwm2.ChangeDutyCycle(50)
def right(fullspeed, ctrl):
GPIO.output(motor1A, True)
GPIO.output(motor1B, False)
pwm1.ChangeDutyCycle(fullspeed)
GPIO.output(motor2A, False)
GPIO.output(motor2B, True)
# pwm2.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(fullspeed-ctrl)
def left(fullspeed, ctrl):
GPIO.output(motor1A, False)
GPIO.output(motor1B, True)
# pwm1.ChangeDutyCycle(0)
pwm1.ChangeDutyCycle(fullspeed-ctrl)
GPIO.output(motor2A, True)
GPIO.output(motor2B, False)
pwm2.ChangeDutyCycle(fullspeed)
def stop():
GPIO.output(motor1A, False)
GPIO.output(motor1B, False)
GPIO.output(motor2A, False)
GPIO.output(motor2B, False)
if __name__ == "__main__":
Motor_Control = Motor_Control()
print('Go')
Motor_Control.go_forward(80)
time.sleep(5)
print('back')
Motor_Control.go_backward(100)
time.sleep(5)
Motor_Control.stop()
print('stopped')
Motor_Control.cleanup()