forked from burrito82/spvr.control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
223 lines (194 loc) · 6.49 KB
/
main.cpp
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
/*
* Copyright (c) 2016
* Somebody
*/
char const copyright[] =
"Copyright (c) 2016\n\tSomebody. All rights reserved.\n\n";
#include "driver/ControlInterface.h"
#include "driver/glm/gtx/quaternion.hpp"
#include "json/json.h"
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <algorithm>
#include <array>
#include <chrono>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <thread>
#include <vector>
static spvr::ControlInterface *g_pControlInterface = nullptr;
struct SpvrPacket
{
float f[4];
std::int32_t m_iCounter;
};
union SpvrPacketByteHack
{
char c[sizeof(SpvrPacket)];
SpvrPacket data;
};
void NtoH(SpvrPacketByteHack &packet)
{
// not the way to to... UB!
for (int iByte = 0; iByte < sizeof(packet.c); iByte += 4)
{
std::swap(packet.c[iByte], packet.c[iByte + 3]);
std::swap(packet.c[iByte + 1], packet.c[iByte + 2]);
}
}
void ProcessPacket(SpvrPacket const &packet)
{
std::cout << "received: {"
<< packet.f[0] << ", \t"
<< packet.f[1] << ", \t"
<< packet.f[2] << ", \t"
<< packet.f[3] << "}" << " \r";//<< std::endl;
glm::quat qRotation{packet.f[0], packet.f[1], packet.f[2], packet.f[3]};
static glm::quat qPreviousRotation = qRotation;
float const fAlpha = 0.90f;
qRotation.w = fAlpha * qRotation.w + (1.0f - fAlpha) * qPreviousRotation.w;
qRotation.x = fAlpha * qRotation.x + (1.0f - fAlpha) * qPreviousRotation.x;
qRotation.y = fAlpha * qRotation.y + (1.0f - fAlpha) * qPreviousRotation.y;
qRotation.z = fAlpha * qRotation.z + (1.0f - fAlpha) * qPreviousRotation.z;
qRotation = glm::normalize(qRotation);
qPreviousRotation = qRotation;
g_pControlInterface->SetRotation(qRotation);
}
void ReceiveTcp()
{
while (true)
{
try
{
using boost::asio::ip::tcp;
boost::asio::io_service oIoService{};
tcp::acceptor oAcceptor{oIoService, tcp::endpoint{tcp::v4(), 4321}};
tcp::socket oSocket{oIoService};
oAcceptor.accept(oSocket);
SpvrPacketByteHack oPacket;
std::memset(oPacket.c, 0, sizeof(oPacket.c));
boost::system::error_code oError{};
while (oError != boost::asio::error::eof)
{
auto uBytesRead = oSocket.read_some(boost::asio::buffer(oPacket.c), oError);
NtoH(oPacket);
ProcessPacket(oPacket.data);
}
}
catch (...)
{
std::cout << "some error occurred..." << std::endl;
}
}
}
void ReceiveUdp()
{
std::int32_t m_iLastMsg = -1;
while (true)
{
try
{
using boost::asio::ip::udp;
boost::asio::io_service oIoService{};
udp::endpoint oEndpoint{udp::v4(), 4321};
udp::socket oSocket{oIoService, oEndpoint};
SpvrPacketByteHack oPacket;
std::memset(oPacket.c, 0, sizeof(oPacket.c));
boost::system::error_code oError{};
while (oError != boost::asio::error::eof)
{
//auto uBytesRead = oSocket.read_some(boost::asio::buffer(aBuffer.c), oError);
//auto uBytesRead = oSocket.receive_from(boost::asio::buffer(aBuffer.c), oEndpoint, 0, oError);
auto uBytesRead = oSocket.receive(boost::asio::buffer(oPacket.c));
NtoH(oPacket);
if (oPacket.data.m_iCounter > m_iLastMsg || oPacket.data.m_iCounter < 1000)
{
m_iLastMsg = oPacket.data.m_iCounter;
}
else
{
continue;
}
ProcessPacket(oPacket.data);
}
}
catch (...)
{
std::cout << "some error occurred..." << std::endl;
}
}
}
int main(int argc, char const *argv[])
{
std::vector<std::string> vecArgs(argv, argv + argc);
std::string strJsonConfigFile = "config.json";
if (vecArgs.size() > 1)
{
strJsonConfigFile = vecArgs[1];
}
std::cout << "=== SmartVR Control ===" << std::endl;
std::cout << copyright << std::endl;
spvr::ControlInterface oControlInterface{};
// GLOBAL VARIABLE...
g_pControlInterface = &oControlInterface;
{
std::ifstream oJsonConfigFile{strJsonConfigFile, std::ifstream::in};
if (oJsonConfigFile)
{
std::cout << "[ ] reading configuration from " << strJsonConfigFile << std::endl;
Json::Value jsonRoot{};
oJsonConfigFile >> jsonRoot;
auto const jsonDistortionK0 = jsonRoot["distortion-k0"];
auto const jsonDistortionK1 = jsonRoot["distortion-k1"];
if (!jsonDistortionK0.empty() && !jsonDistortionK1.empty())
{
auto const k0 = jsonDistortionK0.asFloat();
auto const k1 = jsonDistortionK1.asFloat();
oControlInterface.SetDistortionCoefficients(k0, k1);
}
if (!jsonRoot["distortion-scale"].empty())
{
oControlInterface.SetDistortionScale(jsonRoot["distortion-scale"].asFloat());
}
}
}
float k0, k1;
oControlInterface.GetDistortionCoefficients(k0, k1);
std::cout << "[.] Distortion coefficients: " << k0 << ", " << k1 << std::endl;
std::cout << "[.] Distortion scale: " << oControlInterface.GetDistortionScale() << std::endl;
//auto oThread = std::thread{ReceiveTcp};
auto oThread = std::thread{ReceiveUdp};
std::string strLogLine{};
std::cout << "[ ] waiting for driver... ";
std::cout.flush();
do
{
std::this_thread::sleep_for(std::chrono::seconds{1});
strLogLine = oControlInterface.PullLog();
} while (strLogLine.empty());
std::cout << "connected!" << std::endl;
std::ofstream oLogFileHandle{"latest.log", std::ofstream::out};
int iEmptyMsgCounter = 0;
while (iEmptyMsgCounter < 10000)
{
if (strLogLine.empty())
{
++iEmptyMsgCounter;
}
else
{
std::cout << strLogLine;
oLogFileHandle << strLogLine;
std::cout.flush();
iEmptyMsgCounter = 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds{5});
strLogLine = oControlInterface.PullLog();
}
std::cout << "[ ] No new message, stopped pulling! (driver might still be running!)" << std::endl;
std::cin.ignore();
return 0;
}