NeutralAgent.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 "NeutralAgent.h"
00014 
00015 static double BROADBASE_ANGLE[] = {
00016     45,   // TILT
00017     0,    // PAN
00018     0,    // ROLL
00019 
00020     117,  // RFLEG J1
00021     90,   // RFLEG J2
00022     30,   // RFLEG J3
00023 
00024     117,  // LFLEG J1
00025     90,   // LFLEG J2
00026     30,   // LFLEG J3
00027     
00028     -117, // RRLEG J1
00029     70,   // RRLEG J2
00030     30,   // RRLEG J3
00031 
00032     -117, // LRLEG J1
00033     70,   // LRLEG J2
00034     30    // LRLEG J3
00035 };
00036 
00037 static double SLEEPING_ANGLE[] = {
00038     -10,  // TILT
00039     0,    // PAN
00040     0,    // ROLL
00041 
00042     60,   // RFLEG J1
00043     0,    // RFLEG J2
00044     30,   // RFLEG J3
00045 
00046     60,   // LFLEG J1
00047     0,    // LFLEG J2
00048     30,   // LFLEG J3
00049 
00050     -117, // RRLEG J1
00051     0,    // RRLEG J2
00052     147,  // RRLEG J3
00053 
00054     -117, // LRLEG J1
00055     0,    // LRLEG J2
00056     147   // LRLEG J3
00057 };
00058 
00059 NeutralAgent::NeutralAgent() : neutralAgentState(NAS_IDLE)
00060 {
00061 }
00062 
00063 void
00064 NeutralAgent::Init(ODA* oda)
00065 {
00066 }
00067 
00068 bool
00069 NeutralAgent::AreYou(MoNetAgentID agent)
00070 {
00071     OSYSDEBUG(("NeutralAgent::AreYou(%d)\n", agent));
00072     return (agent == monetagentNEUTRAL) ? true : false;
00073 }
00074 
00075 void
00076 NeutralAgent::NotifyCommand(const MoNetAgentCommand& command,
00077                             MoNetAgentResult* result)
00078 {
00079     OSYSDEBUG(("NeutralAgent::NotifyCommand()\n"));
00080     OSYSDEBUG(("  agent        : %d\n",   command.agent));
00081     OSYSDEBUG(("  index        : %d\n",   command.index));
00082     OSYSDEBUG(("  syncKey      : %08x\n", command.syncKey));
00083     OSYSDEBUG(("  startPosture : %d\n",   command.startPosture));
00084     OSYSDEBUG(("  endPosture   : %d\n",   command.endPosture));
00085 
00086     if (command.agent != monetagentNEUTRAL ||
00087         command.index != monetagentNEUTRAL_NT2SLEEP) {
00088 
00089         result->agent      = command.agent;
00090         result->index      = command.index;
00091         result->status     = monetINVALID_ARG;
00092         result->endPosture = monetpostureUNDEF;
00093         return;
00094     }
00095 
00096     AdjustDiffJointValue(command.syncKey);
00097     neutralAgentState = NAS_ADJUSTING_DIFF_JOINT_VALUE;
00098 
00099     result->agent      = monetagentNEUTRAL;
00100     result->index      = monetagentNEUTRAL_NT2SLEEP;
00101     result->status     = monetSUCCESS;
00102     result->endPosture = monetpostureUNDEF;
00103 }
00104 
00105 void
00106 NeutralAgent::ReadyEffector(const MoNetAgentCommand& command,
00107                             MoNetAgentResult* result)
00108 {
00109     result->agent      = monetagentNEUTRAL;
00110     result->index      = monetagentNEUTRAL_NT2SLEEP;
00111     result->endPosture = monetpostureUNDEF;
00112     
00113     if (neutralAgentState == NAS_IDLE) {
00114 
00115         ; // do nothing
00116 
00117     } else if (neutralAgentState == NAS_ADJUSTING_DIFF_JOINT_VALUE) {
00118 
00119         OSYSDEBUG(("NAS_ADJUSTING_DIFF_JOINT_VALUE\n"));
00120         SetJointGain();
00121         MoNetStatus s = MoveToBroadBase();
00122         neutralAgentState = NAS_MOVING_TO_BROADBASE;
00123         result->status = monetSUCCESS;
00124 
00125     } else if (neutralAgentState == NAS_MOVING_TO_BROADBASE) {
00126 
00127         OSYSDEBUG(("NAS_MOVING_TO_BROADBASE\n"));
00128         MoNetStatus s = MoveToBroadBase();
00129         if (s == monetCOMPLETION) {
00130             neutralAgentState = NAS_MOVING_TO_SLEEPING;
00131         }
00132         result->status = monetSUCCESS;
00133 
00134     } else if (neutralAgentState == NAS_MOVING_TO_SLEEPING) {
00135 
00136         OSYSDEBUG(("NAS_MOVING_TO_SLEEPING\n"));
00137         MoNetStatus s = MoveToSleeping();
00138         if (s == monetCOMPLETION) {
00139             result->status     = monetCOMPLETION;
00140             result->endPosture = monetpostureSLEEP;
00141         } else {
00142             result->status = monetSUCCESS;
00143         }
00144     }
00145 }
00146 
00147 MoNetStatus
00148 NeutralAgent::AdjustDiffJointValue(OVRSyncKey syncKey)
00149 {
00150     OJointValue current[DRX900_NUM_JOINTS];
00151     RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00152     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00153     cmdVecData->vectorInfo.syncKey = syncKey;
00154 
00155     for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00156         OJointValue current;
00157         OPENR::GetJointValue(moNetAgentManager->PrimitiveID(i), &current);
00158         SetJointValue(rgn, i,
00159                       degrees(current.value/1000000.0),
00160                       degrees(current.value/1000000.0));
00161     }
00162 
00163     moNetAgentManager->Effector()->SetData(rgn);
00164     moNetAgentManager->Effector()->NotifyObservers();
00165 
00166     return monetCOMPLETION;
00167 }
00168 
00169 MoNetStatus
00170 NeutralAgent::MoveToBroadBase()
00171 {
00172     static int counter = -1;
00173     static double start[DRX900_NUM_JOINTS];
00174     static double delta[DRX900_NUM_JOINTS];
00175     double ndiv = (double)BROADBASE_MAX_COUNTER;
00176 
00177     if (counter == -1) {
00178 
00179         for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00180             OJointValue current;
00181             OPrimitiveID jointID = moNetAgentManager->PrimitiveID(i);
00182             OPENR::GetJointValue(jointID, &current);
00183             start[i] = degrees(current.value/1000000.0);
00184             delta[i] = (BROADBASE_ANGLE[i] - start[i]) / ndiv;
00185         }
00186 
00187         counter = 0;
00188 
00189         RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00190         for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00191             SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00192             start[i] += delta[i];
00193         }
00194 
00195         moNetAgentManager->Effector()->SetData(rgn);
00196         counter ++;
00197     }
00198 
00199     RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00200     for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00201         SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00202         start[i] += delta[i];
00203     }
00204 
00205     moNetAgentManager->Effector()->SetData(rgn);
00206     moNetAgentManager->Effector()->NotifyObservers();
00207 
00208     counter++;
00209     return (counter == BROADBASE_MAX_COUNTER) ? monetCOMPLETION : monetSUCCESS;
00210 }
00211 
00212 MoNetStatus
00213 NeutralAgent::MoveToSleeping()
00214 {
00215     static int counter = -1;
00216     static double start[DRX900_NUM_JOINTS];
00217     static double delta[DRX900_NUM_JOINTS];
00218     double ndiv = (double)SLEEPING_MAX_COUNTER;
00219 
00220     if (counter == -1) {
00221 
00222         for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00223             start[i] = BROADBASE_ANGLE[i];
00224             delta[i] = (SLEEPING_ANGLE[i] - start[i]) / ndiv;
00225         }
00226 
00227         counter = 0;
00228     }
00229 
00230     RCRegion* rgn = moNetAgentManager->FindFreeCommandRegion();
00231     for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00232         SetJointValue(rgn, i, start[i], start[i] + delta[i]);
00233         start[i] += delta[i];
00234     }
00235 
00236     moNetAgentManager->Effector()->SetData(rgn);
00237     moNetAgentManager->Effector()->NotifyObservers();
00238 
00239     counter++;
00240     return (counter == SLEEPING_MAX_COUNTER) ? monetCOMPLETION : monetSUCCESS;
00241 }
00242 
00243 void
00244 NeutralAgent::SetJointGain()
00245 {
00246     OPrimitiveID tiltID = moNetAgentManager->PrimitiveID(HEAD_TILT);
00247     OPrimitiveID panID  = moNetAgentManager->PrimitiveID(HEAD_PAN);
00248     OPrimitiveID rollID = moNetAgentManager->PrimitiveID(HEAD_ROLL);
00249 
00250     OPENR::EnableJointGain(tiltID);
00251     OPENR::SetJointGain(tiltID,
00252                         TILT_PGAIN, TILT_IGAIN, TILT_DGAIN,
00253                         PSHIFT, ISHIFT, DSHIFT);
00254     
00255     OPENR::EnableJointGain(panID);
00256     OPENR::SetJointGain(panID,
00257                         PAN_PGAIN, PAN_IGAIN, PAN_DGAIN,
00258                         PSHIFT, ISHIFT, DSHIFT);
00259 
00260     OPENR::EnableJointGain(rollID);
00261     OPENR::SetJointGain(rollID,
00262                         ROLL_PGAIN, ROLL_IGAIN, ROLL_DGAIN,
00263                         PSHIFT, ISHIFT, DSHIFT);
00264 
00265     int base = RFLEG_J1;
00266     for (int i = 0; i < 4; i++) {
00267 
00268         OPrimitiveID j1ID = moNetAgentManager->PrimitiveID(base + 3 * i);
00269         OPrimitiveID j2ID = moNetAgentManager->PrimitiveID(base + 3 * i + 1);
00270         OPrimitiveID j3ID = moNetAgentManager->PrimitiveID(base + 3 * i + 2);
00271         
00272         OPENR::EnableJointGain(j1ID);        
00273         OPENR::SetJointGain(j1ID,
00274                             J1_PGAIN, J1_IGAIN, J1_DGAIN,
00275                             PSHIFT, ISHIFT, DSHIFT);
00276 
00277         OPENR::EnableJointGain(j2ID);
00278         OPENR::SetJointGain(j2ID,
00279                             J2_PGAIN, J2_IGAIN, J2_DGAIN,
00280                             PSHIFT, ISHIFT, DSHIFT);
00281 
00282         OPENR::EnableJointGain(j3ID);
00283         OPENR::SetJointGain(j3ID,
00284                             J3_PGAIN, J3_IGAIN, J3_DGAIN,
00285                             PSHIFT, ISHIFT, DSHIFT);
00286     }
00287 }
00288 
00289 void
00290 NeutralAgent::SetJointValue(RCRegion* rgn, int idx, double start, double end)
00291 {
00292     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00293 
00294     OPrimitiveID jointID = moNetAgentManager->PrimitiveID(idx);
00295     OCommandInfo* info = cmdVecData->GetInfo(idx);
00296     info->Set(odataJOINT_COMMAND2, jointID, NUM_FRAMES);
00297 
00298     OCommandData* data = cmdVecData->GetData(idx);
00299     OJointCommandValue2* jval = (OJointCommandValue2*)data->value;
00300 
00301     double delta = end - start;
00302     for (int i = 0; i < NUM_FRAMES; i++) {
00303         double dval = start + (delta * i) / (double)NUM_FRAMES;
00304         jval[i].value = oradians(dval);
00305     }
00306 }

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