AttitudeSimulatorServer.cpp

Go to the documentation of this file.
00001 
00002 
00008 /*  
00009 */
00011 
00012 #include <unistd.h>
00013 #include <stdlib.h>
00014 #include <stdio.h>
00015 #include <fcntl.h>
00016 #include <limits.h>
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 
00020 #include "Matrix.h"
00021 #include "RungeKuttaIntegrator.h"
00022 #include "AttitudeState.h"
00023 #include <vector.h>
00024 using namespace std;
00025 
00026 #include "SSFComm.h"
00027 
00028 
00029 
00030 Vector ControlTorques(Matrix CurrentAttState, Matrix DesAttState, double epoch, double count) // HYK
00031 {
00032   /* This function takes the current and desired attitudes (orbital to body)
00033    * and returns the needed control torque (in N-m)
00034    */
00035   static Matrix Kp(3,3);        // gain on quaternion
00036   static Matrix Kd(3,3);        // gain on angular velocity
00037   static Matrix qCurrent(4,1);  // current quaternion (orbital to body)
00038   static Matrix qDesired(4,1);  // desired quaternion (orbital to body)
00039   static Matrix wCurrent(3,1);  // current angular velocity (orbital to body, rad/s)
00040   static Matrix wDesired(3,1);  // desired angular velocity (orbital to body, rad/s)
00041   static Matrix qError(4,1);    // the error quaternion (desired to body)
00042   static Matrix qErrAxis(3,1);  // Euler axis for desired to body rotation
00043   static double qError4;         // fourth entry of error quaternion
00044   static Matrix wError;          // Angular Velocity error
00045   static Matrix sigma(3,1);     // scaled error quaternion
00046 
00047   static Matrix Inertia(3,3);    // Moments of inertia, kg-m^2
00048 
00049 
00050 
00051   qCurrent = CurrentAttState[mslice(0,0,4,1)];
00052   qDesired = DesAttState[mslice(0,0,4,1)]; //HYKIM Oct 15
00053   wCurrent = CurrentAttState[mslice(4,0,3,1)];
00054   wDesired = DesAttState[mslice(4,0,3,1)];
00055 
00056   qError = ATT_qMult(qCurrent, qDesired);
00057 
00058   wError = wCurrent - wDesired;
00059   qErrAxis = qError[mslice(0,0,3,1)];
00060   qError4 = qError(3,0);
00061 
00062   Kd = .02*eye(3);
00063   Kp = 0.0006*eye(3);
00064 
00065   Matrix Torque_r(3,1);
00066   /* smaller torque */
00067  //    Kd = .05*eye(3);
00068  //   Kp = 0.0005*eye(3);
00069 
00070       sigma = qErrAxis / (1 + qError4);
00071       Torque_r = -Kp * sigma* (eye(1) + ~sigma * sigma) - Kd * wError;
00072 
00073   Matrix B(3,1);
00074   B = ATT1_CalcG(epoch, count);  // HYK
00075   //B = B / B.norm2();
00076   //Torque = Torque_r - (~Torque_r*B)*B;
00077   Matrix K(3,6);
00078 
00079  K(1,1) =  0.040424; K(1,2) =  0.000065; K(1,3) =  0.000110; K(1,3) =  30.393883; K(1,4) = -0.671557;  K(1,5) = -0.233840; 
00080  K(2,1) = -0.000724; K(2,2) =  0.030161; K(2,3) = -0.022378; K(2,3) = -1.024919;  K(2,4) =  34.678584; K(2,5) = -10.027221; 
00081  K(3,1) = -0.000370; K(3,2) =  0.020404; K(3,3) =  0.019692; K(3,3) = -0.326869;  K(3,4) = -1.073717;  K(3,5) =  24.430588;
00082 
00083   Matrix dx(6,1), gm(6,1), Gt(6,3);
00084 
00085 
00086 
00087   dx[mslice(0,0,3,1)] = qErrAxis;
00088   dx[mslice(3,0,3,1)] = wError; 
00089 
00090         Matrix Rto(3,3), qbt(4,1), wbt(3,1) ;
00091         Matrix temp5(3,1);
00092         Rto = ATT_q2R(qDesired);
00093         qbt = ATT_itzhackR2q(ATT_q2R(qCurrent)*~Rto);
00094 
00095         temp5=  qbt[mslice(0,0,3,1)];
00096         //cout<<"up="<<dx<<endl;
00097         dx[mslice(0,0,3,1)] = temp5;
00098         dx[mslice(3,0,3,1)] = wCurrent; 
00099         //cout<<"down="<<dx<<endl;
00100 
00101   Gt[mslice(3,0,3,3)] = -(1.0 / B.norm2()) * skew(B) * skew(B);
00102   gm = -Gt * K * dx;
00103   Torque  = gm[mslice(3,0,3,1)];
00104 //  Torque = ATT_q2R(qCurrent) * Torque;
00105 
00106 
00107 
00108   if (Torque.norm2() > MAXTORQUE)
00109     {
00110       Torque = Torque / Torque.norm2() * MAXTORQUE;
00111     }
00112 
00113   return ATT_TORQUE_VALID;
00114 
00115 }
00116 
00117 int AttitudeServer()
00118 {
00119     // Setup an integrator and any special parameters
00120     RungeKuttaIntegrator myIntegrator; 
00121         int numSteps = 10;
00122         myIntegrator.SetNumSteps(numSteps);
00123         vector<ssfTime> integrationTimes;
00124         ssfTime begin(0);
00125         ssfTime end(begin + 1);
00126         integrationTimes.push_back(begin);
00127         integrationTimes.push_back(end);
00128         simTimeData.simTime.Set(0);
00129 
00130    // Create & initialize an orbit type
00131     OrbitState myOrbit;
00132     
00133     // Setup the initial attitude
00134     AttitudeState myAttitude;
00135         myAttitude.SetRotation(Quaternion(0.5,0.5,0.5,0.5));
00136         Vector AngularVelocity(3);
00137 //      AngularVelocity(2) = 0.03;
00138         myAttitude.SetAngularVelocity(AngularVelocity);
00139 //      myAttitude.SetAttitudeFrame(new AttitudeFrameBI);
00140     
00141     
00142     // Create the matrix of parameters needed for the RHS
00143     Matrix MOI(3,3);
00144         MOI(1,2) =  0.4084; MOI(1,3) =  0.0046; MOI(1,3) = 0.0;
00145         MOI(2,2) =  0.0046; MOI(2,3) =  0.3802; MOI(2,3) = 0.0;
00146         MOI(3,2) =  0.0;    MOI(3,3) =  0.0;    MOI(3,3) = 0.4530;
00147             
00148     SpecificFunctor AttitudeForcesFunctor(&AttitudeForcesFunction);
00149 
00150     // Setup Communications 
00151     int server_fifo_fd, client_fifo_fd;
00152         TimeData simTimeData;
00153         AttitudeData attitudeState;
00154         int read_res;
00155         char client_fifo[256];
00156     
00157     // Create the pipe 
00158     mkfifo(SERVER_FIFO_NAME, 0777);
00159     server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);
00160     if (server_fifo_fd == -1)
00161     {
00162         fprintf(stderr, "Server fifo failure\n");
00163         exit(EXIT_FAILURE);
00164     }
00165     //sleep(5); // lets clients queue
00166     int ii = 0;
00167      while ((simTimeData.simTime.GetSeconds() <= 25) && ii < 5) 
00168      {
00169         read_res = read(server_fifo_fd, &simTimeData, sizeof(simTimeData));
00170         sprintf(client_fifo, CLIENT_FIFO_NAME, simTimeData.client_pid);
00171 
00172         if (read_res > 0)
00173         {
00174             // Set the final integration time to the requested time
00175             integrationTimes[1] = simTimeData.simTime;
00176                 
00177             cout << "PropTime = " << integrationTimes[0].GetSeconds() << " s -> " << integrationTimes[1].GetSeconds() << " s" << endl;
00178 
00179             // Integrate over the desired time interval
00180             Matrix history = myIntegrator.Integrate(
00181                             integrationTimes,           // seconds
00182                             &AttituteDynamics, 
00183                             myAttitude.GetState(),
00184                             myOrbit,
00185                             myAttitude,
00186                             MOI,
00187                             AttitudeForcesFunctor
00188                         );
00189             // on the next pass, the specified time will be the beginning time
00190             integrationTimes[0] = simTimeData.simTime;
00191             attitudeState.time = simTimeData.simTime.GetSeconds();
00192             attitudeState.q1 = (myAttitude.GetState())(1);
00193             attitudeState.q2 = (myAttitude.GetState())(2);
00194             attitudeState.q3 = (myAttitude.GetState())(3);
00195             attitudeState.q4 = (myAttitude.GetState())(4);
00196             attitudeState.w1 = (myAttitude.GetState())(5);
00197             attitudeState.w2 = (myAttitude.GetState())(6);
00198             attitudeState.w3 = (myAttitude.GetState())(7);
00199             
00200             // write out to pipe
00201             client_fifo_fd = open(client_fifo, O_WRONLY);
00202             if(client_fifo_fd != -1)
00203             {
00204                 cout << "sending : " << attitudeState.time << " [ " << attitudeState.q1 << ", " 
00205                                                         << attitudeState.q2 << ", "
00206                                                         << attitudeState.q3 << ", "
00207                                                         << attitudeState.q4 << " : "
00208                                                         << attitudeState.w1 << ", "
00209                                                         << attitudeState.w2 << ", "
00210                                                         << attitudeState.w3 << " ]" << endl;
00211                 write(client_fifo_fd, &attitudeState, sizeof(attitudeState));
00212                 close(client_fifo_fd);
00213             }
00214             else
00215                 cout << "Could not open Client FIFO FD" << endl;
00216         }
00217         else
00218         {
00219             cout << "Server has nothing to read." << endl;
00220             ++ii;
00221         }
00222     }
00223     cout << "Closing Attitude Server" << endl;
00224     close(server_fifo_fd);
00225     unlink (SERVER_FIFO_NAME);
00226     exit(EXIT_SUCCESS);
00227     
00228         
00229     return 0;
00230                             
00231 }
00232 
00233 void DeviceRun()
00234 {
00235             sleep(2);
00236             cout << "Running Device Model" << endl;
00237             SetupSSFComm();
00238             for (int ii = 0; ii < 5; ++ii)
00239             {
00240                 cout << "Run " << ii << ": " << endl;
00241                 SSFGetAttitude();
00242             }
00243             StopSSFComm();
00244 }
00245 
00246 // Starts up the processes
00247 int main()
00248 {
00249     pid_t new_pid;
00250     new_pid = fork();
00251     switch(new_pid)
00252     {
00253         case 0 : // child
00254             cout << "Running Attitude Server" << endl;
00255             AttitudeServer();
00256             break;
00257             
00258         default : // parent
00259             DeviceRun();
00260             break;
00261         }
00262 
00263     return 0;
00264         
00265 }
00266 
00267 // Do not change the comments below - they will be added automatically by CVS
00268 /*****************************************************************************
00269 *       $Log: AttitudeSimulatorServer.cpp,v $
00270 *       Revision 1.1  2003/05/01 15:29:59  nilspace
00271 *       First commit.
00272 *       
00273 *
00274 ******************************************************************************/
00275 
00276                         
00277 

Generated on Wed Aug 6 12:58:43 2003 for Open-Sessame Framework by doxygen1.3