-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_lf.py
107 lines (80 loc) · 2.08 KB
/
master_lf.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
import socket
import sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(2, GPIO.OUT)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
def right():
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.LOW)
GPIO.output(7, GPIO.HIGH)
GPIO.output(17, GPIO.LOW)
#time.sleep(0.5)
def left():
GPIO.output(3, GPIO.HIGH)
GPIO.output(2, GPIO.LOW)
GPIO.output(17, GPIO.HIGH)
GPIO.output(7, GPIO.LOW)
#time.sleep(0.5)
def back():
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.LOW)
GPIO.output(17, GPIO.HIGH)
GPIO.output(7, GPIO.LOW)
#time.sleep(0.5)
def forward():
GPIO.output(3, GPIO.HIGH)
GPIO.output(2, GPIO.LOW)
GPIO.output(7, GPIO.HIGH)
GPIO.output(17, GPIO.LOW)
#time.sleep(0.5)
def create_socket():
try:
global host
global s
global port
host=""
port=9999
s=socket.socket()
except socket.error as mag:
print("socket creation error",str(mag))
def bind_socket():
try:
global host
global s
global port
print("binding the socket")
s.bind((host,port))
s.listen(5)
except socket.error as mag:
print("socket binding error ",str(mag)," retrying")
bind_socket()
def accept():
conn,adress=s.accept()
print("ip : ",adress[0]," port : ",adress[1])
send_command(conn)
conn.close()
def send_command(conn):
while True:
if (GPIO.input(5) == True and GPIO.input(6) == False):
conn.send(str.encode("left"))
left()
elif(GPIO.input(5) == False and GPIO.input(6) == True):
conn.send(str.encode("right"))
right()
elif (GPIO.input(5) == True and GPIO.input(6) == True):
conn.send(str.encode("forward"))
forward()
elif (GPIO.input(5) == False and GPIO.input(6) == False):
conn.send(str.encode("back"))
back()
break
def main():
create_socket()
bind_socket()
accept()
main()