File: amptuDemo.cpp

package info (click to toggle)
libaria 2.8.0%2Brepack-1.2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 13,680 kB
  • ctags: 16,626
  • sloc: cpp: 135,490; makefile: 926; python: 597; java: 570; ansic: 182
file content (198 lines) | stat: -rw-r--r-- 5,601 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
/*
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"

/*
  This is basically just a demo of how to use the AMPTU, but made fancy so
  you can drive around the robot and look at stuff with the camera.  Press
  button 1 to drive the robot and button two to move the camera.
*/

/*
  This class is the core of this demo, it adds itself to the robot given
  as a user task, then drives the robot and camera from the joystick
*/
class Joydrive
{
public:
  // constructor
  Joydrive(ArRobot *robot);
  // destructor, its just empty
  ~Joydrive(void) {}
  
  // the callback function
  void drive(void);

protected:
  // the rotational max for the robot
  int myRotValRobot;
  // translational max for the robot
  int myTransValRobot;
  // pan max for the camera
  int myPanValCamera;
  // tilt max for the camera
  int myTiltValCamera;
  // the joystick handler
  ArJoyHandler myJoyHandler;
  // the camera
  ArAMPTU myCam;
  // whether the camera has been inited or not
  bool myCamInited;
  // pointer to the robot
  ArRobot *myRobot;
  // callback for the drive function
  ArFunctorC<Joydrive> myDriveCB;
};

/*
  Constructor, sets the robot pointer, and some initial values, also note the
  use of constructor chaining on myCam and myDriveCB.
*/
Joydrive::Joydrive(ArRobot *robot) :
  myCam(robot),
  myDriveCB(this, &Joydrive::drive)
{
  // set the robot pointer and add the joydrive as a user task
  myRobot = robot;
  myRobot->addUserTask("joydrive", 50, &myDriveCB);

  // initialize some variables
  myRotValRobot = 100;
  myTransValRobot = 700;
  myPanValCamera = 8;
  myTiltValCamera = 6;
  myCamInited = false;

  // initialize the joystick
  myJoyHandler.init();
  // see if we have a joystick, and let user know the results
  if (myJoyHandler.haveJoystick())
  {
    printf("Have a joystick\n\n");
  }
  // we don't have a joystick, so get out 
  else
  {
    printf("Do not have a joystick, set up the joystick then rerun the program\n\n");
    Aria::shutdown();
    exit(0);    
  }
}

// the important function
void Joydrive::drive(void)
{
  int trans, rot;
  int pan, tilt;

  // if the camera isn't initialized, initialize it here... it has to be 
  // done here instead of above because it needs to be done when the 
  // robot is connected
  if (!myCamInited && myRobot->isConnected())
  {
    myCam.init();
    myCamInited = true;
  }

  // if joystick button one is pushed, then drive the robot
  if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(1))
  {
    // set the speeds on the joyhandler so we get the values out we want
    myJoyHandler.setSpeeds(myRotValRobot, myTransValRobot);
    // get the values from the joyhandler
    myJoyHandler.getAdjusted(&rot, &trans);
    // set the velocities
    myRobot->setVel(trans);
    myRobot->setRotVel(-rot);
  }
  // if the joystick button one isn't pushed, stop the robot
  else
  {
    myRobot->setVel(0);
    myRobot->setRotVel(0);
  }
  
  // if button two is pressed then move the camera
  if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(2))
  {
    // set the speeds so we get out the values we want
    myJoyHandler.setSpeeds(myPanValCamera, myTiltValCamera);
    // get the values
    myJoyHandler.getAdjusted(&pan, &tilt);
    // move the camera
    myCam.panTilt(myCam.getPan() + pan, myCam.getTilt() + tilt);
  } 

}

int main(int argc, char **argv) 
{
  // just some stuff for returns
  std::string str;
  int ret;
  
  // robots connection
  ArSerialConnection con;
  // the robot, this turns state reflection off
  ArRobot robot(NULL, false);
  // the joydrive as defined above, this also adds itself as a user task
  Joydrive joyd(&robot);

  // mandatory init
  Aria::init();

  // open the connection, if it fails, exit
  if ((ret = con.open()) != 0)
  {
    str = con.getOpenMessage(ret);
    printf("Open failed: %s\n", str.c_str());
    Aria::shutdown();
    return 1;
  }

  // set the connection o nthe robot
  robot.setDeviceConnection(&con);
  // connect, if we fail, exit
  if (!robot.blockingConnect())
  {
    printf("Could not connect to robot... exiting\n");
    Aria::shutdown();
    return 1;
  }

  // turn off the sonar, enable the motors, turn off amigobot sounds
  robot.comInt(ArCommands::SONAR, 0);
  robot.comInt(ArCommands::ENABLE, 1);
  robot.comInt(ArCommands::SOUNDTOG, 0);

  // run, if we lose connection to the robot, exit
  robot.run(true);
  
  // shutdown and go away
  Aria::shutdown();
  return 0;
}