AgentManagerA.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #define NUM_STORED_FRAMES 20
00038
00039 #include "BeoSub/BeeBrain/AgentManagerA.H"
00040
00041
00042
00043
00044 void* runForwardVisionAgent(void* a)
00045 {
00046 AgentManagerA* am = (AgentManagerA *)a;
00047 rutz::shared_ptr<ForwardVisionAgent> fv = am->getForwardVisionAgent();
00048
00049 fv->run();
00050
00051 return NULL;
00052 }
00053
00054
00055 void* runPreFrontalCortexAgent(void* a)
00056 {
00057 AgentManagerA* am = (AgentManagerA *)a;
00058 rutz::shared_ptr<PreFrontalCortexAgent> pfc = am->getPreFrontalCortexAgent();
00059
00060 pfc->run();
00061 return NULL;
00062 }
00063
00064
00065 void* runPreMotorComplex(void* a)
00066 {
00067 AgentManagerA* am = (AgentManagerA *)a;
00068 rutz::shared_ptr<PreMotorComplex> pmc = am->getPreMotorComplex();
00069
00070
00071
00072 return NULL;
00073 }
00074
00075
00076 AgentManagerA::AgentManagerA(OptionManager& mgr,
00077 const std::string& descrName,
00078 const std::string& tagName)
00079 :
00080 ModelComponent(mgr, descrName, tagName)
00081 {
00082
00083 rutz::shared_ptr<AgentManagerA> ama(this);
00084
00085
00086 itsForwardVisionAgent.reset(new ForwardVisionAgent());
00087 itsPreFrontalCortexAgent.reset(new PreFrontalCortexAgent("preFrontalCortexAgent",ama));
00088 itsPreMotorComplex.reset(new PreMotorComplex("preMotorComplexAgent"));
00089
00090
00091 itsForwardVisionAgent->setPreFrontalCortexAgent(itsPreFrontalCortexAgent);
00092 itsPreFrontalCortexAgent->setPreMotorComplex(itsPreMotorComplex);
00093 itsPreFrontalCortexAgent->setForwardVisionAgent(itsForwardVisionAgent);
00094 itsPreMotorComplex->setPreFrontalCortexAgent(itsPreFrontalCortexAgent);
00095
00096
00097 pthread_create
00098 (&itsForwardVisionAgentThread, NULL, runForwardVisionAgent,
00099 (void *)this);
00100
00101 pthread_create
00102 (&itsPreFrontalCortexAgentThread, NULL, runPreFrontalCortexAgent,
00103 (void *)this);
00104
00105 pthread_create
00106 (&itsPreMotorComplexThread, NULL, runPreMotorComplex,
00107 (void *)this);
00108
00109 pthread_mutex_init(&itsDisplayLock, NULL);
00110 pthread_mutex_init(&itsCurrentImageLock, NULL);
00111 pthread_mutex_init(&itsCommandsLock, NULL);
00112
00113
00114 itsInputImageTimer.reset(new Timer(1000000));
00115 itsFrameDuration.resize(NUM_STORED_FRAMES);
00116 }
00117
00118
00119 AgentManagerA::~AgentManagerA()
00120 { }
00121
00122
00123 void AgentManagerA::startRun()
00124 {
00125
00126 itsPreFrontalCortexAgent->start();
00127 }
00128
00129
00130 void AgentManagerA::setCurrentImage
00131 (Image<PixRGB<byte> > image, uint fNum)
00132 {
00133
00134 pthread_mutex_lock(&itsCurrentImageLock);
00135 itsCurrentImage = image;
00136 itsFrameNumber = fNum;
00137 pthread_mutex_unlock(&itsCurrentImageLock);
00138
00139
00140 itsFrameDuration[fNum % NUM_STORED_FRAMES] = itsInputImageTimer->get();
00141 itsInputImageTimer->reset();
00142
00143 uint nFrames = NUM_STORED_FRAMES;
00144 if (nFrames < NUM_STORED_FRAMES) nFrames = fNum;
00145 uint64 avg = 0ULL;
00146 for(uint i = 0; i < nFrames; i ++) avg += itsFrameDuration[i];
00147 float frate = 1000000.0F / float(avg) * float(NUM_STORED_FRAMES);
00148
00149 std::string ntext(sformat("%6d: %6.3f fps -> %8.3f ms/fr",
00150 fNum,frate, 1000.0/frate));
00151 writeText(image, Point2D<int>(0,0), ntext.c_str());
00152
00153 if((fNum % 30) == 0)
00154 drawImage(image,Point2D<int>(0,0));
00155
00156 }
00157
00158
00159 void AgentManagerA::pushCommand(CommandType cmdType,
00160 DataTypes dataType,
00161 rutz::shared_ptr<OceanObject> oceanObject)
00162 {
00163 rutz::shared_ptr<AgentManagerCommand> cmd(new AgentManagerCommand());
00164 cmd->itsCommandType = cmdType;
00165 cmd->itsDataType = dataType;
00166 cmd->itsOceanObjectId = oceanObject->getId();
00167 cmd->itsOceanObjectType = oceanObject->getType();
00168
00169 pthread_mutex_lock(&itsCommandsLock);
00170 itsCommands.push_back(cmd);
00171 pthread_mutex_unlock(&itsCommandsLock);
00172 }
00173
00174
00175 uint AgentManagerA::getNumCommands()
00176 {
00177 return itsCommands.size();
00178 }
00179
00180
00181 rutz::shared_ptr<AgentManagerCommand> AgentManagerA::popCommand()
00182 {
00183 rutz::shared_ptr<AgentManagerCommand> amc = itsCommands.front();
00184 itsCommands.pop_front();
00185 return amc;
00186 }
00187
00188
00189 rutz::shared_ptr<ForwardVisionAgent>
00190 AgentManagerA::getForwardVisionAgent()
00191 {
00192 return itsForwardVisionAgent;
00193 }
00194
00195
00196 rutz::shared_ptr<PreFrontalCortexAgent>
00197 AgentManagerA::getPreFrontalCortexAgent()
00198 {
00199 return itsPreFrontalCortexAgent;
00200 }
00201
00202
00203 rutz::shared_ptr<PreMotorComplex>
00204 AgentManagerA::getPreMotorComplex()
00205 {
00206 return itsPreMotorComplex;
00207 }
00208
00209
00210 void AgentManagerA::drawImage(Image<PixRGB<byte> > ima, Point2D<int> point)
00211 {
00212 pthread_mutex_lock(&itsDisplayLock);
00213 inplacePaste(itsDisplayImage, ima, point);
00214 itsWindow->drawImage(itsDisplayImage,0,0);
00215 pthread_mutex_unlock(&itsDisplayLock);
00216
00217 }
00218
00219
00220 bool AgentManagerA::updateOceanObject
00221 (rutz::shared_ptr<OceanObject> oceanObject, DataTypes oceanObjectDataType)
00222 {
00223 bool retVal = false;
00224 pthread_mutex_lock(&itsOceanObjectsLock);
00225
00226 for(uint i = 0; i < itsOceanObjects.size(); i++)
00227 {
00228 if(itsOceanObjects[i]->getId() == oceanObject->getId())
00229 {
00230
00231 switch(oceanObjectDataType)
00232 {
00233 case POSITION:
00234 {
00235 itsOceanObjects[i]->setPosition(oceanObject->getPosition());
00236 break;
00237 }
00238
00239 case ORIENTATION:
00240 {
00241 itsOceanObjects[i]->setOrientation(oceanObject->getOrientation());
00242 break;
00243 }
00244 case FREQUENCY:
00245 {
00246 itsOceanObjects[i]->setFrequency(oceanObject->getFrequency());
00247 break;
00248 }
00249 case DISTANCE:
00250 {
00251 itsOceanObjects[i]->setDistance(oceanObject->getDistance());
00252 break;
00253 }
00254 case MASS:
00255 {
00256 itsOceanObjects[i]->setMass(oceanObject->getMass());
00257 break;
00258 }
00259 default: LFATAL("unknown ocean object data-type");
00260 }
00261 retVal = true;
00262 break;
00263 }
00264 }
00265
00266 pthread_mutex_unlock(&itsOceanObjectsLock);
00267
00268 return retVal;
00269 }
00270
00271
00272
00273
00274
00275