/
speedincrease.py
198 lines (149 loc) · 3.84 KB
/
speedincrease.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import pygame
import RPi.GPIO as GPIO
import time
import webiopi
webiopi.setDebug()
GPIO = webiopi.GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
motorRightPin = 7
motorLeftPin = 11
GPIO.setFunction(motorRightPin, GPIO.OUT)
GPIO.setFunction(motorLeftPin, GPIO.OUT)
motorFdPin = 13
motorRdPin = 15
GPIO.setFunction(motorFdPin, GPIO.OUT)
GPIO.setFunction(motorRdPin, GPIO.OUT)
GPIO.setFunction(motorFdPin, GPIO.PWM)
GPIO.setFunction(motorRdPin, GPIO.PWM)
def initiate():
global acceleration
global motorFspeed
global motorRspeed
global speedstep
global maxspeed
global minspeed
acceleration = 0
motorFspeed = 0
motorRspeed = 0
speedstep = 10
maxspeed = 90
minspeed = 0
def reverse():
GPIO.output(motorRdPin, GPIO.PWM)
GPIO.output(motorFdPin, GPIO.PWM)
def forward():
GPIO.output(motorRdPin, GPIO.PWM)
GPIO.output(motorFdPin, GPIO.PWM)
def stop():
GPIO.digitalWrite(motorRdPin, GPIO.PWM)
GPIO.digitalWrite(motorFdPin, GPIO.PWM)
# motorFspeed, motorRspeed, acceleration
initiate()
return 0, 0, 0
def stear_left():
GPIO.output(motorRightPin, GPIO.LOW)
GPIO.output(motorLeftPin, GPIO.HIGH)
def stear_right():
GPIO.output(motorRightPin, GPIO.HIGH)
GPIO.output(motorLeftPin, GPIO.LOW)
def center():
GPIO.digitalWrite(motorRightPin, GPIO.LOW)
GPIO.digitalWrite(motorLeftPin, GPIO.LOW)
# This functions sets the motor speed.
def setacceleration(value):
global motorFspeed
global motorRspeed
global acceleration
global minspeed
global maxspeed
acceleration = acceleration + value
minspeed, maxspeed = getMinMaxSpeed()
#Set Min and Max values for acceleration
if(acceleration < -90):
acceleration = -90
if(acceleration > 90):
acceleration = 90
if(acceleration > 0):
# drive forward
forward()
motorFspeed = acceleration
motorRspeed = acceleration
print("forward: ", motorFspeed, motorRspeed)
elif(acceleration == 0):
# stopp motors
motorFspeed = acceleration
motorRspeed = acceleration
motorFspeed, motorRspeed, acceleration = stop()
print("stop: ", motorFspeed, motorRspeed)
else:
# drive backward
reverse()
motorFspeed = (acceleration * -1)
motorRspeed = (acceleration * -1)
#print("backward: ", motorFspeed, motorRspeed)
motorFspeed, motorRspeed = check_motorpseed(motorFspeed, motorRspeed)
print("check: ", motorFspeed, motorRspeed)
def check_motorpseed(motorFspeed, motorRspeed):
if (motorFspeed < minspeed):
motorFspeed = minspeed
if (motorFspeed > maxspeed):
motorFspeed = maxspeed
if (motorRspeed < minspeed):
motorRspeed = minspeed
if (motorRspeed > maxspeed):
motorRspeed = maxspeed
return motorFspeed, motorRspeed
# Set Min Max Speed
def getMinMaxSpeed():
minspeed = 0
maxspeed = 90
return minspeed, maxspeed
# Get the motor speed
def getMotorSpeed():
global motorFspeed
global motorRspeed
return motorFspeed, motorRspeed
def getMotorSpeedStep():
return 10
def ButtonForward():
fowardAcc = 0
fowardAcc = getMotorSpeedStep()
setacceleration(fowardAcc)
motorFspeed, motorRspeed = getMotorSpeed()
# percent calculation
valueF = float(motorFspeed)/100
valueR = float(motorRspeed)/100
GPIO.pwmWrite(motorFdPin, valueF)
GPIO.pwmWrite(motorRdPin, valueR)
def ButtonReverse():
backwardAcc = 0
backwardAcc = getMotorSpeedStep()
setacceleration((backwardAcc*-1))
motorFspeed, motorRspeed = getMotorSpeed()
# percent calculation
valueF = float(motorFspeed)/100
valueR = float(motorRspeed)/100
GPIO.pwmWrite(motorFdPin, valueF)
GPIO.pwmWrite(motorRdPin, valueR)
def ButtonTurnLeft():
stear_left()
#print("LEFT: ",valueL,valueR,spotturn)
def ButtonTurnRight():
stear_right()
#print("RIGHT: ",valueL,valueR, spotturn)
def ButtonStop():
center()
stop()
try:
print 'Press [ESC] to quit'
while True:
time.sleep(0.1)
ButtonForward()
time.sleep(2)
ButtonStop()
ButtonStop()
except KeyboardInterrupt:
ButtonStop()
initiate()
GPIO.cleanup()