-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi.py
158 lines (124 loc) · 3.12 KB
/
rpi.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
#Importing Libraries
import cv2
import socket
import freenect
import RPi.GPIO as GPIO
#Function to make a soft left turn
def forward(server):
#Setting GPIO pins
#Motor 1
GPIO.output(3, GPIO.HIGH)
GPIO.output(5,GPIO.LOW)
#Motor2
GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
#Running the command for 1 second
time.sleep(1)
#Sending Feedback
server.send('done')
#Function to make a soft left turn
def soft_left(server):
#Setting GPIO pins
#Motor 1
GPIO.output(3, GPIO.LOW)
GPIO.output(5,GPIO.LOW)
#Motor2
GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
#Running the command for 1 second
time.sleep(1)
#Sending Feedback
server.send('done')
#Function to make a soft right turn
def soft_right(server):
#Setting GPIO pins
#Motor 1
GPIO.output(3, GPIO.HIGH)
GPIO.output(5,GPIO.LOW)
#Motor2
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
#Running the command for 1 second
time.sleep(1)
#Sending Feedback
server.send('done')
#Function to make a left turn
def left(server):
#Setting GPIO pins
#Motor 1
GPIO.output(3, GPIO.LOW)
GPIO.output(5,GPIO.HIGH)
#Motor2
GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
#Running the command for 1 second
time.sleep(1)
#Sending Feedback
server.send('done')
#Function to make a right turn
def right(server):
#Setting GPIO pins
#Motor 1
GPIO.output(3, GPIO.HIGH)
GPIO.output(5,GPIO.LOW)
#Motor2
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
#Running the command for 1 second
time.sleep(1)
#Sending Feedback
server.send('done')
#Function to stop
def stop(server):
#Setting GPIO pins
#Motor 1
GPIO.output(3, GPIO.LOW)
GPIO.output(5,GPIO.LOW)
#Motor2
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
#Running the command for 1 second
time.sleep(1)
#Sending Feedback
server.send('done')
#Function to send image
def send_img(type):
if type == 'image':
frame = freenect.sync_get_video()[0]
elif type == 'depth':
frame = freenect.sync_get_depth()[0]
#Saving Image
cv2.imwrite('send.jpg', frame)
file_name = "test.jpg"
# Send The File name to the recieve function
server.send(file_name.encode())
# open The Selected File
file = open(file_path,'rb')
for i in file:
server.send(i) # send the data to the reciever
file.close()
server.close()
#Main function
def main():
# Intialising TCP server AF_NET is used For TCP Network
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Getting Host Name
host = server.gethostname()
#Using any random port number
#Port Number used for both client and server should be same
port = 12345
#Connecting to server using host address and port
server.connect((host, port))
while True:
msg = server.recv(1024)
#If recieved msg is image
if msg == 'image':
send_img('image', server)
#If recieved msg is depth
elif msg == 'depth':
send_img('depth', server)
#If recieved msg is forward
elif msg == 'forward':
forward(server)
if __name__ == "__main__":
main()