00001 /*!@file SeaBee/MovementController.C Control seabee movement */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Michael Montalbo 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/MovementController.C $ 00035 // $Id: MovementController.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include "SeaBee/MovementController.H" 00039 #include "Component/ModelOptionDef.H" 00040 00041 00042 // ###################################################################### 00043 MovementController::MovementController(OptionManager& mgr, 00044 nub::soft_ref<SubController> subController, 00045 const std::string& descrName, 00046 const std::string& tagName): 00047 00048 ModelComponent(mgr, descrName, tagName), 00049 itsDepthErrThresh(&OPT_DepthErrThresh, this, ALLOW_ONLINE_CHANGES), 00050 itsHeadingErrThresh(&OPT_HeadingErrThresh, this, ALLOW_ONLINE_CHANGES), 00051 pipeP("pipeP", this, 0, ALLOW_ONLINE_CHANGES), 00052 pipeI("pipeI", this, 0, ALLOW_ONLINE_CHANGES), 00053 pipeD("pipeD", this, 0, ALLOW_ONLINE_CHANGES), 00054 itsPipePID(pipeP.getVal(), pipeI.getVal(), pipeD.getVal(), -20, 20, 00055 5, 0, 0, 100, -100, 150, true, 1, 25, -25), 00056 setDiveValue(&OPT_DiveValue, this, ALLOW_ONLINE_CHANGES), 00057 setGoStraightTimeValue(&OPT_GoStraightTime, this, ALLOW_ONLINE_CHANGES), 00058 setSpeedValue(&OPT_SpeedValue, this, ALLOW_ONLINE_CHANGES), 00059 setHeadingValue(&OPT_HeadingValue, this, ALLOW_ONLINE_CHANGES), 00060 setRelative("setRelative", this, false, ALLOW_ONLINE_CHANGES), 00061 setTimeout(&OPT_TimeoutValue, this, ALLOW_ONLINE_CHANGES), 00062 itsSubController(subController) 00063 { 00064 00065 } 00066 00067 // ###################################################################### 00068 MovementController::~MovementController() 00069 { 00070 00071 } 00072 00073 // ###################################################################### 00074 bool MovementController::dive(int depth, bool relative, int timeout) 00075 { 00076 LINFO("Dive to depth: %d", depth); 00077 if(relative) 00078 { 00079 int currentDepth = itsSubController->getDepth(); 00080 00081 // wait for depth to initialize 00082 if(currentDepth == -1) 00083 sleep(1); 00084 00085 currentDepth = itsSubController->getDepth(); 00086 if(currentDepth == -1) 00087 return false; 00088 00089 00090 itsSubController->setDepth(depth + currentDepth); 00091 LINFO("Diving to depth: %d", currentDepth + depth); 00092 } 00093 else 00094 { 00095 itsSubController->setDepth(depth); 00096 } 00097 00098 int time = 0; 00099 00100 while(itsSubController->getDepthErr() > itsDepthErrThresh.getVal()) 00101 { 00102 // LINFO("err: %d, thresh: %d",itsSubController->getDepthErr(),itsDepthErrThresh.getVal()); 00103 usleep(3000); 00104 time++; 00105 // if(time++ > timeout) return false; 00106 //<TODO mmontalbo> turn off diving 00107 // LINFO("err: %d, thresh: %d",itsSubController->getDepthErr(),itsDepthErrThresh.getVal()); 00108 } 00109 00110 return true; 00111 } 00112 00113 // ###################################################################### 00114 bool MovementController::goStraight(int speed, int time) 00115 { 00116 LINFO("Go Straight"); 00117 itsSubController->setHeading(itsSubController->getHeading()); 00118 itsSubController->setSpeed(speed); 00119 sleep(time); 00120 itsSubController->setSpeed(0); 00121 return true; 00122 } 00123 00124 // ###################################################################### 00125 bool MovementController::setHeading(int heading, bool relative, int timeout) 00126 { 00127 if(relative) 00128 { 00129 itsSubController->setHeading(heading + itsSubController->getHeading()); 00130 } 00131 else 00132 { 00133 itsSubController->setHeading(heading); 00134 } 00135 00136 int time = 0; 00137 00138 while(itsSubController->getHeadingErr() > itsHeadingErrThresh.getVal()) 00139 { 00140 // usleep(10000); 00141 if(time++ > timeout) return false; 00142 //<TODO mmontalbo> turn off turning 00143 } 00144 00145 return true; 00146 } 00147 00148 // ###################################################################### 00149 int MovementController::trackPipe(const Point2D<int>& pointToTrack, 00150 const Point2D<int>& desiredPoint) 00151 { 00152 float pipeCorrection = (float)itsPipePID.update(pointToTrack.i, desiredPoint.i); 00153 00154 // itsSubController->setHeading(itsSubController->getHeading() 00155 // + pipeCorrection); 00156 00157 itsSubController->setTurningSpeed(pipeCorrection); 00158 00159 return abs(pointToTrack.i - desiredPoint.i); 00160 } 00161 00162 // ###################################################################### 00163 void MovementController::paramChanged(ModelParamBase* const param, 00164 const bool valueChanged, 00165 ParamClient::ChangeStatus* status) 00166 { 00167 // if (param == &setDiveValue && valueChanged) 00168 // { 00169 // if(dive(setDiveValue.getVal(), setRelative.getVal(), setTimeout.getVal())) 00170 // LINFO("Dive completed successfully"); 00171 // else 00172 // LINFO("Dive failed"); 00173 // } 00174 // else if(param == &setGoStraightTimeValue && valueChanged) 00175 // { 00176 // if(goStraight(setSpeedValue.getVal(), setGoStraightTimeValue.getVal())) 00177 // LINFO("Go straight completed successfully"); 00178 // else 00179 // LINFO("Go straight failed"); 00180 // } 00181 // else if(param == &setHeadingValue && valueChanged) 00182 // { 00183 // if(setHeading(setHeadingValue.getVal(), setRelative.getVal(), setTimeout.getVal())) 00184 // LINFO("Turn completed successfully"); 00185 // else 00186 // LINFO("Turn failed"); 00187 // } 00188 //////// Pipe PID constants/gain change //////// 00189 if (param == &pipeP && valueChanged) 00190 itsPipePID.setPIDPgain(pipeP.getVal()); 00191 else if(param == &pipeI && valueChanged) 00192 itsPipePID.setPIDIgain(pipeI.getVal()); 00193 else if(param == &pipeD && valueChanged) 00194 itsPipePID.setPIDDgain(pipeD.getVal()); 00195 00196 } 00197 00198 // ###################################################################### 00199 /* So things look consistent in everyone's emacs... */ 00200 /* Local Variables: */ 00201 /* indent-tabs-mode: nil */ 00202 /* End: */