Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

ArServerModeDrive.cpp

Go to the documentation of this file.
00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 #include "ArExport.h"
00028 #include "ArServerModeDrive.h"
00029 
00030 AREXPORT ArServerModeDrive::ArServerModeDrive(ArServerBase *server, 
00031                                               ArRobot *robot,
00032                                               bool takeControlOnJoystick) : 
00033   ArServerMode(robot, server, "drive"),
00034   myDriveGroup(robot),
00035   myServerDriveJoystickCB(this, &ArServerModeDrive::serverDriveJoystick),
00036   myJoyUserTaskCB(this, &ArServerModeDrive::joyUserTask),
00037   myServerSafeDrivingEnableCB(this, 
00038                               &ArServerModeDrive::serverSafeDrivingEnable),
00039   myServerSafeDrivingDisableCB(this, 
00040                               &ArServerModeDrive::serverSafeDrivingDisable)
00041 {
00042   myHandlerCommands = NULL;
00043   myTakeControlOnJoystick = takeControlOnJoystick;
00044   myDriveSafely = true;
00045   myJoydriveAction.setStopIfNoButtonPressed(false);
00046   myDriveGroup.addAction(&myJoydriveAction, 75);
00047   myDriveGroup.addAction(&myStopAction, 60);
00048   myInputAction = myDriveGroup.getActionInput();
00049   setThrottleParams(200, 2000);
00050   myExtraUnsafeAction = NULL;
00051   if (myServer != NULL)
00052   {
00053     addModeData("driveJoystick", 
00054                 "drives the robot as with a joystick",
00055                 &myServerDriveJoystickCB,
00056                 "byte2: vel, byte2: rotVel",
00057                 "none", "Movement", "RETURN_NONE");    
00058   }
00059   if ((myJoyHandler = Aria::getJoyHandler()) == NULL)
00060   {
00061     myJoyHandler = new ArJoyHandler;
00062     myJoyHandler->init();
00063     Aria::setJoyHandler(myJoyHandler);
00064   }
00065   myRobot->addUserTask("driveJoyUserTask", 75, &myJoyUserTaskCB);
00066 }
00067 
00068 AREXPORT ArServerModeDrive::~ArServerModeDrive()
00069 {
00070 
00071 }
00072 
00073 AREXPORT void ArServerModeDrive::activate(void)
00074 {
00075   driveJoystick(0, 0);
00076 }
00077 
00078 AREXPORT void ArServerModeDrive::deactivate(void)
00079 {
00080   myDriveGroup.deactivate();
00081   baseDeactivate();
00082 }
00083 
00084 AREXPORT void ArServerModeDrive::setSafeDriving(bool safe)
00085 {
00086   // if this is a change then print it
00087   if (safe != myDriveSafely)
00088   {
00089     if (safe)
00090     {
00091       ArLog::log(ArLog::Normal, "Driving safely again");
00092     }
00093     else
00094     {
00095       ArLog::log(ArLog::Normal, "Driving UNSAFELY");
00096     }
00097     myNewDriveSafely = true;
00098   }
00099   myDriveSafely = safe;
00100   if (isActive())
00101     driveJoystick(myVel, myRotVel);
00102 }
00103 
00104 AREXPORT bool ArServerModeDrive::getSafeDriving(void)
00105 {
00106   return myDriveSafely;
00107 }
00108 
00109 AREXPORT void ArServerModeDrive::serverSafeDrivingEnable(void)
00110 {
00111   myRobot->lock();
00112   setSafeDriving(true);
00113   myRobot->unlock();
00114 }
00115 
00116 AREXPORT void ArServerModeDrive::serverSafeDrivingDisable(void)
00117 {
00118   myRobot->lock();
00119   setSafeDriving(false);
00120   myRobot->unlock();
00121 }
00122 
00123 AREXPORT void ArServerModeDrive::addControlCommands(ArServerHandlerCommands *handlerCommands)
00124 {
00125   myHandlerCommands = handlerCommands;
00126   myHandlerCommands->addCommand(
00127           "safeDrivingEnable",
00128           "Enables safe driving, which will attempt to prevent collisions (default)",
00129           &myServerSafeDrivingEnableCB, "UnsafeMovement");
00130   myHandlerCommands->addCommand(
00131           "safeDrivingDisable",
00132           "Disables safe driving, this is UNSAFE and will let you drive your robot into things or down stairs, use at your own risk",
00133           &myServerSafeDrivingDisableCB, "UnsafeMovement");
00134 }
00135 
00136 AREXPORT void ArServerModeDrive::setThrottleParams(int lowSpeed, int highSpeed)
00137 {
00138   myJoydriveAction.setThrottleParams(lowSpeed, highSpeed);
00139 }
00140   
00141 AREXPORT void ArServerModeDrive::driveJoystick(double vel, double rotVel)
00142 {
00143   bool wasActive;
00144   wasActive = isActive();
00145   if (!wasActive && !baseActivate())
00146     return;
00147   if (!wasActive || myNewDriveSafely)
00148   {
00149     myRobot->clearDirectMotion();
00150     if (myDriveSafely)
00151     {
00152       myDriveGroup.activateExclusive();
00153       myMode = "Drive";
00154       ArLog::log(ArLog::Verbose, "Driving safely");
00155     }
00156     else
00157     {
00158       myRobot->deactivateActions();
00159       myJoydriveAction.activate();
00160       myInputAction->activate();
00161       if (myExtraUnsafeAction != NULL)
00162         myExtraUnsafeAction->activate();
00163       myMode = "UNSAFE Drive";
00164       ArLog::log(ArLog::Verbose, "Driving unsafely");
00165     }
00166   }
00167   myNewDriveSafely = false;
00168   myVel = vel;
00169   myRotVel = rotVel;
00170   myDriveGroup.setVel(vel);
00171   myDriveGroup.setRotVel(rotVel);
00172   setActivityTimeToNow();
00173 }
00174 
00175 AREXPORT void ArServerModeDrive::serverDriveJoystick(ArServerClient *client, 
00176                                                      ArNetPacket *packet)
00177 {
00178   double vel;
00179   double rotVel;
00180 
00181   vel = packet->bufToByte2();
00182   rotVel = packet->bufToByte2();
00183   myRobot->lock();
00184   driveJoystick(vel, rotVel);
00185   myRobot->unlock();
00186 }
00187 
00188 AREXPORT void ArServerModeDrive::joyUserTask(void)
00189 {
00190   // if we're not active but we should be
00191   if (myTakeControlOnJoystick && !isActive() && 
00192       myJoyHandler->haveJoystick() && myJoyHandler->getButton(1))
00193   {
00194     if (ArServerMode::getActiveMode() != NULL)
00195       ArLog::log(ArLog::Normal, 
00196                  "ArServerModeDrive: Activating instead of %s because of local joystick", ArServerMode::getActiveMode()->getName());
00197     else
00198       ArLog::log(ArLog::Normal, 
00199                  "ArServerModeDrive: Activating because of local joystick");
00200     activate();
00201   }
00202 }
00203 
00204 AREXPORT void ArServerModeDrive::userTask(void)
00205 {
00206   // Sets the robot so that we always thing we're trying to move in
00207   // this mode
00208   myRobot->forceTryingToMove();
00209 
00210   if (myJoyHandler->haveJoystick() && myJoyHandler->getButton(1))
00211     setActivityTimeToNow();
00212   if (!myStatusSetThisCycle)
00213   {
00214     if (myRobot->isLeftMotorStalled() || myRobot->isRightMotorStalled())
00215       myStatus = "Stalled";
00216     // this works because if the motors stalled above caught it, if
00217     // not and more values it means a stall
00218     else if (myRobot->getStallValue())
00219       myStatus = "Bumped";
00220     else if (ArMath::fabs(myRobot->getVel()) < 2 && 
00221              ArMath::fabs(myRobot->getRotVel()) < 2)
00222       myStatus = "Stopped";
00223     else
00224       myStatus = "Driving";
00225   }
00226   myStatusSetThisCycle = false;
00227 }
00228 
00229 
00230 
00231 

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0