forked from iskinmike/test_robot_module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_robot_module.cpp
More file actions
372 lines (323 loc) · 9.26 KB
/
test_robot_module.cpp
File metadata and controls
372 lines (323 loc) · 9.26 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <stdint.h>
#include <unistd.h>
#include <cstdarg>
#include <cstddef>
#endif
#include "module.h"
#include "robot_module.h"
#include "test_robot_module.h"
/* GLOBALS CONFIG */
#define IID "RCT.Test_robot_module_v107"
const unsigned int COUNT_ROBOTS = 99;
const unsigned int COUNT_FUNCTIONS = 7;
const unsigned int COUNT_AXIS = 3;
#define ADD_ROBOT_AXIS(AXIS_NAME, UPPER_VALUE, LOWER_VALUE) \
robot_axis[axis_id] = new AxisData; \
robot_axis[axis_id]->axis_index = axis_id + 1; \
robot_axis[axis_id]->upper_value = UPPER_VALUE; \
robot_axis[axis_id]->lower_value = LOWER_VALUE; \
robot_axis[axis_id]->name = AXIS_NAME; \
++axis_id;
TestRobotModule::TestRobotModule() {
#if MODULE_API_VERSION > 000
mi = new ModuleInfo;
mi->uid = IID;
mi->mode = ModuleInfo::Modes::PROD;
mi->version = BUILD_NUMBER;
mi->digest = NULL;
#endif
{
robot_functions = new FunctionData *[COUNT_FUNCTIONS];
system_value function_id = 0;
FunctionData::ParamTypes *pt;
robot_functions[function_id] =
new FunctionData(function_id + 1, 0, NULL, "none");
function_id++;
pt = new FunctionData::ParamTypes[1];
pt[0] = FunctionData::ParamTypes::FLOAT;
robot_functions[function_id] =
new FunctionData(function_id + 1, 1, pt, "do_something");
function_id++;
pt = new FunctionData::ParamTypes[1];
pt[0] = FunctionData::ParamTypes::FLOAT;
robot_functions[function_id] =
new FunctionData(function_id + 1, 1, pt, "get_some_value");
function_id++;
robot_functions[function_id] =
new FunctionData(function_id + 1, 0, NULL, "throw_exception");
function_id++;
pt = new FunctionData::ParamTypes[2];
pt[0] = FunctionData::ParamTypes::STRING;
pt[1] = FunctionData::ParamTypes::FLOAT;
robot_functions[function_id] =
new FunctionData(function_id + 1, 2, pt, "print");
function_id++;
pt = new FunctionData::ParamTypes[1];
pt[0] = FunctionData::ParamTypes::FLOAT;
robot_functions[function_id] =
new FunctionData(function_id + 1, 1, pt, "throw_value");
function_id++;
pt = new FunctionData::ParamTypes[1];
pt[0] = FunctionData::ParamTypes::STRING;
robot_functions[function_id] =
new FunctionData(function_id + 1, 1, pt, "debug");
function_id++;
}
{
robot_axis = new AxisData *[COUNT_AXIS];
system_value axis_id = 0;
ADD_ROBOT_AXIS("X", 100, -100)
ADD_ROBOT_AXIS("Y", 1, 0)
ADD_ROBOT_AXIS("Z", 100, 0)
}
}
#if MODULE_API_VERSION > 000
const struct ModuleInfo &TestRobotModule::getModuleInfo() { return *mi; }
#else
const char *TestRobotModule::getUID() { return IID; }
#endif
void TestRobotModule::prepare(colorPrintfModule_t *colorPrintf_p,
colorPrintfModuleVA_t *colorPrintfVA_p) {
this->colorPrintf_p = colorPrintfVA_p;
}
FunctionData **TestRobotModule::getFunctions(unsigned int *count_functions) {
(*count_functions) = COUNT_FUNCTIONS;
return robot_functions;
}
AxisData **TestRobotModule::getAxis(unsigned int *count_axis) {
(*count_axis) = COUNT_AXIS;
return robot_axis;
}
void *TestRobotModule::writePC(unsigned int *buffer_length) {
(*buffer_length) = 0;
return NULL;
}
int TestRobotModule::init() {
for (unsigned int i = 0; i < COUNT_ROBOTS; ++i) {
aviable_connections.push_back(new TestRobot(i + 1));
}
return 0;
}
#if MODULE_API_VERSION > 100
int TestRobotModule::readPC(int pc_index, void *buffer, unsigned int buffer_length) {
return 0;
}
int TestRobotModule::startProgram(int run_index, int pc_index) {
return 0;
}
AviableRobotsResult *TestRobotModule::getAviableRobots(int run_index) {
std::vector<TestRobot*> aviable_robots;
for (auto i = aviable_connections.begin();
i != aviable_connections.end(); ++i) {
if ((*i)->isAviable) {
aviable_robots.push_back(*i);
}
}
unsigned int count_robots = aviable_robots.size();
if (!count_robots) {
return NULL;
}
Robot **robots = new Robot*[count_robots];
for (unsigned int i = 0; i < count_robots; ++i) {
robots[i] = aviable_robots[i];
}
return new AviableRobotsResult(robots, count_robots);
}
Robot *TestRobotModule::robotRequire(int run_index, Robot *robot) {
for (auto i = aviable_connections.begin();
i != aviable_connections.end(); ++i) {
if ((*i)->isAviable) {
if ((!robot) || (robot == (*i))) {
(*i)->isAviable = false;
return (*i);
}
}
}
return NULL;
}
#else
int TestRobotModule::startProgram(int run_index) { return 0; }
Robot *TestRobotModule::robotRequire() {
for (auto i = aviable_connections.begin();
i != aviable_connections.end(); ++i) {
if ((*i)->isAviable) {
(*i)->isAviable = false;
return (*i);
}
}
return NULL;
}
#endif
#if MODULE_API_VERSION > 100
void TestRobotModule::robotFree(int run_index, Robot *robot) {
#else
void TestRobotModule::robotFree(Robot *robot) {
#endif
TestRobot *test_robot = reinterpret_cast<TestRobot *>(robot);
for (auto i = aviable_connections.begin();
i != aviable_connections.end(); ++i) {
if ((*i) == test_robot) {
test_robot->isAviable = true;
return;
}
}
}
void TestRobotModule::final() {
for (auto i = aviable_connections.begin();
i != aviable_connections.end(); ++i) {
delete (*i);
}
aviable_connections.clear();
}
int TestRobotModule::endProgram(int run_index) { return 0; }
void TestRobotModule::destroy() {
#if MODULE_API_VERSION > 000
delete mi;
#endif
for (unsigned int j = 0; j < COUNT_FUNCTIONS; ++j) {
if (robot_functions[j]->count_params) {
delete[] robot_functions[j]->params;
}
delete robot_functions[j];
}
for (unsigned int j = 0; j < COUNT_AXIS; ++j) {
delete robot_axis[j];
}
delete[] robot_functions;
delete[] robot_axis;
delete this;
}
void TestRobotModule::colorPrintf(ConsoleColor colors, const char *mask, ...) {
va_list args;
va_start(args, mask);
(*colorPrintf_p)(this, colors, mask, args);
va_end(args);
}
TestRobot::TestRobot(unsigned int uniq_index) : isAviable(true) {
uniq_name = new char[40];
sprintf(uniq_name, "robot-%u", uniq_index);
}
void TestRobot::prepare(colorPrintfRobot_t *colorPrintf_p,
colorPrintfRobotVA_t *colorPrintfVA_p) {
this->colorPrintf_p = colorPrintfVA_p;
}
#if MODULE_API_VERSION > 100
const char *TestRobot::getUniqName() {
return uniq_name;
}
FunctionResult *TestRobot::executeFunction(int run_index, CommandMode mode,
system_value command_index,
void **args) {
#else
FunctionResult *TestRobot::executeFunction(CommandMode mode,
system_value command_index,
void **args) {
#endif
FunctionResult *fr = NULL;
switch (command_index) {
case 1: { // none
break;
}
case 2: { // do_something
variable_value *vv = (variable_value *)args[0];
#ifdef _WIN32
Sleep((DWORD)*vv);
#else
usleep(((uint32_t)*vv) * 1000);
#endif
break;
}
case 3: { // get_some_value
variable_value *vv = (variable_value *)args[0];
#if MODULE_API_VERSION > 000
fr = new FunctionResult(FunctionResult::Types::VALUE, *vv);
#else
fr = new FunctionResult(1, *vv);
#endif
break;
}
case 4: { // throw_exception
#if MODULE_API_VERSION > 000
fr = new FunctionResult(FunctionResult::Types::EXCEPTION);
#else
fr = new FunctionResult(0);
#endif
break;
}
case 5: { // print
variable_value *vv = (variable_value *)args[1];
#ifdef _WIN32
Sleep((DWORD)*vv);
#else
usleep(((uint32_t)*vv) * 1000);
#endif
const char *tmp = (const char *)args[0];
colorPrintf(ConsoleColor(ConsoleColor::white), "%s", tmp);
break;
}
case 6:{ // throw_value
variable_value *vv = (variable_value *)args[0];
#if MODULE_API_VERSION > 000
fr = new FunctionResult(FunctionResult::Types::EXCEPTION, *vv);
#else
fr = new FunctionResult(0, *vv);
#endif
}
case 7: { // debug
const char *tmp = (const char *)args[0];
colorPrintf(ConsoleColor(ConsoleColor::white), "%s", tmp);
break;
}
default:
break;
}
return fr;
}
void TestRobot::axisControl(system_value axis_index, variable_value value) {
const char *name;
switch (axis_index) {
case 1: {
name = "X";
break;
}
case 2: {
name = "Y";
break;
}
case 3: {
name = "Z";
break;
}
default: {
name = "O_o";
break;
};
}
colorPrintf(ConsoleColor(ConsoleColor::green), "change axis value: %s = %f\n",
name, value);
}
TestRobot::~TestRobot() { delete[] uniq_name; }
void TestRobot::colorPrintf(ConsoleColor colors, const char *mask, ...) {
va_list args;
va_start(args, mask);
#if MODULE_API_VERSION > 100
(*colorPrintf_p)(this, colors, mask, args);
#else
(*colorPrintf_p)(this, uniq_name, colors, mask, args);
#endif
va_end(args);
}
#if MODULE_API_VERSION > 000
PREFIX_FUNC_DLL unsigned short getRobotModuleApiVersion() {
return MODULE_API_VERSION;
};
#endif
PREFIX_FUNC_DLL RobotModule *getRobotModuleObject() {
return new TestRobotModule();
}