-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
157 lines (135 loc) · 3.42 KB
/
remote.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
import serial
import RPi.GPIO as GPIO
import time
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
def isNumeric(s):
try:
int(s)
return True
except ValueError:
return False
def initSensor():
_ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)
return _ser
def initMotor():
pwm0 = 18
pwm1 = 13
ain1 = 2
ain2 = 3
bin1 = 5
bin2 = 6
GPIO.setmode(GPIO.BCM)
GPIO.setup(pwm0, GPIO.OUT)
GPIO.setup(pwm1, GPIO.OUT)
GPIO.setup(ain1, GPIO.OUT)
GPIO.setup(ain2, GPIO.OUT)
GPIO.setup(bin1, GPIO.OUT)
GPIO.setup(bin2, GPIO.OUT)
GPIO.output(ain1, False)
GPIO.output(ain2, True)
GPIO.output(bin1, True)
GPIO.output(bin2, False)
p0 = GPIO.PWM(pwm0, 50)
p0.start(50)
p0.ChangeDutyCycle(0)
p0.ChangeFrequency(50)
p1 = GPIO.PWM(pwm1, 50)
p1.start(50)
p1.ChangeDutyCycle(0)
p1.ChangeFrequency(50)
return p0,p1
def setMotor(lr, cmd):
FWD1 = False if lr=="r" else True
FWD2 = True if lr=="r" else False
in1 = 2 if lr=="r" else 5
in2 = 3 if lr=="r" else 6
if cmd == 'F':
GPIO.output(in1, FWD1)
GPIO.output(in2, FWD2)
elif cmd == 'R':
GPIO.output(in1, FWD2)
GPIO.output(in2, FWD1)
elif cmd == 'S':
GPIO.output(in1, False)
GPIO.output(in2, False)
def setMotorSpeed(lr, speed):
global ml
global mr
if isNumeric(speed):
speed = speed if speed<100 else 100
speed = speed if speed>0 else 0
if lr=="r":
mr.ChangeDutyCycle(speed)
elif lr=="l":
ml.ChangeDutyCycle(speed)
def pt_right():
setMotorSpeed("l",90);
setMotorSpeed("r",90);
setMotor("l",'F')
setMotor("r",'R')
time.sleep(0.2)
setMotor("l",'S')
setMotor("r",'S')
def pt_left():
setMotorSpeed("l",90);
setMotorSpeed("r",90);
setMotor("l",'R')
setMotor("r",'F')
time.sleep(0.2)
setMotor("l",'S')
setMotor("r",'S')
def straight_a_bit():
setMotorSpeed("l",90);
setMotorSpeed("r",90);
setMotor("l",'F')
setMotor("r",'F')
time.sleep(0.2)
setMotor("l",'S')
setMotor("r",'S')
def bstraight_a_bit():
setMotorSpeed("l",90);
setMotorSpeed("r",90);
setMotor("l",'R')
setMotor("r",'R')
time.sleep(0.2)
setMotor("l",'S')
setMotor("r",'S')
mr,ml = initMotor()
ser = initSensor()
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("<html><body><h1>hi!</h1></body></html>")
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
if "rt" in post_data:
pt_right()
elif "lt" in post_data:
pt_left()
elif "ff" in post_data:
straight_a_bit()
elif "rr" in post_data:
bstraight_a_bit()
print "POST request"
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()