-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROBOT_PI.py
60 lines (46 loc) · 1.29 KB
/
ROBOT_PI.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
import cv2
import io
import socket
import struct
import time
import pickle
import zlib
import time
import RPi.GPIO as GPIO
# Declare the GPIO settings
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('10.18.150.189', 9999))
connection = client_socket.makefile('wb')
cam = cv2.VideoCapture(0)
cam.set(3, 240) # width of frames
cam.set(4, 240) # height of frames
img_counter = 0
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
while True:
ret, frame = cam.read()
result, frame = cv2.imencode('Picture.png', frame, encode_param) # == cv2.imwrite()
data = pickle.dumps(frame, 0)
size = len(data)
#print("{}: {}".format(img_counter, size))
client_socket.sendall(struct.pack(">L", size) + data)
img_counter += 1
prob_blocked = client_socket.recv(1024) # receive response
prob_blocked = struct.unpack('<1f', prob_blocked)
# Turn right
if prob_blocked[0] > 0.5:
GPIO.output(17, GPIO.HIGH)
GPIO.output(27, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
time.sleep(1)
# Go forward
GPIO.output(17, GPIO.HIGH)
GPIO.output(27, GPIO.LOW)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.LOW)
cam.release()