Skip to content

Instantly share code, notes, and snippets.

@yoneken
Last active April 6, 2022 13:34
Show Gist options
  • Save yoneken/03561a831344c08eda9a2f37a8d56bcb to your computer and use it in GitHub Desktop.
Save yoneken/03561a831344c08eda9a2f37a8d56bcb to your computer and use it in GitHub Desktop.
Tipron scripts
from urllib.request import Request, urlopen
import json
from socket import socket, AF_INET, SOCK_DGRAM
import cv2
import numpy as np
SELF_IP = '192.168.0.102'
TIPRON_IP = '192.168.0.106'
RECEIVE_PORT = 8082
# Kick camera stream
req = Request(url="http://" + TIPRON_IP + ":8080/camera_control/", data=json.dumps({"eventType":"startCameraStream","ip":SELF_IP,"port":RECEIVE_PORT}).encode(), headers={}, method='POST')
with urlopen(req) as res:
body = res.read().decode()
print(body)
# Receive camera stream and show
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', RECEIVE_PORT))
while True:
msg, address = s.recvfrom(80000)
a = msg.find(b'\xff\xd8')
b = msg.find(b'\xff\xd9')
if a != -1 and b != -1:
jpg = msg[a:b+2]
i = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
cv2.imshow('cam', i)
if cv2.waitKey(1) == 27:
exit(0)
#print(f"message: {msg}\nfrom: {address}")
s.close()
# Stop camera stream
req = Request(url="http://" + TIPRON_IP + ":8080/camera_control/", data=json.dumps({"eventType":"stopCameraStream","ip":SELF_IP,"port":RECEIVE_PORT}).encode(), headers={}, method='POST')
with urlopen(req) as res:
body = res.read().decode()
print(body)
from socket import socket, AF_INET, SOCK_DGRAM
HOST = ''
PORT = 9999
ADDRESS = "192.168.0.106"
s = socket(AF_INET, SOCK_DGRAM)
#msg = '{"clearQueue":false,"commandId":1,"commandName":"moveFront","commandValue":"200"}' # Fast go ahead
#msg = '{"clearQueue":false,"commandId":1,"commandName":"moveFront","commandValue":"-50"}' # Slow go back
#msg = '{"clearQueue":false,"commandId":1,"commandName":"turnLeft","commandValue":"-50"}' # Turn right
msg = '{"clearQueue":false,"commandId":1,"commandName":"turnLeft","commandValue":"100"}' # Turn left
s.sendto(msg.encode(), (ADDRESS, PORT))
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment