-
Notifications
You must be signed in to change notification settings - Fork 1
/
robot.cpp
208 lines (182 loc) · 5.99 KB
/
robot.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
//Robot.cpp
//Includes
#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include <chrono>
#include <string>
#include <stdexcept>
#include <jsoncpp/json/json.h>
#include "robot.h"
#include "util/log.h"
#include "util/udpsocket.h"
#include "util/watchdog.h"
#include "robot/robotInterface.h"
#include "robot/dummyRobot.h"
//Constructor
Robot::Robot() : Robot("127.0.0.1", "8353", "", NULL) {
}
Robot::Robot(const char *address, const char *port, const char *key, bool listen) : Robot(address, port, key, listen, NULL, "") {
}
Robot::Robot(const char *address, const char *port, const char *key, bool listen, const char *libFile) : Robot(address, port, key, listen, libFile, NULL) {
}
Robot::Robot(const char *address, const char *port, const char *key, bool listen, const char *libFile, const char *libOptions) : Server(address, port, key, listen),
watchdog(100, false), //5 packets
robotLibrary(NULL),
robotInterface(NULL),
axesSize(0),
buttonsSize(0),
axes(NULL),
buttons(NULL) {
//Set up robot robotInterface
if(libFile != NULL) {
loadRobotLibrary(libFile);
} else {
createInterface = &createRobot;
destroyInterface = &destroyRobot;
}
robotInterface = createInterface(libOptions);
//Set up output packet
out["frameNum"] = 0;
out["isClient"] = false;
out["isRobot"] = true;
out["ping"] = false;
out["key"] = key;
out["time"] = 0;
//Set up ping packet
ping["frameNum"] = 0;
ping["isClient"] = false;
ping["isRobot"] = true;
ping["ping"] = true;
ping["key"] = key;
ping["time"] = 0;
//Send initialization ping
sendPing();
}
//Destructor
Robot::~Robot() {
robotInterface->stop();
destroyInterface(robotInterface);
#ifndef _WIN32 //Linux/POSIX shared library destructor
if(robotLibrary != NULL) {
dlclose(robotLibrary);
}
#else //Windows shared library destructor
FreeLibrary((HMODULE)robotLibrary);
#endif
delete[] axes;
delete[] buttons;
}
//Functions
//Load library for robot I/O at runtime
void Robot::loadRobotLibrary(const char *filename) {
#ifndef _WIN32 //Linux/POSIX support
//Load library
robotLibrary = dlopen(filename, RTLD_LAZY);
if(!robotLibrary) {
Log::logf(Log::ERR, "Failed to load robot library: %s\n", dlerror());
throw std::runtime_error("failed to load robot library");
} else {
Log::logf(Log::INFO, "Loaded robot library: %s\n", filename);
}
//Set up robot constructor
createInterface = (RobotInterface* (*)(const char *))dlsym(robotLibrary, "createRobot");
if(!createInterface) {
Log::logf(Log::ERR, "Failed to load robot constructor: %s\n", dlerror());
throw std::runtime_error("failed to load robot constructor");
}
//Set up robot destructor
destroyInterface = (void (*)(RobotInterface*))dlsym(robotLibrary, "destroyRobot");
if(!destroyInterface) {
Log::logf(Log::ERR, "Failed to load robot destructor: %s\n", dlerror());
throw std::runtime_error("failed to load robot destructor");
}
#else //Windows support
//https://msdn.microsoft.com/en-us/library/ms810279.aspx
robotLibrary = (/*HMODULE*/ void*)LoadLibrary(filename);
if(!robotLibrary) {
Log::logf(Log::ERR, "Failed to load robot library: Win32 error %lu\n", GetLastError());
throw std::runtime_error("failed to load robot library");
} else {
Log::logf(Log::INFO, "Loaded robot library: %s\n", filename);
}
//Set up robot contructor
createInterface = (RobotInterface* (*)(const char *))GetProcAddress((HMODULE)robotLibrary, "createRobot");
if(!createInterface) {
Log::logf(Log::ERR, "Failed to load robot constructor: Win32 error %lu\n", GetLastError());
throw std::runtime_error("failed to load robot constructor");
}
//Set up robot destructor
destroyInterface = (void (*)(RobotInterface*))GetProcAddress((HMODULE)robotLibrary, "destroyRobot");
if(!destroyInterface) {
Log::logf(Log::ERR, "Failed to load robot destructor: Win32 error %lu\n", GetLastError());
throw std::runtime_error("failed to load robot destructor");
}
#endif
}
//Main robot loop
void Robot::run() {
//Check the watchdog timer
if(!watchdog.isAlive()) {
robotInterface->stop();
}
//Display last received data and send a ping once every ~500ms
//Don't display pings, just wait for the next loop
if(!keepalive.isAlive() && !in.get("ping", false).asBool()) {
if(watchdog.isAlive()) {
printData(in);
}
sendPing();
keepalive.feed();
}
//Get control data from server
buffer[0] = '\0';
unknownAddressLength = sizeof(unknownAddress);
if(s.readData((void *)buffer, sizeof(buffer), &unknownAddress, &unknownAddressLength) < 0 || buffer[0] == '\0') {
return;
} else {
reader.parse(buffer, in, false);
if(!validateKey(in)) {
//Ignore packets with invalid keys
return;
}
watchdog.feed();
}
//Handle pings
if(in.get("ping", false).asBool()) {
handlePing();
}
//Data recieved from client
if(in.get("isClient", false).asBool() && watchdog.isAlive()) {
if(in["axes"].size() > 0 && in["axes"].size() != axesSize) {
axesSize = in["axes"].size();
delete[] axes;
axes = new double[axesSize];
}
if(in["buttons"].size() > 0 && in["buttons"].size() != buttonsSize) {
buttonsSize = in["buttons"].size();
delete[] buttons;
buttons = new bool[buttonsSize];
}
for(unsigned int i = 0; i < axesSize; i++) {
axes[i] = in["axes"].get(i, 0.0).asDouble();
}
for(unsigned int i = 0; i < buttonsSize; i++) {
buttons[i] = in["buttons"].get(i, false).asBool();
}
robotInterface->run(axesSize, axes, buttonsSize, buttons);
}
}
//Ping the server
void Robot::sendPing() {
ping["time"] = (Json::Value::Int64)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
if(listening) {
sendPing(&clientAddress, clientAddressLength);
} else {
std::string pingJSON = writer.write(ping);
s.writeData((void *)pingJSON.c_str(), pingJSON.length());
}
ping["frameNum"] = ping.get("frameNum", 0).asUInt() + 1;
}