-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.py
123 lines (102 loc) · 2.99 KB
/
move.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
# -*- coding: utf-8 -*-
'''
Time: 2021/8/19 18:40
Author: Sanzo
Github: github.com/Sanzo00
Email: [email protected]
'''
import RPi.GPIO as GPIO
import time
class Move:
def __init__(self, ENA, ENB, INs):
self.IN = INs
self.ENA = ENA
self.ENB = ENB
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.ENA, GPIO.OUT)
GPIO.setup(self.ENB, GPIO.OUT)
for i in self.IN:
GPIO.setup(i, GPIO.OUT)
self.speed_type = 'slow'
self.pwm_left = GPIO.PWM(self.ENA, 100)
self.pwm_right = GPIO.PWM(self.ENB, 100)
self.pwm_left.start(40)
self.pwm_right.start(40)
self.stop()
print(f"{self.get_time()} init is done")
def get_time(self):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def __del__(self):
self.pwm_left.stop()
self.pwm_right.stop()
GPIO.cleanup()
def speed(self, speed_type):
if speed_type == 'slow':
self.speed_type = 'slow'
self.pwm_left.ChangeDutyCycle(40)
self.pwm_right.ChangeDutyCycle(40)
elif speed_type == 'medium':
self.speed_type = 'medium'
self.pwm_left.ChangeDutyCycle(78)
self.pwm_right.ChangeDutyCycle(78)
elif speed_type == 'fast':
self.speed_type = 'fast'
self.pwm_left.ChangeDutyCycle(100)
self.pwm_right.ChangeDutyCycle(100)
def right(self):
print(f"{self.get_time()} right")
GPIO.output(self.IN[0], GPIO.LOW)
GPIO.output(self.IN[1], GPIO.HIGH)
GPIO.output(self.ENA, GPIO.HIGH)
GPIO.output(self.IN[2], GPIO.LOW)
GPIO.output(self.IN[3], GPIO.HIGH)
GPIO.output(self.ENB, GPIO.HIGH)
def right45(self):
self.right()
time.sleep(0.13)
self.stop()
def right90(self):
self.right()
time.sleep(0.5)
self.stop()
def left(self):
print(f"{self.get_time()} left")
GPIO.output(self.IN[0], GPIO.HIGH)
GPIO.output(self.IN[1], GPIO.LOW)
GPIO.output(self.ENA, GPIO.HIGH)
GPIO.output(self.IN[2], GPIO.HIGH)
GPIO.output(self.IN[3], GPIO.LOW)
GPIO.output(self.ENB, GPIO.HIGH)
def left45(self):
self.left()
time.sleep(0.13)
self.stop()
def left90(self):
self.left()
time.sleep(0.48)
self.stop()
def back(self):
print(f"{self.get_time()} back")
GPIO.output(self.IN[0], GPIO.HIGH)
GPIO.output(self.IN[1], GPIO.LOW)
GPIO.output(self.ENA, GPIO.HIGH)
GPIO.output(self.IN[2], GPIO.LOW)
GPIO.output(self.IN[3], GPIO.HIGH)
GPIO.output(self.ENB, GPIO.HIGH)
def forward(self):
print(f"{self.get_time()} forward")
GPIO.output(self.IN[0], GPIO.LOW)
GPIO.output(self.IN[1], GPIO.HIGH)
GPIO.output(self.ENA, GPIO.HIGH)
GPIO.output(self.IN[2], GPIO.HIGH)
GPIO.output(self.IN[3], GPIO.LOW)
GPIO.output(self.ENB, GPIO.HIGH)
def stop(self):
print(f"{self.get_time()} stop")
GPIO.output(self.IN[0], GPIO.LOW)
GPIO.output(self.IN[1], GPIO.LOW)
GPIO.output(self.ENA, GPIO.HIGH)
GPIO.output(self.IN[2], GPIO.LOW)
GPIO.output(self.IN[3], GPIO.LOW)
GPIO.output(self.ENB, GPIO.HIGH)