MOOS 0.2375
|
00001 00002 // 00003 // MOOS - Mission Oriented Operating Suite 00004 // 00005 // A suit of Applications and Libraries for Mobile Robotics Research 00006 // Copyright (C) 2001-2005 Massachusetts Institute of Technology and 00007 // Oxford University. 00008 // 00009 // This software was written by Paul Newman at MIT 2001-2002 and Oxford 00010 // University 2003-2005. email: pnewman@robots.ox.ac.uk. 00011 // 00012 // This file is part of a MOOS Core Component. 00013 // 00014 // This program is free software; you can redistribute it and/or 00015 // modify it under the terms of the GNU General Public License as 00016 // published by the Free Software Foundation; either version 2 of the 00017 // License, or (at your option) any later version. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 // General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00030 // PitchZPID.cpp: implementation of the CPitchZPID class. 00031 // 00033 #include <MOOSGenLib/MOOSGenLibGlobalHelper.h> 00034 #include <math.h> 00035 #include "PitchZPID.h" 00036 00038 // Construction/Destruction 00040 00041 CPitchZPID::CPitchZPID() 00042 { 00043 00044 m_dfPitchDesired = 0.0; 00045 00046 m_PitchPID.SetGains(MOOSDeg2Rad(10),0,0); 00047 m_PitchPID.SetLimits(MOOSDeg2Rad(10),MOOSDeg2Rad(15.0)); 00048 00049 //by default we control in +ve X direction 00050 m_bIsDepth = false; 00051 00052 m_bReversing = false; 00053 } 00054 00055 CPitchZPID::~CPitchZPID() 00056 { 00057 00058 } 00059 00060 00061 bool CPitchZPID::Run(double dfZError, double dfErrorTime, double &dfOut, double dfPitch,double dfPitchTime) 00062 { 00063 //dfError is Z error 00064 //calculate pitch change... 00065 //+ve pitch and forward makes us go up in Z direction... 00066 if(m_bIsDepth) 00067 { 00068 //if we are actually controlling depth then this varies 00069 //with -z 00070 //m_dfPitchDesired = -m_dfPitchDesired; 00071 dfZError = -dfZError; 00072 } 00073 00074 if(m_bReversing) 00075 { 00076 dfZError = -dfZError; 00077 } 00078 00079 //we are controlling on depth via pitch 00080 //to get around logging issues we need 00081 //to keep a copy of our Z goal 00082 double dfGoalCopy = m_dfGoal; 00083 00084 //call overloaded version that may spot change in direction 00085 //due to really controlling depth (-Z) 00086 m_PitchPID.SetGoal(m_dfGoal); 00087 00088 //figure out a desired pitch... 00089 if(!m_PitchPID.Run(dfZError,dfErrorTime,m_dfPitchDesired)) 00090 return false; 00091 00092 //get pitch error signal... 00093 double dfPitchError = m_dfPitchDesired-dfPitch; 00094 00095 //reset our goal to pitch temporarily 00096 //not I'm not using the class method as 00097 //this may negate goal if depth controlling! 00098 CScalarPID::SetGoal(m_dfPitchDesired); 00099 00100 //use this as input into the base type controller (controlling pitch) 00101 if(CScalarPID::Run(dfPitchError,dfPitchTime,dfOut)) 00102 { 00103 if(m_bReversing) 00104 { 00105 dfOut = dfOut; 00106 } 00107 else 00108 { 00109 //finally positive elevator makes vehicle dive... 00110 //so dfOut is requiring more positive pitch we need more NEGATIVE 00111 //elevator 00112 dfOut = -dfOut; 00113 //MOOSTrace("Desired = %f, Error = %f, Cmd = %f\n",dfPitchDesired,dfPitchError,dfOut); 00114 } 00115 CScalarPID::SetGoal(dfGoalCopy); 00116 return true; 00117 } 00118 else 00119 { 00120 CScalarPID::SetGoal(dfGoalCopy); 00121 return false; 00122 } 00123 } 00124 00125 00126 bool CPitchZPID::SetAsDepthController(bool bDepth) 00127 { 00128 m_bIsDepth = bDepth; 00129 return true; 00130 } 00131 00132 bool CPitchZPID::SetReversing(bool bReverse) 00133 { 00134 m_bReversing = bReverse; 00135 return true; 00136 } 00137 00138 bool CPitchZPID::SetLogPath(std::string &sPath) 00139 { 00140 m_sLogPath = sPath; 00141 m_PitchPID.SetLogPath(sPath); 00142 return true; 00143 } 00144 00145 00146 bool CPitchZPID::SetGains(double dfZToPitchKp, 00147 double dfZToPitchKd, 00148 double dfZToPitchKi, 00149 double dfPitchKp, 00150 double dfPitchKd, 00151 double dfPitchKi) 00152 { 00153 00154 00155 //we have another 'owned controller' that maps z error to pitch 00156 m_PitchPID.SetGains(dfZToPitchKp,dfZToPitchKd,dfZToPitchKi); 00157 00158 std::string sPitch = MOOSFormat("%sZToPitch",m_sName.c_str()); 00159 m_PitchPID.SetName(sPitch); 00160 m_PitchPID.SetLog(m_bLog); 00161 00162 00163 //the pitch controller is our fundamental concern 00164 CScalarPID::SetGains(dfPitchKp,dfPitchKd,dfPitchKi); 00165 std::string sMe = MOOSFormat("%sPitchToElevator",m_sName.c_str()); 00166 SetName(sMe); 00167 00168 00169 return true; 00170 } 00171 00172 bool CPitchZPID::SetLimits(double dfMaxPitch, double dfMaxElevator, double dfPitchIntegralLimit, double dfElevatorIntegralLimit) 00173 { 00174 //set limits on depth to pitch control 00175 m_PitchPID.SetLimits(dfPitchIntegralLimit,dfMaxPitch); 00176 00177 //set limit on elevator control 00178 CScalarPID::SetLimits(dfElevatorIntegralLimit,dfMaxElevator); 00179 00180 return true; 00181 } 00182 00183 bool CPitchZPID::SetGoal(double dfGoal) 00184 { 00185 if(m_bIsDepth) 00186 { 00187 m_dfGoal = -dfGoal; 00188 } 00189 return true; 00190 }