-
Notifications
You must be signed in to change notification settings - Fork 0
/
rov_API.py
189 lines (163 loc) · 3.97 KB
/
rov_API.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Communicate from server to raspberry pi"""
import socket
import sys
import queue
import serial
import syslog
import time
import math
import threading
'''from TopsidesGlobals import GLOBALS
#import topsidesComms
# Change IP addresses for a production or development environment
if ((len(sys.argv) > 1) and (sys.argv[1] == "--dev")):
ipSend = GLOBALS['ipSend-5-dev']
ipHost = GLOBALS['ipHost-5-dev']
else:
ipSend = GLOBALS['ipSend-4']
ipHost = GLOBALS['ipHost']
portSend = GLOBALS['portSend-5']
portHost = GLOBALS['portHost']
received = queue.Queue()
# Try opening a socket for communication
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error:
print("Failed To Create Socket")
sys.exit()
except Exception as e:
print("failed")
# Bind the ip and port of topsides to the socket and loop coms
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ipHost, portHost))
# Queue to hold send commands to be read by simulator
simulator = queue.Queue()'''
# This function sends data to the ROV
def sendData(inputData):
global s
#s.sendto(inputData.encode('utf-8'), ("192.168.88.5", portSend))
# TESTING
def sendDataB(inputData):
global s
#s.sendto(inputData.encode('utf-8'), (ipSend, portSend))
# This function is constantly trying to receive data from the ROV
def receiveData(flag):
global s
while True:
outputData, addr = s.recvfrom(1024)
outputData = outputData.decode("utf-8")
if (outputData == "exit"):
break
received.put(outputData)
if flag.is_set():
break
def putMessage(msg):
sendData(msg)
simulator.put(msg, timeout=0.005)
def runThruster(tData):
for control in tData:
val = tData[control]
putMessage("runThruster.py " + str(GLOBALS["thrusterPorts"][control]) + " " + str(val))
print("good")
# TODO:
# sway
# yaw
# pitch
# heave
# roll
def sway(power):
good = False
try:
float(power)
good = True
except ValueError:
good = False
return False
tData = {
"fore-port-horz": power,
"fore-star-horz": -power,
"aft-port-horz": -power,
"aft-star-horz": power,
}
print("Send command")
runThruster(tData)
def yaw(power):
good = False
try:
float(power)
good = True
except ValueError:
good = False
return False
tData = {
"fore-port-horz": power,
"fore-star-horz": power,
"aft-port-horz": power,
"aft-star-horz": -power,
}
print("Send command")
runThruster(tData)
def heave(power):
good = False
try:
float(power)
good = True
except ValueError:
good = False
return False
tData = {
"fore-port-vert": power,
"fore-star-vert": power,
"aft-port-vert": -power,
"aft-star-vert": -power,
}
print("Send command")
runThruster(tData)
def pitch(power):
good = False
try:
float(power)
good = True
except ValueError:
good = False
return False
tData = {
"fore-port-vert": power,
"fore-star-vert": power,
"aft-port-vert": power,
"aft-star-vert": power,
}
print("Send command")
runThruster(tData)
def roll(power):
good = False
try:
float(power)
good = True
except ValueError:
good = False
return False
tData = {
"fore-port-vert": power,
"fore-star-vert": -power,
"aft-port-vert": -power,
"aft-star-vert": power,
}
print("Send command")
runThruster(tData)
def surge(power):
good = False
try:
float(power)
good = True
except ValueError:
good = False
return False
tData = {
"fore-port-horz": power,
"fore-star-horz": -power,
"aft-port-horz": -power,
"aft-star-horz": power,
}
print("Send command")
runThruster(tData)