Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

Sensors/AccelerationClient.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////////////////////////
00002 /*! \file: AccelerationClient.cpp
00003 *  \brief: Send acceleration control vector to GPS propagator.
00004 *  \author $Author: jayhawk_hokie $
00005 *  \version $Revision: 1.1 $
00006 *  \date    $Date: 2007/08/31 16:10:54 $
00007 *//////////////////////////////////////////////////////////////////////////////////////////////////
00008 /*!
00009 */
00010 //////////////////////////////////////////////////////////////////////////////////////////////////
00011 
00012 
00013 #include "AccelerationClient.h"
00014 
00015 using namespace std;
00016 
00017 /*! \brief Default constructor. Object will be useless until <code>Connect()</code>
00018  *   is called.
00019  */
00020 AccelerationMessage::AccelerationMessage( )
00021     : m_clientSocket( NULL )
00022 {
00023         m_deltaV = 0;
00024 }
00025 
00026 /*! \brief Constructs the device and calls Connect().  This method has no return
00027  *  value so failures in the Connect() method may not be reported.  If
00028  * failures must be handled, you should use the default constructor and
00029  * call Connect() explicitly.
00030  *
00031  * @param serverName argument is the hostname or dotted decimal IP address at
00032  * which Sim Propagator is to be found.  The port number is presumed to be the
00033  * default port.
00034  * @param port
00035  */
00036 AccelerationMessage::AccelerationMessage( const char *serverName, int port )
00037    : m_clientSocket( NULL )
00038 {
00039         Connect( serverName, port );
00040         m_deltaV = 0;
00041 }
00042 
00043 /*! \brief Destroys the AccelerationMessage.
00044  */
00045 AccelerationMessage::~AccelerationMessage( )
00046 {
00047         delete m_clientSocket;
00048 }
00049 
00050 /*! \brief Attempts to connect the SimPropagator.
00051  *
00052  * @param serverName argument is the hostname or dotted decimal IP address at
00053  * which the Sim Propagator is to be found.
00054  * @param portNumber argument is the port to access on the server.
00055  * @return 0 on success or -1 on failure.
00056  */
00057 int AccelerationMessage::Connect( const char *serverName, int portNumber )
00058 {
00059         int returnValue = -1;
00060 
00061         try
00062         {
00063 
00064                 if ( m_clientSocket != NULL)
00065                 {
00066                         delete m_clientSocket;
00067                 }
00068 
00069                 m_clientSocket = new ClientSocket;
00070 
00071                 m_clientSocket -> Connect( serverName, portNumber );
00072         }
00073         catch ( SocketException& error )
00074         {
00075                 std::cout << "Exception was caught: " << error.description( ) << "\n";
00076                 return( returnValue );
00077         }
00078         returnValue = 0;
00079         return( returnValue );
00080 }
00081 
00082 /*! \brief Send a message that is stored in m_message to the acceleration server
00083  *
00084  *  returns the length of the message or -1 for failure.
00085  */
00086 int AccelerationMessage::SendMessage( )
00087 {
00088         int returnValue = -1;
00089 
00090         try
00091         {
00092                 *m_clientSocket << m_message;
00093         }
00094         catch ( SocketException& error )
00095         {
00096                 std::cout << "Exception was caught: " << error.description( ) << "\n";
00097                 return( returnValue );
00098         }
00099 
00100         returnValue = m_message.length( );
00101         return ( returnValue );
00102 }
00103 
00104 
00105 /*! \brief Receive a message from the acceleration server
00106  *
00107  *  returns the length of the message or -1 for failure.
00108  */
00109 int AccelerationMessage::ReceiveMessage( )
00110 {
00111         int returnValue = -1;
00112 
00113         try
00114         {
00115                 *m_clientSocket >> m_response;
00116         }
00117         catch ( SocketException& error )
00118         {
00119                 std::cout << "Exception was caught: " << error.description( ) << "\n";
00120                 return( returnValue );
00121         }
00122 
00123         returnValue = m_response.length( );
00124         return ( returnValue );
00125 }
00126 
00127 
00128 /*! \brief Parses response from Acceleration Server.
00129  *
00130  *  $PASHR ACK
00131  *  $PASHR NACK
00132  *  $PASHR VEL deltaV
00133  */
00134 void AccelerationMessage::CheckResponse( )
00135 {
00136         stringstream _response( m_response );
00137         string command;
00138         string function;
00139 
00140         _response >> command;
00141 
00142         switch ( ConvertString( command ) )
00143         {
00144                 case $PASHR:
00145                         _response >> function;
00146                         switch( ConvertString( function ) )
00147                         {
00148                                 case VEL:
00149                                         _response >> m_deltaV;
00150                                         break;
00151                                 case ACK:
00152                                         cout << "Command Successfully Sent." << endl;
00153                                         break;
00154                                 case NACK:
00155                                         cout << "WARNING: Command NOT Successfully Sent. " << endl;
00156                                         break;
00157                                 default:
00158                                         cout << "WARNING: BAD FUNCTION RESPONSE FROM ACCELERATION SERVER " << function << endl;
00159                                         break;
00160                         }
00161                         break;
00162 
00163                 default:
00164                         cout << "WARNING: BAD RESPONSE FROM ACCELERATION SERVER " << m_response << endl; 
00165                         break;
00166 
00167         }
00168         
00169 }
00170 
00171 /*! \brief Convert string to int for switch case
00172  *
00173  *  @param data
00174  */
00175 int AccelerationMessage::ConvertString( string data )
00176 {
00177         if( data == "$PASHR" ) return( $PASHR );
00178         else if( data == "VEL" ) return( VEL );
00179         else if( data == "ACK" ) return( ACK );
00180         else if( data == "NACK" ) return( NACK );
00181         else return( -1 );
00182 }
00183 
00184 /*! \brief Send acceleration message to acceleration server
00185  * 
00186  */
00187 void AccelerationMessage::SendAccelerationMessage( int _spaceVehicleNumber, double _startTime, double _duration, CAMdoubleVector _acceleration, double _mass )
00188 {
00189         stringstream _message;
00190         _message.precision( 16 );
00191         _message << "$PASHS ACC " << _spaceVehicleNumber << " "
00192                  << _startTime << " "
00193                  << _duration << " "
00194                  << _acceleration(1) << " "
00195                  << _acceleration(2) << " "
00196                  << _acceleration(3) << " "
00197                  << _mass << endl;
00198         m_message = _message.str( );
00199 
00200         if( SendMessage( ) == -1 )
00201                 cerr << "WARNING: MESSAGE FAILED TO SEND CORRECTLY TO ACCELERATION SERVER " << endl;
00202 
00203         if( ReceiveMessage( ) == -1 )
00204                 cerr << "WARNING: MESSAGE FAILED TO RECEIVE CORRECTLY FROM ACCELERATION SERVER " << endl;
00205 
00206         CheckResponse( );
00207 }
00208 
00209 
00210 double AccelerationMessage::GetDeltaV( )
00211 {
00212         stringstream _message;
00213         _message.precision( 18 );
00214         _message << "$PASHQ VEL" << endl;
00215         m_message = _message.str( );
00216         
00217         if( SendMessage( ) == -1 )
00218                 cerr << "WARNING: MESSAGE FAILED TO SEND CORRECTLY TO ACCELERATION SERVER " << endl;
00219 
00220         if( ReceiveMessage( ) == -1 )
00221                 cerr << "WARNING: MESSAGE FAILED TO RECEIVE CORRECTLY FROM ACCELERATION SERVER " << endl;
00222 
00223         CheckResponse( );
00224         
00225         return( m_deltaV );     
00226 }
00227 
00228 
00229 void AccelerationMessage::ResetValues( )
00230 {
00231         stringstream _message;
00232         _message.precision( 18 );
00233         _message << "$PASHS RST" << endl;
00234         m_message = _message.str( );
00235         
00236         if( SendMessage( ) == -1 )
00237                 cerr << "WARNING: MESSAGE FAILED TO SEND CORRECTLY TO ACCELERATION SERVER " << endl;
00238 
00239         if( ReceiveMessage( ) == -1 )
00240                 cerr << "WARNING: MESSAGE FAILED TO RECEIVE CORRECTLY FROM ACCELERATION SERVER " << endl;
00241 
00242         CheckResponse( );
00243 }
00244 
00245 // Do not change the comments below - they will be added automatically by CVS
00246 /*****************************************************************************
00247 *       $Log: AccelerationClient.cpp,v $
00248 *       Revision 1.1  2007/08/31 16:10:54  jayhawk_hokie
00249 *       Initial Submission.
00250 *
00251 *
00252 *
00253 ******************************************************************************/
00254 

Generated on Wed Sep 5 12:54:17 2007 for DSACSS Operational Code by  doxygen 1.3.9.1