NeutralAgent7.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 "NeutralAgent7.h"
00014 
00015 static double BROADBASE_ANGLE[] = {
00016     0,    // TILT1
00017     0,    // PAN
00018     40,   // TILT2
00019 
00020     120,  // RFLEG J1
00021     90,   // RFLEG J2
00022     30,   // RFLEG J3
00023 
00024     120,  // LFLEG J1
00025     90,   // LFLEG J2
00026     30,   // LFLEG J3
00027     
00028     -120, // RRLEG J1
00029     70,   // RRLEG J2
00030     30,   // RRLEG J3
00031 
00032     -120, // LRLEG J1
00033     70,   // LRLEG J2
00034     30,   // LRLEG J3
00035 
00036     0,    // TAIL TILT
00037     0,    // TAIL PAN
00038     0     // MOUTH
00039 };
00040 
00041 static double SLEEPING_ANGLE[] = {
00042     0,    // TILT1
00043     0,    // PAN
00044     0,    // TILT2
00045 
00046     59,   // RFLEG J1
00047     0,    // RFLEG J2
00048     30,   // RFLEG J3
00049 
00050     59,   // LFLEG J1
00051     0,    // LFLEG J2
00052     30,   // LFLEG J3
00053 
00054     -119, // RRLEG J1
00055     4,    // RRLEG J2
00056     122,  // RRLEG J3
00057 
00058     -119, // LRLEG J1
00059     4,    // LRLEG J2
00060     122,  // LRLEG J3
00061 
00062     0,    // TAIL TILT
00063     0,    // TAIL PAN
00064     0     // MOUTH
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         ; // do nothing
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), &current);
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, &current);
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 }

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