00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "MWCMGController.h"
00015 #include <fstream.h>
00016
00017 int MWCMGController::Initialize() {
00018
00019 LoadLUT("ReferenceMotion.txt");
00020
00021 Matrix k(3,3);
00022 k(1,1) = 1;
00023 k(2,2) = 1;
00024 k(3,3) = 2;
00025
00026 m_gainMatrix = k;
00027 A = FindA();
00028 MOI_sw = FindMOI_sw();
00029 gettimeofday(&InitialTime,NULL);
00030
00031 return 0;
00032 }
00033
00034 ofstream MRPoFile("MRPcurrent.txt",ios::out);
00035
00036
00037
00038
00039 int MWCMGController::Run()
00040 {
00041
00042
00043 Vector recentOmega_BL = m_whorl->GetOmegaBL();
00044 Matrix recentMOI = m_whorl->GetMOI();
00045 Vector Omega_s(3);
00046 double samplingTime;
00047 m_whorl->GetMomentumWheel("Wheel1")->GetWheelSpeed( Omega_s(1), samplingTime );
00048 m_whorl->GetMomentumWheel("Wheel2")->GetWheelSpeed( Omega_s(2), samplingTime );
00049 m_whorl->GetMomentumWheel("Wheel3")->GetWheelSpeed( Omega_s(3), samplingTime );
00050
00051 cout<<"Omega_BL = "<<recentOmega_BL<<endl;
00052
00053 Vector Qcurrent(4);
00054 Qcurrent = m_whorl->GetQuaternion();
00055
00056
00057
00058 Vector MRPcurrent(3);
00059 MRPcurrent(1) = Qcurrent(1)/(1+Qcurrent(4));
00060 MRPcurrent(2) = Qcurrent(2)/(1+Qcurrent(4));
00061 MRPcurrent(3) = Qcurrent(3)/(1+Qcurrent(4));
00062
00063 double MRPnorm;
00064 MRPnorm = MRPcurrent(1)*MRPcurrent(1)+MRPcurrent(2)*MRPcurrent(2)+MRPcurrent(3)*MRPcurrent(3);
00065
00066
00067 if (MRPnorm > 1)
00068 {
00069 MRPcurrent(1) = -MRPcurrent(1)/MRPnorm;
00070 MRPcurrent(2) = -MRPcurrent(2)/MRPnorm;
00071 MRPcurrent(3) = -MRPcurrent(3)/MRPnorm;
00072
00073 }
00074
00075
00076
00077
00078 gettimeofday(&CurrentTime,NULL);
00079
00080 double RunTime = static_cast<double>(CurrentTime.tv_sec - InitialTime.tv_sec) + (static_cast<double>(CurrentTime.tv_usec - InitialTime.tv_usec)) * 1e-6;
00081 cout<<"RunTime = "<<RunTime<<endl;
00082
00083
00084 double *myRow = tlu(RunTime);
00085
00086
00087
00088 Vector MRPRef(3);
00089 MRPRef(1) = myRow[3];
00090 MRPRef(2) = myRow[4];
00091 MRPRef(3) = myRow[5];
00092
00093
00094 Vector omegaRef(3);
00095 omegaRef(1) = myRow[0];
00096 omegaRef(2) = myRow[1];
00097 omegaRef(3) = myRow[2];
00098
00099
00100 Vector hdotRef(3);
00101 hdotRef(1) = myRow[6];
00102 hdotRef(2) = myRow[7];
00103 hdotRef(3) = myRow[8];
00104
00105
00106
00107
00108 Matrix RBR(3,3);
00109 Vector dMRP(3);
00110 Vector domega(3);
00111
00112 dMRP = ((1-MRPcurrent(1)*MRPcurrent(1)-MRPcurrent(2)*MRPcurrent(2)-MRPcurrent(3)*MRPcurrent(3))*MRPRef +(1-MRPRef(1)*MRPRef(1)-MRPRef(2)*MRPRef(2)-MRPRef(3)*MRPRef(3))*MRPcurrent + 2*skew(MRPcurrent)*MRPRef)/(1+(MRPcurrent(1)*MRPcurrent(1)+MRPcurrent(2)*MRPcurrent(2)+MRPcurrent(3)*MRPcurrent(3))*(MRPRef(1)*MRPRef(1)+MRPRef(2)*MRPRef(2)+MRPRef(3)*MRPRef(3))+2*MRPcurrent(1)*MRPRef(1)+MRPcurrent(2)*MRPRef(2)+MRPcurrent(3)*MRPRef(3));
00113
00114 cout<<"dMRP " << dMRP << endl;
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 SIGMA = 1 - (dMRP(1)*dMRP(1) + dMRP(2)*dMRP(2) + dMRP(3)*dMRP(3));
00128
00129
00130 RBR(1,1) = 4*(dMRP(1)*dMRP(1) - dMRP(2)*dMRP(2) - dMRP(3)*dMRP(3)) + SIGMA*SIGMA;
00131 RBR(2,1) = 8*dMRP(2)*dMRP(1) - 4*dMRP(3)*SIGMA;
00132 RBR(3,1) = 8*dMRP(3)*dMRP(1) + 4*dMRP(2)*SIGMA;
00133
00134 RBR(1,2) = 8*dMRP(1)*dMRP(2) + 4*dMRP(3)*SIGMA;
00135 RBR(2,2) = 4*(-dMRP(1)*dMRP(1) + dMRP(2)*dMRP(2) - dMRP(3)*dMRP(3)) + SIGMA*SIGMA;
00136 RBR(3,2) = 8*dMRP(3)*dMRP(2) - 4*dMRP(1)*SIGMA;
00137
00138 RBR(1,3) = 8*dMRP(1)*dMRP(3) - 4*dMRP(2) * SIGMA;
00139 RBR(2,3) = 8*dMRP(2)*dMRP(3) + 4*dMRP(1)*SIGMA;
00140 RBR(3,3) = 4*(-dMRP(1)*dMRP(1) - dMRP(2)*dMRP(2) + dMRP(3)*dMRP(3)) + SIGMA*SIGMA;
00141
00142 RBR=1/pow((1+(dMRP(1)*dMRP(1) + dMRP(2)*dMRP(2) + dMRP(3)*dMRP(3))),2) * RBR;
00143
00144
00145
00146 domega = recentOmega_BL + RBR * omegaRef;
00147 cout<<"domega"<<domega<<endl;
00148
00149
00150 Matrix k1(3,3);
00151 k1(1,1) = 1;
00152 k1(2,2) = 1;
00153 k1(3,3) = 2;
00154
00155 Matrix k2(3,3);
00156 k2(1,1) = 1;
00157 k2(2,2) = 1;
00158 k2(3,3) = 1;
00159
00160
00161
00162
00163
00164
00165 m_controlTorque = - k1*dMRP - k2*domega ;
00166 cout<<"Control Torque = " << m_controlTorque<<endl;
00167
00168
00169 m_whorl->SetControl(m_controlTorque);
00170
00171
00172
00173
00174
00175
00176
00177 if ( norm2(m_controlTorque) < 0.5 )
00178 SetWheelTorque(m_controlTorque);
00179 else {
00180 m_controlTorque = 0.5 * ( m_controlTorque / norm2(m_controlTorque) );
00181 SetWheelTorque(m_controlTorque);
00182 };
00183
00184
00185
00186 return 0;
00187 }
00188
00189
00190
00191
00192 double *MWCMGController::tlu(double curtime) {
00193 int i;
00194 double *row;
00195 const double TOL = 0.001;
00196
00197
00198
00199
00200
00201
00202 for (i = 0;
00203 (lut[i][0] >= 0.0) && (lut[i][0] <= curtime) && fabs(lut[i][0] - curtime) > TOL;
00204 i++);
00205
00206
00207
00208
00209 if (lut[i][0] < 0.0)
00210 return NULL;
00211
00212
00213 row = new double[9];
00214
00215
00216
00217
00218 if (fabs(lut[i][0] - curtime) < TOL) {
00219 for (int j = 1; j <= 9; j++)
00220 row[j-1] = lut[i][j];
00221
00222 } else {
00223 for (int j = 1; j <= 9; j++)
00224 row[j-1] = (curtime-lut[i-1][0])/(lut[i][0]-lut[i-1][0]) *
00225 (lut[i][j] - lut[i-1][j]) + lut[i-1][j];
00226 }
00227
00228
00229
00230
00231 return row;
00232 }
00233
00234
00235 int MWCMGController::LoadLUT(char *file) {
00236 FILE *fp;
00237 char *line, *ptr;
00238 int lc = 0;
00239 const char *DELIM = "\t";
00240 int i,j;
00241 line = new char[1024];
00242
00243 if (!(fp = fopen(file, "r")))
00244 return -1;
00245
00246
00247
00248
00249 while (fgets(line, 1023, fp)) lc++;
00250
00251
00252
00253 lut = (double **)malloc(sizeof(double *)*(lc+1));
00254 for (int i = 0; i <= lc; i++)
00255 lut[i] = (double *)malloc(sizeof(double)*10);
00256
00257
00258
00259 rewind(fp);
00260
00261
00262
00263 for (i = 0; fgets(line, 1023, fp); i++) {
00264 for (j = 0, ptr = strtok(line, DELIM);
00265 j < 10;
00266 j++, ptr = strtok(NULL, DELIM)) {
00267 if (ptr)
00268 lut[i][j] = atof(ptr);
00269 else {
00270 printf("Error loading table.\n");
00271 lut[i][j] = 0.0;
00272 }
00273 }
00274
00275 }
00276
00277 lut[i][0] = -1.0;
00278 fclose(fp);
00279 delete [] line;
00280 return 0;
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324