00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "PhysicalControlMomentGyro.h"
00014 #include <fstream>
00015
00016 int PhysicalControlMomentGyro::m_fd = -1;
00017
00018
00019
00020
00021
00022 PhysicalControlMomentGyro::PhysicalControlMomentGyro()
00023 {
00024 }
00025
00026 PhysicalControlMomentGyro::~PhysicalControlMomentGyro()
00027 {
00028 Stop();
00029 }
00030
00031
00032
00033
00034
00035
00036
00037 void PhysicalControlMomentGyro::CommandWheelSpeed(double wheelOmega)
00038 {
00039 wheelOmega = wheelOmega*64425.156/(2*3.14159265359);
00040
00041 wheelOmega = round(wheelOmega);
00042
00043 static char *speedCommand = new char[80];
00044 sprintf(speedCommand, "%cV%d\n", m_WheelAddress, (int)wheelOmega);
00045 say(m_fd, speedCommand);
00046
00047 GoCommand(m_WheelAddress);
00048
00049 }
00050
00051
00052
00053 void PhysicalControlMomentGyro::CommandWheelTorque(double wheelTorque)
00054 {
00055
00056
00057 wheelTorque = wheelTorque/0.0018;
00058 wheelTorque = floor(wheelTorque);
00059 int torque;
00060 torque = (int) wheelTorque;
00061 static char *torqueCommand = new char[80];
00062
00063 sprintf(torqueCommand, "%cMT\n\n", m_WheelAddress);
00064 say(m_fd, torqueCommand);
00065
00066 sprintf(torqueCommand, "%cT%d\n\n", m_WheelAddress, torque);
00067 say(m_fd, torqueCommand);
00068
00069 m_CurrentWheelTorque = wheelTorque;
00070 }
00071
00072
00073 void PhysicalControlMomentGyro::CommandAngle(double wheelAngle)
00074 {
00075
00076
00077
00078 wheelAngle = wheelAngle/0.000010224;
00079 wheelAngle = round(wheelAngle);
00080
00081 int DesiredLAPosition;
00082
00083 DesiredLAPosition = m_LAMid + (int)wheelAngle;
00084
00085 if (DesiredLAPosition > m_LAMax){
00086 DesiredLAPosition = m_LAMax;
00087 } else if (DesiredLAPosition < m_LAMin){
00088 DesiredLAPosition = m_LAMin;
00089 }
00090
00091 static char *AngleCommand = new char[80];
00092 sprintf(AngleCommand, "%cP%d\n\n", m_LAAddress, DesiredLAPosition);
00093
00094 say(m_fd, AngleCommand);
00095 GoCommand(m_LAAddress);
00096
00097 m_CurrentAngle = DesiredLAPosition*0.000010224;
00098 }
00099
00100 void PhysicalControlMomentGyro::CommandGimbalRate(double gimbalRate)
00101 {
00102
00103 gimbalRate = gimbalRate * 1.0/0.000010224 * 1.0/2000.0 * 32212.578;
00104
00105
00106
00107 gimbalRate = round(gimbalRate);
00108
00109
00110 static char *off = new char[80];
00111 sprintf(off, "%c%s\n\n", m_LAAddress, "OFF");
00112 say(m_fd, off);
00113
00114 static char *velCommand = new char[80];
00115 sprintf(velCommand, "%cMV\n\n", m_LAAddress);
00116 say(m_fd, velCommand);
00117
00118 static char *accCommand = new char[80];
00119 sprintf(accCommand, "%cA%d\n\n", m_LAAddress, 3);
00120 say(m_fd, accCommand);
00121
00122 static char *GimbalCommand = new char[80];
00123 sprintf(GimbalCommand, "%cV%f\n\n", m_LAAddress, gimbalRate);
00124
00125
00126 say(m_fd, GimbalCommand);
00127 GoCommand(m_LAAddress);
00128 }
00129
00130
00131 int PhysicalControlMomentGyro::Stop()
00132 {
00133 CommandWheelSpeed(0.0);
00134 CommandWheelTorque(0.0);
00135 return 0;
00136 }
00137
00138
00139 int PhysicalControlMomentGyro::Initialize()
00140 {
00141
00142 m_WheelAddress = 130;
00143 m_LAAddress = 129;
00144
00145
00146 char *stringPort;
00147 stringPort = (char *)malloc(80);
00148
00149 cout << m_DaisyChainPort << endl;
00150 if (m_fd < 0) {
00151 sprintf(stringPort,"/dev/tty%s",m_DaisyChainPort.c_str());
00152
00153 init_serial(stringPort,B9600,&m_fd);
00154
00155 cout << stringPort << endl;
00156
00157 char *testCommand;
00158 testCommand = new char[80];
00159
00160 sprintf(testCommand, "%c%s\x0D", 0x80, "Z");
00161 say(m_fd, testCommand);
00162 usleep(500000);
00163
00164 sprintf(testCommand, "%c%s\x0D", 0x80, "SADDR1");
00165 say(m_fd, testCommand);
00166
00167 usleep(500000);
00168 sprintf(testCommand, "%c%s\x0D", 0x81, "ECHO");
00169 say(m_fd, testCommand);
00170 usleep(500000);
00171
00172 sprintf(testCommand, "%c%s\x0D", 0x81, "SLEEP");
00173 say(m_fd, testCommand);
00174 usleep(500000);
00175
00176 sprintf(testCommand, "%c%s\x0D", 0x80, "SADDR2");
00177 say(m_fd, testCommand);
00178 usleep(500000);
00179
00180 sprintf(testCommand, "%c%s\x0D", 0x82, "ECHO");
00181 say(m_fd, testCommand);
00182 usleep(500000);
00183
00184 sprintf(testCommand, "%c%s\x0D", 0x81, "WAKE");
00185 say(m_fd, testCommand);
00186 usleep(500000);
00187
00188
00189 FindLimits();
00190
00191 sleep(3);
00192
00193
00194 sprintf(testCommand, "%c%s\x0D", 0x81, "V50000");
00195 say(m_fd, testCommand);
00196 usleep(500000);
00197
00198 GoCommand(m_LAAddress);
00199
00200
00201
00202
00203 sprintf(testCommand, "%c%s\x0D", 0x82, "MV");
00204 say(m_fd, testCommand);
00205 usleep(500000);
00206
00207 sprintf(testCommand, "%c%s\x0D", 0x82, "KI0");
00208 say(m_fd, testCommand);
00209 usleep(500000);
00210
00211 sprintf(testCommand, "%c%s\x0D", 0x82, "KP50");
00212 say(m_fd, testCommand);
00213 usleep(500000);
00214
00215 sprintf(testCommand, "%c%s\x0D", 0x82, "KD50");
00216 say(m_fd, testCommand);
00217 usleep(500000);
00218
00219 sprintf(testCommand, "%c%s\x0D", 0x82, "KL200");
00220 say(m_fd, testCommand);
00221 usleep(500000);
00222
00223 sprintf(testCommand, "%c%s\x0D", 0x82, "F");
00224 say(m_fd, testCommand);
00225 usleep(500000);
00226
00227 sprintf(testCommand, "%c%s\x0D", 0x82, "V1830000");
00228 say(m_fd, testCommand);
00229 usleep(500000);
00230
00231 sprintf(testCommand, "%c%s\x0D", 0x82, "A25");
00232 say(m_fd, testCommand);
00233 usleep(500000);
00234
00235 GoCommand(m_WheelAddress);
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 cout << "\nCMG Initialized" << endl;
00246
00247 }
00248 return 0;
00249 }
00250
00251
00252 int PhysicalControlMomentGyro::Deinitialize()
00253 {
00254 close(m_fd);
00255 return 0;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265 void PhysicalControlMomentGyro::SetWheelSpeedLimits(double minWheelSpeed, double maxWheelSpeed)
00266 {
00267 m_MinWheelSpeed = minWheelSpeed;
00268 m_MaxWheelSpeed = maxWheelSpeed;
00269 }
00270
00271
00272 void PhysicalControlMomentGyro::SetWheelTorqueLimits(double minWheelTorque,double maxWheelTorque)
00273 {
00274 m_MinWheelTorque = minWheelTorque;
00275 m_MaxWheelTorque = maxWheelTorque;
00276 }
00277
00278
00279 void PhysicalControlMomentGyro::SetMaxTorqueStep(double maxTorqueStep)
00280 {
00281 m_MaxTorqueStep = maxTorqueStep;
00282 }
00283
00284 void PhysicalControlMomentGyro::SetDaisyChainNumber(int DaisyChainNumber)
00285 {
00286 m_DaisyChainNumber = DaisyChainNumber;
00287 }
00288
00289 void PhysicalControlMomentGyro::SetDaisyChainPort(string Port)
00290 {
00291 m_DaisyChainPort = Port;
00292 }
00293
00294
00295
00296
00297
00298 double PhysicalControlMomentGyro::QueryWheelSpeed()
00299 {
00300
00301 char* clear = new char[80];
00302 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00303
00304
00305
00306 int pos1 = 0;
00307 int pos2 = 0;
00308 struct timeval *time1 = new struct timeval;
00309 struct timeval *time2 = new struct timeval;
00310 int dt;
00311 char *posString = new char[80];
00312 double velocity;
00313 int readdelay =10000;
00314
00315
00316 char *RPCommand = new char[80];
00317 sprintf(RPCommand, "%cRP\n", m_WheelAddress);
00318 say(m_fd, RPCommand);
00319
00320 usleep(10000);
00321
00322 hear(m_fd, posString, 79, 0, 1000000, '\n');
00323
00324 hear(m_fd, posString, 79, 0, 1000000, '\r');
00325
00326 pos1 = atoi(posString);
00327
00328 gettimeofday(time1,NULL);
00329
00330 usleep(readdelay);
00331
00332
00333 say(m_fd, RPCommand);
00334
00335 usleep(10000);
00336
00337 hear(m_fd, posString, 15, 0, 1000000, '\n');
00338
00339 hear(m_fd, posString, 79, 0, 1000000, '\r');
00340
00341 pos2 = atoi(posString);
00342
00343 gettimeofday(time2,NULL);
00344
00345 delete RPCommand;
00346 delete posString;
00347
00348
00349 dt = time2->tv_usec - time1->tv_usec;
00350
00351 if (dt<0) {
00352 dt = 1000000 + dt;
00353 }
00354
00355 velocity = ((pos2 - pos1)/((double) dt))*2*3.14159*250.0;
00356
00357
00358 m_CurrentWheelSpeed = velocity;
00359 return m_CurrentWheelSpeed;
00360 }
00361
00362
00363 double PhysicalControlMomentGyro::QueryWheelTorque()
00364 {
00365
00366
00367 char* clear = new char[80];
00368 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00369
00370
00371 static char *RTCommand = new char[80];
00372 sprintf(RTCommand, "%cRT\n\n", m_WheelAddress);
00373 say(m_fd, RTCommand);
00374
00375 static char *torqueString = new char[80];
00376 hear(m_fd, torqueString, 20, 0, 1000000, '\n');
00377
00378 hear(m_fd, torqueString, 20, 0, 1000000, '\r');
00379
00380 double Wtorque = atoi(torqueString) * 0.0018;
00381
00382 m_CurrentWheelTorque = Wtorque;
00383
00384
00385 return m_CurrentWheelTorque;
00386 }
00387
00388
00389 double PhysicalControlMomentGyro::QueryAngle()
00390 {
00391
00392 char* clear = new char[80];
00393 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00394
00395
00396 static char *RPCommand = new char[80];
00397 sprintf(RPCommand, "%cRP\n\n", m_LAAddress);
00398 say(m_fd, RPCommand);
00399
00400 static char *posString = new char[80];
00401 hear(m_fd, posString, 15, 0, 1000000, '\n');
00402
00403 hear(m_fd, posString, 79, 0, 1000000, '\r');
00404
00405 int LAPosition = atoi(posString);
00406
00407 m_CurrentAngle = (LAPosition - m_LAMid)*0.000010224;
00408
00409 return m_CurrentAngle;
00410
00411 }
00412
00413
00414 void PhysicalControlMomentGyro::GetWheelSpeedLimits(double& minWheelSpeed, double& maxWheelSpeed)
00415 {
00416 minWheelSpeed = m_MinWheelSpeed;
00417 maxWheelSpeed = m_MaxWheelSpeed;
00418 }
00419
00420
00421 void PhysicalControlMomentGyro::GetWheelTorqueLimits(double& minWheelTorque, double& maxWheelTorque)
00422 {
00423 minWheelTorque = m_MinWheelTorque;
00424 maxWheelTorque = m_MaxWheelTorque;
00425 }
00426
00427
00428 void PhysicalControlMomentGyro::GetMaxTorqueStep(double& maxTorqueStep)
00429 {
00430 maxTorqueStep = m_MaxTorqueStep;
00431 }
00432
00433
00434 void PhysicalControlMomentGyro::GoCommand(int Address)
00435 {
00436
00437
00438 static char *GoString = new char[80];
00439 sprintf(GoString, "%cG\n", Address);
00440 say(m_fd, GoString);
00441 }
00442
00443
00444 void PhysicalControlMomentGyro::FindLimits()
00445 {
00446
00447 char *testCommand = new char[80];
00448
00449 sprintf(testCommand, "%c%s\x0D", 0x81, "MP");
00450 say(m_fd, testCommand);
00451 usleep(500000);
00452
00453 sprintf(testCommand, "%c%s\x0D", 0x81, "F1");
00454 say(m_fd, testCommand);
00455 usleep(500000);
00456
00457 sprintf(testCommand, "%c%s\x0D", 0x81, "V500000");
00458 say(m_fd, testCommand);
00459 usleep(500000);
00460
00461 sprintf(testCommand, "%c%s\x0D", 0x81, "A1000");
00462 say(m_fd, testCommand);
00463 usleep(500000);
00464
00465
00466
00467 char* clear = new char[80];
00468 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00469
00470 char* ErrorString = new char[80];
00471 sprintf(ErrorString, "%cRBp\n\n", m_LAAddress);
00472 say(m_fd, ErrorString);
00473
00474 hear(m_fd, ErrorString, 79, 0, 1000000, '\n');
00475
00476 hear(m_fd, ErrorString, 79, 0, 1000000, '\r');
00477 int PosLimit = atoi(ErrorString);
00478
00479 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00480
00481 sprintf(ErrorString, "%cRBm\n\n", m_LAAddress);
00482 say(m_fd, ErrorString);
00483
00484 hear(m_fd, ErrorString, 79, 0, 1000000, '\n');
00485 hear(m_fd, ErrorString, 79, 0, 1000000, '\r');
00486 int NegLimit = atoi(ErrorString);
00487
00488 if (PosLimit){
00489 sprintf(testCommand, "%cP1000000\n", m_LAAddress);
00490 say(m_fd, testCommand);
00491 GoCommand(m_LAAddress);
00492 sleep(5);
00493 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00494 sprintf(ErrorString, "%cRP\n\n", m_LAAddress);
00495 say(m_fd, ErrorString);
00496 usleep(10000);
00497
00498 hear(m_fd, ErrorString, 79, 0, 1000000, '\n');
00499
00500 hear(m_fd, ErrorString, 79, 0, 1000000, '\r');
00501 m_LAMax = atoi(ErrorString);
00502 m_LAMin = 0;
00503
00504 } else if(NegLimit){
00505 sprintf(testCommand, "%cP-1000000\n", m_LAAddress);
00506 say(m_fd, testCommand);
00507 GoCommand(m_LAAddress);
00508 sleep(5);
00509 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00510 sprintf(ErrorString, "%cRP\n\n", m_LAAddress);
00511 say(m_fd, ErrorString);
00512
00513 usleep(10000);
00514 hear(m_fd, ErrorString, 79, 0, 1000000, '\n');
00515
00516 hear(m_fd, ErrorString, 79, 0, 1000000, '\r');
00517 m_LAMin = atoi(ErrorString);
00518 m_LAMax = 0;
00519 } else {
00520 sprintf(testCommand, "%cP1000000\n", m_LAAddress);
00521 say(m_fd, testCommand);
00522 GoCommand(m_LAAddress);
00523 sleep(5);
00524 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00525 sprintf(ErrorString, "%cRP\n\n", m_LAAddress);
00526 say(m_fd, ErrorString);
00527 usleep(10000);
00528
00529 hear(m_fd, ErrorString, 79, 0, 1000000, '\n');
00530 hear(m_fd, ErrorString, 79, 0, 1000000, '\r');
00531 m_LAMax = atoi(ErrorString);
00532 sprintf(testCommand, "%cP-1000000\n", m_LAAddress);
00533 say(m_fd, testCommand);
00534 GoCommand(m_LAAddress);
00535 sleep(7);
00536 while( !hear(m_fd, clear, 79, 0, 10000, 'c'));
00537 sprintf(ErrorString, "%cRP\n\n", m_LAAddress);
00538 say(m_fd, ErrorString);
00539 usleep(10000);
00540
00541 hear(m_fd, ErrorString, 79, 0, 1000000, '\n');
00542 hear(m_fd, ErrorString, 79, 0, 1000000, '\r');
00543 m_LAMin = atoi(ErrorString);
00544 }
00545 double LAMid = ( m_LAMax + m_LAMin)/2.0;
00546 m_LAMid = (int)LAMid;
00547 sprintf(testCommand, "%cP%d", m_LAAddress, m_LAMid);
00548 say(m_fd, testCommand);
00549 GoCommand(m_LAAddress);
00550 }