forked from cyberbotics/webots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.cpp
More file actions
623 lines (525 loc) · 17.2 KB
/
Robot.cpp
File metadata and controls
623 lines (525 loc) · 17.2 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Copyright 1996-2023 Cyberbotics Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define WB_ALLOW_MIXING_C_AND_CPP_API
#include <webots/device.h>
#include <webots/plugins/robot_window/robot_window.h>
#include <webots/plugins/robot_window/robot_wwi.h>
#include <webots/robot.h>
#include <webots/Device.hpp>
#include <webots/Robot.hpp>
#include <webots/Accelerometer.hpp>
#include <webots/Altimeter.hpp>
#include <webots/Brake.hpp>
#include <webots/Camera.hpp>
#include <webots/Compass.hpp>
#include <webots/Connector.hpp>
#include <webots/Device.hpp>
#include <webots/Display.hpp>
#include <webots/DistanceSensor.hpp>
#include <webots/Emitter.hpp>
#include <webots/GPS.hpp>
#include <webots/Gyro.hpp>
#include <webots/InertialUnit.hpp>
#include <webots/Joystick.hpp>
#include <webots/Keyboard.hpp>
#include <webots/LED.hpp>
#include <webots/Lidar.hpp>
#include <webots/LightSensor.hpp>
#include <webots/Motor.hpp>
#include <webots/Mouse.hpp>
#include <webots/Pen.hpp>
#include <webots/PositionSensor.hpp>
#include <webots/Radar.hpp>
#include <webots/RangeFinder.hpp>
#include <webots/Receiver.hpp>
#include <webots/Skin.hpp>
#include <webots/Speaker.hpp>
#include <webots/TouchSensor.hpp>
#include <webots/VacuumGripper.hpp>
#include <cassert>
#include <cstdlib>
#include <list>
using namespace std;
using namespace webots;
Robot *Robot::cInstance = NULL;
std::vector<Device *> Robot::deviceList = std::vector<Device *>();
Robot::Robot() {
if (cInstance == NULL)
cInstance = this;
else {
cerr << "Only one instance of the Robot class should be created" << endl;
wb_robot_step(0);
wb_robot_cleanup();
exit(-1);
}
wb_robot_init();
mKeyboard = new Keyboard();
mJoystick = new Joystick();
mMouse = new Mouse();
}
Robot::~Robot() {
for (size_t i = 0; i < deviceList.size(); ++i)
delete deviceList[i];
deviceList.clear();
delete mKeyboard;
delete mJoystick;
delete mMouse;
wb_robot_cleanup();
}
int Robot::step(int duration) {
return wb_robot_step(duration);
}
int Robot::stepBegin(int duration) {
return wb_robot_step_begin(duration);
}
int Robot::stepEnd() {
return wb_robot_step_end();
}
Robot::UserInputEvent Robot::waitForUserInputEvent(UserInputEvent event_type, int timeout) {
return UserInputEvent(wb_robot_wait_for_user_input_event(WbUserInputEvent(event_type), timeout));
}
string Robot::getName() const {
return string(wb_robot_get_name());
}
string Robot::getUrdf(string prefix) const {
return string(wb_robot_get_urdf(prefix.c_str()));
}
double Robot::getTime() const {
return wb_robot_get_time();
}
string Robot::getModel() const {
return string(wb_robot_get_model());
}
string Robot::getCustomData() const {
return string(wb_robot_get_custom_data());
}
void Robot::setCustomData(const std::string &data) {
wb_robot_set_custom_data(data.c_str());
}
string Robot::getData() const {
fprintf(stderr, "Robot::getData is deprecated, please use Robot::getCustomData instead\n");
return string(wb_robot_get_custom_data());
}
void Robot::setData(const std::string &data) {
fprintf(stderr, "Robot::setData is deprecated, please use Robot::setCustomData instead\n");
wb_robot_set_custom_data(data.c_str());
}
Robot::Mode Robot::getMode() const {
return Mode(wb_robot_get_mode());
}
void Robot::setMode(Mode m, const char *arg) {
wb_robot_set_mode(WbRobotMode(m), arg);
}
bool Robot::getSupervisor() const {
return wb_robot_get_supervisor();
}
bool Robot::getSynchronization() const {
return wb_robot_get_synchronization();
}
string Robot::getProjectPath() const {
return string(wb_robot_get_project_path());
}
string Robot::getWorldPath() const {
return string(wb_robot_get_world_path());
}
double Robot::getBasicTimeStep() const {
return wb_robot_get_basic_time_step();
}
int Robot::getNumberOfDevices() const {
return wb_robot_get_number_of_devices();
}
Device *Robot::getDeviceByIndex(int index) {
return getOrCreateDevice(wb_robot_get_device_by_index(index));
}
Device *Robot::getDevice(const std::string &name) {
return getOrCreateDevice(wb_robot_get_device(name.c_str()));
}
void Robot::batterySensorEnable(int sampling_period) {
wb_robot_battery_sensor_enable(sampling_period);
}
void Robot::batterySensorDisable() {
wb_robot_battery_sensor_disable();
}
int Robot::batterySensorGetSamplingPeriod() const {
return wb_robot_battery_sensor_get_sampling_period();
}
double Robot::batterySensorGetValue() const {
return wb_robot_battery_sensor_get_value();
}
Accelerometer *Robot::getAccelerometer(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_ACCELEROMETER))
return NULL;
return dynamic_cast<Accelerometer *>(getOrCreateDevice(tag));
}
Accelerometer *Robot::createAccelerometer(const string &name) const {
return new Accelerometer(name);
}
Altimeter *Robot::getAltimeter(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_ALTIMETER))
return NULL;
return dynamic_cast<Altimeter *>(getOrCreateDevice(tag));
}
Altimeter *Robot::createAltimeter(const string &name) const {
return new Altimeter(name);
}
Brake *Robot::getBrake(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_BRAKE))
return NULL;
return dynamic_cast<Brake *>(getOrCreateDevice(tag));
}
Brake *Robot::createBrake(const string &name) const {
return new Brake(name);
}
Camera *Robot::getCamera(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_CAMERA))
return NULL;
return dynamic_cast<Camera *>(getOrCreateDevice(tag));
}
Camera *Robot::createCamera(const string &name) const {
return new Camera(name);
}
Compass *Robot::getCompass(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_COMPASS))
return NULL;
return dynamic_cast<Compass *>(getOrCreateDevice(tag));
}
Compass *Robot::createCompass(const string &name) const {
return new Compass(name);
}
Connector *Robot::getConnector(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_CONNECTOR))
return NULL;
return dynamic_cast<Connector *>(getOrCreateDevice(tag));
}
Connector *Robot::createConnector(const string &name) const {
return new Connector(name);
}
Display *Robot::getDisplay(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_DISPLAY))
return NULL;
return dynamic_cast<Display *>(getOrCreateDevice(tag));
}
Display *Robot::createDisplay(const string &name) const {
return new Display(name);
}
DistanceSensor *Robot::getDistanceSensor(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_DISTANCE_SENSOR))
return NULL;
return dynamic_cast<DistanceSensor *>(getOrCreateDevice(tag));
}
DistanceSensor *Robot::createDistanceSensor(const string &name) const {
return new DistanceSensor(name);
}
Emitter *Robot::getEmitter(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_EMITTER))
return NULL;
return dynamic_cast<Emitter *>(getOrCreateDevice(tag));
}
Emitter *Robot::createEmitter(const string &name) const {
return new Emitter(name);
}
GPS *Robot::getGPS(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_GPS))
return NULL;
return dynamic_cast<GPS *>(getOrCreateDevice(tag));
}
GPS *Robot::createGPS(const string &name) const {
return new GPS(name);
}
Gyro *Robot::getGyro(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_GYRO))
return NULL;
return dynamic_cast<Gyro *>(getOrCreateDevice(tag));
}
Gyro *Robot::createGyro(const string &name) const {
return new Gyro(name);
}
InertialUnit *Robot::getInertialUnit(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_INERTIAL_UNIT))
return NULL;
return dynamic_cast<InertialUnit *>(getOrCreateDevice(tag));
}
InertialUnit *Robot::createInertialUnit(const string &name) const {
return new InertialUnit(name);
}
LED *Robot::getLED(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_LED))
return NULL;
return dynamic_cast<LED *>(getOrCreateDevice(tag));
}
LED *Robot::createLED(const string &name) const {
return new LED(name);
}
Lidar *Robot::getLidar(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_LIDAR))
return NULL;
return dynamic_cast<Lidar *>(getOrCreateDevice(tag));
}
Lidar *Robot::createLidar(const string &name) const {
return new Lidar(name);
}
LightSensor *Robot::getLightSensor(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_LIGHT_SENSOR))
return NULL;
return dynamic_cast<LightSensor *>(getOrCreateDevice(tag));
}
LightSensor *Robot::createLightSensor(const string &name) const {
return new LightSensor(name);
}
Motor *Robot::getMotor(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_LINEAR_MOTOR) && !Device::hasType(tag, WB_NODE_ROTATIONAL_MOTOR))
return NULL;
return dynamic_cast<Motor *>(getOrCreateDevice(tag));
}
Motor *Robot::createMotor(const string &name) const {
return new Motor(name);
}
Pen *Robot::getPen(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_PEN))
return NULL;
return dynamic_cast<Pen *>(getOrCreateDevice(tag));
}
Pen *Robot::createPen(const string &name) const {
return new Pen(name);
}
PositionSensor *Robot::getPositionSensor(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_POSITION_SENSOR))
return NULL;
return dynamic_cast<PositionSensor *>(getOrCreateDevice(tag));
}
PositionSensor *Robot::createPositionSensor(const string &name) const {
return new PositionSensor(name);
}
Radar *Robot::getRadar(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_RADAR))
return NULL;
return dynamic_cast<Radar *>(getOrCreateDevice(tag));
}
Radar *Robot::createRadar(const string &name) const {
return new Radar(name);
}
RangeFinder *Robot::getRangeFinder(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_RANGE_FINDER))
return NULL;
return dynamic_cast<RangeFinder *>(getOrCreateDevice(tag));
}
RangeFinder *Robot::createRangeFinder(const string &name) const {
return new RangeFinder(name);
}
Receiver *Robot::getReceiver(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_RECEIVER))
return NULL;
return dynamic_cast<Receiver *>(getOrCreateDevice(tag));
}
Receiver *Robot::createReceiver(const string &name) const {
return new Receiver(name);
}
Skin *Robot::getSkin(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_SKIN))
return NULL;
return dynamic_cast<Skin *>(getOrCreateDevice(tag));
}
Skin *Robot::createSkin(const string &name) const {
return new Skin(name);
}
Speaker *Robot::getSpeaker(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_SPEAKER))
return NULL;
return dynamic_cast<Speaker *>(getOrCreateDevice(tag));
}
Speaker *Robot::createSpeaker(const string &name) const {
return new Speaker(name);
}
TouchSensor *Robot::getTouchSensor(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_TOUCH_SENSOR))
return NULL;
return dynamic_cast<TouchSensor *>(getOrCreateDevice(tag));
}
TouchSensor *Robot::createTouchSensor(const string &name) const {
return new TouchSensor(name);
}
VacuumGripper *Robot::getVacuumGripper(const string &name) {
WbDeviceTag tag = wb_robot_get_device(name.c_str());
if (!Device::hasType(tag, WB_NODE_VACUUM_GRIPPER))
return NULL;
return dynamic_cast<VacuumGripper *>(getOrCreateDevice(tag));
}
VacuumGripper *Robot::createVacuumGripper(const string &name) const {
return new VacuumGripper(name);
}
Device *Robot::getDeviceFromTag(int tag) {
if (tag == 0)
return NULL;
int size = (int)deviceList.size();
if (size == 0 || size <= tag)
return NULL;
return deviceList[tag];
}
Device *Robot::getOrCreateDevice(int tag) {
if (tag == 0)
return NULL;
const int count = wb_robot_get_number_of_devices();
const int size = (int)deviceList.size();
// if new devices have been added, then count is greater than devices.length
// deleted devices are not removed from the C API list and don't affect the number of devices
if (size == count + 1 && size > 0 && tag < size)
return deviceList[tag];
// (re-)initialize deviceList
if (tag > count)
return NULL;
deviceList.resize(count + 1);
deviceList[0] = NULL;
for (int i = 0; i < count; i++) {
WbDeviceTag otherTag = wb_robot_get_device_by_index(i);
const char *name = wb_device_get_name(otherTag);
assert(otherTag <= count);
switch (wb_device_get_node_type(otherTag)) {
case WB_NODE_ACCELEROMETER:
deviceList[otherTag] = createAccelerometer(name);
break;
case WB_NODE_ALTIMETER:
deviceList[otherTag] = createAltimeter(name);
break;
case WB_NODE_BRAKE:
deviceList[otherTag] = createBrake(name);
break;
case WB_NODE_CAMERA:
deviceList[otherTag] = createCamera(name);
break;
case WB_NODE_COMPASS:
deviceList[otherTag] = createCompass(name);
break;
case WB_NODE_CONNECTOR:
deviceList[otherTag] = createConnector(name);
break;
case WB_NODE_DISPLAY:
deviceList[otherTag] = createDisplay(name);
break;
case WB_NODE_DISTANCE_SENSOR:
deviceList[otherTag] = createDistanceSensor(name);
break;
case WB_NODE_EMITTER:
deviceList[otherTag] = createEmitter(name);
break;
case WB_NODE_GPS:
deviceList[otherTag] = createGPS(name);
break;
case WB_NODE_GYRO:
deviceList[otherTag] = createGyro(name);
break;
case WB_NODE_INERTIAL_UNIT:
deviceList[otherTag] = createInertialUnit(name);
break;
case WB_NODE_LED:
deviceList[otherTag] = createLED(name);
break;
case WB_NODE_LIDAR:
deviceList[otherTag] = createLidar(name);
break;
case WB_NODE_LIGHT_SENSOR:
deviceList[otherTag] = createLightSensor(name);
break;
case WB_NODE_LINEAR_MOTOR:
case WB_NODE_ROTATIONAL_MOTOR:
deviceList[otherTag] = createMotor(name);
break;
case WB_NODE_PEN:
deviceList[otherTag] = createPen(name);
break;
case WB_NODE_POSITION_SENSOR:
deviceList[otherTag] = createPositionSensor(name);
break;
case WB_NODE_RADAR:
deviceList[otherTag] = createRadar(name);
break;
case WB_NODE_RANGE_FINDER:
deviceList[otherTag] = createRangeFinder(name);
break;
case WB_NODE_RECEIVER:
deviceList[otherTag] = createReceiver(name);
break;
case WB_NODE_SKIN:
deviceList[otherTag] = createSkin(name);
break;
case WB_NODE_SPEAKER:
deviceList[otherTag] = createSpeaker(name);
break;
case WB_NODE_TOUCH_SENSOR:
deviceList[otherTag] = createTouchSensor(name);
break;
case WB_NODE_VACUUM_GRIPPER:
deviceList[otherTag] = createVacuumGripper(name);
break;
default:
deviceList[otherTag] = NULL;
break;
}
}
return deviceList[tag];
}
int Robot::getDeviceTypeFromTag(int tag) {
return wb_device_get_node_type(tag);
}
std::string Robot::getDeviceNameFromTag(int tag) {
return wb_device_get_name(tag);
}
int Robot::getDeviceTagFromIndex(int index) {
return wb_robot_get_device_by_index(index);
}
int Robot::getDeviceTagFromName(const std::string &name) {
return wb_robot_get_device(name.c_str());
}
void *Robot::windowCustomFunction(void *arg) {
return wb_robot_window_custom_function(arg);
}
void Robot::wwiSend(const char *data, int size) {
wb_robot_wwi_send(data, size);
}
const char *Robot::wwiReceive(int *size) {
return wb_robot_wwi_receive(size);
}
string Robot::wwiReceiveText() {
const char *text = wb_robot_wwi_receive_text();
if (text)
return string(text);
else
return string();
}
void Robot::wwiSendText(const string &text) {
wb_robot_wwi_send(text.c_str(), (int)text.length() + 1);
}