PIDControl7.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002,2004 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OUnits.h>
00014 #include <OPENR/OSyslog.h>
00015 #include <OPENR/core_macro.h>
00016 #include "PIDControl7.h"
00017 
00018 PIDControl7::PIDControl7() : pidControl7State(PCS7_IDLE),
00019                              pidControl7Info(),
00020                              initSensorIndex(false)
00021 {
00022     for (int i = 0; i < NUM_JOINTS; i++) jointID[i] = oprimitiveID_UNDEF;
00023     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) region[i] = 0;
00024     for (int i = 0; i < NUM_JOINTS; i++) sensorIndex[i] = -1;
00025 }
00026 
00027 OStatus
00028 PIDControl7::DoInit(const OSystemEvent& event)
00029 {
00030     OSYSDEBUG(("PIDControl7::DoInit()\n"));
00031 
00032     NEW_ALL_SUBJECT_AND_OBSERVER;
00033     REGISTER_ALL_ENTRY;
00034     SET_ALL_READY_AND_NOTIFY_ENTRY;
00035 
00036     OpenPrimitives();
00037     NewCommandVectorData();
00038     OPENR::SetMotorPower(opowerON);
00039 
00040     size_t memsize = HOLD_TIME_NUM_FRAMES*sizeof(OJointValue);
00041     logData = (OJointValue*)malloc(memsize);
00042     if (logData == 0) {
00043         OSYSLOG1((osyslogERROR,
00044                   "PIDControl7::DoInit() : malloc() failed."));
00045     }
00046     
00047     return oSUCCESS;
00048 }
00049 
00050 OStatus
00051 PIDControl7::DoStart(const OSystemEvent& event)
00052 {
00053     OSYSDEBUG(("PIDControl7::DoStart()\n"));
00054 
00055     if (subject[sbjEffector]->IsReady() == true) {
00056         AdjustDiffJointValue();
00057         pidControl7State = PCS7_ADJUSTING_DIFF_JOINT_VALUE;
00058     } else {
00059         pidControl7State = PCS7_START;
00060     }
00061 
00062     ENABLE_ALL_SUBJECT;
00063     ASSERT_READY_TO_ALL_OBSERVER;
00064 
00065     return oSUCCESS;
00066 }
00067 
00068 OStatus
00069 PIDControl7::DoStop(const OSystemEvent& event)
00070 {
00071     OSYSDEBUG(("PIDControl7::DoStop()\n"));
00072 
00073     pidControl7State = PCS7_IDLE;
00074 
00075     DISABLE_ALL_SUBJECT;
00076     DEASSERT_READY_TO_ALL_OBSERVER;
00077 
00078     return oSUCCESS;
00079 }
00080 
00081 OStatus
00082 PIDControl7::DoDestroy(const OSystemEvent& event)
00083 {
00084     DELETE_ALL_SUBJECT_AND_OBSERVER;
00085     return oSUCCESS;
00086 }
00087 
00088 void
00089 PIDControl7::ReadyEffector(const OReadyEvent& event)
00090 {
00091     OSYSDEBUG(("PIDControl7::Ready()\n"));
00092 
00093     if (pidControl7State == PCS7_IDLE) {
00094 
00095         OSYSDEBUG(("PCS7_IDLE\n"));
00096         ; // do nothing
00097 
00098     } else if (pidControl7State == PCS7_START) {
00099 
00100         OSYSDEBUG(("PCS7_START\n"));
00101         AdjustDiffJointValue();
00102         pidControl7State = PCS7_ADJUSTING_DIFF_JOINT_VALUE;
00103 
00104     } else if (pidControl7State == PCS7_ADJUSTING_DIFF_JOINT_VALUE) {
00105 
00106         OSYSDEBUG(("PCS7_ADJUSTING_DIFF_JOINT_VALUE\n"));
00107         SetJointGain();
00108         MovingResult r = MoveToBroadBase();
00109         pidControl7State = PCS7_MOVING_TO_BROADBASE;
00110 
00111     } else if (pidControl7State == PCS7_MOVING_TO_BROADBASE) {
00112 
00113         OSYSDEBUG(("PCS7_MOVING_TO_BROADBASE\n"));
00114         MovingResult r = MoveToBroadBase();
00115         if (r == MOVING_FINISH) {
00116             pidControl7State = PCS7_MOVING_TO_SLEEPING;
00117         }
00118 
00119     } else if (pidControl7State == PCS7_MOVING_TO_SLEEPING) {
00120 
00121         OSYSDEBUG(("PCS7_MOVING_TO_SLEEPING\n"));
00122         MovingResult r = MoveToSleeping();
00123         if (r == MOVING_FINISH) {
00124             pidControl7State = PCS7_DISABLE_JOINT_GAIN;
00125         }
00126 
00127     } else if (pidControl7State == PCS7_DISABLE_JOINT_GAIN) {
00128 
00129         OSYSDEBUG(("PCS7_DISABLE_JOINT_GAIN\n"));
00130         DisableJointGainOfAllLegs();       
00131         if (YesNo("PIDControl7 continue? (y/n) > ") != true) {
00132             OBootCondition bootCond(obcbPAUSE_SW);
00133             OPENR::Shutdown(bootCond);
00134             pidControl7State = PCS7_IDLE;
00135             return;
00136         }
00137         AdjustDiffJointValue();
00138         pidControl7State = PCS7_INPUT_PARAMETER;
00139         
00140     } else if (pidControl7State == PCS7_INPUT_PARAMETER) {
00141 
00142         OSYSDEBUG(("PCS7_INPUT_PARAMETER\n"));
00143         SetJointGain();
00144         InputParameter();
00145         observer[obsSensor]->AssertReady();
00146         StepInput();
00147         pidControl7State = PCS7_STEP_INPUT;
00148 
00149     } else if (pidControl7State == PCS7_STEP_INPUT) {
00150 
00151         OSYSDEBUG(("PCS7_INPUT_PARAMETER\n"));
00152         MovingResult r = StepInput();
00153         if (r == MOVING_FINISH) {
00154             SaveLogData();
00155             pidControl7State = PCS7_DISABLE_JOINT_GAIN;
00156         }
00157     }
00158 }
00159 
00160 void
00161 PIDControl7::NotifySensor(const ONotifyEvent& event)
00162 {
00163     OSensorFrameVectorData* sensorVec;
00164 
00165     if (initSensorIndex == false) {
00166         sensorVec = (OSensorFrameVectorData*)event.Data(0);
00167         InitSensorIndex(sensorVec);
00168         initSensorIndex = true;
00169         observer[event.ObsIndex()]->DeassertReady();
00170     }
00171 
00172     if (pidControl7State == PCS7_STEP_INPUT) {
00173         
00174         OJointValue* log = &logData[pidControl7Info.sensorCounter];
00175         sensorVec = (OSensorFrameVectorData*)event.Data(0);
00176         int sidx = sensorIndex[pidControl7Info.jointIndex];
00177         OSensorFrameData* data = sensorVec->GetData(sidx);
00178 
00179         memcpy((void*)log, (void*)data->frame,
00180                STEP_INPUT_NUM_FRAMES * sizeof(OJointValue));
00181 
00182         if (pidControl7Info.sensorCounter < HOLD_TIME_NUM_FRAMES) {
00183             pidControl7Info.sensorCounter += STEP_INPUT_NUM_FRAMES;
00184         }
00185                 
00186         if (pidControl7Info.sensorCounter == HOLD_TIME_NUM_FRAMES) {
00187             observer[event.ObsIndex()]->DeassertReady();
00188         } else {
00189             observer[event.ObsIndex()]->AssertReady();
00190         }
00191     }
00192 }
00193 
00194 void
00195 PIDControl7::OpenPrimitives()
00196 {
00197     for (int i = 0; i < NUM_JOINTS; i++) {
00198         OStatus result = OPENR::OpenPrimitive(JOINT_LOCATOR[i], &jointID[i]);
00199         if (result != oSUCCESS) {
00200             OSYSLOG1((osyslogERROR, "%s : %s %d",
00201                       "PIDControl7::DoInit()",
00202                       "OPENR::OpenPrimitive() FAILED", result));
00203         }
00204     }
00205 }
00206 
00207 void
00208 PIDControl7::NewCommandVectorData()
00209 {
00210     OStatus result;
00211     MemoryRegionID      cmdVecDataID;
00212     OCommandVectorData* cmdVecData;
00213     OCommandInfo*       info;
00214 
00215     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00216 
00217         result = OPENR::NewCommandVectorData(NUM_JOINTS, 
00218                                              &cmdVecDataID, &cmdVecData);
00219         if (result != oSUCCESS) {
00220             OSYSLOG1((osyslogERROR, "%s : %s %d",
00221                       "PIDControl7::NewCommandVectorData()",
00222                       "OPENR::NewCommandVectorData() FAILED", result));
00223         }
00224 
00225         region[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00226                                  cmdVecData->vectorInfo.offset,
00227                                  (void*)cmdVecData,
00228                                  cmdVecData->vectorInfo.totalSize);
00229 
00230         cmdVecData->SetNumData(NUM_JOINTS);
00231 
00232         for (int j = 0; j < NUM_JOINTS; j++) {
00233             info = cmdVecData->GetInfo(j);
00234             info->Set(odataJOINT_COMMAND2, jointID[j], ocommandMAX_FRAMES);
00235         }
00236     }
00237 }
00238 
00239 void
00240 PIDControl7::SetJointGain()
00241 {
00242     OPENR::EnableJointGain(jointID[HEAD_TILT1]);
00243     OPENR::SetJointGain(jointID[HEAD_TILT1],
00244                         TILT1_PGAIN, TILT1_IGAIN, TILT1_DGAIN,
00245                         PSHIFT, ISHIFT, DSHIFT);
00246     
00247     OPENR::EnableJointGain(jointID[HEAD_PAN]);
00248     OPENR::SetJointGain(jointID[HEAD_PAN],
00249                         PAN_PGAIN, PAN_IGAIN, PAN_DGAIN,
00250                         PSHIFT, ISHIFT, DSHIFT);
00251 
00252     OPENR::EnableJointGain(jointID[HEAD_TILT2]);
00253     OPENR::SetJointGain(jointID[HEAD_TILT2],
00254                         TILT2_PGAIN, TILT2_IGAIN, TILT2_DGAIN,
00255                         PSHIFT, ISHIFT, DSHIFT);
00256 
00257     int base = RFLEG_J1;
00258     for (int i = 0; i < 4; i++) {
00259 
00260         int j1 = base + 3 * i;
00261         int j2 = base + 3 * i + 1;
00262         int j3 = base + 3 * i + 2; 
00263             
00264         OPENR::EnableJointGain(jointID[j1]);        
00265         OPENR::SetJointGain(jointID[j1],
00266                             J1_PGAIN, J1_IGAIN, J1_DGAIN,
00267                             PSHIFT, ISHIFT, DSHIFT);
00268 
00269         OPENR::EnableJointGain(jointID[j2]);        
00270         OPENR::SetJointGain(jointID[j2],
00271                             J2_PGAIN, J2_IGAIN, J2_DGAIN,
00272                             PSHIFT, ISHIFT, DSHIFT);
00273 
00274         OPENR::EnableJointGain(jointID[j3]);        
00275         OPENR::SetJointGain(jointID[j3],
00276                             J3_PGAIN, J3_IGAIN, J3_DGAIN,
00277                             PSHIFT, ISHIFT, DSHIFT);
00278     }
00279 }
00280 
00281 MovingResult
00282 PIDControl7::AdjustDiffJointValue()
00283 {
00284     RCRegion* rgn = FindFreeRegion();
00285     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00286     cmdVecData->SetNumData(NUM_JOINTS);
00287 
00288     OJointValue current[NUM_JOINTS];    
00289     for (int i = 0; i < NUM_JOINTS; i++) {
00290         OJointValue current;
00291         OPENR::GetJointValue(jointID[i], &current);
00292         SetJointValue(rgn, i,
00293                       degrees(current.value/1000000.0),
00294                       degrees(current.value/1000000.0));
00295     }
00296 
00297     subject[sbjEffector]->SetData(rgn);
00298     subject[sbjEffector]->NotifyObservers();
00299 
00300     return MOVING_FINISH;
00301 }
00302 
00303 MovingResult
00304 PIDControl7::MoveToBroadBase()
00305 {
00306     static int counter = -1;
00307     static double start[NUM_JOINTS];
00308     static double delta[NUM_JOINTS];
00309     double ndiv = (double)BROADBASE_MAX_COUNTER;
00310 
00311     if (counter == -1) {
00312 
00313         for (int i = 0; i < NUM_JOINTS; i++) {
00314             OJointValue current;
00315             OPENR::GetJointValue(jointID[i], &current);
00316             start[i] = degrees(current.value/1000000.0);
00317             delta[i] = (BROADBASE_ANGLE[i] - start[i]) / ndiv;
00318         }
00319 
00320         counter = 0;
00321 
00322         RCRegion* rgn = FindFreeRegion();
00323         for (int i = 0; i < NUM_JOINTS; i++) {
00324             SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00325             start[i] += delta[i];
00326         }
00327 
00328         subject[sbjEffector]->SetData(rgn);
00329         counter ++;
00330     }
00331 
00332     RCRegion* rgn = FindFreeRegion();
00333     for (int i = 0; i < NUM_JOINTS; i++) {
00334         SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00335         start[i] += delta[i];
00336     }
00337 
00338     subject[sbjEffector]->SetData(rgn);
00339     subject[sbjEffector]->NotifyObservers();
00340 
00341     counter++;
00342     return (counter == BROADBASE_MAX_COUNTER) ? MOVING_FINISH : MOVING_CONT;
00343 }
00344 
00345 MovingResult
00346 PIDControl7::MoveToSleeping()
00347 {
00348     static int counter = -1;
00349     static double start[NUM_JOINTS];
00350     static double delta[NUM_JOINTS];
00351     double ndiv = (double)SLEEPING_MAX_COUNTER;
00352 
00353     if (counter == -1) {
00354 
00355         for (int i = 0; i < NUM_JOINTS; i++) {
00356             start[i] = BROADBASE_ANGLE[i];
00357             delta[i] = (SLEEPING_ANGLE[i] - start[i]) / ndiv;
00358         }
00359 
00360         counter = 0;
00361     }
00362 
00363     RCRegion* rgn = FindFreeRegion();
00364     for (int i = 0; i < NUM_JOINTS; i++) {
00365         SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00366         start[i] += delta[i];
00367     }
00368 
00369     subject[sbjEffector]->SetData(rgn);
00370     subject[sbjEffector]->NotifyObservers();
00371 
00372     counter++;
00373     return (counter == SLEEPING_MAX_COUNTER) ? MOVING_FINISH : MOVING_CONT;
00374 }
00375 
00376 RCRegion*
00377 PIDControl7::FindFreeRegion()
00378 {
00379     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00380         if (region[i]->NumberOfReference() == 1) return region[i];
00381     }
00382 
00383     return 0;
00384 }
00385 
00386 void
00387 PIDControl7::SetJointValue(RCRegion* rgn, int idx, double start, double end)
00388 {
00389     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00390 
00391     OCommandInfo* info = cmdVecData->GetInfo(idx);
00392     info->Set(odataJOINT_COMMAND2, jointID[idx], ocommandMAX_FRAMES);
00393 
00394     OCommandData* data = cmdVecData->GetData(idx);
00395     OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00396 
00397     double delta = end - start;
00398     for (int i = 0; i < ocommandMAX_FRAMES; i++) {
00399         double dval = start + (delta * i) / (double)ocommandMAX_FRAMES;
00400         jval[i].value = oradians(dval);
00401     }
00402 }
00403 
00404 void
00405 PIDControl7::SetJointValueForStepInput(RCRegion* rgn, int idx, double val)
00406 {
00407     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00408 
00409     OCommandInfo* info = cmdVecData->GetInfo(0);
00410     info->Set(odataJOINT_COMMAND2, jointID[idx], STEP_INPUT_NUM_FRAMES);
00411 
00412     OCommandData* data = cmdVecData->GetData(0);
00413     OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00414 
00415     for (int i = 0; i < STEP_INPUT_NUM_FRAMES; i++) {
00416         jval[i].value = oradians(val);
00417     }
00418 }
00419 
00420 void 
00421 PIDControl7::InitSensorIndex(OSensorFrameVectorData* sensorVec)
00422 {
00423     for (int i = 0; i < NUM_JOINTS; i++) {
00424         for (int j = 0; j < sensorVec->vectorInfo.numData; j++) {
00425             OSensorFrameInfo* info = sensorVec->GetInfo(j);
00426             if (info->primitiveID == jointID[i]) {
00427                 sensorIndex[i] = j;
00428                 OSYSDEBUG(("[%2d] %s\n", sensorIndex[i], JOINT_LOCATOR[i]));
00429                 break;
00430             }
00431         }
00432     }
00433 }
00434 
00435 void
00436 PIDControl7::DisableJointGainOfAllLegs()
00437 {
00438     OPENR::DisableJointGain(jointID[RFLEG_J1]);
00439     OPENR::DisableJointGain(jointID[RFLEG_J2]);
00440     OPENR::DisableJointGain(jointID[RFLEG_J3]);
00441     OPENR::DisableJointGain(jointID[RRLEG_J1]);
00442     OPENR::DisableJointGain(jointID[RRLEG_J2]);
00443     OPENR::DisableJointGain(jointID[RRLEG_J3]);
00444     OPENR::DisableJointGain(jointID[LFLEG_J1]);
00445     OPENR::DisableJointGain(jointID[LFLEG_J2]);
00446     OPENR::DisableJointGain(jointID[LFLEG_J3]);
00447     OPENR::DisableJointGain(jointID[LRLEG_J1]);
00448     OPENR::DisableJointGain(jointID[LRLEG_J2]);
00449     OPENR::DisableJointGain(jointID[LRLEG_J3]);
00450 }
00451 
00452 void
00453 PIDControl7::InputParameter()
00454 {
00455     OSYSPRINT(("[ 0]  RFLEG_J1\n"));
00456     OSYSPRINT(("[ 1]  RFLEG_J2\n"));
00457     OSYSPRINT(("[ 2]  RFLEG_J3\n"));
00458     OSYSPRINT(("[ 3]  LFLEG_J1\n"));
00459     OSYSPRINT(("[ 4]  LFLEG_J2\n"));
00460     OSYSPRINT(("[ 5]  LFLEG_J3\n"));
00461     OSYSPRINT(("[ 6]  RRLEG_J1\n"));
00462     OSYSPRINT(("[ 7]  RRLEG_J2\n"));
00463     OSYSPRINT(("[ 8]  RRLEG_J3\n"));
00464     OSYSPRINT(("[ 9]  LRLEG_J1\n"));
00465     OSYSPRINT(("[10]  LRLEG_J2\n"));
00466     OSYSPRINT(("[11]  LRLEG_J3\n"));
00467     pidControl7Info.jointIndex   = InputKey("jointIndex > ") + RFLEG_J1;
00468     pidControl7Info.pgain        = InputKey("pgain > ");
00469     pidControl7Info.igain        = InputKey("igain > ");
00470     pidControl7Info.dgain        = InputKey("dgain > ");
00471     pidControl7Info.desiredValue = InputKey("desiredValue [deg] > ");
00472 
00473     pidControl7Info.effectorCounter = 0;
00474     pidControl7Info.sensorCounter   = 0;
00475 
00476     OPENR::SetJointGain(jointID[pidControl7Info.jointIndex],
00477                         pidControl7Info.pgain,
00478                         pidControl7Info.igain,
00479                         pidControl7Info.dgain,
00480                         PSHIFT, ISHIFT, DSHIFT);
00481 }
00482 
00483 MovingResult
00484 PIDControl7::StepInput()
00485 {
00486     RCRegion* rgn = FindFreeRegion();
00487     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00488 
00489     cmdVecData->SetNumData(1);
00490     SetJointValueForStepInput(rgn, pidControl7Info.jointIndex,
00491                               (double)pidControl7Info.desiredValue);
00492 
00493     subject[sbjEffector]->SetData(rgn);
00494     subject[sbjEffector]->NotifyObservers();
00495 
00496     if (pidControl7Info.effectorCounter < HOLD_TIME_NUM_FRAMES) {
00497         pidControl7Info.effectorCounter += STEP_INPUT_NUM_FRAMES;
00498     }
00499 
00500     if (pidControl7Info.effectorCounter == HOLD_TIME_NUM_FRAMES &&
00501         pidControl7Info.sensorCounter == HOLD_TIME_NUM_FRAMES) {
00502         return MOVING_FINISH;
00503     } else {
00504         return MOVING_CONT;
00505     }
00506 }
00507 
00508 bool
00509 PIDControl7::YesNo(const char* prompt)
00510 {
00511     char line[80];
00512     
00513     while (1) {
00514         OSYSPRINT(("%s", prompt));
00515         gets(line);
00516         if (strcmp(line, "y") == 0) return true;
00517         if (strcmp(line, "n") == 0) return false;
00518     }
00519 }
00520 
00521 int
00522 PIDControl7::InputKey(const char* prompt)
00523 {
00524     OSYSPRINT(("%s", prompt));
00525     char line[80];
00526     gets(line);
00527     return (int)strtol(line, (char**)NULL, 0);
00528 }
00529 
00530 void
00531 PIDControl7::SaveLogData()
00532 {
00533     char file[16];
00534     char path[128];
00535 
00536     OSYSPRINT(("filename > "));
00537     gets(file);
00538     sprintf(path, "/MS/OPEN-R/MW/DATA/P/%s", file);
00539     
00540     FILE* fp = fopen(path, "w");
00541     if (fp == 0) {
00542         OSYSLOG1((osyslogERROR, "fopen() : can't open %s", path));
00543         return;
00544     }
00545     
00546     for (int i = 0; i < HOLD_TIME_NUM_FRAMES; i++) {
00547         fprintf(fp, "%f\t%f\t%d\n",
00548                 degrees(logData[i].value / 1000000.0),
00549                 degrees(logData[i].refValue / 1000000.0),
00550                 logData[i].pwmDuty);
00551     }
00552 
00553     fclose(fp);
00554 }

Generated on Sun Dec 2 23:04:30 2007 for openSDK by  doxygen 1.3.9.1