-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.py
161 lines (116 loc) · 3.36 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
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
from time import sleep
import RPi.GPIO as GPIO
class Move:
def __init__(self,in1=5,in2=3,in3=31,in4=10,left_pwm_pin=15,right_pwm_pin=13,right_frequency=100,left_frequency=100,duty_cycle=40):
'''
Intializing variables:
int1 , in2 , in3 , in4 are 4 GPIO pins used as INPUT to l293d motor driver .
left_pwm_pin , right_pwm_pin are pins for Pulse Width Modulation attached to enable pins of l293d motor driver
'''
#setting up mode of board
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
self.in1 = in1
self.in2 = in2
self.in3= in3
self.in4 = in4
self.left_pwm_pin = left_pwm_pin
self.right_pwm_pin = right_pwm_pin
GPIO.setup([in1,in2,in3,in4],GPIO.OUT,initial=False)
GPIO.setup([self.left_pwm_pin,self.right_pwm_pin],GPIO.OUT,initial=True)
self.right_frequency = right_frequency
self.left_frequency = left_frequency
self.left_pwm = GPIO.PWM(self.left_pwm_pin,self.left_frequency)
self.right_pwm = GPIO.PWM(self.right_pwm_pin,self.right_frequency)
self.duty_cycle = duty_cycle
# Flags
self.ON = 1
self.OFF = 0
self.right_pwm_start = self.OFF
self.left_pwm_start = self.OFF
def moveBackward(self,timeFrame = -1):
GPIO.output([self.in2,self.in4],True)
if timeFrame != -1:
sleep(timeFrame)
self.stop()
def moveForward(self,timeFrame=-1):
GPIO.output([self.in1,self.in3],True)
if timeFrame != -1:
sleep(timeFrame)
self.stop()
def sharpRight(self,timeFrame=-1):
GPIO.output(self.in1,True)
if timeFrame != -1:
sleep(timeFrame)
self.stop()
def sharpLeft(self,timeFrame=-1):
GPIO.output(self.in3,True)
if timeFrame != -1:
sleep(timeFrame)
self.stop()
def swiftForwardRight(self,timeFrame = -1):
self.moveForward()
self.right_pwm.start(self.duty_cycle)
self.right_pwm_start = self.ON
if timeFrame != -1:
sleep(timeFrame)
stop()
def swiftForwardLeft(self,timeFrame = -1):
self.moveForward()
self.left_pwm.start(self.duty_cycle)
self.left_pwm_start = self.ON
if timeFrame !=-1:
sleep(timeFrame)
self.stop()
def swiftBackwardRight(self,timeFrame = -1):
self.moveBackward()
self.right_pwm.start(self.duty_cycle)
self.right_pwm_start = self.ON
if timeFrame !=-1:
sleep(timeFrame)
self.stop()
def swiftBackwardLeft(self,timeFrame = -1):
self.moveBackward()
self.left_pwm.start(self.duty_cycle)
self.left_pwm_start = self.ON
if timeFrame !=-1:
sleep(timeFrame)
self.stop()
def stop(self):
#stop logic
GPIO.output([self.in1,self.in2,self.in3,self.in4],False)
if (self.right_pwm_start):
#self.right_pwm.stop()
#GPIO.output(self.right_pwm_pin,True)
self.right_pwm.ChangeDutyCycle(100)
self.right_pwm_start = self.OFF
if (self.left_pwm_start):
#self.left_pwm.stop()
#GPIO.output(self.left_pwm_pin,)
self.right_pwm.ChangeDutyCycle(100)
print "left pwm 100"
self.left_pwm_start = self.OFF
obj = Move()
while(True):
ch = raw_input("Enter text: ")
if ch == 's':
obj.stop()
elif ch == 'w':
obj.moveForward()
elif ch == 'x':
obj.moveBackward()
elif ch == 'a':
obj.sharpLeft()
elif ch == 'd':
obj.sharpRight()
elif ch == 'e':
obj.swiftForwardRight()
elif ch == 'q':
obj.swiftForwardLeft()
elif ch == 'z':
obj.swiftBackwardLeft()
elif ch == 'c':
obj.swiftBackwardLeft()
else:
obj.stop()
exit()