-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdirectMotionExample.cpp
More file actions
338 lines (306 loc) · 9.52 KB
/
directMotionExample.cpp
File metadata and controls
338 lines (306 loc) · 9.52 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
/*
Adept MobileRobots Robotics Interface for Applications (ARIA)
Copyright (C) 2004-2005 ActivMedia Robotics LLC
Copyright (C) 2006-2010 MobileRobots Inc.
Copyright (C) 2011-2014 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 "Aria.h"
/** @example directMotionExample.cpp Sends a sequence of motion commands directly
* to the robot using ArRobot's direct motion command methods.
This demo starts up the robot in its own thread, then calls a series of the
motion commands methods in ArRobot to simply send commands directly
to the robot (rather than using e.g. ArAction objects.)
Note that if you want to stop direct motion
commands and let ArActions take over, you must call ArRobot::clearDirectMotion() (otherwise ArRobot may continue sending motion commands that conflict
with the requests of the action resolver).
See the section on motion commands and actions for details.
*/
/*
This is a connection handler class, to demonstrate how to run code in
response to events such as the program connecting an disconnecting
from the robot.
*/
class ConnHandler
{
public:
// Constructor
ConnHandler(ArRobot *robot);
// Destructor, its just empty
~ConnHandler(void) {}
// to be called if the connection was made
void connected(void);
// to call if the connection failed
void connFail(void);
// to be called if the connection was lost
void disconnected(void);
protected:
// robot pointer
ArRobot *myRobot;
// the functor callbacks
ArFunctorC<ConnHandler> myConnectedCB;
ArFunctorC<ConnHandler> myConnFailCB;
ArFunctorC<ConnHandler> myDisconnectedCB;
};
ConnHandler::ConnHandler(ArRobot *robot) :
myConnectedCB(this, &ConnHandler::connected),
myConnFailCB(this, &ConnHandler::connFail),
myDisconnectedCB(this, &ConnHandler::disconnected)
{
myRobot = robot;
myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
}
// just exit if the connection failed
void ConnHandler::connFail(void)
{
printf("directMotionDemo connection handler: Failed to connect.\n");
myRobot->stopRunning();
Aria::exit(1);
return;
}
// turn on motors, and off sonar, and off amigobot sounds, when connected
void ConnHandler::connected(void)
{
printf("directMotionDemo connection handler: Connected\n");
myRobot->comInt(ArCommands::SONAR, 0);
myRobot->comInt(ArCommands::ENABLE, 1);
myRobot->comInt(ArCommands::SOUNDTOG, 0);
}
// lost connection, so just exit
void ConnHandler::disconnected(void)
{
printf("directMotionDemo connection handler: Lost connection, exiting program.\n");
Aria::exit(0);
}
int main(int argc, char **argv)
{
Aria::init();
ArArgumentParser argParser(&argc, argv);
argParser.loadDefaultArguments();
ArRobot robot;
ArRobotConnector con(&argParser, &robot);
// the connection handler from above
ConnHandler ch(&robot);
if(!Aria::parseArgs())
{
Aria::logOptions();
Aria::exit(1);
return 1;
}
if(!con.connectRobot())
{
ArLog::log(ArLog::Normal, "directMotionExample: Could not connect to the robot. Exiting.");
if(argParser.checkHelpAndWarnUnparsed())
{
Aria::logOptions();
}
Aria::exit(1);
return 1;
}
ArLog::log(ArLog::Normal, "directMotionExample: Connected.");
if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
{
Aria::logOptions();
Aria::exit(1);
}
// Run the robot processing cycle in its own thread. Note that after starting this
// thread, we must lock and unlock the ArRobot object before and after
// accessing it.
robot.runAsync(false);
// Send the robot a series of motion commands directly, sleeping for a
// few seconds afterwards to give the robot time to execute them.
printf("directMotionExample: Setting rot velocity to 100 deg/sec then sleeping 3 seconds\n");
robot.lock();
robot.setRotVel(100);
robot.unlock();
ArUtil::sleep(3*1000);
printf("Stopping\n");
robot.lock();
robot.setRotVel(0);
robot.unlock();
ArUtil::sleep(200);
printf("directMotionExample: Telling the robot to go 300 mm on left wheel and 100 mm on right wheel for 5 seconds\n");
robot.lock();
robot.setVel2(300, 100);
robot.unlock();
ArTime start;
start.setToNow();
while (1)
{
robot.lock();
if (start.mSecSince() > 5000)
{
robot.unlock();
break;
}
robot.unlock();
ArUtil::sleep(50);
}
printf("directMotionExample: Telling the robot to move forwards one meter, then sleeping 5 seconds\n");
robot.lock();
robot.move(1000);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isMoveDone())
{
printf("directMotionExample: Finished distance\n");
robot.unlock();
break;
}
if (start.mSecSince() > 5000)
{
printf("directMotionExample: Distance timed out\n");
robot.unlock();
break;
}
robot.unlock();
ArUtil::sleep(50);
}
printf("directMotionExample: Telling the robot to move backwards one meter, then sleeping 5 seconds\n");
robot.lock();
robot.move(-1000);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isMoveDone())
{
printf("directMotionExample: Finished distance\n");
robot.unlock();
break;
}
if (start.mSecSince() > 10000)
{
printf("directMotionExample: Distance timed out\n");
robot.unlock();
break;
}
robot.unlock();
ArUtil::sleep(50);
}
printf("directMotionExample: Telling the robot to turn to 180, then sleeping 4 seconds\n");
robot.lock();
robot.setHeading(180);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isHeadingDone(5))
{
printf("directMotionExample: Finished turn\n");
robot.unlock();
break;
}
if (start.mSecSince() > 5000)
{
printf("directMotionExample: Turn timed out\n");
robot.unlock();
break;
}
robot.unlock();
ArUtil::sleep(100);
}
printf("directMotionExample: Telling the robot to turn to 90, then sleeping 2 seconds\n");
robot.lock();
robot.setHeading(90);
robot.unlock();
start.setToNow();
while (1)
{
robot.lock();
if (robot.isHeadingDone(5))
{
printf("directMotionExample: Finished turn\n");
robot.unlock();
break;
}
if (start.mSecSince() > 5000)
{
printf("directMotionExample: turn timed out\n");
robot.unlock();
break;
}
robot.unlock();
ArUtil::sleep(100);
}
printf("directMotionExample: Setting vel2 to 200 mm/sec on both wheels, then sleeping 3 seconds\n");
robot.lock();
robot.setVel2(200, 200);
robot.unlock();
ArUtil::sleep(3000);
printf("directMotionExample: Stopping the robot, then sleeping for 2 seconds\n");
robot.lock();
robot.stop();
robot.unlock();
ArUtil::sleep(2000);
printf("directMotionExample: Setting velocity to 200 mm/sec then sleeping 3 seconds\n");
robot.lock();
robot.setVel(200);
robot.unlock();
ArUtil::sleep(3000);
printf("directMotionExample: Stopping the robot, then sleeping for 2 seconds\n");
robot.lock();
robot.stop();
robot.unlock();
ArUtil::sleep(2000);
printf("directMotionExample: Setting vel2 with 0 on left wheel, 200 mm/sec on right, then sleeping 5 seconds\n");
robot.lock();
robot.setVel2(0, 200);
robot.unlock();
ArUtil::sleep(5000);
printf("directMotionExample: Telling the robot to rotate at 50 deg/sec then sleeping 5 seconds\n");
robot.lock();
robot.setRotVel(50);
robot.unlock();
ArUtil::sleep(5000);
printf("directMotionExample: Telling the robot to rotate at -50 deg/sec then sleeping 5 seconds\n");
robot.lock();
robot.setRotVel(-50);
robot.unlock();
ArUtil::sleep(5000);
printf("directMotionExample: Setting vel2 with 0 on both wheels, then sleeping 3 seconds\n");
robot.lock();
robot.setVel2(0, 0);
robot.unlock();
ArUtil::sleep(3000);
printf("directMotionExample: Now having the robot change heading by -125 degrees, then sleeping for 6 seconds\n");
robot.lock();
robot.setDeltaHeading(-125);
robot.unlock();
ArUtil::sleep(6000);
printf("directMotionExample: Now having the robot change heading by 45 degrees, then sleeping for 6 seconds\n");
robot.lock();
robot.setDeltaHeading(45);
robot.unlock();
ArUtil::sleep(6000);
printf("directMotionExample: Setting vel2 with 200 on left wheel, 0 on right wheel, then sleeping 5 seconds\n");
robot.lock();
robot.setVel2(200, 0);
robot.unlock();
ArUtil::sleep(5000);
printf("directMotionExample: Done, exiting.\n");
Aria::exit(0);
return 0;
}