-
Notifications
You must be signed in to change notification settings - Fork 4
/
sensorCarController.py
83 lines (60 loc) · 2 KB
/
sensorCarController.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
"""
Combines the network with a socketServer. Received data is evaluated in
the network and emitted back via the socketServer
"""
import socketio
from flask import Flask
import eventlet
import numpy as np
class SensorCarController():
"""
Combines the network with the socketServer. Received data is evaluated
in the network and emitted back via the socketServer
"""
network = None
def __init__(self, network):
"""
Initiate sensorCarController with a instance of network which is
used for evaluating sensorInformation
"""
self.network = network
self.sio = socketio.Server()
# Define events
@self.sio.on('connect')
def connect(sid, environ):
print("Client connected: " + str(sid))
@self.sio.on('disconnect')
def disconnect(sid):
print("Client disconnected: " + str(sid))
@self.sio.on('evaluate')
def evaluate(sid, data):
print("Received package for evaluation")
self.receive(data)
def startServer(self):
"""
Starts the socketserver which listens for specific events
"""
wsgiApp = Flask(__name__)
app = socketio.Middleware(self.sio, wsgiApp)
try:
eventlet.wsgi.server(eventlet.listen(("", 4567)), app)
print("SocketServer started")
except KeyboardInterrupt:
print("SocketServer stopped")
def receive(self, data):
"""
Called in the evaluate event. data is the JSON object received from
the simulation. Has to be converted into an array and them evaluated
in the net. In the end the evaluated data is emitted
"""
# Convert string of json to float list
# inputVector = np.array([float(data[key]) for key in data], dtype=np.float32)
inputVector = np.array([[float(data[key]) for key in data]], dtype=np.float32)
# inputVector.ex
print("inputV", inputVector)
# evaluate in net
outputVector = self.network.evaluate(inputVector)[0][0]
# todo make general
print(inputVector, "->", outputVector)
# returne evaluated values to simulation
self.sio.emit('steer', data={'steering_angle': str(25 * outputVector)}, skip_sid=True)