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)
00031 {
00032
00033
00034
00035 static Matrix Kp(3,3);
00036 static Matrix Kd(3,3);
00037 static Matrix qCurrent(4,1);
00038 static Matrix qDesired(4,1);
00039 static Matrix wCurrent(3,1);
00040 static Matrix wDesired(3,1);
00041 static Matrix qError(4,1);
00042 static Matrix qErrAxis(3,1);
00043 static double qError4;
00044 static Matrix wError;
00045 static Matrix sigma(3,1);
00046
00047 static Matrix Inertia(3,3);
00048
00049
00050
00051 qCurrent = CurrentAttState[mslice(0,0,4,1)];
00052 qDesired = DesAttState[mslice(0,0,4,1)];
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
00067
00068
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);
00075
00076
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
00097 dx[mslice(0,0,3,1)] = temp5;
00098 dx[mslice(3,0,3,1)] = wCurrent;
00099
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
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
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
00131 OrbitState myOrbit;
00132
00133
00134 AttitudeState myAttitude;
00135 myAttitude.SetRotation(Quaternion(0.5,0.5,0.5,0.5));
00136 Vector AngularVelocity(3);
00137
00138 myAttitude.SetAngularVelocity(AngularVelocity);
00139
00140
00141
00142
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
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
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
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
00175 integrationTimes[1] = simTimeData.simTime;
00176
00177 cout << "PropTime = " << integrationTimes[0].GetSeconds() << " s -> " << integrationTimes[1].GetSeconds() << " s" << endl;
00178
00179
00180 Matrix history = myIntegrator.Integrate(
00181 integrationTimes,
00182 &AttituteDynamics,
00183 myAttitude.GetState(),
00184 myOrbit,
00185 myAttitude,
00186 MOI,
00187 AttitudeForcesFunctor
00188 );
00189
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
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
00247 int main()
00248 {
00249 pid_t new_pid;
00250 new_pid = fork();
00251 switch(new_pid)
00252 {
00253 case 0 :
00254 cout << "Running Attitude Server" << endl;
00255 AttitudeServer();
00256 break;
00257
00258 default :
00259 DeviceRun();
00260 break;
00261 }
00262
00263 return 0;
00264
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277