MovingLegs/MovingLegs/MovingLegs.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002 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 <math.h>
00013 #include <OPENR/OPENRAPI.h>
00014 #include <OPENR/OUnits.h>
00015 #include <OPENR/OSyslog.h>
00016 #include <OPENR/core_macro.h>
00017 #include "MovingLegs.h"
00018 
00019 MovingLegs::MovingLegs() : movingLegsState(MLS_IDLE)
00020 {
00021 }
00022 
00023 OStatus
00024 MovingLegs::DoInit(const OSystemEvent& event)
00025 {
00026     OSYSDEBUG(("MovingLegs::DoInit()\n"));
00027 
00028     NEW_ALL_SUBJECT_AND_OBSERVER;
00029     REGISTER_ALL_ENTRY;
00030     SET_ALL_READY_AND_NOTIFY_ENTRY;
00031 
00032     OpenPrimitives();
00033     NewCommandVectorData();
00034 
00035     // 
00036     // OPENR::SetMotorPower(opowerON) is executed in blinkingLED.
00037     // So, it isn't necessary here.
00038     //
00039     
00040     return oSUCCESS;
00041 }
00042 
00043 OStatus
00044 MovingLegs::DoStart(const OSystemEvent& event)
00045 {
00046     OSYSDEBUG(("MovingLegs::DoStart()\n"));
00047 
00048     if (subject[sbjMove]->IsReady() == true) {
00049         AdjustDiffJointValue();
00050         movingLegsState = MLS_ADJUSTING_DIFF_JOINT_VALUE;
00051     } else {
00052         movingLegsState = MLS_START;
00053     }
00054 
00055     ENABLE_ALL_SUBJECT;
00056     ASSERT_READY_TO_ALL_OBSERVER;
00057 
00058     return oSUCCESS;
00059 }
00060 
00061 OStatus
00062 MovingLegs::DoStop(const OSystemEvent& event)
00063 {
00064     OSYSDEBUG(("MovingLegs::DoStop()\n"));
00065 
00066     movingLegsState = MLS_IDLE;
00067 
00068     DISABLE_ALL_SUBJECT;
00069     DEASSERT_READY_TO_ALL_OBSERVER;
00070 
00071     return oSUCCESS;
00072 }
00073 
00074 OStatus
00075 MovingLegs::DoDestroy(const OSystemEvent& event)
00076 {
00077     DELETE_ALL_SUBJECT_AND_OBSERVER;
00078     return oSUCCESS;
00079 }
00080 
00081 void
00082 MovingLegs::Ready(const OReadyEvent& event)
00083 {
00084     OSYSDEBUG(("MovingLegs::Ready()\n"));
00085 
00086     if (movingLegsState == MLS_IDLE) {
00087 
00088         OSYSDEBUG(("MLS_IDLE\n"));
00089         ; // do nothing
00090 
00091     } else if (movingLegsState == MLS_START) {
00092 
00093         OSYSDEBUG(("MLS_START\n"));
00094         AdjustDiffJointValue();
00095         movingLegsState = MLS_ADJUSTING_DIFF_JOINT_VALUE;
00096 
00097     } else if (movingLegsState == MLS_ADJUSTING_DIFF_JOINT_VALUE) {
00098 
00099         OSYSDEBUG(("MLS_ADJUSTING_DIFF_JOINT_VALUE\n"));
00100         SetJointGain();
00101         MovingResult r = MoveToBroadBase();
00102         movingLegsState = MLS_MOVING_TO_BROADBASE;
00103 
00104     } else if (movingLegsState == MLS_MOVING_TO_BROADBASE) {
00105 
00106         OSYSDEBUG(("MLS_MOVING_TO_BROADBASE\n"));
00107         MovingResult r = MoveToBroadBase();
00108         if (r == MOVING_FINISH) {
00109             movingLegsState = MLS_MOVING_TO_SLEEPING;
00110         }
00111 
00112     } else if (movingLegsState == MLS_MOVING_TO_SLEEPING) {
00113 
00114         OSYSDEBUG(("MLS_MOVING_TO_SLEEPING\n"));
00115         MovingResult r = MoveToSleeping();
00116         if (r == MOVING_FINISH) {
00117             movingLegsState = MLS_IDLE;
00118         }
00119     }
00120 }
00121 
00122 void
00123 MovingLegs::OpenPrimitives()
00124 {
00125     for (int i = 0; i < NUM_JOINTS; i++) {
00126         OStatus result = OPENR::OpenPrimitive(JOINT_LOCATOR[i], &jointID[i]);
00127         if (result != oSUCCESS) {
00128             OSYSLOG1((osyslogERROR, "%s : %s %d",
00129                       "MovingLegs::DoInit()",
00130                       "OPENR::OpenPrimitive() FAILED", result));
00131         }
00132     }
00133 }
00134 
00135 void
00136 MovingLegs::NewCommandVectorData()
00137 {
00138     OStatus result;
00139     MemoryRegionID      cmdVecDataID;
00140     OCommandVectorData* cmdVecData;
00141     OCommandInfo*       info;
00142 
00143     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00144 
00145         result = OPENR::NewCommandVectorData(NUM_JOINTS, 
00146                                              &cmdVecDataID, &cmdVecData);
00147         if (result != oSUCCESS) {
00148             OSYSLOG1((osyslogERROR, "%s : %s %d",
00149                       "MovingLegs::NewCommandVectorData()",
00150                       "OPENR::NewCommandVectorData() FAILED", result));
00151         }
00152 
00153         region[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00154                                  cmdVecData->vectorInfo.offset,
00155                                  (void*)cmdVecData,
00156                                  cmdVecData->vectorInfo.totalSize);
00157 
00158         cmdVecData->SetNumData(NUM_JOINTS);
00159 
00160         for (int j = 0; j < NUM_JOINTS; j++) {
00161             info = cmdVecData->GetInfo(j);
00162             info->Set(odataJOINT_COMMAND2, jointID[j], ocommandMAX_FRAMES);
00163         }
00164     }
00165 }
00166 
00167 void
00168 MovingLegs::SetJointGain()
00169 {
00170     for (int i = 0; i < 4; i++) {
00171 
00172         int j1 = 3 * i;
00173         int j2 = 3 * i + 1;
00174         int j3 = 3 * i + 2; 
00175             
00176         OPENR::EnableJointGain(jointID[j1]);        
00177         OPENR::SetJointGain(jointID[j1],
00178                             J1_PGAIN, J1_IGAIN, J1_DGAIN,
00179                             PSHIFT, ISHIFT, DSHIFT);
00180 
00181         OPENR::EnableJointGain(jointID[j2]);        
00182         OPENR::SetJointGain(jointID[j2],
00183                             J2_PGAIN, J2_IGAIN, J2_DGAIN,
00184                             PSHIFT, ISHIFT, DSHIFT);
00185 
00186         OPENR::EnableJointGain(jointID[j3]);        
00187         OPENR::SetJointGain(jointID[j3],
00188                             J3_PGAIN, J3_IGAIN, J3_DGAIN,
00189                             PSHIFT, ISHIFT, DSHIFT);
00190     }
00191 }
00192 
00193 MovingResult
00194 MovingLegs::AdjustDiffJointValue()
00195 {
00196     OJointValue current[NUM_JOINTS];
00197 
00198     for (int i = 0; i < NUM_JOINTS; i++) {
00199         OJointValue current;
00200         OPENR::GetJointValue(jointID[i], &current);
00201         SetJointValue(region[0], i,
00202                       degrees(current.value/1000000.0),
00203                       degrees(current.value/1000000.0));
00204     }
00205 
00206     subject[sbjMove]->SetData(region[0]);
00207     subject[sbjMove]->NotifyObservers();
00208 
00209     return MOVING_FINISH;
00210 }
00211 
00212 MovingResult
00213 MovingLegs::MoveToBroadBase()
00214 {
00215     static int counter = -1;
00216     static double start[NUM_JOINTS];
00217     static double delta[NUM_JOINTS];
00218     double ndiv = (double)BROADBASE_MAX_COUNTER;
00219 
00220     if (counter == -1) {
00221 
00222         for (int i = 0; i < NUM_JOINTS; i++) {
00223             OJointValue current;
00224             OPENR::GetJointValue(jointID[i], &current);
00225             start[i] = degrees(current.value/1000000.0);
00226             delta[i] = (BROADBASE_ANGLE[i] - start[i]) / ndiv;
00227         }
00228 
00229         counter = 0;
00230 
00231         RCRegion* rgn = FindFreeRegion();
00232         for (int i = 0; i < NUM_JOINTS; i++) {
00233             SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00234             start[i] += delta[i];
00235         }
00236 
00237         subject[sbjMove]->SetData(rgn);
00238         counter ++;
00239     }
00240 
00241     RCRegion* rgn = FindFreeRegion();
00242     for (int i = 0; i < NUM_JOINTS; i++) {
00243         SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00244         start[i] += delta[i];
00245     }
00246 
00247     subject[sbjMove]->SetData(rgn);
00248     subject[sbjMove]->NotifyObservers();
00249 
00250     counter++;
00251     return (counter == BROADBASE_MAX_COUNTER) ? MOVING_FINISH : MOVING_CONT;
00252 }
00253 
00254 MovingResult
00255 MovingLegs::MoveToSleeping()
00256 {
00257     static int counter = -1;
00258     static double start[NUM_JOINTS];
00259     static double delta[NUM_JOINTS];
00260     double ndiv = (double)SLEEPING_MAX_COUNTER;
00261 
00262     if (counter == -1) {
00263 
00264         for (int i = 0; i < NUM_JOINTS; i++) {
00265             start[i] = BROADBASE_ANGLE[i];
00266             delta[i] = (SLEEPING_ANGLE[i] - start[i]) / ndiv;
00267         }
00268 
00269         counter = 0;
00270     }
00271 
00272     RCRegion* rgn = FindFreeRegion();
00273     for (int i = 0; i < NUM_JOINTS; i++) {
00274         SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00275         start[i] += delta[i];
00276     }
00277 
00278     subject[sbjMove]->SetData(rgn);
00279     subject[sbjMove]->NotifyObservers();
00280 
00281     counter++;
00282     return (counter == SLEEPING_MAX_COUNTER) ? MOVING_FINISH : MOVING_CONT;
00283 }
00284 
00285 RCRegion*
00286 MovingLegs::FindFreeRegion()
00287 {
00288     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00289         if (region[i]->NumberOfReference() == 1) return region[i];
00290     }
00291 
00292     return 0;
00293 }
00294 
00295 void
00296 MovingLegs::SetJointValue(RCRegion* rgn, int idx, double start, double end)
00297 {
00298     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00299 
00300     OCommandInfo* info = cmdVecData->GetInfo(idx);
00301     info->Set(odataJOINT_COMMAND2, jointID[idx], ocommandMAX_FRAMES);
00302 
00303     OCommandData* data = cmdVecData->GetData(idx);
00304     OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00305 
00306     double delta = end - start;
00307     for (int i = 0; i < ocommandMAX_FRAMES; i++) {
00308         double dval = start + (delta * i) / (double)ocommandMAX_FRAMES;
00309         jval[i].value = oradians(dval);
00310     }
00311 }

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