ALMA Computing Group

acscourseMount4Impl.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 *    ALMA - Atacama Large Millimiter Array
00003 *    (c) Associated Universities Inc., 2002 *
00004 *    (c) European Southern Observatory, 2002
00005 *    Copyright by ESO (in the framework of the ALMA collaboration)
00006 *    and Cosylab 2002, All rights reserved
00007 *
00008 *    This library is free software; you can redistribute it and/or
00009 *    modify it under the terms of the GNU Lesser General Public
00010 *    License as published by the Free Software Foundation; either
00011 *    version 2.1 of the License, or (at your option) any later version.
00012 *
00013 *    This library is distributed in the hope that it will be useful,
00014 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 *    Lesser General Public License for more details.
00017 *
00018 *    You should have received a copy of the GNU Lesser General Public
00019 *    License along with this library; if not, write to the Free Software
00020 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00021 *
00022 *
00023 *
00024 * "@(#) $Id: acscourseMount4Impl.cpp,v 1.11 2008/10/02 08:58:19 cparedes Exp $"
00025 *
00026 */
00027  
00028 #include <acscourseMount4Impl.h>
00029 
00030 PositionControlThread::PositionControlThread(
00031     const ACE_CString& name, 
00032     Mount4Impl *_mount_p, 
00033     const ACS::TimeInterval& responseTime, 
00034     const ACS::TimeInterval& sleepTime) :
00035     ACS::Thread(name),
00036     mount_p(_mount_p)
00037 {
00038     ACS_TRACE("PositionControlThread::PositionControlThread");
00039 }
00040 
00041 PositionControlThread::~PositionControlThread() 
00042 {
00043     ACS_TRACE("PositionControlThread::~PositionControlThread"); 
00044 }
00045 
00046 void PositionControlThread::runLoop()
00047 {
00048     ACS_STATIC_TRACE("PositionControlThread::runLoop");
00049 
00050     ACS::Time timestamp;
00051 
00052     double cmdAzValue = mount_p->m_cmdAz_sp->getDevIO()->read(timestamp);
00053     double cmdElValue = mount_p->m_cmdEl_sp->getDevIO()->read(timestamp);
00054     double actAzValue = mount_p->m_actAz_sp->getDevIO()->read(timestamp);
00055     double actElValue = mount_p->m_actEl_sp->getDevIO()->read(timestamp);
00056 
00057     if ( (cmdAzValue != actAzValue) ||
00058          (cmdElValue != actElValue)    )
00059         {
00060         // Simulated control
00061         ACS_STATIC_SHORT_LOG((LM_INFO, "Moving to (%f,%f)...", cmdAzValue, cmdElValue));
00062         try
00063             {
00064             mount_p->m_actAz_sp->getDevIO()->write(cmdAzValue, timestamp);
00065             mount_p->m_actEl_sp->getDevIO()->write(cmdElValue, timestamp);
00066             }
00067         catch (...) 
00068             {
00069             // Here we have to better handle errors!
00070             ACS_SHORT_LOG((LM_ERROR,"Error accessing devIO"));
00071             }
00072         } 
00073 }
00074 
00078 const static int OBJFIX_ACTION  = 1;
00079 
00080 /* ----------------------------------------------------------------*/
00081 Mount4Impl::Mount4Impl(const ACE_CString &_name, maci::ContainerServices *containerServices) :
00082     CharacteristicComponentImpl(_name, containerServices),
00083     m_cmdAz_sp(new baci::ROdouble(_name+":cmdAz", getComponent()),this),
00084     m_cmdEl_sp(new baci::ROdouble(_name+":cmdEl", getComponent()),this),
00085     m_actAz_sp(new baci::ROdouble(_name+":actAz", getComponent()),this),
00086     m_actEl_sp(new baci::ROdouble(_name+":actEl", getComponent()),this)
00087 {
00088     // ACS_TRACE is used for debugging purposes
00089     ACS_TRACE("::Mount4Impl::Mount4Impl");
00090     // Initialize control loop thread
00091 
00092     PositionControlThread *pct = new PositionControlThread("positionControl",this);
00093     getComponent()->getThreadManager()->add("positionControl",pct);
00094 
00095     ACS_SHORT_LOG((LM_INFO,"positionControl thread spawned.")); 
00096 }
00097 
00098 /* ----------------------------------------------------------------*/
00099 Mount4Impl::~Mount4Impl()
00100 {
00101 
00102     // ACS_TRACE is used for debugging purposes
00103     ACS_TRACE("::Mount4Impl::~Mount4Impl");
00104 }
00105 
00106 
00107 /* --------------- [ Action implementator interface ] -------------- */
00108 
00109 baci::ActionRequest 
00110 Mount4Impl::invokeAction (int function,
00111                     baci::BACIComponent *cob_p, 
00112                     const int &callbackID, 
00113                     const CBDescIn &descIn, 
00114                     baci::BACIValue *value_p, 
00115                     Completion &completion, 
00116                     CBDescOut &descOut) 
00117 {
00118     
00119     // better implementation with array is possible
00120     switch (function) 
00121         {
00122         case OBJFIX_ACTION:
00123           {
00124           return objfixAction(cob_p, callbackID, descIn, value_p, completion, descOut);
00125           }
00126         default:
00127           {
00128           return baci::reqDestroy;
00129           }
00130         }
00131 }
00132 
00134 baci::ActionRequest 
00135 Mount4Impl::objfixAction (baci::BACIComponent *cob_p, 
00136                 const int &callbackID,
00137                 const CBDescIn &descIn, 
00138                 baci::BACIValue *value_p,
00139                 Completion &completion, 
00140                 CBDescOut &descOut)
00141 {
00142     ACS_DEBUG_PARAM("::Mount4::objfixAction", "%s", getComponent()->getName());
00143     
00144     ACS::Time timestamp;
00145     
00146     // convert the methods parameters back into something we can use
00147     __objfix_action *param_p = 
00148         static_cast<__objfix_action *>(const_cast<void *>(value_p->pointerValue()));
00149     
00150     try
00151         {
00152         m_cmdAz_sp->getDevIO()->write(param_p->az,   timestamp);
00153         m_cmdEl_sp->getDevIO()->write(param_p->elev, timestamp);
00154         }
00155     catch (...) 
00156         {
00157         // Here we have to better handle errors!
00158         ACS_SHORT_LOG((LM_ERROR,"Error accessing devIO"));
00159         }
00160 
00161     ACS_SHORT_LOG((LM_INFO, "Executed asynchronous objfix command. Az: %f, El: %f", 
00162                    param_p->az, param_p->elev));
00163 
00164     completion = ACSErrTypeOK::ACSErrOKCompletion();
00165 
00166     
00167     // if OK action will be destroyed and we do not need it anymore
00168     if (param_p!=0) 
00169         {
00170         delete param_p;
00171         }
00172     
00173      // complete action requesting done invocation, 
00174     // otherwise return reqInvokeWorking and set descOut.estimated_timeout
00175     return baci::reqInvokeDone;
00176 }
00177 
00178 
00179 /* --------------------- [ CORBA interface ] ----------------------*/
00180 void 
00181 Mount4Impl::objfix (CORBA::Double az,
00182                     CORBA::Double elev)
00183 {
00184     ACS::Time timestamp;
00185     
00186     try
00187         {
00188         m_cmdAz_sp->getDevIO()->write(az,   timestamp);
00189         m_cmdEl_sp->getDevIO()->write(elev, timestamp);
00190         }
00191     catch (...) 
00192         {
00193         // Here we have to better handle errors!
00194         ACS_SHORT_LOG((LM_ERROR,"Error accessing devIO"));
00195         }
00196 
00197     ACS_SHORT_LOG((LM_INFO, "Received objfix command. Az: %f, El: %f", 
00198                    az, elev));
00199 
00200 }
00201 
00202 void
00203 Mount4Impl::objfix_async (CORBA::Double az,
00204                       CORBA::Double elev,
00205                       ACS::CBvoid_ptr callBack,
00206                       const ACS::CBDescIn &desc
00207     )
00208 {
00209     // convert this method's parameters into something ActionImplementor can use
00210     __objfix_action *param_p = new __objfix_action();
00211     param_p->az=az; 
00212     param_p->elev=elev;
00213     
00214     // register the action in a queue so that control is returned immediately
00215     getComponent()->registerAction(baci::BACIValue::type_null, callBack, desc, this, OBJFIX_ACTION, baci::BACIValue(param_p)); // ID = 1
00216 }
00217 
00218 ACS::ROdouble_ptr
00219 Mount4Impl::cmdAz ()
00220 {
00221     if (m_cmdAz_sp == 0)
00222         {
00223         return ACS::ROdouble::_nil();
00224         }
00225 
00226     ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_cmdAz_sp->getCORBAReference());
00227     return prop._retn();
00228 }
00229 
00230 
00231 ACS::ROdouble_ptr
00232 Mount4Impl::cmdEl ()
00233 {
00234     if (m_cmdEl_sp == 0)
00235         {
00236         return ACS::ROdouble::_nil();
00237         }
00238 
00239     ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_cmdEl_sp->getCORBAReference());
00240     return prop._retn();
00241 }
00242 
00243 
00244 ACS::ROdouble_ptr
00245 Mount4Impl::actAz ()
00246 {
00247     if (m_actAz_sp == 0)
00248         {
00249         return ACS::ROdouble::_nil();
00250         }
00251 
00252     ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_actAz_sp->getCORBAReference());
00253     return prop._retn();
00254 }
00255 
00256 
00257 ACS::ROdouble_ptr
00258 Mount4Impl::actEl ()
00259 {
00260     if (m_actEl_sp == 0)
00261         {
00262         return ACS::ROdouble::_nil();
00263         }
00264     
00265     ACS::ROdouble_var prop = ACS::ROdouble::_narrow(m_actEl_sp->getCORBAReference());
00266     return prop._retn();
00267 }
00268 
00269 /* --------------- [ MACI DLL support functions ] -----------------*/
00270 #include <maciACSComponentDefines.h>
00271 MACI_DLL_SUPPORT_FUNCTIONS(Mount4Impl)
00272 /* ----------------------------------------------------------------*/
00273 
00274 
00275 /*___oOo___*/
00276 
00277 
00278 
00279