ALMA Computing Group

acsexmplSlowMountImpl.cpp

Go to the documentation of this file.
00001 /******************************************************************************* 00002 * ALMA - Atacama Large Millimiter Array 00003 * (c) European Southern Observatory, 2002 00004 * Copyright by ESO (in the framework of the ALMA collaboration) 00005 * and Cosylab 2002, All rights reserved 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * 00022 * 00023 * "@(#) $Id: acsexmplSlowMountImpl.cpp,v 1.13 2008/10/01 04:30:47 cparedes Exp $" 00024 * 00025 * who when what 00026 * -------- -------- ---------------------------------------------- 00027 * acaproni 2004--8-16 Created from acsexmplMountImpl.cpp 00028 */ 00029 00035 #include <acsexmplSlowMountImpl.h> 00036 #include <baciDB.h> 00037 #include <iostream> 00038 #include <time.h> 00039 #include <math.h> 00040 00041 ACE_RCSID(acsexmpl, acsexmplSlowMountImpl, "$Id: acsexmplSlowMountImpl.cpp,v 1.13 2008/10/01 04:30:47 cparedes Exp $") 00042 using namespace baci; 00043 00050 struct __obstar_action { 00051 double ra; 00052 double dec; 00053 double pmRa; 00054 double pmDec; 00055 double radVel; 00056 double par; 00057 MOUNT_ACS::Mount::coordType type; 00058 }; 00059 00060 struct __objfix_action { 00061 double az; 00062 double elev; 00063 }; 00064 00065 SlowMount::SlowMount( 00066 ACE_CString _name, 00067 maci::ContainerServices * containerServices) : 00068 CharacteristicComponentImpl(_name, containerServices), 00069 m_cmdAz_sp(new ROdouble(_name+":cmdAz", getComponent()),this), 00070 m_cmdEl_sp(new ROdouble(_name+":cmdEl", getComponent()),this), 00071 m_actAz_sp(new ROdouble(_name+":actAz", getComponent()),this), 00072 m_actEl_sp(new ROdouble(_name+":actEl", getComponent()),this) 00073 { 00074 ACS_TRACE("::SlowMount::SlowMount"); 00075 00076 // register actions for use with invokeAction(...) 00077 m_actions[0] = &SlowMount::obstarAction; 00078 m_actions[1] = &SlowMount::objfixAction; 00079 } 00080 00081 SlowMount::~SlowMount() 00082 { 00083 ACS_TRACE("::SlowMount::~SlowMount"); 00084 } 00085 00086 /* --------------- [ Action implementator interface ] -------------- */ 00087 00088 ActionRequest 00089 SlowMount::invokeAction (int function, 00090 BACIComponent *cob_p, 00091 const int &callbackID, 00092 const CBDescIn &descIn, 00093 BACIValue *value_p, 00094 Completion &completion, 00095 CBDescOut &descOut) 00096 { 00097 ACS_TRACE("SlowMount::invokeAction"); 00098 if (function < 2) 00099 { 00100 // call the asynchronous method 00101 return (this->*m_actions[function])(cob_p, callbackID, descIn, value_p, completion, descOut); 00102 } 00103 else 00104 { 00105 return reqDestroy; 00106 } 00107 } 00108 00109 /* ------------------ [ Action implementations ] ----------------- */ 00110 00112 ActionRequest 00113 SlowMount::obstarAction (BACIComponent *cob_p, 00114 const int &callbackID, 00115 const CBDescIn &descIn, 00116 BACIValue *value_p, 00117 Completion &completion, 00118 CBDescOut &descOut) 00119 { 00120 ACE_UNUSED_ARG(cob_p); 00121 ACE_UNUSED_ARG(callbackID); 00122 ACE_UNUSED_ARG(descIn); 00123 ACE_UNUSED_ARG(descOut); 00124 00125 ACS_TRACE("SlowMount::obstarAction"); 00126 00127 // convert the methods parameters back into something we can use 00128 __obstar_action *param_p = static_cast<__obstar_action *>(const_cast<void *>(value_p->pointerValue())); 00129 00130 ACS::Time timestamp; 00131 00132 // simulate changing the antenna's commanded and actual position 00133 m_cmdAz_sp->getDevIO()->write(param_p->ra, timestamp); 00134 m_actAz_sp->getDevIO()->write(param_p->ra, timestamp); 00135 m_cmdEl_sp->getDevIO()->write(param_p->dec, timestamp); 00136 m_actEl_sp->getDevIO()->write(param_p->dec, timestamp); 00137 00138 #ifdef debugMount 00139 ACS_SHORT_LOG((LM_DEBUG, "(SlowMount::obstarAction) command: %s %s %s", 00140 getComponent()->getName(), "obstar", getStringifiedTimeStamp().c_str())); 00141 #endif 00142 00143 DBConnector::writeCommand(getComponent()->getName(), "obstar", getStringifiedTimeStamp()); 00144 00145 completion = ACSErrTypeOK::ACSErrOKCompletion(); 00146 00147 // if OK action will be destroyed and we do not need it anymore 00148 if (param_p!=0) 00149 { 00150 delete param_p; 00151 } 00152 00153 // complete action requesting done invokation, 00154 // otherwise return reqInvokeWorking and set descOut.estimated_timeout 00155 return reqInvokeDone; 00156 } 00157 00158 00159 // implementation of async. objfix() method 00160 // 00161 // This is the method who simulates the movement 00162 ActionRequest 00163 SlowMount::objfixAction (BACIComponent *cob_p, 00164 const int &callbackID, 00165 const CBDescIn &descIn, 00166 BACIValue *value_p, 00167 Completion &completion, 00168 CBDescOut &descOut) 00169 { 00170 00171 ACE_UNUSED_ARG(cob_p); 00172 ACE_UNUSED_ARG(callbackID); 00173 //ACE_UNUSED_ARG(descIn); 00174 ACE_UNUSED_ARG(descOut); 00175 00176 ACS_TRACE("SlowMount::objfixAction"); 00177 00178 #ifdef debugMount 00179 ACS_SHORT_LOG((LM_DEBUG, "(SlowMount::objfixAction) %s", getComponent()->getName())); 00180 #endif 00181 00182 // convert the methods parameters back into something we can use 00183 __objfix_action *param_p = static_cast<__objfix_action *>(const_cast<void *>(value_p->pointerValue())); 00184 00185 ACS::Time timestamp; 00186 00187 // We need to know if this is the first call to this method 00188 // to initialize the variables for the simulation 00189 // This variable enhance the readability of the code 00190 static bool firstCall=true; 00191 static time_t startTime=0; // The time of the first invocation 00192 static time_t endTime=0; // ETA (Estimated Time of Arrival) 00193 static time_t lastUpdateTime=0; // The second of the last update (one second granularity) 00194 static double deltaAz=0.0; // AZ step 00195 static double deltaEl=0.0; // El step 00196 00197 // Read the actual position of the antenna 00198 double actAz=m_actAz_sp->getDevIO()->read(timestamp); 00199 double actEl=m_actEl_sp->getDevIO()->read(timestamp); 00200 00201 00202 // The first call is used to set the variables 00203 // to simulate the movement 00204 if (firstCall) { 00205 firstCall=false; 00206 startTime=time(NULL); 00207 endTime=startTime+30; // We decide to arrive in 30 secs!!! 00208 00209 // Evaluate the steps 00210 deltaAz=(fabs(param_p->az)-fabs(actAz))/30; 00211 if (actAz>param_p->az) deltaAz=-deltaAz; 00212 deltaEl=(fabs(param_p->elev)-fabs(actEl))/30; 00213 if (actEl>param_p->elev) deltaEl=-deltaEl; 00214 00215 // Set the commanded positions of the antenna 00216 m_cmdAz_sp->getDevIO()->write(param_p->az, timestamp); 00217 m_cmdEl_sp->getDevIO()->write(param_p->elev, timestamp); 00218 00219 // Write the command in the database 00220 DBConnector::writeCommand(getComponent()->getName(), "objfix", getStringifiedTimeStamp()); 00221 } 00222 00223 // Simulate the movement of the antenna 00224 // Granularity is the second (this method is called several 00225 // times per second) 00226 time_t now=time(NULL); // 00227 if (lastUpdateTime<endTime && now!=lastUpdateTime) 00228 { 00229 m_actAz_sp->getDevIO()->write(actAz+deltaAz, timestamp); 00230 m_actEl_sp->getDevIO()->write(actEl+deltaEl, timestamp); 00231 char logStr[256]; 00232 sprintf(logStr, 00233 "SlowMount::objfixAction Moving to [az=%lf, el=%lf] @ %ld", 00234 actAz+deltaAz, 00235 actEl+deltaEl, 00236 now); 00237 ACS_SHORT_LOG((LM_INFO,logStr)); 00238 lastUpdateTime=now; 00239 } else if (lastUpdateTime>=endTime) { 00240 // The time is ended: force the antenna in position 00241 m_actAz_sp->getDevIO()->write(param_p->az, timestamp); 00242 m_actEl_sp->getDevIO()->write(param_p->elev, timestamp); 00243 } 00244 00245 completion = ACSErrTypeOK::ACSErrOKCompletion(); 00246 00247 // Check if the antenna is position 00248 if (actAz==param_p->az && actEl==param_p->elev) { 00249 // The antennza is in position 00250 ACS_SHORT_LOG((LM_INFO, 00251 "SlowMount::objfixAction In position @ %d", 00252 now)); 00253 00254 // Reset static variables to be ready for next call 00255 firstCall=true; 00256 lastUpdateTime=startTime=endTime=0; 00257 deltaEl=deltaAz=0.0; 00258 00259 // if OK action will be destroyed and we do not need it anymore 00260 // Non lo distruggo perchè non ho finito 00261 if (param_p!=0) 00262 { 00263 delete param_p; 00264 } 00265 00266 // complete action requesting done invokation, 00267 // otherwise return reqInvokeWorking and set descOut.estimated_timeout 00268 return reqInvokeDone; 00269 } else { 00270 // We need a further iteration 00271 return reqInvokeWorking; 00272 } 00273 } 00274 00275 00276 /* --------------------- [ CORBA interface ] ----------------------*/ 00277 ACS::ROdouble_ptr 00278 SlowMount::cmdAz () 00279 { 00280 ACS_TRACE("SlowMount::cmdAz"); 00281 if (m_cmdAz_sp == 0) 00282 { 00283 return ACS::ROdouble::_nil(); 00284 } 00285 00286 ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_cmdAz_sp->getCORBAReference()); 00287 return prop._retn(); 00288 } 00289 00290 ACS::ROdouble_ptr 00291 SlowMount::cmdEl () 00292 { 00293 ACS_TRACE("SlowMount::cmdEl"); 00294 if (m_cmdEl_sp == 0) 00295 { 00296 return ACS::ROdouble::_nil(); 00297 } 00298 00299 ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_cmdEl_sp->getCORBAReference()); 00300 return prop._retn(); 00301 } 00302 00303 ACS::ROdouble_ptr 00304 SlowMount::actAz () 00305 { 00306 ACS_TRACE("SlowMount::actAz"); 00307 if (m_actAz_sp == 0) 00308 { 00309 return ACS::ROdouble::_nil(); 00310 } 00311 00312 ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_actAz_sp->getCORBAReference()); 00313 return prop._retn(); 00314 } 00315 00316 ACS::ROdouble_ptr 00317 SlowMount::actEl () 00318 { 00319 ACS_TRACE("SlowMount::actEl"); 00320 if (m_actEl_sp == 0) 00321 { 00322 return ACS::ROdouble::_nil(); 00323 } 00324 00325 ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_actEl_sp->getCORBAReference()); 00326 return prop._retn(); 00327 } 00328 00329 void 00330 SlowMount::obstar (CORBA::Double ra, 00331 CORBA::Double dec, 00332 CORBA::Double pmRa, 00333 CORBA::Double pmDec, 00334 CORBA::Double radVel, 00335 CORBA::Double par, 00336 MOUNT_ACS::Mount::coordType type, 00337 ACS::CBvoid_ptr callBack, 00338 const ACS::CBDescIn &desc 00339 ) 00340 { 00341 ACS_TRACE("SlowMount::obstar"); 00342 // convert this method's parameters into something ActionImplementor can use 00343 __obstar_action *param_p = new __obstar_action(); 00344 param_p->ra=ra; 00345 param_p->dec=dec; 00346 param_p->pmRa=pmRa; 00347 param_p->pmDec=pmDec; 00348 param_p->radVel=radVel; 00349 param_p->par=par; 00350 param_p->type=type; 00351 00352 // register the action in a queue so that control is returned immediately 00353 getComponent()->registerAction(BACIValue::type_null, callBack, desc, this, 0, BACIValue(param_p)); // ID = 0 00354 } 00355 00356 void 00357 SlowMount::objfix (CORBA::Double az, 00358 CORBA::Double elev, 00359 ACS::CBvoid_ptr callBack, 00360 const ACS::CBDescIn &desc 00361 ) 00362 { 00363 ACS_TRACE("SlowMount::objfix"); 00364 // convert this method's parameters into something ActionImplementor can use 00365 __objfix_action *param_p = new __objfix_action(); 00366 param_p->az=az; 00367 param_p->elev=elev; 00368 00369 // register the action in a queue so that control is returned immediately 00370 getComponent()->registerAction(BACIValue::type_null, callBack, desc, this, 1, BACIValue(param_p)); // ID = 1 00371 } 00372 00373 00374 /* --------------- [ MACI DLL support functions ] -----------------*/ 00375 #include <maciACSComponentDefines.h> 00376 MACI_DLL_SUPPORT_FUNCTIONS(SlowMount) 00377 /* ----------------------------------------------------------------*/ 00378 00379 00380 00381 00382