This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
CommandServer.cs
94 lines (86 loc) · 2.77 KB
/
CommandServer.cs
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
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using SocketIO;
using UnityStandardAssets.Vehicles.Car;
using System;
using System.Security.AccessControl;
public class CommandServer : MonoBehaviour
{
public CarRemoteControl CarRemoteControl;
public Camera FrontFacingCamera;
private SocketIOComponent _socket;
private CarController _carController;
// Use this for initialization
void Start()
{
_socket = GameObject.Find("SocketIO").GetComponent<SocketIOComponent>();
_socket.On("open", OnOpen);
_socket.On("steer", OnSteer);
_socket.On("manual", onManual);
_carController = CarRemoteControl.GetComponent<CarController>();
}
// Update is called once per frame
void Update()
{
}
void OnOpen(SocketIOEvent obj)
{
Debug.Log("Connection Open");
EmitTelemetry(obj);
}
//
void onManual(SocketIOEvent obj)
{
EmitTelemetry (obj);
}
void OnSteer(SocketIOEvent obj)
{
JSONObject jsonObject = obj.data;
// print(float.Parse(jsonObject.GetField("steering_angle").str));
CarRemoteControl.SteeringAngle = float.Parse(jsonObject.GetField("steering_angle").str);
CarRemoteControl.Acceleration = float.Parse(jsonObject.GetField("throttle").str);
EmitTelemetry(obj);
}
void EmitTelemetry(SocketIOEvent obj)
{
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
print("Attempting to Send...");
// send only if it's not being manually driven
if ((Input.GetKey(KeyCode.W)) || (Input.GetKey(KeyCode.S))) {
_socket.Emit("telemetry", new JSONObject());
}
else {
// Collect Data from the Car
Dictionary<string, string> data = new Dictionary<string, string>();
data["steering_angle"] = _carController.CurrentSteerAngle.ToString("N4");
data["throttle"] = _carController.AccelInput.ToString("N4");
data["speed"] = _carController.CurrentSpeed.ToString("N4");
data["image"] = Convert.ToBase64String(CameraHelper.CaptureFrame(FrontFacingCamera));
_socket.Emit("telemetry", new JSONObject(data));
}
});
// UnityMainThreadDispatcher.Instance().Enqueue(() =>
// {
//
//
//
// // send only if it's not being manually driven
// if ((Input.GetKey(KeyCode.W)) || (Input.GetKey(KeyCode.S))) {
// _socket.Emit("telemetry", new JSONObject());
// }
// else {
// // Collect Data from the Car
// Dictionary<string, string> data = new Dictionary<string, string>();
// data["steering_angle"] = _carController.CurrentSteerAngle.ToString("N4");
// data["throttle"] = _carController.AccelInput.ToString("N4");
// data["speed"] = _carController.CurrentSpeed.ToString("N4");
// data["image"] = Convert.ToBase64String(CameraHelper.CaptureFrame(FrontFacingCamera));
// _socket.Emit("telemetry", new JSONObject(data));
// }
//
////
// });
}
}