00001 #include "Controller.h"
00002 #include "Motion/EmergencyStopMC.h"
00003 #include "Motion/LedMC.h"
00004 #include "Motion/MMAccessor.h"
00005 #include "Shared/SharedObject.h"
00006 #include "Shared/WorldState.h"
00007 #include "Shared/get_time.h"
00008 #include "SoundPlay/SoundManager.h"
00009 #include "Events/TextMsgEvent.h"
00010 #include "Shared/ERS210Info.h"
00011 #include "Shared/ERS220Info.h"
00012 #include "Shared/ERS7Info.h"
00013 #include "Shared/string_util.h"
00014 #include <sstream>
00015
00016 Controller * Controller::theOneController=NULL;
00017
00018
00019 EventBase Controller::nextItem;
00020 EventBase Controller::prevItem;
00021 EventBase Controller::nextItemFast;
00022 EventBase Controller::prevItemFast;
00023 EventBase Controller::selectItem;
00024 EventBase Controller::cancel;
00025
00026 using namespace string_util;
00027
00028
00029 void Controller::DoStart() {
00030 BehaviorBase::DoStart();
00031 sndman->LoadFile(config->controller.select_snd);
00032 sndman->LoadFile(config->controller.next_snd);
00033 sndman->LoadFile(config->controller.prev_snd);
00034 sndman->LoadFile(config->controller.read_snd);
00035 sndman->LoadFile(config->controller.cancel_snd);
00036 erouter->addListener(this,EventBase::estopEGID);
00037
00038 gui_comm=wireless->socket(SocketNS::SOCK_STREAM, 2048, 32000);
00039 wireless->setReceiver(gui_comm->sock, gui_comm_callback);
00040 wireless->setDaemon(gui_comm,true);
00041 wireless->listen(gui_comm->sock, config->controller.gui_port);
00042 theOneController=this;
00043 SharedObject<LedMC> leds;
00044 leds->setWeights(~FaceLEDMask,0);
00045 leds->setWeights(FaceLEDMask,.75);
00046 display=motman->addMotion(leds,isControlling?MotionManager::kEmergencyPriority:MotionManager::kIgnoredPriority);
00047 reset();
00048 }
00049
00050 void Controller::DoStop() {
00051 motman->removeMotion(display);
00052
00053 for(unsigned int i=LEDOffset; i<LEDOffset+NumLEDs; i++)
00054 motman->setOutput(NULL,i,0.f);
00055 display=MotionManager::invalid_MC_ID;
00056 sndman->ReleaseFile(config->controller.select_snd);
00057 sndman->ReleaseFile(config->controller.next_snd);
00058 sndman->ReleaseFile(config->controller.prev_snd);
00059 sndman->ReleaseFile(config->controller.read_snd);
00060 sndman->ReleaseFile(config->controller.cancel_snd);
00061 erouter->forgetListener(this);
00062 reset();
00063 gui_comm->printf("goodbye\n");
00064 wireless->setDaemon(gui_comm,false);
00065 wireless->close(gui_comm);
00066 theOneController=NULL;
00067 BehaviorBase::DoStop();
00068 }
00069
00070 bool Controller::trapEvent(const EventBase& e) {
00071 if(!chkCmdStack())
00072 return false;
00073 last_time=cur_time;
00074 cur_time=get_time();
00075
00076 if(state->buttons[nextItem.getSourceID()] && state->buttons[prevItem.getSourceID()] && state->buttons[selectItem.getSourceID()])
00077 return true;
00078
00079 if(nextItem.sameGenSource(e)) {
00080 nextEv_val=e.getMagnitude();
00081 nextEv_dur=e.getDuration();
00082 if(nextEv_val==0 && prevEv_val==0)
00083 alreadyGotBoth=false;
00084 if(nextEv_val>.75 && prevEv_val>.75 && nextEv_dur<666 && prevEv_dur<666)
00085 if(alreadyGotBoth)
00086 return true;
00087 else {
00088 alreadyGotBoth=true;
00089 return setNext(cmdstack.top()->doReadStdIn());
00090 }
00091 if(e.getTypeID()==nextItem.getTypeID() && e.getDuration()<666)
00092 return setNext(cmdstack.top()->doNextItem());
00093 if(e.getTypeID()==nextItemFast.getTypeID() && e.getDuration()>666 && calcPulse(cur_time,last_time,static_cast<unsigned int>(50/e.getMagnitude())))
00094 return setNext(cmdstack.top()->doNextItem());
00095 }
00096 if(prevItem.sameGenSource(e)) {
00097 prevEv_val=e.getMagnitude();
00098 prevEv_dur=e.getDuration();
00099 if(nextEv_val==0 && prevEv_val==0)
00100 alreadyGotBoth=false;
00101 if(nextEv_val>.75 && prevEv_val>.75 && nextEv_dur<666 && prevEv_dur<666)
00102 if(alreadyGotBoth)
00103 return true;
00104 else {
00105 alreadyGotBoth=true;
00106 return setNext(cmdstack.top()->doReadStdIn());
00107 }
00108 if(e.getTypeID()==prevItem.getTypeID() && e.getDuration()<666)
00109 return setNext(cmdstack.top()->doPrevItem());
00110 if(e.getTypeID()==prevItemFast.getTypeID() && e.getDuration()>666 && calcPulse(cur_time,last_time,static_cast<unsigned int>(50/e.getMagnitude())))
00111 return setNext(cmdstack.top()->doPrevItem());
00112 }
00113 if(e.getDuration()>250) {
00114 if(e==selectItem)
00115 return setNext(cmdstack.top()->doSelect());
00116 if(e==cancel)
00117 return setNext(cmdstack.top()->doCancel());
00118 }
00119 return true;
00120 }
00121
00122 void Controller::processEvent(const EventBase& event) {
00123 if(event.getTypeID()==EventBase::activateETID) {
00124 if(!isControlling)
00125 activate();
00126 } else {
00127 if(isControlling)
00128 deactivate();
00129 }
00130 }
00131
00132 void Controller::reset() {
00133 while(cmdstack.size()>1)
00134 pop();
00135 if(!cmdstack.empty()) {
00136 cmdstack.top()->deactivate();
00137 cmdstack.pop();
00138 }
00139 refresh();
00140 }
00141
00142 void Controller::refresh() {
00143 if(!chkCmdStack())
00144 return;
00145 cmdstack.top()->refresh();
00146 }
00147
00148 void Controller::push(ControlBase* c) {
00149 if(!chkCmdStack())
00150 return;
00151 cmdstack.top()->pause();
00152 cmdstack.push(c);
00153 theOneController->gui_comm->printf("push\n");
00154 setNext(cmdstack.top()->activate(display,gui_comm));
00155 }
00156
00157 void Controller::pop() {
00158 cmdstack.top()->deactivate();
00159 cmdstack.pop();
00160 theOneController->gui_comm->printf("pop\n");
00161 refresh();
00162 }
00163
00164 Controller& Controller::setRoot(ControlBase* r) {
00165 reset();
00166 root=r;
00167 refresh();
00168 return *this;
00169 }
00170
00171 Controller& Controller::setEStopID(MotionManager::MC_ID estopid) {
00172 estop_id=estopid;
00173 if(static_cast<EmergencyStopMC*>(motman->peekMotion(estopid))->getStopped()) {
00174 if(!isControlling)
00175 activate();
00176 } else {
00177 if(isControlling)
00178 deactivate();
00179 }
00180 return *this;
00181 }
00182
00183 void Controller::loadGUI(const std::string& type, const std::string& name, unsigned int port, const std::vector<std::string>& args) {
00184 if(theOneController==NULL)
00185 return;
00186 std::stringstream ss;
00187 ss << "load\n" << type << '\n' << name << '\n' << port << '\n';
00188 for(unsigned int i=0; i<args.size(); i++) {
00189 ss << '"';
00190 for(unsigned int j=0; j<args[i].size(); j++) {
00191 if(args[i][j]=='\\' || args[i][j]=='"' || args[i][j]=='\n')
00192 ss << '\\';
00193 ss << args[i][j];
00194 }
00195 ss << "\" ";
00196 }
00197 ss << '\n';
00198 theOneController->gui_comm->write((const byte*)ss.str().c_str(),ss.str().size());
00199 }
00200
00201 void Controller::closeGUI(const std::string& name) {
00202 if(theOneController==NULL)
00203 return;
00204 ASSERTRET(theOneController->gui_comm!=NULL,"null gui_comm");
00205
00206 theOneController->gui_comm->printf("close\n%s\n",name.c_str());
00207 }
00208
00209 int Controller::gui_comm_callback(char *buf, int bytes) {
00210 std::string s(buf,bytes);
00211
00212 if(theOneController==NULL)
00213 return 0;
00214
00215 static std::string incomplete;
00216
00217
00218 while(s.size()>0) {
00219 unsigned int endline=s.find('\n');
00220 if(endline==std::string::npos) {
00221 incomplete+=s;
00222 return 0;
00223 }
00224 incomplete+=s.substr(0,endline);
00225 theOneController->takeLine(incomplete);
00226 incomplete.erase();
00227 s=s.substr(endline+1);
00228 }
00229
00230 return 0;
00231 }
00232
00233 int Controller::console_callback(char *buf, int bytes) {
00234 std::string s(buf,bytes);
00235
00236 if(theOneController==NULL)
00237 return 0;
00238
00239 static std::string incomplete;
00240
00241
00242 while(s.size()>0) {
00243 unsigned int endline=s.find('\n');
00244 if(endline==std::string::npos) {
00245 incomplete+=s;
00246 return 0;
00247 }
00248 incomplete+=s.substr(0,endline);
00249
00250 if(wireless->isConnected(theOneController->gui_comm->sock))
00251 erouter->postEvent(new TextMsgEvent(incomplete));
00252 else
00253 theOneController->takeLine(incomplete);
00254 incomplete.erase();
00255 s=s.substr(endline+1);
00256 }
00257
00258 return 0;
00259 }
00260
00261 void Controller::init() {
00262 if(state->robotDesign & WorldState::ERS210Mask) {
00263 nextItem=EventBase(EventBase::buttonEGID,ERS210Info::HeadFrButOffset,EventBase::deactivateETID,0);
00264 prevItem=EventBase(EventBase::buttonEGID,ERS210Info::HeadBkButOffset,EventBase::deactivateETID,0);
00265 nextItemFast=EventBase(EventBase::buttonEGID,ERS210Info::HeadFrButOffset,EventBase::statusETID,666);
00266 prevItemFast=EventBase(EventBase::buttonEGID,ERS210Info::HeadBkButOffset,EventBase::statusETID,666);
00267 selectItem=EventBase(EventBase::buttonEGID,ERS210Info::ChinButOffset,EventBase::deactivateETID,250);
00268 cancel=EventBase(EventBase::buttonEGID,ERS210Info::BackButOffset,EventBase::deactivateETID,250);
00269 } else if(state->robotDesign & WorldState::ERS220Mask) {
00270 nextItem=EventBase(EventBase::buttonEGID,ERS220Info::TailLeftButOffset,EventBase::deactivateETID,0);
00271 prevItem=EventBase(EventBase::buttonEGID,ERS220Info::TailRightButOffset,EventBase::deactivateETID,0);
00272
00273
00274
00275
00276
00277 nextItemFast=EventBase(EventBase::buttonEGID,ERS220Info::TailLeftButOffset,EventBase::statusETID,666);
00278 prevItemFast=EventBase(EventBase::buttonEGID,ERS220Info::TailRightButOffset,EventBase::statusETID,666);
00279 selectItem=EventBase(EventBase::buttonEGID,ERS220Info::TailCenterButOffset,EventBase::deactivateETID,50);
00280 cancel=EventBase(EventBase::buttonEGID,ERS220Info::BackButOffset,EventBase::deactivateETID,50);
00281 } else if(state->robotDesign & WorldState::ERS7Mask) {
00282 nextItem=EventBase(EventBase::buttonEGID,ERS7Info::FrontBackButOffset,EventBase::deactivateETID,0);
00283 prevItem=EventBase(EventBase::buttonEGID,ERS7Info::RearBackButOffset,EventBase::deactivateETID,0);
00284 nextItemFast=EventBase(EventBase::buttonEGID,ERS7Info::FrontBackButOffset,EventBase::statusETID,500);
00285 prevItemFast=EventBase(EventBase::buttonEGID,ERS7Info::RearBackButOffset,EventBase::statusETID,500);
00286 selectItem=EventBase(EventBase::buttonEGID,ERS7Info::MiddleBackButOffset,EventBase::deactivateETID,25);
00287 cancel=EventBase(EventBase::buttonEGID,ERS7Info::HeadButOffset,EventBase::deactivateETID,25);
00288 } else {
00289 serr->printf("Controller: Unsupported model!\n");
00290 }
00291 }
00292
00293 void Controller::takeLine(const std::string& s) {
00294
00295 if(s.size()==0)
00296 return;
00297
00298 std::vector<std::string> args;
00299 std::vector<unsigned int> offsets;
00300 if(!string_util::parseArgs(s,args,offsets)) {
00301 serr->printf("Controller::takeLine(\"%s\") was malformed.\n",s.c_str());
00302 return;
00303 }
00304 if(args.size()==0 || offsets.size()==0)
00305 return;
00306
00307 unsigned int last=offsets[0];
00308 for(unsigned int i=0; i<args.size(); i++) {
00309 if(args[i]==";") {
00310 takeLine(s.substr(last,offsets[i]-last));
00311 if(i+1==args.size())
00312 return;
00313 last=offsets[i+1];
00314 }
00315 if(args[i]=="\\;")
00316 args[i]=";";
00317 }
00318 if(!chkCmdStack())
00319 return;
00320 if(args[0][0]!='!') {
00321 setNext(cmdstack.top()->takeInput(s));
00322 } else {
00323 if(last!=offsets[0]) {
00324 takeLine(s.substr(last));
00325 } else if(args[0]=="!refresh") {
00326 refresh();
00327 } else if(args[0]=="!reset") {
00328 reset();
00329 } else if(args[0]=="!cancel") {
00330 setNext(cmdstack.top()->doCancel());
00331 } else if(args[0]=="!select") {
00332 setNext(cmdstack.top()->doSelect());
00333 } else if(args[0]=="!next") {
00334 setNext(cmdstack.top()->doNextItem());
00335 } else if(args[0]=="!prev") {
00336 setNext(cmdstack.top()->doPrevItem());
00337 } else if(args[0]=="!msg") {
00338 if(offsets.size()>1)
00339 erouter->postEvent(new TextMsgEvent(s.substr(offsets[1])));
00340 else
00341 erouter->postEvent(new TextMsgEvent(""));
00342 } else if(args[0]=="!hello") {
00343 static unsigned int count=0;
00344 count++;
00345 theOneController->gui_comm->printf("hello\n%d\n",count);
00346 } else if(args[0]=="!root") {
00347 ControlBase * ret=root->takeInput(s.substr(offsets[1]));
00348 if(ret!=NULL)
00349 setNext(ret);
00350 } else if(args[0]=="!hilight") {
00351 std::vector<unsigned int> hilights;
00352 for(unsigned int i=1; i<args.size(); i++)
00353 hilights.push_back(atoi(args[i].c_str()));
00354 cmdstack.top()->setHilights(hilights);
00355 } else if(args[0]=="!input") {
00356 const std::vector<unsigned int>& hilights=cmdstack.top()->getHilights();
00357 const std::vector<ControlBase*>& slots=cmdstack.top()->getSlots();
00358 std::string in=s.substr(offsets[1]);
00359 for(unsigned int i=0; i<hilights.size(); i++)
00360 if(hilights[i]<slots.size() && slots[hilights[i]]!=NULL) {
00361 ControlBase * ret=slots[hilights[i]]->takeInput(in);
00362 if(ret!=NULL)
00363 setNext(ret);
00364 }
00365 refresh();
00366 } else if(args[0]=="!set") {
00367 setConfig(s.substr(offsets[1]).c_str());
00368 } else
00369 setNext(cmdstack.top()->takeInput(s));
00370 }
00371 }
00372
00373 int Controller::setConfig(const char *str) {
00374 char buf[80];
00375 strncpy(buf, str, 79);
00376 char *value=index(buf, '=');
00377 char *key=index(buf, '.');
00378 if (key==NULL || value==NULL) return -1;
00379 if (key>=value) return -1;
00380 *key=0;
00381 key++;
00382 *value=0;
00383 value++;
00384 Config::section_t section=config->parseSection(buf);
00385 if (section==Config::sec_invalid) return -2;
00386 config->setValue(section, key, value, true);
00387
00388
00389
00390
00391 return 0;
00392 }
00393
00394 bool Controller::setNext(ControlBase* next) {
00395 if(next==NULL)
00396 pop();
00397 else if(next!=cmdstack.top())
00398 push(next);
00399 return true;
00400 }
00401
00402 void Controller::activate() {
00403 motman->setPriority(display,MotionManager::kEmergencyPriority);
00404 erouter->addTrapper(this,EventBase::buttonEGID);
00405 isControlling=true;
00406 if(!cmdstack.empty())
00407 cmdstack.top()->activate(display,gui_comm);
00408 else
00409 chkCmdStack();
00410 }
00411
00412 void Controller::deactivate() {
00413
00414 motman->setPriority(display,MotionManager::kIgnoredPriority);
00415 isControlling=false;
00416 for(unsigned int i=LEDOffset; i<LEDOffset+NumLEDs; i++)
00417 motman->setOutput(NULL,i,0.f);
00418 erouter->removeTrapper(this);
00419 cmdstack.top()->pause();
00420 }
00421
00422 bool Controller::chkCmdStack() {
00423 if(cmdstack.empty()) {
00424 if(root==NULL)
00425 return false;
00426 cmdstack.push(root);
00427 ControlBase * next = cmdstack.top()->activate(display,gui_comm);
00428 if(next==NULL)
00429 cout << "*** WARNING Controller root returned NULL on activate!" << endl;
00430 else if(next!=root)
00431 push(next);
00432 }
00433 return true;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446