-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathariac.cpp
More file actions
702 lines (620 loc) · 15.2 KB
/
Copy pathariac.cpp
File metadata and controls
702 lines (620 loc) · 15.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
(C)Copyright 2012 Adept Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you wish to redistribute ARIA under different terms, contact
Adept MobileRobots for information about a commercial version of ARIA at
robots@mobilerobots.com or
Adept MobileRobots, 10 Columbia Drive, Amherst, NH 03031; +1-603-881-7960
*/
#include "ArExport.h"
#include "ariaInternal.h"
#include "ArRobot.h"
#include "ArRobotConnector.h"
#include "ArArgumentParser.h"
#include "ArCommands.h"
#include "ArRobotConfigPacketReader.h"
#ifndef AREXPORT
#error ARIA headers did not define AREXPORT
#endif
#include "ariac.h"
#ifdef MATLAB
#include <mex.h>
AREXPORT void mexLog(const char *s)
{
mexPrintf("aria: %s\n", s);
}
#endif
/* Ideas for changes or additions:
* 1. Put ArRobot and other objects into a struct returned by arrobot_connect()
* and passed in to all other functions (or which can be "set" in a static
* global).
* 2. Use a sensor interp task to cache data from SIP in static variables (i.e.
* local to instance of this library). If neccesary, have separate mutexes for
* each variable. (I.e. optimize for very frequent independent access of each
* value.)
* 3. cycle counter
* 4. Include access to ArRobot callbacks including robot cycle tasks and connection callbacks.
*/
#define AR_ASSERT_RETURN(ptr) { \
if(!ptr) { \
ArLog::log(ArLog::Terse, "ariac: failed assertion: (%s) at %s:%d", #ptr, __FILE__, __LINE__); \
return; \
} \
}
#define AR_ASSERT_RETURN_VAL(ptr, retval) { \
if(!ptr) { \
ArLog::log(ArLog::Terse, "ariac: failed assertion: (%s) at %s:%d", #ptr, __FILE__, __LINE__); \
return retval; \
} \
}
// Switch the following to print debugging information at the start of every function call
/*
#ifdef _MSC_VER
#define AR_DEBUG_LOGINFO() { arloginfo(__FUNCTION__); }
#else
#define AR_DEBUG_LOGINFO() { arloginfo(__func__); }
#endif
*/
#define AR_DEBUG_LOGINFO() {}
bool init = false;
bool connected = false;
ArArgumentParser *argParser = NULL;
ArRobot *robot = NULL;
ArRobotConnector *robotConnector = NULL;
int argc = 0;
char **argv = NULL;
ArGlobalFunctor1<const char*> *logFunc = NULL;
AREXPORT void arloginfo(const char* m)
{
std::string args;
for(int i = 0; i < argc; ++i)
{
args += argv[i];
args += " ";
}
ArLog::log(ArLog::Terse, "%s: init=%d connected=%d argParser=0x%x robot=0x%x robotConnector=0x%x argc=%d argv=0x%x (%s)",
m, init, connected, argParser, robot, robotConnector, argc, argv, args.c_str());
if(robot)
robot->getOrigRobotConfig()->log();
else
ArLog::log(ArLog::Terse, "no robot object.");
}
AREXPORT int aria_init(int _argc, char **_argv)
{
AR_DEBUG_LOGINFO();
if(init) {
ArLog::log(ArLog::Normal, "aria_init: Already initialized.");
return 1;
}
Aria::init();
#ifdef MATLAB
aria_setloghandler(&mexLog);
#endif
argc = _argc;
argv = _argv;
ArLog::log(ArLog::Normal, "aria_init(%d, 0x%x)", argc, argv);
argParser = new ArArgumentParser(&argc, argv);
argParser->loadDefaultArguments();
init = true;
return 1;
}
AREXPORT void aria_setloghandler(void(*logfn)(const char*))
{
logFunc = new ArGlobalFunctor1<const char*>(logfn);
ArLog::setFunctor(logFunc);
}
void disconnected()
{
ArLog::log(ArLog::Terse, "ariac: robot disconnected.");
arrobot_disconnect();
}
ArGlobalFunctor disconnectCB(&disconnected);
AREXPORT int arrobot_connect()
{
AR_DEBUG_LOGINFO();
if(!init)
{
ArLog::log(ArLog::Terse, "arrobot_connect: Error: aria_init() must be called before arrobot_connect().");
return 0;
}
if(connected) {
ArLog::log(ArLog::Normal, "arrobot_connect: Already connected.");
return 1;
}
argParser->log();
ArLog::log(ArLog::Normal, "arrobot_connect: Connecting to robot...");
robot = new ArRobot;
robotConnector = new ArRobotConnector(argParser, robot);
robot->addDisconnectOnErrorCB(&disconnectCB);
if(!robotConnector->connectRobot())
{
ArLog::log(ArLog::Normal, "arrobot_connect: Could not connect to the robot. Check connections or set connection options as program arguments in init.");
/*if(argParser->checkHelpAndWarnUnparsed())
{
Aria::logOptions();
}
*/
arrobot_disconnect();
return 0;
}
if(!Aria::parseArgs() || !argParser->checkHelpAndWarnUnparsed())
{
Aria::logOptions();
arrobot_disconnect();
return 0;
}
robot->enableMotors();
robot->runAsync(true);
ArUtil::sleep(300);
ArLog::log(ArLog::Normal, "arrobot_connect: Connected to robot. ArRobot processing cycle is running in background thread.");
connected = true;
return 1;
}
AREXPORT void arrobot_disconnect()
{
AR_DEBUG_LOGINFO();
if(!connected) return;
connected = false;
if(!robot) { ArLog::log(ArLog::Terse, "arrobot_disconnect: internal error: NULL robot!"); return; }
robot->clearDirectMotion();
robot->stopRunning();
robot->waitForRunExit();
delete robot;
robot = NULL;
if(!robotConnector) { ArLog::log(ArLog::Terse, "arrobot_disconnect: internal error: NULL robotConnector!"); return; }
delete robotConnector;
robotConnector = NULL;
}
/** Block until robot connection is broken (e.g. by robot reset). */
AREXPORT void arrobot_wait()
{
AR_DEBUG_LOGINFO();
AR_ASSERT_RETURN(robot);
robot->waitForRunExit();
}
/** Exit program after quick disconnection from robot and other devices. */
AREXPORT void aria_exit(int code)
{
AR_DEBUG_LOGINFO();
Aria::exit(code);
}
AREXPORT double arrobot_getx()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double x = robot->getX();
robot->unlock();
return x;
}
AREXPORT double arrobot_gety()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double y = robot->getY();
robot->unlock();
return y;
}
AREXPORT double arrobot_getth()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double th = robot->getTh();
robot->unlock();
return th;
}
AREXPORT arpose arrobot_getpose()
{
AR_DEBUG_LOGINFO();
arpose cp;
if(!connected)
{
cp.x = 0;
cp.y = 0;
cp.th = 0;
return cp;
}
robot->lock();
ArPose p = robot->getPose();
robot->unlock();
cp.x = p.getX();
cp.y = p.getY();
cp.th = p.getTh();
return cp;
}
AREXPORT double arrobot_getvel()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double v = robot->getVel();
robot->unlock();
return v;
}
AREXPORT double arrobot_getrotvel()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double v = robot->getRotVel();
robot->unlock();
return v;
}
AREXPORT double arrobot_getlatvel()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double v = robot->getLatVel();
robot->unlock();
return v;
}
AREXPORT arpose arrobot_getvels()
{
AR_DEBUG_LOGINFO();
arpose p;
if(!connected) {
p.x = 0;
p.y = 0;
p.th = 0;
return p;
}
robot->lock();
p.x = robot->getVel();
p.y = robot->getLatVel();
p.th = robot->getRotVel();
robot->unlock();
return p;
}
AREXPORT double arrobot_radius()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
return robot->getRobotRadius();
}
AREXPORT double arrobot_width()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
return robot->getRobotWidth();
}
AREXPORT double arrobot_length()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
return robot->getRobotLength();
}
AREXPORT double arrobot_getleftvel()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double v = robot->getLeftVel();
robot->unlock();
return v;
}
AREXPORT double arrobot_getrightvel()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
double v = robot->getRightVel();
robot->unlock();
return v;
}
AREXPORT int arrobot_isleftstalled()
{
AR_DEBUG_LOGINFO();
if(!connected) return 1;
AR_ASSERT_RETURN_VAL(robot, 1);
robot->lock();
bool v = robot->isLeftMotorStalled();
robot->unlock();
return v;
}
AREXPORT int arrobot_isrightstalled()
{
AR_DEBUG_LOGINFO();
if(!connected) return 1;
AR_ASSERT_RETURN_VAL(robot, 1);
robot->lock();
bool v = robot->isRightMotorStalled();
robot->unlock();
return v;
}
AREXPORT int arrobot_isstalled()
{
AR_DEBUG_LOGINFO();
if(!connected) return 1;
AR_ASSERT_RETURN_VAL(robot, 1);
robot->lock();
bool v = robot->isRightMotorStalled() || robot->isLeftMotorStalled();
robot->unlock();
return v;
}
AREXPORT char arrobot_getdigin()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0;
AR_ASSERT_RETURN_VAL(robot, 0);
robot->lock();
char v = robot->getDigIn();
robot->unlock();
return v;
}
AREXPORT void arrobot_setdigout(char c)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->com2Bytes(ArCommands::DIGOUT, (char)0xFF, c);
robot->unlock();
}
AREXPORT double arrobot_getsonarrange(int i)
{
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0);
robot->lock();
int v = robot->getSonarRange(i);
robot->unlock();
return v;
}
AREXPORT int arrobot_getnumsonar()
{
AR_DEBUG_LOGINFO();
if(!connected) return 0;
AR_ASSERT_RETURN_VAL(robot, 0);
robot->lock();
int n = robot->getNumSonar();
robot->unlock();
return n;
}
AREXPORT void arrobot_getsonar(double s[AR_MAX_NUM_SONAR])
{
AR_DEBUG_LOGINFO();
if(!connected) return;
robot->lock();
for(int i = 0; i < robot->getNumSonar(); ++i)
{
s[i] = robot->getSonarRange(i);
}
robot->unlock();
}
AREXPORT void arrobot_setpose(double x, double y, double th)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->moveTo(ArPose(x, y, th));
robot->unlock();
}
AREXPORT void arrobot_stop()
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->stop();
robot->unlock();
}
AREXPORT void arrobot_setvels(arpose v)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
robot->lock();
robot->setVel(v.x);
robot->setLatVel(v.y);
robot->setRotVel(v.th);
robot->unlock();
}
AREXPORT void arrobot_setvel(double vel)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->setVel(vel);
robot->unlock();
}
AREXPORT void arrobot_setwheelvels(double left, double right)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->setVel2(left, right);
robot->unlock();
}
AREXPORT void arrobot_setrotvel(double rotvel)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->setRotVel(rotvel);
robot->unlock();
}
AREXPORT void arrobot_setlatvel(double vel)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->setLatVel(vel);
robot->unlock();
}
AREXPORT double arrobot_getbatteryvoltage()
{
double v;
AR_DEBUG_LOGINFO();
if(!connected) return 0.0;
AR_ASSERT_RETURN_VAL(robot, 0.0);
robot->lock();
v = robot->getRealBatteryVoltage();
robot->unlock();
return v;
}
AREXPORT int arrobot_num_front_bumpers()
{
int n;
AR_DEBUG_LOGINFO();
if(!connected) return 0;
AR_ASSERT_RETURN_VAL(robot, 0);
robot->lock();
n = robot->getNumFrontBumpers();
robot->unlock();
return n;
}
AREXPORT int arrobot_num_rear_bumpers()
{
int n;
AR_DEBUG_LOGINFO();
if(!connected) return 0;
AR_ASSERT_RETURN_VAL(robot, 0);
robot->lock();
n = robot->getNumRearBumpers();
robot->unlock();
return n;
}
AREXPORT void arrobot_get_bumpers(char *front, char *rear)
{
int s;
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
s = robot->getStallValue();
robot->unlock();
// Extract just the bumper flags from the stall and bumper flags.
// Bit 0 will be the first bumper, bit 2 the second, etc. in each byte.
if(front)
*front = ((robot->getStallValue() & 0xff00) >> 8) >> 1; // second byte is front bumpers; BIT0 is the left stall flag, shift over it
if(rear)
*rear = (robot->getStallValue() & 0x00ff) >> 1; // LSB is front bumpers; BIT0 is the right stall flag, shift over it
}
AREXPORT int arrobot_get_front_bumper(int i)
{
char fb;
AR_DEBUG_LOGINFO();
if(!connected) return 0;
AR_ASSERT_RETURN_VAL(robot, 0);
arrobot_get_bumpers(&fb, NULL);
return fb & (ArUtil::BIT0 << i);
}
AREXPORT int arrobot_get_rear_bumper(int i)
{
char rb;
AR_DEBUG_LOGINFO();
if(!connected) return 0;
AR_ASSERT_RETURN_VAL(robot, 0);
arrobot_get_bumpers(NULL, &rb);
return rb & (ArUtil::BIT0 << i);
}
AREXPORT void arrobot_setdeltaheading(double d)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->setDeltaHeading(d);
robot->unlock();
}
AREXPORT void arrobot_command(int c)
{
AR_DEBUG_LOGINFO();
AR_ASSERT_RETURN(robot);
robot->lock();
robot->com(c);
robot->unlock();
}
AREXPORT void arrobot_command_int(int c, int a)
{
AR_DEBUG_LOGINFO();
AR_ASSERT_RETURN(robot);
robot->lock();
robot->comInt(c, a);
robot->unlock();
}
AREXPORT void arrobot_command_2bytes(int c, char b1, char b2)
{
AR_DEBUG_LOGINFO();
AR_ASSERT_RETURN(robot);
robot->lock();
robot->com2Bytes(c, b1, b2);
robot->unlock();
}
AREXPORT void arrobot_resetpos()
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
ArPose p = robot->getPose();
robot->com(ArCommands::SETO);
robot->moveTo(ArPose(0, 0, 0), p);
ArLog::log(ArLog::Normal, "arrobot_resetpos: Reset robot odometery to origin and transformed ArRobot pose, it is now %0.2f, %0.2f, %0.2f.\n", robot->getX(), robot->getY(), robot->getTh());
robot->unlock();
}
AREXPORT void arrobot_move(double d)
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->move(d);
robot->unlock();
}
AREXPORT int arrobot_motors_enabled()
{
AR_DEBUG_LOGINFO();
if(!connected) return false;
AR_ASSERT_RETURN_VAL(robot, false);
robot->lock();
int b = robot->areMotorsEnabled();
robot->unlock();
return b;
}
AREXPORT void arrobot_enable_motors()
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->enableMotors();
robot->unlock();
}
AREXPORT void arrobot_disable_motors()
{
AR_DEBUG_LOGINFO();
if(!connected) return;
AR_ASSERT_RETURN(robot);
robot->lock();
robot->disableMotors();
robot->unlock();
}