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