00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "BeoSub/BeoSubAction.H"
00039 #include "BeoSub/BeoSub.H"
00040 #include <cmath>
00041
00042
00043
00044
00045 BeoSubAction::BeoSubAction(BeoSub *bs, const bool pulseWidthControl) :
00046 itsBeoSub(bs), itsDeadBand(10.0f), pulseWidthControl(pulseWidthControl)
00047 { }
00048
00049
00050 BeoSubAction::~BeoSubAction()
00051 { }
00052
00053
00054 bool BeoSubAction::execute(const float target, const bool stabil, const int itsMaxIter)
00055 {
00056
00057 if(pulseWidthControl){
00058 return pulseWidthExecute(target, stabil, itsMaxIter);
00059 }
00060 int iter = 0;
00061 itsErrIndex = 0;
00062 itsErrorHistory[itsErrIndex] = difference(getPosition(), target);
00063
00064
00065 while(iter < itsMaxIter)
00066 {
00067 float pos = getPosition();
00068
00069 itsErrorHistory[(itsErrIndex+1)%20] = difference(pos, target);
00070
00071 float errDiff = itsErrorHistory[(itsErrIndex+1)%20] - itsErrorHistory[itsErrIndex];
00072
00073 LDEBUG("pos = %f, target = %f", pos, target);
00074 LDEBUG("Current error = %f, Previous Error = %f, errDiff = %f", itsErrorHistory[(itsErrIndex+1)%20], itsErrorHistory[itsErrIndex], errDiff);
00075
00076
00077 if (fabsf(difference(pos, target)) < itsDeadBand) return true;
00078
00079
00080 float totalErr = 0;
00081 for(int i = 0; i < 20; i ++)
00082 totalErr += itsErrorHistory[i];
00083
00084 LDEBUG("Total error = %f", totalErr);
00085
00086
00087 u = itsGainP*itsErrorHistory[(itsErrIndex+1)%20]
00088 + itsGainD*errDiff
00089 + itsGainI*totalErr;
00090 LDEBUG("u = %f", u);
00091 int numOfPulses;
00092 if(fabsf(u) > 8 )
00093 numOfPulses = 8;
00094 else
00095 numOfPulses = int(fabsf(u));
00096
00097 for(float i = 0; i < numOfPulses; i++){
00098 activate(u > 0);
00099
00100 if (stabil) stabilize();
00101 }
00102
00103 iter ++;
00104 itsErrIndex = (itsErrIndex +1)%20;
00105 usleep(50000);
00106 }
00107
00108 LINFO("Max number (%d) of iterations reached -- GIVING UP", itsMaxIter);
00109 return false;
00110 }
00111
00112
00113
00114 bool BeoSubAction::pulseWidthExecute(const float target, const bool stabil, const int itsMaxIter)
00115 {
00116 int iter = 0;
00117 itsErrIndex = 0;
00118 itsErrorHistory[itsErrIndex] = difference(getPosition(), target);
00119
00120
00121 while(iter < itsMaxIter)
00122 {
00123 float pos = getPosition();
00124
00125 itsErrorHistory[(itsErrIndex+1)%20] = difference(pos, target);
00126
00127 float errDiff = itsErrorHistory[(itsErrIndex+1)%20] - itsErrorHistory[itsErrIndex];
00128
00129 LDEBUG("Pulse Width: pos = %f, target = %f", pos, target);
00130 LDEBUG("Pulse Width: Current error = %f, Previous Error = %f, errDiff = %f", itsErrorHistory[(itsErrIndex+1)%20], itsErrorHistory[itsErrIndex], errDiff);
00131
00132
00133 if (fabsf(difference(pos, target)) < itsDeadBand) return true;
00134
00135
00136 float totalErr = 0;
00137 for(int i = 0; i < 20; i ++)
00138 totalErr += itsErrorHistory[i];
00139
00140 LDEBUG("Pulse Width: Total error = %f", totalErr);
00141
00142
00143 u = itsGainP*itsErrorHistory[(itsErrIndex+1)%20]
00144 + itsGainD*errDiff
00145 + itsGainI*totalErr;
00146 LDEBUG("u = %f", u);
00147 if(turnOnTime < 0)
00148 turnOnTime ++;
00149
00150 if(turnOnTime == 0){
00151 turnOnTime = int(fabsf(u));
00152 turnOnMotor(u > 0);
00153 }
00154
00155 if(turnOnTime > 0){
00156 turnOnTime --;
00157 if(turnOnTime == 0)
00158 turnOffMotor(u > 0);
00159 turnOnTime = -3;
00160 }
00161
00162
00163 if (stabil) stabilize(pulseWidthControl);
00164 iter ++;
00165 itsErrIndex = (itsErrIndex +1)%20;
00166 usleep(50000);
00167 }
00168
00169 LINFO("Max number (%d) of iterations reached -- GIVING UP", itsMaxIter);
00170 return false;
00171 }
00172
00173
00174
00175
00176
00177 BeoSubActionDive::BeoSubActionDive(BeoSub *bs, const bool pulseWidthControl) :
00178 BeoSubAction(bs, pulseWidthControl)
00179 {
00180 itsDeadBand = 5.0f;
00181
00182 if(pulseWidthControl){
00183 itsGainP = 10.0f/5;
00184 itsGainD = 0;
00185 itsGainI = 0.01f/1;
00186 }else{
00187 itsGainP = 13.0f/5;
00188 itsGainD = 0;
00189 itsGainI = 0.01f/1;
00190 }
00191
00192 itsHeading = itsBeoSub->getHeading();
00193 }
00194
00195
00196 BeoSubActionDive::~BeoSubActionDive()
00197 { }
00198
00199
00200 bool BeoSubActionDive::activate(const bool incr)
00201 {
00202
00203 itsBeoSub->pulseMotor(BSM_UPDOWN, !incr);
00204 return true;
00205 }
00206
00207
00208 bool BeoSubActionDive::turnOnMotor(const bool incr){
00209 itsBeoSub->turnOnMotor(BSM_UPDOWN, !incr);
00210 return true;
00211 }
00212
00213
00214 bool BeoSubActionDive::turnOffMotor(const bool incr){
00215 itsBeoSub->turnOffMotor(BSM_UPDOWN);
00216 return true;
00217 }
00218
00219
00220 float BeoSubActionDive::getPosition() const
00221 { return itsBeoSub->getDepth(); }
00222
00223
00224 bool BeoSubActionDive::stabilize(const bool pulseWidthControl)
00225 {
00226 BeoSubActionTurn t(itsBeoSub, pulseWidthControl);
00227
00228 return t.execute(itsHeading, false, 1);
00229 }
00230
00231
00232 float BeoSubActionDive::difference(float pos, float target)
00233 {
00234 return pos - target;
00235 }
00236
00237
00238
00239
00240
00241 BeoSubActionTurn::BeoSubActionTurn(BeoSub *bs, const bool pulseWidthControl) :
00242 BeoSubAction(bs, pulseWidthControl)
00243 {
00244 itsDeadBand = 10.0f;
00245
00246 if(pulseWidthControl){
00247 itsGainP = 5.0f/20;
00248 itsGainD = 0;
00249 itsGainI = 0.01f/5;
00250 }
00251 else{
00252 itsGainP = 5.0f/20;
00253 itsGainD = 0;
00254 itsGainI = 0.01f/5;
00255 }
00256
00257 itsDepth = itsBeoSub->getDepth();
00258 }
00259
00260
00261 BeoSubActionTurn::~BeoSubActionTurn()
00262 { }
00263
00264
00265 bool BeoSubActionTurn::activate(const bool incr)
00266 {
00267
00268 itsBeoSub->pulseMotor(BSM_LEFTRIGHT, incr);
00269 return true;
00270 }
00271
00272 bool BeoSubActionTurn::turnOnMotor(const bool incr){
00273 itsBeoSub->turnOnMotor(BSM_UPDOWN, !incr);
00274 return true;
00275 }
00276
00277
00278 bool BeoSubActionTurn::turnOffMotor(const bool incr){
00279 itsBeoSub->turnOffMotor(BSM_UPDOWN);
00280 return true;
00281 }
00282
00283
00284 float BeoSubActionTurn::getPosition() const
00285 { return itsBeoSub->getHeading(); }
00286
00287
00288 bool BeoSubActionTurn::stabilize(const bool pulseWidthControl)
00289 {
00290 BeoSubActionDive d(itsBeoSub, pulseWidthControl);
00291 return d.execute(itsDepth, false, 1);
00292 }
00293
00294
00295 float BeoSubActionTurn::difference(float pos, float target)
00296 {
00297
00298 if(fabsf(pos - target) > 180){
00299 if(pos > target)
00300 return target + 360 - pos;
00301 else
00302 return pos + 360 - target;
00303 }
00304 else
00305 return (pos - target);
00306
00307 }
00308
00309
00310
00311
00312
00313