-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pwm.py
176 lines (143 loc) · 4.11 KB
/
run_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import RPi.GPIO as GPIO
import time
import mag3110,servo_mg996r
import sonic_distance
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(40, GPIO.OUT,initial=False)
p1= GPIO.PWM(11, 100) #right back
p2= GPIO.PWM(13, 100) #right front
p3= GPIO.PWM(15, 100) #left front
p4= GPIO.PWM(16, 100) #left back
def front(d):
#d=d*0.7
p1.start(00)
p2.start(d)
p3.start(d)
p4.start(00)
def back(d):
#d=d*0.7
p1.start(d)
p2.start(00)
p3.start(00)
p4.start(d)
def right(d):
#d=d*0.7
p1.start(00)
p2.start(d)
p3.start(00)
p4.start(d)
def left(d):
#d=d*0.7
p1.start(d)
p2.start(00)
p3.start(d)
p4.start(00)
def stop():
p1.start(0)
p2.start(0)
p3.start(0)
p4.start(0)
def servo_openmv_p1():
servo_mg996r.servo_V(180)
servo_mg996r.servo_T(132)
time.sleep(1.2)
servo_mg996r.servo_silent()
print("Servo moved to openmv position1!")
def fix_front_dist(x):
i=0
bias_itg=0
bias_div=0
bias_d_last=0
while i<=50:
a=sonic_distance.sonic_detect()
if abs(x-a)>=3:
bias_d=a-x
bias_itg+=bias_d
bias_div=bias_d_last-bias_d
output=bias_d*1.5#+bias_itg*0.01+bias_div*0.01 (-75,75)
if output>=0:
if output>=75:
output=75
back(25+output)
else:
if output<=-75:
output=-75
front(25+(-output))
time.sleep(0.04)
bias_d_last=bias_d
else:
i+=2
stop()
time.sleep(0.04)
print("Move to front distance:%dcm done" % x)
def move_mag(ang_target): #car will rotate mag3110 to target angle
i=0
bias_ang_itg=0
bias_ang_div_last=0
bias_ang_last=0
while i<=50:
ang_current=mag3110.mag3110_ang()
bias_ang=ang_target-ang_current
bias_ang_itg+=bias_ang
bias_ang_div=bias_ang_last-bias_ang
bias_ang_last=bias_ang
if abs(bias_ang)>=2:
#bace on L298N input voltage 8.2V,car start rotate pwm is 42
output=bias_ang*0.4#+bias_ang_itg*0.1+bias_ang_div*0.3
if abs(bias_ang)<=330:
if output>=35:
output=35
elif output<=-35:
output=-35
if output>0:
if bias_ang>=40: #if angle biger than 40 full spd
right(90)
else:
right(35+output)
elif output<0:
if bias_ang<=-40: #if angle biger than 40 full spd
left(90)
else:
left(35+abs(output))
elif abs(bias_ang)>330:
bias_ang2=360-abs(bias_ang)
output=bias_ang2*0.3
#print(output,ang_current)
if ang_target>=340:
left(35+output)
elif ang_target<20:
right(35+abs(output))
else:
i+=1
stop()
time.sleep(0.02) #dont't refresh too fast,mag3110 cycle time
print("Move to angle:%d" % ang_target)
def move_ang(angle): #base on current angle,and left/right side rotate
ang_current=mag3110.mag3110_ang()
ang_target=ang_current+angle
if ang_target>360:
ang_target=ang_target-360
if ang_target<0:
ang_target=360+ang_target
move_mag(ang_target)
def openmv_camera_test():
while 1:
print("Close cam 2 S!")
GPIO.output(40,0)
time.sleep(2)
print("Open cam 2 S!")
GPIO.output(40,1)
time.sleep(2)
if __name__=='__main__':
#openmv_camera_test()
stop()
print("Car will move in 0.5 secs!!!")
move_mag(180)
fix_front_dist(20)
time.sleep(2)
fix_front_dist(40)