-
Notifications
You must be signed in to change notification settings - Fork 0
/
coppelia_flask.py
237 lines (199 loc) · 6.31 KB
/
coppelia_flask.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from flask import Blueprint, request, Response
import portpicker
import sim
from time import sleep
from PIL import Image
import io
import numpy as np
from random import shuffle
simulator = Blueprint('simulator', __name__, template_folder='templates')
cameras = []
cview = 0
client = None
playing = False
loading = [Image.open("static/loading_0.jpg"),Image.open("static/loading_1.jpg"),Image.open("static/loading_2.jpg"),Image.open("static/loading_3.jpg")]
shuffle(loading)
cLoading = 0
againIn = 0.1
def verboseResp(resp,func):
print("Client: ",client," - Port",PORT," - Function:", func," - ", resp)
def reset():
global cview, cameras, client, playing
cameras = []
cview = 0
client = None
playing = False
@simulator.route("/shuffleLoading")
def shuffleLoading():
global loading
shuffle(loading)
return "0"
@simulator.route("/startStopSim")
def startStopSim():
global PORT, client, cameras
if client is None:
# No client, then Start
PORT = int(request.args.get('port'))
print("waiting")
while portpicker.is_port_free(PORT):
sleep(5*againIn)
print(".")
client=sim.simxStart('127.0.0.1',PORT,True,True,5000,5)
verboseResp(client,"simxStart")
return str(client)
else:
# There is a client, then Stop
stop()
sleep(.5)
response = sim.simxSetIntegerSignal(client,'doClose',1,sim.simx_opmode_oneshot_wait)
verboseResp(response,"simxSetIntegerSignal doClose")
try:
finish()
except:
verboseResp("already finished?","finish")
reset()
if response == -1:
return "-1"
else:
return "0"
@simulator.route("/setCameraBuffer")
def setCameraBuffer():
global cameras, client
v = 0
cameras = []
while True:
[response,view] = sim.simxGetObjectHandle(client,"view_"+str(v),sim.simx_opmode_blocking)
verboseResp(response,"simxGetObjectHandle view"+str(v))
if response > 0:
break
cameras.append(view)
v += 1
sleep(0.3)
for view in cameras:
[resp2,res,img] = sim.simxGetVisionSensorImage(client,view,0,sim.simx_opmode_streaming)
verboseResp(resp2,"simxGetVisionSensorImage view"+str(view))
return str(-1*(response==1))
def getFrame():
global client, cLoading, loading, cameras, cview
if len(cameras)>0 and cview < len(cameras) and client is not None:
verboseResp(len(cameras),cview)
response,resolution,image=sim.simxGetVisionSensorImage(client,cameras[cview],0,sim.simx_opmode_buffer)
verboseResp(response,"simxGetVisionSensorImage")
if len(resolution)==0:
im = loading[cLoading]
else:
im = Image.frombuffer("RGB", (resolution[0],resolution[1]), np.array(image,dtype=np.uint8), "raw", "RGB", 0, 1)
else:
im = loading[cLoading]
imgByteArr = io.BytesIO()
im.save(imgByteArr, format="jpeg")
return imgByteArr.getvalue()
def generateVideoStream():
while True:
sleep(0.01)
frame = getFrame()
if frame == None :
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(frame) + b'\r\n')
@simulator.route("/changeView")
def changeView():
global cameras, cview, cLoading, client
if client == None:
cLoading = (cLoading+1)%len(loading)
else:
cview = (cview+1)%len(cameras)
return "0"
@simulator.route("/videoStream")
def videoStream():
# return the response generated along with the specific media
# type (mime type)
return Response(generateVideoStream(),
mimetype = "multipart/x-mixed-replace; boundary=frame")
@simulator.route("/findAPort")
def findAPort():
global PORT
PORT = portpicker.pick_unused_port()
return str(PORT)
@simulator.route("/playPause")
def playPause():
global client, playing
if client == None:
return "0"
if playing:
return pause()
else:
return play()
@simulator.route("/restart")
def restart():
ans = 0 + (stop() == "0")
sleep(1)
ans += (play() == "0")
return str(ans)
@simulator.route("/play")
def play():
global client, playing
if client == None:
return "-1"
response = sim.simxStartSimulation(client,sim.simx_opmode_blocking)
while response == 1:
verboseResp(response,"simxStartSimulation")
response = sim.simxStartSimulation(client,sim.simx_opmode_blocking)
sleep(againIn)
verboseResp(response,"simxStartSimulation")
playing = (response == 0)
return str(response)
@simulator.route("/pause")
def pause():
global client, playing
if client == None:
return "-1"
response = sim.simxPauseSimulation(client,sim.simx_opmode_blocking)
while response == 1:
verboseResp(response,"simxPauseSimulation")
response = sim.simxPauseSimulation(client,sim.simx_opmode_blocking)
sleep(againIn)
if response == 0:
playing = False
verboseResp(response,"simxPauseSimulation")
return str(response)
@simulator.route("/stop")
def stop():
global client, playing
if client == None:
return "-1"
response = sim.simxStopSimulation(client,sim.simx_opmode_blocking)
while response == 1:
verboseResp(response,"simxStopSimulation")
response = sim.simxStopSimulation(client,sim.simx_opmode_blocking)
sleep(againIn)
if response == 0:
playing = False
verboseResp(response,"simxStopSimulation")
return str(response)
@simulator.route("/finish")
def finish():
global client
if client == None:
return "-1"
response = sim.simxFinish(client)
verboseResp(response,"simxFinish")
return str(response)
@simulator.route("/loadScene")
def loadScene():
global client
if client is None:
return "-1"
scene = request.args.get('scene')
stop()
response=sim.simxLoadScene(client,scene,0xFF,sim.simx_opmode_blocking)
while response == 1:
verboseResp(response,"simxLoadScene "+scene)
response = sim.simxLoadScene(client,scene,0xFF,sim.simx_opmode_blocking)
sleep(againIn)
verboseResp(response,"simxLoadScene "+scene)
if not response:
while setCameraBuffer()=="-1":
sleep(againIn)
return play()
return str(response)