00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <OPENR/OPENRAPI.h>
00013 #include "NeutralAgent7.h"
00014
00015 static double BROADBASE_ANGLE[] = {
00016 0,
00017 0,
00018 40,
00019
00020 120,
00021 90,
00022 30,
00023
00024 120,
00025 90,
00026 30,
00027
00028 -120,
00029 70,
00030 30,
00031
00032 -120,
00033 70,
00034 30,
00035
00036 0,
00037 0,
00038 0
00039 };
00040
00041 static double SLEEPING_ANGLE[] = {
00042 0,
00043 0,
00044 0,
00045
00046 59,
00047 0,
00048 30,
00049
00050 59,
00051 0,
00052 30,
00053
00054 -119,
00055 4,
00056 122,
00057
00058 -119,
00059 4,
00060 122,
00061
00062 0,
00063 0,
00064 0
00065 };
00066
00067 NeutralAgent7::NeutralAgent7() : neutralAgentState(NAS7_IDLE)
00068 {
00069 }
00070
00071 void
00072 NeutralAgent7::Init(ODA* oda)
00073 {
00074 }
00075
00076 bool
00077 NeutralAgent7::AreYou(MoNetAgentID agent)
00078 {
00079 OSYSDEBUG(("NeutralAgent7::AreYou(%d)\n", agent));
00080 return (agent == monetagentNEUTRAL) ? true : false;
00081 }
00082
00083 void
00084 NeutralAgent7::NotifyCommand(const MoNetAgentCommand& command,
00085 MoNetAgentResult* result)
00086 {
00087 OSYSDEBUG(("NeutralAgent7::NotifyCommand()\n"));
00088 OSYSDEBUG((" agent : %d\n", command.agent));
00089 OSYSDEBUG((" index : %d\n", command.index));
00090 OSYSDEBUG((" syncKey : %08x\n", command.syncKey));
00091 OSYSDEBUG((" startPosture : %d\n", command.startPosture));
00092 OSYSDEBUG((" endPosture : %d\n", command.endPosture));
00093
00094 if (command.agent != monetagentNEUTRAL ||
00095 command.index != monetagentNEUTRAL_NT2SLEEP) {
00096
00097 result->agent = command.agent;
00098 result->index = command.index;
00099 result->status = monetINVALID_ARG;
00100 result->endPosture = monetpostureUNDEF;
00101 return;
00102 }
00103
00104 AdjustDiffJointValue(command.syncKey);
00105 neutralAgentState = NAS7_ADJUSTING_DIFF_JOINT_VALUE;
00106
00107 result->agent = monetagentNEUTRAL;
00108 result->index = monetagentNEUTRAL_NT2SLEEP;
00109 result->status = monetSUCCESS;
00110 result->endPosture = monetpostureUNDEF;
00111 }
00112
00113 void
00114 NeutralAgent7::ReadyEffector(const MoNetAgentCommand& command,
00115 MoNetAgentResult* result)
00116 {
00117 result->agent = monetagentNEUTRAL;
00118 result->index = monetagentNEUTRAL_NT2SLEEP;
00119 result->endPosture = monetpostureUNDEF;
00120
00121 if (neutralAgentState == NAS7_IDLE) {
00122
00123 ;
00124
00125 } else if (neutralAgentState == NAS7_ADJUSTING_DIFF_JOINT_VALUE) {
00126
00127 OSYSDEBUG(("NAS7_ADJUSTING_DIFF_JOINT_VALUE\n"));
00128 SetJointGain();
00129 MoNetStatus s = MoveToBroadBase();
00130 neutralAgentState = NAS7_MOVING_TO_BROADBASE;
00131 result->status = monetSUCCESS;
00132
00133 } else if (neutralAgentState == NAS7_MOVING_TO_BROADBASE) {
00134
00135 OSYSDEBUG(("NAS7_MOVING_TO_BROADBASE\n"));
00136 MoNetStatus s = MoveToBroadBase();
00137 if (s == monetCOMPLETION) {
00138 neutralAgentState = NAS7_MOVING_TO_SLEEPING;
00139 }
00140 result->status = monetSUCCESS;
00141
00142 } else if (neutralAgentState == NAS7_MOVING_TO_SLEEPING) {
00143
00144 OSYSDEBUG(("NAS7_MOVING_TO_SLEEPING\n"));
00145 MoNetStatus s = MoveToSleeping();
00146 if (s == monetCOMPLETION) {
00147 result->status = monetCOMPLETION;
00148 result->endPosture = monetpostureSLEEP;
00149 } else {
00150 result->status = monetSUCCESS;
00151 }
00152 }
00153 }
00154
00155 MoNetStatus
00156 NeutralAgent7::AdjustDiffJointValue(OVRSyncKey syncKey)
00157 {
00158 OJointValue current[DRX1000_NUM_JOINT2];
00159 RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00160 OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00161 cmdVecData->vectorInfo.syncKey = syncKey;
00162
00163 for (int i = 0; i < DRX1000_NUM_JOINT2; i++) {
00164 OJointValue current;
00165 OPENR::GetJointValue(moNetAgentManager->PrimitiveID(i), ¤t);
00166 SetJointValue(rgn, i,
00167 degrees(current.value/1000000.0),
00168 degrees(current.value/1000000.0));
00169 }
00170
00171 cmdVecData->SetNumData(DRX1000_NUM_JOINT2);
00172
00173 moNetAgentManager->Effector()->SetData(rgn);
00174 moNetAgentManager->Effector()->NotifyObservers();
00175
00176 return monetCOMPLETION;
00177 }
00178
00179 MoNetStatus
00180 NeutralAgent7::MoveToBroadBase()
00181 {
00182 static int counter = -1;
00183 static double start[DRX1000_NUM_JOINT2];
00184 static double delta[DRX1000_NUM_JOINT2];
00185 double ndiv = (double)BROADBASE_MAX_COUNTER;
00186
00187 if (counter == -1) {
00188
00189 for (int i = 0; i < DRX1000_NUM_JOINT2; i++) {
00190 OJointValue current;
00191 OPrimitiveID jointID = moNetAgentManager->PrimitiveID(i);
00192 OPENR::GetJointValue(jointID, ¤t);
00193 start[i] = degrees(current.value/1000000.0);
00194 delta[i] = (BROADBASE_ANGLE[i] - start[i]) / ndiv;
00195 }
00196
00197 counter = 0;
00198
00199 RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00200 for (int i = 0; i < DRX1000_NUM_JOINT2; i++) {
00201 SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00202 start[i] += delta[i];
00203 }
00204
00205 OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00206 cmdVecData->SetNumData(DRX1000_NUM_JOINT2);
00207
00208 moNetAgentManager->Effector()->SetData(rgn);
00209 counter ++;
00210 }
00211
00212 RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00213 for (int i = 0; i < DRX1000_NUM_JOINT2; i++) {
00214 SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00215 start[i] += delta[i];
00216 }
00217
00218 OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00219 cmdVecData->SetNumData(DRX1000_NUM_JOINT2);
00220
00221 moNetAgentManager->Effector()->SetData(rgn);
00222 moNetAgentManager->Effector()->NotifyObservers();
00223
00224 counter++;
00225 return (counter == BROADBASE_MAX_COUNTER) ? monetCOMPLETION : monetSUCCESS;
00226 }
00227
00228 MoNetStatus
00229 NeutralAgent7::MoveToSleeping()
00230 {
00231 static int counter = -1;
00232 static double start[DRX1000_NUM_JOINT2];
00233 static double delta[DRX1000_NUM_JOINT2];
00234 double ndiv = (double)SLEEPING_MAX_COUNTER;
00235
00236 if (counter == -1) {
00237
00238 for (int i = 0; i < DRX1000_NUM_JOINT2; i++) {
00239 start[i] = BROADBASE_ANGLE[i];
00240 delta[i] = (SLEEPING_ANGLE[i] - start[i]) / ndiv;
00241 }
00242
00243 counter = 0;
00244 }
00245
00246 RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00247 for (int i = 0; i < DRX1000_NUM_JOINT2; i++) {
00248 SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00249 start[i] += delta[i];
00250 }
00251
00252 OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00253 cmdVecData->SetNumData(DRX1000_NUM_JOINT2);
00254
00255 moNetAgentManager->Effector()->SetData(rgn);
00256 moNetAgentManager->Effector()->NotifyObservers();
00257
00258 counter++;
00259 return (counter == SLEEPING_MAX_COUNTER) ? monetCOMPLETION : monetSUCCESS;
00260 }
00261
00262 void
00263 NeutralAgent7::SetJointGain()
00264 {
00265 OSYSDEBUG(("NeutralAgent7::SetJointGain()\n"));
00266
00267 OPrimitiveID tilt1ID = moNetAgentManager->PrimitiveID(HEAD_TILT1);
00268 OPrimitiveID panID = moNetAgentManager->PrimitiveID(HEAD_PAN);
00269 OPrimitiveID tilt2ID = moNetAgentManager->PrimitiveID(HEAD_TILT2);
00270
00271 OPENR::EnableJointGain(tilt1ID);
00272 OPENR::SetJointGain(tilt1ID,
00273 TILT1_PGAIN, TILT1_IGAIN, TILT1_DGAIN,
00274 PSHIFT, ISHIFT, DSHIFT);
00275
00276 OPENR::EnableJointGain(panID);
00277 OPENR::SetJointGain(panID,
00278 PAN_PGAIN, PAN_IGAIN, PAN_DGAIN,
00279 PSHIFT, ISHIFT, DSHIFT);
00280
00281 OPENR::EnableJointGain(tilt2ID);
00282 OPENR::SetJointGain(tilt2ID,
00283 TILT2_PGAIN, TILT2_IGAIN, TILT2_DGAIN,
00284 PSHIFT, ISHIFT, DSHIFT);
00285
00286 int base = RFLEG_J1;
00287 for (int i = 0; i < 4; i++) {
00288
00289 OPrimitiveID j1ID = moNetAgentManager->PrimitiveID(base + 3 * i);
00290 OPrimitiveID j2ID = moNetAgentManager->PrimitiveID(base + 3 * i + 1);
00291 OPrimitiveID j3ID = moNetAgentManager->PrimitiveID(base + 3 * i + 2);
00292
00293 OPENR::EnableJointGain(j1ID);
00294 OPENR::SetJointGain(j1ID,
00295 J1_PGAIN, J1_IGAIN, J1_DGAIN,
00296 PSHIFT, ISHIFT, DSHIFT);
00297
00298 OPENR::EnableJointGain(j2ID);
00299 OPENR::SetJointGain(j2ID,
00300 J2_PGAIN, J2_IGAIN, J2_DGAIN,
00301 PSHIFT, ISHIFT, DSHIFT);
00302
00303 OPENR::EnableJointGain(j3ID);
00304 OPENR::SetJointGain(j3ID,
00305 J3_PGAIN, J3_IGAIN, J3_DGAIN,
00306 PSHIFT, ISHIFT, DSHIFT);
00307 }
00308
00309 OPrimitiveID tailtiltID = moNetAgentManager->PrimitiveID(TAIL_TILT);
00310 OPrimitiveID tailpanID = moNetAgentManager->PrimitiveID(TAIL_PAN);
00311
00312 OPENR::EnableJointGain(tailtiltID);
00313 OPENR::SetJointGain(tailtiltID,
00314 TAIL_PGAIN, TAIL_IGAIN, TAIL_DGAIN,
00315 PSHIFT, ISHIFT, DSHIFT);
00316
00317 OPENR::EnableJointGain(tailpanID);
00318 OPENR::SetJointGain(tailpanID,
00319 TAIL_PGAIN, TAIL_IGAIN, TAIL_DGAIN,
00320 PSHIFT, ISHIFT, DSHIFT);
00321 }
00322
00323 void
00324 NeutralAgent7::SetJointValue(RCRegion* rgn, int idx, double start, double end)
00325 {
00326 OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00327
00328 OPrimitiveID jointID = moNetAgentManager->PrimitiveID(idx);
00329 OCommandInfo* info = cmdVecData->GetInfo(idx);
00330 info->Set(odataJOINT_COMMAND2, jointID, NUM_FRAMES);
00331
00332 OCommandData* data = cmdVecData->GetData(idx);
00333 OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00334
00335 double delta = end - start;
00336 for (int i = 0; i < NUM_FRAMES; i++) {
00337 double dval = start + (delta * i) / (double)NUM_FRAMES;
00338 jval[i].value = oradians(dval);
00339 }
00340 }