File: twoRobotWander.cpp

package info (click to toggle)
libaria 2.8.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,628 kB
  • ctags: 16,574
  • sloc: cpp: 135,490; makefile: 925; python: 597; java: 570; ansic: 182
file content (231 lines) | stat: -rw-r--r-- 7,477 bytes parent folder | download | duplicates (2)
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
/*
Adept MobileRobots Robotics Interface for Applications (ARIA)
Copyright (C) 2004, 2005 ActivMedia Robotics LLC
Copyright (C) 2006, 2007, 2008, 2009, 2010 MobileRobots Inc.
Copyright (C) 2011, 2012, 2013 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 showing how two ArRobot objects can be used in one program.
 
  Normally, a program will only connect to one robot, and will only have
  one ArRobot object.   However, it is possible to connect to more than
  one robot from the same program -- for example, this may be useful for doing 
  multirobot experiments in a simulator, or to control remote robots with
  wireless-serial bridge devices from an offboard computer. This program 
  demonstrates this by connecting to two robots over TCP connections. 
  (Old systems with dedicated serial-radio links for each robot would require 
  modifying this program to use serial connections instead.)

  Specify the hostnames of each robot with -rh1 and -rh2 command line arguments
  (defaults are "localhost" for each). Specify port numbers for each with
  -rp1 and -rp2 command line arguments (defaults are 8101 for each if 
  the hostnames are different, or 8101 and 8102 if the hostnames are the same).

  This program will just have two robots wander around, avoiding obstacles
  detected by the sonar, or otherwise moving forward at constant velocity.
  First it has one robot wander for a period of time, then it has the 
  second robot wander around.
*/

int main(int argc, char** argv)
{
  int ret;
  std::string str;
  int curRobot;

  // get hostnames and port numbers for connecting to the robots.
  ArArgumentParser argParser(&argc, argv);
  char* host1 = argParser.checkParameterArgument("-rh1");
  if(!host1) host1 = "localhost";
  char* host2 = argParser.checkParameterArgument("-rh2");
  if(!host2) host2 = "localhost";

  int port1 = 8101;
  int port2 = 8101;
  if(strcmp(host1, host2) == 0 )
  {
    // same host, it must be using two ports (but can still override below with -rp2)
    port2++;
  }

  bool argSet = false;
  argParser.checkParameterArgumentInteger("-rp1", &port1, &argSet);  
  if(!argSet) argParser.checkParameterArgumentInteger("-rrtp1", &port1);
  argSet = false;
  argParser.checkParameterArgumentInteger("-rp2", &port2, &argSet);
  if(!argSet) argParser.checkParameterArgumentInteger("-rrtp2", &port2);
  
  if(!argParser.checkHelpAndWarnUnparsed())
  {
    ArLog::log(ArLog::Terse, "Usage: twoRobotWander [-rh1 <hostname1>] [-rh2 <hostname2>] [-rp1 <port1>] [-rp2 <port2>]\n"\
      "\t<hostname1> Is the network host name of the first robot."\
          " Default is localhost (for the simulator).\n"\
      "\t<hostname2> Is the network host name of the second robot."\
          " Default is localhost (for the simulator).\n"\
      "\t<port1> Is the TCP port number of the first robot. Default is 8101.\n"\
      "\t<port2> Is the TCP port number of the second robot. Default is 8102 if"\
        " both robots have the same hostname, or 8101 if they differ.\n\n");
    return 1;
  }

  

  //
  // The first robot's objects
  //
  // the first robot connection
  ArTcpConnection con1;
  // the first robot
  ArRobot robot1;
  // sonar, must be added to the first robot
  ArSonarDevice sonar1;
  // the actions we'll use to wander for the first robot
  ArActionStallRecover recover1;
  ArActionBumpers bumpers1;
  ArActionAvoidFront avoidFront1;
  ArActionConstantVelocity constantVelocity1("Constant Velocity", 400);

  //
  // The second robot's objects
  //
  // the second robot connection
  ArTcpConnection con2;
  // the second robot
  ArRobot robot2;
  // sonar, must be added to the second robot
  ArSonarDevice sonar2;
  // the actions we'll use to wander for the second robot
  ArActionStallRecover recover2;
  ArActionBumpers bumpers2;
  ArActionAvoidFront avoidFront2;
  ArActionConstantVelocity constantVelocity2("Constant Velocity", 400);

  // mandatory init
  Aria::init();

  //
  // Lets get robot 1 going
  //

  // open the connection, if this fails exit
  ArLog::log(ArLog::Normal, "Connecting to first robot at %s:%d...", host1, port1);
  if ((ret = con1.open(host1, port1)) != 0)
  {
    str = con1.getOpenMessage(ret);
    printf("Open failed to robot 1: %s\n", str.c_str());
    Aria::exit(1);
    return 1;
  }
  
  // add the sonar to the robot
  robot1.addRangeDevice(&sonar1);
  
  // set the device connection on the robot
  robot1.setDeviceConnection(&con1);
  
  // try to connect, if we fail exit
  if (!robot1.blockingConnect())
  {
    printf("Could not connect to robot 1... exiting\n");
    Aria::exit(1);
    return 1;
  }

  // turn on the motors, turn off amigobot sounds
  robot1.comInt(ArCommands::ENABLE, 1);
  robot1.comInt(ArCommands::SOUNDTOG, 0);

  // add the actions
  robot1.addAction(&recover1, 100);
  robot1.addAction(&bumpers1, 75);
  robot1.addAction(&avoidFront1, 50);
  robot1.addAction(&constantVelocity1, 25);


  //
  // Lets get robot 2 going
  //

  // open the connection, if this fails exit
  ArLog::log(ArLog::Normal, "Connecting to second robot at %s:%d...", host2, port2);
  if ((ret = con2.open(host2, port2)) != 0)
  {
    str = con2.getOpenMessage(ret);
    printf("Open failed to robot 2: %s\n", str.c_str());
    Aria::exit(1);
    return 1;
  }
  
  // add the sonar to the robot
  robot2.addRangeDevice(&sonar2);
  
  // set the device connection on the robot
  robot2.setDeviceConnection(&con2);
  
  // try to connect, if we fail exit
  if (!robot2.blockingConnect())
  {
    printf("Could not connect to robot 2... exiting\n");
    Aria::exit(1);
    return 1;
  }

  // turn on the motors, turn off amigobot sounds
  robot2.comInt(ArCommands::ENABLE, 1);
  robot2.comInt(ArCommands::SOUNDTOG, 0);

  // add the actions
  robot2.addAction(&recover2, 100);
  robot2.addAction(&bumpers2, 75);
  robot2.addAction(&avoidFront2, 50);
  robot2.addAction(&constantVelocity2, 25);
  
  // start the robots running. true so that if we lose connection to either
  // robot, the run stops.
  robot1.runAsync(true);
  robot2.runAsync(true);

  // As long as both robot loops are running, lets alternate between the
  // two wandering around.
  curRobot=1;
  while (robot1.isRunning() && robot2.isRunning())
  {
    ArUtil::sleep(10000);
    if (curRobot == 1)
    {
      robot1.stop();
      robot2.clearDirectMotion();
      curRobot=2;
    }
    else
    {
      robot2.stop();
      robot1.clearDirectMotion();
      curRobot=1;
    }
  }
  
  // now exit
  Aria::exit(0);
  return 0;
}