File: sickSafeAutoLogger.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 (269 lines) | stat: -rw-r--r-- 7,306 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
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
/*
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"

int main(int argc, char **argv)
{
  bool done;

  bool shouldSpin = true;
  int spinTime = 10000;
  double distToTravel = 1000;

  double rotVelMax = 50;
  double rotAccel = 100;
  double rotDecel = 100;
  double transVelMax = 1500;
  double transAccel = 600;
  double transDecel = 600;

  Aria::init();


    // set up our parser
  ArArgumentParser parser(&argc, argv);
  // set up our simple connector
  ArSimpleConnector simpleConnector(&parser);
  // robot
  ArRobot robot;
  // a laser in case one is used
  ArSick sick;
  // sonar, must be added to the robot, for teleop and wander
  ArSonarDevice sonarDev;


  parser.addDefaultArgument("-connectLaser -laserIncrement half");

  // load the default arguments 
  parser.loadDefaultArguments();

  // parse the command line... fail and print the help if the parsing fails
  // or if the help was requested
  if (!Aria::parseArgs() || !parser.checkHelpAndWarnUnparsed())
  {    
    Aria::logOptions();
    exit(1);
  }
  
  //ArLog::init(ArLog::StdOut, ArLog::Verbose);

  // if you're using a program at startup in linux you must NOT
  // include a keyhandler or you'll get lockups when trying to read
  // keys with no terminal

  // a key handler so we can do our key handling
  ArKeyHandler keyHandler;
  // let the global aria stuff know about it
  Aria::setKeyHandler(&keyHandler);
  // toss it on the robot
  robot.attachKeyHandler(&keyHandler);
  printf("You may press escape to exit\n");

  // add the sonar to the robot
  robot.addRangeDevice(&sonarDev);
  // add the laser to the robot
  robot.addRangeDevice(&sick);

  // add a gyro, it'll see if it should attach to the robot or not
  ArAnalogGyro gyro(&robot);

  // set up the robot for connecting
  if (!simpleConnector.connectRobot(&robot))
  {
    printf("Could not connect to robot... exiting\n");
    Aria::exit(1);
  }

  robot.setRotVelMax(rotVelMax);
  robot.setRotAccel(rotAccel);
  robot.setRotDecel(rotDecel);
  robot.setTransVelMax(transVelMax);
  robot.setTransAccel(transAccel);
  robot.setTransDecel(transDecel);

  // start the robot running, true so that if we lose connection the run stops
  robot.runAsync(true);

  // set up the laser before handing it to the laser mode
  sick.runAsync();

  // connect the laser if it was requested
  if (!simpleConnector.connectLaser(&sick))
  {
    printf("Could not connect to laser... exiting\n");
    Aria::exit(2);
  }

  ArUtil::sleep(300);

  robot.lock();

  // set a default filename
  //std::string filename = "c:\\log\\1scans.2d";
  std::string filename = "1scans.2d";
  // see if we want to use a different filename
  //if (argc > 1)
  printf("Logging to file %s\n", filename.c_str());
  // start the logger with good values
  ArSickLogger logger(&robot, &sick, 300, 25, filename.c_str());

  //Make a couple action groups, one for driving in a straight line, one for turning
  
  ArActionGroup drive(&robot);
  ArActionDriveDistance driveAction("driveDistance", transVelMax, transDecel);
  ArActionDeceleratingLimiter limiterAction;
  limiterAction.setParameters(200, 100, 100, 0, 100, 100, 2000, 1000, true, 0);

  drive.addAction(&driveAction, 50);
  drive.addAction(&limiterAction, 75);

  ArActionGroup spin(&robot);
  ArActionInput spinAction;
  
  spin.addAction(&spinAction, 100);

		  

  robot.unlock();

#ifdef WIN32
  // wait until someone pushes the motor button to go (since vnc hoses
  // timing)
  while (1)
  {
    robot.lock();
    if (!robot.isRunning())
      exit(0);
    if (robot.areMotorsEnabled())
    {
      robot.unlock();
      break;
    }
    robot.unlock();
    ArUtil::sleep(100);
  }
#endif

  // basically from here on down the robot just cruises around a bit

  robot.lock();
  // enable the motors
  robot.comInt(ArCommands::ENABLE, 1);

  ArTime startTime;

  ArLog::log(ArLog::Normal, "Driving out");
  // move a couple meters
  drive.activateExclusive();
  driveAction.setDistance(distToTravel);
  robot.unlock();
  done = false;
  startTime.setToNow();
  do {
    ArUtil::sleep(100);
    robot.lock();
    done = (driveAction.haveAchievedDistance() || startTime.secSince() > 10 || 
	    (startTime.secSince() > 2 && fabs(robot.getVel() < 2)));
    robot.unlock();
  } while (!done);


  if (shouldSpin)
  {
    ArLog::log(ArLog::Normal, "Spinning counter clockwise");
    robot.lock();
    spin.activateExclusive();
    spinAction.setRotVel(rotVelMax);
    spinAction.setVel(0);
    robot.unlock();
    ArUtil::sleep(spinTime);
    ArLog::log(ArLog::Normal, "Spinning clockwise");
    robot.lock();
    spin.activateExclusive();
    spinAction.setRotVel(-rotVelMax);
    spinAction.setVel(0);
    robot.unlock();
    ArUtil::sleep(spinTime);
  }

  ArLog::log(ArLog::Normal, "Pointing back");
  robot.lock();
  spin.activateExclusive();
  spinAction.setHeading(180);
  robot.unlock();
  done = false;
  startTime.setToNow();
  do {
    ArUtil::sleep(100);
    robot.lock();
    done = (fabs(ArMath::subAngle(robot.getTh(), 180)) < 2 && 
	    fabs(robot.getRotVel() < 2)) || startTime.secSince() > 10;
    robot.unlock();
  } while (!done);

  ArLog::log(ArLog::Normal, "Driving back");
  robot.lock();
  drive.activateExclusive();
  driveAction.setDistance(distToTravel);
  robot.unlock();
  done = false;
  startTime.setToNow();
  do {
    ArUtil::sleep(100);
    robot.lock();
    done = (driveAction.haveAchievedDistance() || startTime.secSince() > 10 || 
	    (startTime.secSince() > 2 && fabs(robot.getVel() < 2)));
    robot.unlock();
  } while (!done);


  ArLog::log(ArLog::Normal, "Pointing out");
  robot.lock();
  spin.activateExclusive();
  spinAction.setHeading(0);
  robot.unlock();
  done = false;
  startTime.setToNow();
  do {
    ArUtil::sleep(100);
    robot.lock();
    done = (fabs(ArMath::subAngle(robot.getTh(), 0)) < 2 && 
	    fabs(robot.getRotVel() < 2)) || startTime.secSince() > 10;
    robot.unlock();
  } while (!done);

  ArLog::log(ArLog::Normal, "Done");
  sick.lockDevice();
  sick.disconnect();
  sick.unlockDevice();
  robot.lock();
  robot.disconnect();
  robot.unlock();
  // now exit
  Aria::shutdown();
  return 0;
}