MoNetAgentManager.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 <OPENR/OSyslog.h>
00014 #include "MoNetAgentManager.h"
00015 #include "MoNetAgent.h"
00016 
00017 MoNetAgentManager::MoNetAgentManager() : 
00018     prevMoNetAgent(0),
00019     activeMoNetAgent(0),
00020     activeAgentCommand(),
00021     moNetAgentList(),
00022     effectorSubject(0),
00023     motionDataID(odesigndataID_UNDEF), 
00024     motionODA()
00025 {
00026     for (int i = 0; i < MAX_NUM_JOINTS; i++)
00027         primitiveID[i] = oprimitiveID_UNDEF;
00028 
00029     for (int i = 0; i < NUM_COMMAND_VECTOR; i++)
00030         commandRegions[i] = 0;
00031 }
00032 
00033 void
00034 MoNetAgentManager::InitDRX900()
00035 {
00036     OSYSDEBUG(("MoNetAgentManager::InitDRX900()\n"));
00037 
00038     OpenPrimitivesDRX900();
00039     NewCommandVectorDataDRX900();
00040     LoadMotionODA();
00041 
00042     list<MoNetAgent*>::iterator iter = moNetAgentList.begin();
00043     list<MoNetAgent*>::iterator last = moNetAgentList.end();
00044     while (iter != last) {
00045         (*iter)->Init(&motionODA);
00046         ++iter;
00047     }
00048 }
00049 
00050 void
00051 MoNetAgentManager::InitDRX1000()
00052 {
00053     OSYSDEBUG(("MoNetAgentManager::InitDRX1000()\n"));
00054 
00055     OpenPrimitivesDRX1000();
00056     NewCommandVectorDataDRX1000();
00057     LoadMotionODA();
00058 
00059     list<MoNetAgent*>::iterator iter = moNetAgentList.begin();
00060     list<MoNetAgent*>::iterator last = moNetAgentList.end();
00061     while (iter != last) {
00062         (*iter)->Init(&motionODA);
00063         ++iter;
00064     }
00065 }
00066 
00067 void
00068 MoNetAgentManager::Start(OSubject* effector)
00069 {
00070     OSYSDEBUG(("MoNetAgentManager::Start()\n"));
00071     effectorSubject = effector;
00072 }
00073 
00074 void
00075 MoNetAgentManager::RegisterMoNetAgent(MoNetAgent* m)
00076 {
00077     OSYSDEBUG(("MoNetAgentManager::RegisterMoNetAgent()\n"));
00078     moNetAgentList.push_back(m);
00079     m->SetMoNetAgentManager(this);
00080 }
00081 
00082 void
00083 MoNetAgentManager::NotifyCommand(const ONotifyEvent& event,
00084                                  MoNetAgentResult* result)
00085 {
00086     MoNetAgentCommand* cmd = (MoNetAgentCommand*)event.Data(0);
00087 
00088     if (activeMoNetAgent != 0) {
00089         // BUSY
00090         result->agent      = cmd->agent;
00091         result->index      = cmd->index;
00092         result->status     = monetBUSY;
00093         result->endPosture = monetpostureUNDEF;
00094         return;
00095     }
00096     
00097     list<MoNetAgent*>::iterator iter = moNetAgentList.begin();
00098     list<MoNetAgent*>::iterator last = moNetAgentList.end();
00099     while (iter != last) {
00100         if ((*iter)->AreYou(cmd->agent) == true) {
00101             if (*iter != prevMoNetAgent &&
00102                 cmd->agent != monetagentNEUTRAL) {
00103                 (*iter)->SetJointGain();
00104             }
00105             (*iter)->NotifyCommand(*cmd, result);
00106             if (result->status != monetSUCCESS) return;
00107             activeMoNetAgent   = *iter;
00108             activeAgentCommand = *cmd;
00109             return;
00110         }
00111         ++iter;
00112     }
00113 
00114     // INVALID_ARG
00115     result->agent      = cmd->agent;
00116     result->index      = cmd->index;
00117     result->status     = monetINVALID_ARG;
00118     result->endPosture = monetpostureUNDEF;
00119 }
00120 
00121 void
00122 MoNetAgentManager::ReadyEffector(const OReadyEvent& event,
00123                                  MoNetAgentResult* result)
00124     
00125 {
00126     if (activeMoNetAgent == 0) {
00127         result->agent      = monetagentUNDEF;
00128         result->index      = -1;
00129         result->status     = monetSUCCESS;
00130         result->endPosture = monetpostureUNDEF;
00131         return;
00132     }
00133 
00134     activeMoNetAgent->ReadyEffector(activeAgentCommand, result);
00135     if (result->status != monetSUCCESS) {
00136         prevMoNetAgent = activeMoNetAgent;
00137         activeMoNetAgent = 0;
00138         activeAgentCommand.Clear();
00139     }
00140 }
00141 
00142 void
00143 MoNetAgentManager::OpenPrimitivesDRX900()
00144 {
00145     OSYSDEBUG(("MoNetAgentManager::OpenPrimitivesDRX900()\n"));
00146 
00147     OStatus result;
00148 
00149     for (int i = 0; i < DRX900_NUM_JOINTS; i++) {
00150         result = OPENR::OpenPrimitive(DRX900_JOINT_LOCATOR[i],
00151                                       &primitiveID[i]);
00152         if (result != oSUCCESS) {
00153             OSYSLOG1((osyslogERROR, "%s : %s %d",
00154                       "MoNetAgentManager::OpenPrimitivesDRX900()",
00155                       "OPENR::OpenPrimitive() FAILED", result));
00156         }
00157     }
00158 }
00159 
00160 void
00161 MoNetAgentManager::OpenPrimitivesDRX1000()
00162 {
00163     OSYSDEBUG(("MoNetAgentManager::OpenPrimitivesDRX1000()\n"));
00164 
00165     OStatus result;
00166 
00167     for (int i = 0; i < DRX1000_NUM_JOINTS; i++) {
00168         result = OPENR::OpenPrimitive(DRX1000_JOINT_LOCATOR[i],
00169                                       &primitiveID[i]);
00170         if (result != oSUCCESS) {
00171             OSYSLOG1((osyslogERROR, "%s : %s %d",
00172                       "MoNetAgentManager::OpenPrimitivesDRX1000()",
00173                       "OPENR::OpenPrimitive() FAILED", result));
00174         }
00175 
00176         OSYSDEBUG(("%s %x\n", DRX1000_JOINT_LOCATOR[i], primitiveID[i]));
00177     }
00178 }
00179 
00180 void
00181 MoNetAgentManager::NewCommandVectorDataDRX900()
00182 {
00183     OSYSDEBUG(("MoNetAgentManager::NewCommandVectorData()\n"));
00184 
00185     OStatus result;
00186     MemoryRegionID      cmdVecDataID;
00187     OCommandVectorData* cmdVecData;
00188     OCommandInfo*       info;
00189 
00190     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00191 
00192         result = OPENR::NewCommandVectorData(DRX900_NUM_JOINTS, 
00193                                              &cmdVecDataID, &cmdVecData);
00194         if (result != oSUCCESS) {
00195             OSYSLOG1((osyslogERROR, "%s : %s %d",
00196                       "MoNetAgentManager::NewCommandVectorData()",
00197                       "OPENR::NewCommandVectorData() FAILED", result));
00198         }
00199 
00200         commandRegions[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00201                                          cmdVecData->vectorInfo.offset,
00202                                          (void*)cmdVecData,
00203                                          cmdVecData->vectorInfo.totalSize);
00204 
00205         cmdVecData->SetNumData(DRX900_NUM_JOINTS);
00206 
00207         for (int j = 0; j < DRX900_NUM_JOINTS; j++) {
00208             info = cmdVecData->GetInfo(j);
00209             info->Set(odataJOINT_COMMAND2, primitiveID[j], NUM_FRAMES);
00210         }
00211     }
00212 }
00213 
00214 void
00215 MoNetAgentManager::NewCommandVectorDataDRX1000()
00216 {
00217     OSYSDEBUG(("MoNetAgentManager::NewCommandVectorData()\n"));
00218 
00219     OStatus result;
00220     MemoryRegionID      cmdVecDataID;
00221     OCommandVectorData* cmdVecData;
00222     OCommandInfo*       info;
00223 
00224     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00225 
00226         result = OPENR::NewCommandVectorData(DRX1000_NUM_JOINTS, 
00227                                              &cmdVecDataID, &cmdVecData);
00228         if (result != oSUCCESS) {
00229             OSYSLOG1((osyslogERROR, "%s : %s %d",
00230                       "MoNetAgentManager::NewCommandVectorData()",
00231                       "OPENR::NewCommandVectorData() FAILED", result));
00232         }
00233 
00234         commandRegions[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00235                                          cmdVecData->vectorInfo.offset,
00236                                          (void*)cmdVecData,
00237                                          cmdVecData->vectorInfo.totalSize);
00238 
00239         for (int j = 0; j < DRX1000_NUM_JOINT2; j++) {
00240             info = cmdVecData->GetInfo(j);
00241             info->Set(odataJOINT_COMMAND2, primitiveID[j], NUM_FRAMES);
00242         }
00243 
00244 
00245 
00246         for (int j = DRX1000_NUM_JOINT2; j < DRX1000_NUM_JOINTS; j++) {
00247             info = cmdVecData->GetInfo(j);
00248             info->Set(odataJOINT_COMMAND4, primitiveID[j], NUM_FRAMES);
00249         }
00250 
00251         // cmdVecData->SetNumData(DRX1000_NUM_JOINT2);
00252         cmdVecData->SetNumData(DRX1000_NUM_JOINTS);
00253     }
00254 }
00255 
00256 RCRegion*
00257 MoNetAgentManager::FindFreeCommandRegion()
00258 {   
00259     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00260         if (commandRegions[i]->NumberOfReference() == 1) {
00261             return commandRegions[i];
00262         }
00263     }
00264 
00265     return 0;
00266 }
00267 
00268 void
00269 MoNetAgentManager::LoadMotionODA()
00270 {
00271     OSYSDEBUG(("MoNetAgentManager::LoadMotionODA()\n"));
00272 
00273     OStatus result;
00274     byte*   addr;
00275     size_t  size;
00276 
00277     result = OPENR::FindDesignData(MONET_MOTION_KEYWORD,
00278                                    &motionDataID, &addr, &size);
00279     if (result != oSUCCESS) {
00280         OSYSLOG1((osyslogERROR, "%s : %s %d",
00281                   "MTNAgent::LoadMotionODA()",
00282                   "OPENR::FindDesignData() FAILED", result));
00283         return;
00284     }
00285 
00286     motionODA.Set(addr);
00287 }

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