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
00038 #include "Neuro/SaccadeController.H"
00039
00040 #include "Simulation/SimEventQueue.H"
00041 #include "Neuro/NeuroOpts.H"
00042 #include "Neuro/NeuroSimEvents.H"
00043 #include "Util/log.H"
00044 #include "Util/sformat.H"
00045
00046
00047 SaccadeController::SaccadeController(OptionManager& mgr,
00048 const std::string& descrName,
00049 const std::string& tagName,
00050 const SaccadeBodyPart bodypart,
00051 const int percept_qlen,
00052 const int decision_qlen) :
00053 SimModule(mgr, std::string(saccadeBodyPartName(bodypart)) +
00054 std::string(" ") + descrName,
00055 std::string(saccadeBodyPartName(bodypart)) + tagName),
00056 itsInitialPosition(bodypart == SaccadeBodyPartEye
00057 ? &OPT_SCeyeInitialPosition
00058 : &OPT_SCheadInitialPosition, this),
00059 percept(), decision(), pqlen(percept_qlen), dqlen(decision_qlen),
00060 itsState(SACSTATE_UNK), itsPrevState(SACSTATE_UNK),
00061 itsBlinkState(false), itsPrevBlinkState(false),
00062 itsBodyPart(bodypart)
00063 {
00064 if (pqlen < 2 || dqlen < 2)
00065 LFATAL("Queue lengths must be at least 2 for proper operation.");
00066 }
00067
00068
00069 SaccadeController::~SaccadeController()
00070 { }
00071
00072
00073 void SaccadeController::reset1()
00074 {
00075
00076 percept.clear(); decision.clear();
00077 itsState = SACSTATE_UNK; itsPrevState = SACSTATE_UNK;
00078 itsBlinkState = false; itsPrevBlinkState = false;
00079
00080
00081 SimModule::reset1();
00082 }
00083
00084
00085 void SaccadeController::setPercept(const WTAwinner& fix, SimEventQueue& q)
00086 {
00087
00088 percept.push_front(fix);
00089
00090
00091 while(percept.size() > pqlen) percept.pop_back();
00092
00093
00094 computeWhenNewPercept(q);
00095 }
00096
00097
00098 void SaccadeController::resetPos(const Point2D<int>& p, SimEventQueue& q)
00099 {
00100
00101 const WTAwinner win(p, q.now(), 0.0, false);
00102 percept.push_front(win);
00103
00104
00105 while(percept.size() > pqlen) percept.pop_back();
00106
00107
00108 computeWhenResetPos(q);
00109
00110
00111
00112 if (p.isValid()) itsState = SACSTATE_FIX;
00113 else itsState = SACSTATE_UNK;
00114
00115
00116 itsBlinkState = false;
00117
00118 CLDEBUG("### State reset to [%s] (%d,%d) %.1fms ###",
00119 saccadeStateName(itsState), p.i, p.j, q.now().msecs());
00120 CLDEBUG("## Blink state reset to [Not-Blinking] (%d,%d) %.1fms ##",
00121 p.i, p.j, q.now().msecs());
00122 }
00123
00124
00125 Point2D<int> SaccadeController::getDecision(SimEventQueue& q,
00126 const bool dopoststatus)
00127 {
00128
00129
00130 if (percept.empty() && q.now() == SimTime::ZERO())
00131 {
00132 const Point2D<int> p = itsInitialPosition.getVal();
00133 if (p.i == -2 && p.j == -2)
00134 {
00135
00136
00137 if (SeC<SimEventRetinaImage> e =
00138 q.check<SimEventRetinaImage>(this, SEQ_ANY))
00139 {
00140 const Point2D<int> c = e->center();
00141 resetPos(c, q);
00142 CLINFO("Starting %s at centered initial position (%d, %d).",
00143 saccadeBodyPartName(itsBodyPart), c.i, c.j);
00144 }
00145 else
00146 CLFATAL("ooops, cannot find a retina image to get its center?");
00147 }
00148 else if (p.isValid())
00149 {
00150
00151 resetPos(p, q);
00152 CLINFO("Starting %s at specified initial position (%d, %d).",
00153 saccadeBodyPartName(itsBodyPart), p.i, p.j);
00154 }
00155 else
00156 CLINFO("Starting %s at unspecified initial position.",
00157 saccadeBodyPartName(itsBodyPart));
00158 }
00159
00160
00161
00162
00163
00164 Point2D<int> last(-1, -1);
00165 if (decision.size()) last = decision.front().p;
00166 Point2D<int> loc = computeWhenNewDecision(itsState, itsBlinkState, q);
00167 bool nochange = (loc == last);
00168 if (loc.isValid() && nochange == false)
00169 decision.push_front(Point2DT(loc, q.now()));
00170
00171
00172 while(decision.size() > dqlen) decision.pop_back();
00173
00174
00175 if (itsState != itsPrevState)
00176 CLDEBUG("### State change [%s -> %s] at (%d,%d) %.1fms ###",
00177 saccadeStateName(itsPrevState), saccadeStateName(itsState),
00178 loc.i, loc.j, q.now().msecs());
00179 if (itsBlinkState != itsPrevBlinkState)
00180 CLDEBUG("## Blink state change [%s -> %s] at (%d,%d) %.1fms ##",
00181 itsPrevBlinkState ? "Blinking" : "Not-Blinking",
00182 itsBlinkState ? "Blinking" : "Not-Blinking",
00183 loc.i, loc.j, q.now().msecs());
00184
00185
00186 if (dopoststatus)
00187 {
00188 if (itsBodyPart == SaccadeBodyPartEye)
00189 {
00190 rutz::shared_ptr<SimEventSaccadeStatusEye>
00191 s(new SimEventSaccadeStatusEye(this, getPreviousDecision(0).p,
00192 itsState, itsPrevState,
00193 itsBlinkState, itsPrevBlinkState));
00194 q.post(s);
00195 }
00196 else
00197 {
00198 rutz::shared_ptr<SimEventSaccadeStatusHead>
00199 s(new SimEventSaccadeStatusHead(this, getPreviousDecision(0).p,
00200 itsState, itsPrevState,
00201 itsBlinkState, itsPrevBlinkState));
00202 q.post(s);
00203 }
00204 }
00205
00206
00207 if (nochange) return Point2D<int>(-1, -1); else return loc;
00208 }
00209
00210
00211 void SaccadeController::dumpQueues() const
00212 {
00213 std::string ptxt, dtxt;
00214 for (unsigned int i = 0; i < percept.size(); i ++) {
00215 WTAwinner win = percept[i];
00216 ptxt += sformat("[(%d, %d) %.2fms %.2fmV] ", win.p.i, win.p.j,
00217 win.t.msecs(), win.sv * 1000.0);
00218 }
00219 CLINFO("PERCEPT: %s", ptxt.c_str());
00220 for (unsigned int i = 0; i < decision.size(); i ++) {
00221 Point2DT pt = decision[i];
00222 dtxt += sformat("[(%d, %d) %.2fms] ", pt.p.i, pt.p.j, pt.t.msecs());
00223 }
00224 CLINFO("DECISION: %s", dtxt.c_str());
00225 }
00226
00227
00228 WTAwinner SaccadeController::getPreviousPercept(const unsigned int
00229 index) const
00230 {
00231 if (percept.size() > index) return percept[index];
00232 else return WTAwinner::NONE();
00233 }
00234
00235
00236 bool SaccadeController::havePercepts() const
00237 { return !(percept.empty()); }
00238
00239
00240 int SaccadeController::getPqlen() const
00241 { return pqlen; }
00242
00243
00244 void SaccadeController::resetPqlen(const int len)
00245 { percept.clear(); pqlen = len; }
00246
00247
00248 void SaccadeController::killPercepts()
00249 { while(percept.size()) percept.pop_front(); }
00250
00251
00252 Point2DT SaccadeController::getPreviousDecision(const unsigned int
00253 index) const
00254 {
00255 if (decision.size() > index) return decision[index];
00256 else return Point2DT(-1, -1, SimTime::ZERO());
00257 }
00258
00259
00260 bool SaccadeController::haveDecisions() const
00261 { return !(decision.empty()); }
00262
00263
00264 int SaccadeController::getDqlen() const
00265 { return dqlen; }
00266
00267
00268 void SaccadeController::resetDqlen(const int len)
00269 { decision.clear(); dqlen = len; }
00270
00271
00272 void SaccadeController::killDecisions()
00273 { while(decision.size()) decision.pop_front(); }
00274
00275
00276 SaccadeBodyPart SaccadeController::bodyPart() const
00277 { return itsBodyPart; }
00278
00279
00280 void SaccadeController::evolve(SimEventQueue& q)
00281 {
00282
00283 itsPrevState = itsState;
00284 itsPrevBlinkState = itsBlinkState;
00285
00286
00287 this->doEvolve(q);
00288 }
00289
00290
00291 SaccadeState SaccadeController::getState() const
00292 { return itsState; }
00293
00294
00295 bool SaccadeController::getBlinkState() const
00296 { return itsBlinkState; }
00297
00298
00299
00300
00301
00302
00303