00001 #include "Controller.h"
00002 #include "Motion/EmergencyStopMC.h"
00003 #include "Motion/MMAccessor.h"
00004 #include "IPC/SharedObject.h"
00005 #include "Shared/WorldState.h"
00006 #include "Shared/get_time.h"
00007 #include "Sound/SoundManager.h"
00008 #include "Events/TextMsgEvent.h"
00009 #include "Shared/RobotInfo.h"
00010 #include "Shared/ERS210Info.h"
00011 #include "Shared/ERS220Info.h"
00012 #include "Shared/ERS2xxInfo.h"
00013 #include "Shared/ERS7Info.h"
00014 #include "Shared/ChiaraInfo.h"
00015 #include "Shared/string_util.h"
00016 #include "Shared/ProjectInterface.h"
00017 #include "Shared/Config.h"
00018 #include "Shared/debuget.h"
00019
00020 #include "Shared/RobotInfo.h"
00021 #ifdef TGT_HAS_LEDS
00022 # include "Motion/LedMC.h"
00023 #endif
00024
00025 #include <sstream>
00026
00027 Controller * Controller::theOneController=NULL;
00028
00029
00030 EventBase Controller::nextItem;
00031 EventBase Controller::prevItem;
00032 EventBase Controller::nextItemFast;
00033 EventBase Controller::prevItemFast;
00034 EventBase Controller::selectItem;
00035 EventBase Controller::cancel;
00036
00037 using namespace string_util;
00038 using namespace std;
00039
00040 void Controller::doStart() {
00041 BehaviorBase::doStart();
00042 sndman->loadFile(config->controller.select_snd);
00043 sndman->loadFile(config->controller.next_snd);
00044 sndman->loadFile(config->controller.prev_snd);
00045 sndman->loadFile(config->controller.read_snd);
00046 sndman->loadFile(config->controller.cancel_snd);
00047 erouter->addListener(this,EventBase::estopEGID);
00048
00049 gui_comm=wireless->socket(Socket::SOCK_STREAM, 2048, 32000);
00050 wireless->setReceiver(gui_comm->sock, gui_comm_callback);
00051 wireless->setDaemon(gui_comm,true);
00052 wireless->listen(gui_comm->sock, config->controller.gui_port);
00053 theOneController=this;
00054 #ifdef TGT_HAS_LEDS
00055 SharedObject<LedMC> leds;
00056 leds->setWeights(~FaceLEDMask,0);
00057 leds->setWeights(FaceLEDMask,.75f);
00058 display=motman->addPersistentMotion(leds,isControlling?MotionManager::kEmergencyPriority:MotionManager::kIgnoredPriority);
00059 #endif
00060 reset();
00061 }
00062
00063 void Controller::doStop() {
00064 sndman->releaseFile(config->controller.select_snd);
00065 sndman->releaseFile(config->controller.next_snd);
00066 sndman->releaseFile(config->controller.prev_snd);
00067 sndman->releaseFile(config->controller.read_snd);
00068 sndman->releaseFile(config->controller.cancel_snd);
00069 erouter->removeListener(this);
00070 reset();
00071 motman->removeMotion(display);
00072 display=MotionManager::invalid_MC_ID;
00073
00074 #ifdef TGT_HAS_LEDS
00075 for(unsigned int i=LEDOffset; i<LEDOffset+NumLEDs; i++)
00076 motman->setOutput(NULL,i,0.f);
00077 #endif
00078 gui_comm->printf("goodbye\n");
00079 wireless->setDaemon(gui_comm,false);
00080 wireless->close(gui_comm);
00081 theOneController=NULL;
00082 BehaviorBase::doStop();
00083 }
00084
00085 void Controller::doEvent() {
00086 if(event->getTypeID()==EventBase::activateETID) {
00087 if(!isControlling)
00088 activate();
00089 } else {
00090 if(isControlling)
00091 deactivate();
00092 }
00093 }
00094
00095 bool Controller::trapEvent(const EventBase& e) {
00096 if(!chkCmdStack())
00097 return false;
00098 last_time=cur_time;
00099 cur_time=get_time();
00100
00101 if(state->buttons[nextItem.getSourceID()] && state->buttons[prevItem.getSourceID()] && state->buttons[selectItem.getSourceID()])
00102 return true;
00103
00104 if(nextItem.sameGenSource(e)) {
00105 nextEv_val=e.getMagnitude();
00106 nextEv_dur=e.getDuration();
00107 if(nextEv_val==0 && prevEv_val==0)
00108 alreadyGotBoth=false;
00109 if(nextEv_val>.75 && prevEv_val>.75 && nextEv_dur<666 && prevEv_dur<666) {
00110 if(alreadyGotBoth)
00111 return true;
00112 else {
00113 alreadyGotBoth=true;
00114 return setNext(cmdstack.top()->doReadStdIn());
00115 }
00116 }
00117 if(e.getTypeID()==nextItem.getTypeID() && e.getDuration()<666)
00118 return setNext(cmdstack.top()->doNextItem());
00119 if(e.getTypeID()==nextItemFast.getTypeID() && e.getDuration()>666 && calcPulse(cur_time,last_time,static_cast<unsigned int>(50/e.getMagnitude())))
00120 return setNext(cmdstack.top()->doNextItem());
00121 }
00122 if(prevItem.sameGenSource(e)) {
00123 prevEv_val=e.getMagnitude();
00124 prevEv_dur=e.getDuration();
00125 if(nextEv_val==0 && prevEv_val==0)
00126 alreadyGotBoth=false;
00127 if(nextEv_val>.75 && prevEv_val>.75 && nextEv_dur<666 && prevEv_dur<666) {
00128 if(alreadyGotBoth)
00129 return true;
00130 else {
00131 alreadyGotBoth=true;
00132 return setNext(cmdstack.top()->doReadStdIn());
00133 }
00134 }
00135 if(e.getTypeID()==prevItem.getTypeID() && e.getDuration()<666)
00136 return setNext(cmdstack.top()->doPrevItem());
00137 if(e.getTypeID()==prevItemFast.getTypeID() && e.getDuration()>666 && calcPulse(cur_time,last_time,static_cast<unsigned int>(50/e.getMagnitude())))
00138 return setNext(cmdstack.top()->doPrevItem());
00139 }
00140 if(e.getDuration()>250) {
00141 if(e==selectItem)
00142 return setNext(cmdstack.top()->doSelect());
00143 if(e==cancel)
00144 return setNext(cmdstack.top()->doCancel());
00145 }
00146 return true;
00147 }
00148
00149 void Controller::reset() {
00150 while(cmdstack.size()>1)
00151 pop();
00152 if(!cmdstack.empty()) {
00153 cmdstack.top()->deactivate();
00154 cmdstack.pop();
00155 }
00156 refresh();
00157 }
00158
00159 void Controller::refresh() {
00160 if(!chkCmdStack())
00161 return;
00162 cmdstack.top()->refresh();
00163 }
00164
00165 void Controller::push(ControlBase* c) {
00166 if(!chkCmdStack())
00167 return;
00168 cmdstack.top()->pause();
00169 cmdstack.push(c);
00170 theOneController->gui_comm->printf("push\n");
00171 setNext(cmdstack.top()->activate(display,gui_comm));
00172 }
00173
00174 void Controller::pop() {
00175 cmdstack.top()->deactivate();
00176 cmdstack.pop();
00177 theOneController->gui_comm->printf("pop\n");
00178 refresh();
00179 }
00180
00181 Controller& Controller::setRoot(ControlBase* r) {
00182 reset();
00183 root=r;
00184 refresh();
00185 return *this;
00186 }
00187
00188 Controller& Controller::setEStopID(MotionManager::MC_ID estopid) {
00189 estop_id=estopid;
00190 if(static_cast<EmergencyStopMC*>(motman->peekMotion(estopid))->getStopped()) {
00191 if(!isControlling)
00192 activate();
00193 } else {
00194 if(isControlling)
00195 deactivate();
00196 }
00197 return *this;
00198 }
00199
00200 void Controller::loadGUI(const std::string& type, const std::string& name, unsigned int port, const std::vector<std::string>& args) {
00201 if(theOneController==NULL)
00202 return;
00203 std::stringstream ss;
00204 ss << "load\n" << type << '\n' << name << '\n' << port << '\n';
00205 for(unsigned int i=0; i<args.size(); i++) {
00206 ss << '"';
00207 for(unsigned int j=0; j<args[i].size(); j++) {
00208 if(args[i][j]=='\\' || args[i][j]=='"' || args[i][j]=='\n')
00209 ss << '\\';
00210 ss << args[i][j];
00211 }
00212 ss << "\" ";
00213 }
00214 ss << '\n';
00215 theOneController->gui_comm->write((const byte*)ss.str().c_str(),ss.str().size());
00216 }
00217
00218 void Controller::closeGUI(const std::string& name) {
00219 if(theOneController==NULL)
00220 return;
00221 ASSERTRET(theOneController->gui_comm!=NULL,"null gui_comm");
00222
00223 theOneController->gui_comm->printf("close\n%s\n",name.c_str());
00224 }
00225
00226 int Controller::gui_comm_callback(char *buf, int bytes) {
00227 std::string s(buf,bytes);
00228
00229 if(theOneController==NULL)
00230 return 0;
00231
00232 static std::string incomplete;
00233
00234
00235 while(s.size()>0) {
00236 std::string::size_type endline=s.find('\n');
00237 if(endline==std::string::npos) {
00238 incomplete+=s;
00239 return 0;
00240 }
00241
00242
00243 if(endline>0 && s[endline-1]=='\r')
00244 incomplete+=s.substr(0,endline-1);
00245 else
00246 incomplete+=s.substr(0,endline);
00247
00248
00249 theOneController->takeLine(incomplete);
00250 incomplete.erase();
00251 s=s.substr(endline+1);
00252 }
00253
00254 return 0;
00255 }
00256
00257 int Controller::console_callback(char *buf, int bytes) {
00258 std::string s(buf,bytes);
00259
00260 if(theOneController==NULL)
00261 return 0;
00262
00263 static std::string incomplete;
00264
00265
00266 while(s.size()>0) {
00267 std::string::size_type endline=s.find('\n');
00268 if(endline==std::string::npos) {
00269 incomplete+=s;
00270 return 0;
00271 }
00272
00273
00274 if(endline>0 && s[endline-1]=='\r')
00275 incomplete+=s.substr(0,endline-1);
00276 else
00277 incomplete+=s.substr(0,endline);
00278
00279
00280 switch(config->main.consoleMode) {
00281 case Config::main_config::CONTROLLER:
00282 theOneController->takeLine(incomplete); break;
00283 case Config::main_config::TEXTMSG:
00284 erouter->postEvent(TextMsgEvent(incomplete,0)); break;
00285 case Config::main_config::AUTO:
00286 if(wireless->isConnected(theOneController->gui_comm->sock))
00287 erouter->postEvent(TextMsgEvent(incomplete,0));
00288 else
00289 theOneController->takeLine(incomplete);
00290 break;
00291 }
00292 incomplete.erase();
00293 s=s.substr(endline+1);
00294 }
00295
00296 return 0;
00297 }
00298
00299
00300
00301
00302
00303
00304 void Controller::init() {
00305 usesButtons=false;
00306
00307 if(TargetName == ERS2xxInfo::TargetName) {
00308
00309
00310
00311
00312 if(RobotName == ERS210Info::TargetName) {
00313 initButtons(666,250,ERS2xxInfo::HeadFrButOffset,ERS2xxInfo::HeadBkButOffset,ERS2xxInfo::HeadFrButOffset,ERS2xxInfo::HeadBkButOffset,ERS2xxInfo::ChinButOffset,ERS2xxInfo::BackButOffset);
00314 } else if(RobotName == ERS220Info::TargetName) {
00315
00316
00317
00318
00319
00320 initButtons(666,50,ERS2xxInfo::TailLeftButOffset,ERS2xxInfo::TailRightButOffset,ERS2xxInfo::TailLeftButOffset,ERS2xxInfo::TailRightButOffset,ERS2xxInfo::TailCenterButOffset,ERS2xxInfo::BackButOffset);
00321 } else {
00322 cerr << "Controller: Unsupported 2xx model '" << RobotName << "'! Appears to have buttons, but Controller doesn't know how to use them." << endl;
00323 }
00324 } else if(RobotName == ERS210Info::TargetName) {
00325 initButtons(666,250,ERS210Info::HeadFrButOffset,ERS210Info::HeadBkButOffset,ERS210Info::HeadFrButOffset,ERS210Info::HeadBkButOffset,ERS210Info::ChinButOffset,ERS210Info::BackButOffset);
00326 } else if(RobotName == ERS220Info::TargetName) {
00327
00328
00329
00330
00331
00332 initButtons(666,50,ERS220Info::TailLeftButOffset,ERS220Info::TailRightButOffset,ERS220Info::TailLeftButOffset,ERS220Info::TailRightButOffset,ERS220Info::TailCenterButOffset,ERS220Info::BackButOffset);
00333 } else if(RobotName == ERS7Info::TargetName) {
00334 initButtons(500,25,ERS7Info::FrontBackButOffset,ERS7Info::RearBackButOffset,ERS7Info::FrontBackButOffset,ERS7Info::RearBackButOffset,ERS7Info::MiddleBackButOffset,ERS7Info::HeadButOffset);
00335 } else if(RobotName == ChiaraInfo::TargetName) {
00336
00337 } else {
00338 #ifdef TGT_HAS_BUTTONS
00339
00340
00341 #endif
00342 }
00343 }
00344
00345 void Controller::initButtons(unsigned fastTime, unsigned downTime, unsigned nextB, unsigned prevB, unsigned nextFastB, unsigned prevFastB, unsigned selectB, unsigned cancelB) {
00346 nextItem=EventBase(EventBase::buttonEGID,nextB,EventBase::deactivateETID,0);
00347 prevItem=EventBase(EventBase::buttonEGID,prevB,EventBase::deactivateETID,0);
00348 nextItemFast=EventBase(EventBase::buttonEGID,nextFastB,EventBase::statusETID,fastTime);
00349 prevItemFast=EventBase(EventBase::buttonEGID,prevFastB,EventBase::statusETID,fastTime);
00350 selectItem=EventBase(EventBase::buttonEGID,selectB,EventBase::deactivateETID,downTime);
00351 cancel=EventBase(EventBase::buttonEGID,cancelB,EventBase::deactivateETID,downTime);
00352 usesButtons=true;
00353 }
00354
00355
00356 bool Controller::select(ControlBase* item, const std::string& name) {
00357
00358 const std::vector<ControlBase*>& slots = item->getSlots();
00359 for(unsigned int i=0; i<slots.size(); i++) {
00360 if (slots[i] != NULL) {
00361 if (slots[i]->getName() == name) {
00362 char in[10];
00363 snprintf(in, 9, "%d", i); in[9]='\0';
00364 ControlBase * ret = item->takeInput(in);
00365 if(ret!=NULL) {
00366 setNext(ret);
00367 return true;
00368 }
00369 } else {
00370 if (select(slots[i], name))
00371 return true;
00372 }
00373 }
00374 }
00375 return false;
00376 }
00377
00378 void Controller::takeLine(const std::string& s) {
00379
00380 if(s.size()==0)
00381 return;
00382
00383 std::vector<std::string> args;
00384 std::vector<unsigned int> offsets;
00385 if(!string_util::parseArgs(s,args,offsets)) {
00386 serr->printf("Controller::takeLine(\"%s\") was malformed.\n",s.c_str());
00387 return;
00388 }
00389 if(args.size()==0 || offsets.size()==0)
00390 return;
00391
00392 unsigned int last=offsets[0];
00393 for(unsigned int i=0; i<args.size(); i++) {
00394 if(args[i]==";") {
00395 takeLine(s.substr(last,offsets[i]-last));
00396 if(i+1==args.size())
00397 return;
00398 last=offsets[i+1];
00399 }
00400 if(args[i]=="\\;")
00401 args[i]=";";
00402 }
00403 if(!chkCmdStack())
00404 return;
00405 if(args[0][0]!='!') {
00406 setNext(cmdstack.top()->takeInput(s));
00407 } else {
00408 if(last!=offsets[0]) {
00409 takeLine(s.substr(last));
00410 } else if(args[0]=="!refresh") {
00411 refresh();
00412 } else if(args[0]=="!reset") {
00413 reset();
00414 } else if(args[0]=="!cancel") {
00415 setNext(cmdstack.top()->doCancel());
00416 } else if(args[0]=="!select") {
00417 if (args.size() == 1)
00418 setNext(cmdstack.top()->doSelect());
00419 else {
00420 select(root, args[1].c_str());
00421 refresh();
00422 }
00423 } else if(args[0]=="!next") {
00424 setNext(cmdstack.top()->doNextItem());
00425 } else if(args[0]=="!prev") {
00426 setNext(cmdstack.top()->doPrevItem());
00427 } else if(args[0]=="!dump_stack") {
00428 dumpStack();
00429 } else if(args[0]=="!post") {
00430 if(args.size()<4) {
00431 serr->printf("Bad post command, need at least 3 arguments: generator source type [duration]\n");
00432 return;
00433 }
00434
00435 int egid=0;
00436 for(;egid<EventBase::numEGIDs && args[1]!=EventBase::EventGeneratorNames[egid];egid++) {}
00437 if(egid==EventBase::numEGIDs) {
00438 egid=atoi(args[1].c_str());
00439 if(egid==0 && args[1]!="0") {
00440 serr->printf("Bad event generator '%s'\n",args[1].c_str());
00441 return;
00442 }
00443 }
00444
00445
00446 unsigned int source;
00447 if(egid==EventBase::buttonEGID) {
00448 source=capabilities.findButtonOffset(args[2].c_str());
00449 if(source==-1U) {
00450 source=atoi(args[2].c_str());
00451 if(source==0 && args[2]!="0") {
00452 serr->printf("Invalid button name or index '%s'\n",args[2].c_str());
00453 return;
00454 }
00455 }
00456 } else {
00457 source=atoi(args[2].c_str());
00458 }
00459
00460 int etid=0;
00461 for(;etid<EventBase::numETIDs && args[3]!=EventBase::EventTypeNames[etid];etid++) {}
00462 if(etid==EventBase::numETIDs) {
00463 etid=0;
00464 for(;etid<EventBase::numETIDs && args[3]!=EventBase::EventTypeAbbr[etid];etid++) {}
00465 if(etid==EventBase::numETIDs) {
00466 etid=atoi(args[3].c_str());
00467 if(etid==0 && args[3]!="0") {
00468 serr->printf("Bad event type '%s'\n",args[3].c_str());
00469 return;
00470 }
00471 }
00472 }
00473
00474 int dur=0;
00475 if(args.size()>4)
00476 dur=atoi(args[4].c_str());
00477
00478 if(egid==EventBase::buttonEGID && isControlling && usesButtons)
00479 erouter->removeTrapper(this);
00480 erouter->postEvent((EventBase::EventGeneratorID_t)egid,source,(EventBase::EventTypeID_t)etid,dur);
00481 if(egid==EventBase::buttonEGID && isControlling && usesButtons)
00482 erouter->addTrapper(this,EventBase::buttonEGID);
00483 } else if(args[0]=="!msg") {
00484 if(offsets.size()>1)
00485 erouter->postEvent(TextMsgEvent(s.substr(offsets[1]),0));
00486 else
00487 erouter->postEvent(TextMsgEvent("",0));
00488 } else if(args[0]=="!hello") {
00489 static unsigned int count=0;
00490 count++;
00491 theOneController->gui_comm->printf("hello\n%d\n",count);
00492 } else if(args[0]=="!root") {
00493 ControlBase * ret=root->takeInput(s.substr(offsets[1]));
00494 if(ret!=NULL)
00495 setNext(ret);
00496
00497 dumpStack();
00498 } else if(args[0]=="!hilight") {
00499 std::vector<unsigned int> hilights;
00500 for(unsigned int i=1; i<args.size(); i++)
00501 hilights.push_back(atoi(args[i].c_str()));
00502 cmdstack.top()->setHilights(hilights);
00503 } else if(args[0]=="!input") {
00504 const std::vector<unsigned int>& hilights=cmdstack.top()->getHilights();
00505 const std::vector<ControlBase*>& slots=cmdstack.top()->getSlots();
00506 std::string in=s.substr(offsets[1]);
00507 for(unsigned int i=0; i<hilights.size(); i++)
00508 if(hilights[i]<slots.size() && slots[hilights[i]]!=NULL) {
00509 ControlBase * ret=slots[hilights[i]]->takeInput(in);
00510 if(ret!=NULL)
00511 setNext(ret);
00512 }
00513 refresh();
00514 } else if(args[0]=="!set") {
00515 setConfig(s.substr(offsets[1]).c_str());
00516 } else if(args[0]=="!sim") {
00517 #ifdef PLATFORM_APERIOS
00518 serr->printf("!sim command invalid -- not running in simulator!\n");
00519 #else
00520 ProjectInterface::sendCommand(s.substr(offsets[1]));
00521 #endif
00522 } else
00523 setNext(cmdstack.top()->takeInput(s));
00524 }
00525 }
00526
00527 void Controller::dumpStack() {
00528 theOneController->gui_comm->printf("stack_dump\n%lu\n",(unsigned long)cmdstack.size());
00529
00530 std::stack< ControlBase* > tmpstack;
00531 while(!cmdstack.empty()) {
00532 tmpstack.push(cmdstack.top());
00533 cmdstack.pop();
00534 }
00535 while(!tmpstack.empty()) {
00536 theOneController->gui_comm->printf("%s\n",tmpstack.top()->getName().c_str());
00537 cmdstack.push(tmpstack.top());
00538 tmpstack.pop();
00539 }
00540 }
00541
00542 int Controller::setConfig(const std::string& str) {
00543 string::size_type eq=str.find('=');
00544 if(eq==string::npos)
00545 return -2;
00546 plist::ObjectBase* entry = config->resolveEntry(string_util::trim(str.substr(0,eq)));
00547 if(entry==NULL) {
00548 string::size_type p=str.find('.');
00549 string sec=string_util::trim(str.substr(0,p));
00550 string key=string_util::trim(str.substr(p+1,eq-p-1));
00551 string val=string_util::trim(str.substr(eq+1));
00552 if(config->setValue(sec,key,val)==NULL)
00553 return -2;
00554 return 0;
00555 }
00556 plist::PrimitiveBase* prim = dynamic_cast<plist::PrimitiveBase*>(entry);
00557 if(prim==NULL)
00558 return -2;
00559 prim->set(string_util::trim(str.substr(eq+1)));
00560 return 0;
00561 }
00562
00563 bool Controller::setNext(ControlBase* next) {
00564 if(next==NULL)
00565 pop();
00566 else if(next!=cmdstack.top())
00567 push(next);
00568 return true;
00569 }
00570
00571 void Controller::activate() {
00572 #ifdef TGT_HAS_LEDS
00573 motman->setPriority(display,MotionManager::kEmergencyPriority);
00574 #endif
00575 if(usesButtons)
00576 erouter->addTrapper(this,EventBase::buttonEGID);
00577 isControlling=true;
00578 if(!cmdstack.empty())
00579 cmdstack.top()->activate(display,gui_comm);
00580 else
00581 chkCmdStack();
00582 }
00583
00584 void Controller::deactivate() {
00585
00586 isControlling=false;
00587 #ifdef TGT_HAS_LEDS
00588 motman->setPriority(display,MotionManager::kIgnoredPriority);
00589 for(unsigned int i=LEDOffset; i<LEDOffset+NumLEDs; i++)
00590 motman->setOutput(NULL,i,0.f);
00591 #endif
00592 erouter->removeTrapper(this);
00593 cmdstack.top()->pause();
00594 }
00595
00596 bool Controller::chkCmdStack() {
00597 if(cmdstack.empty()) {
00598 if(root==NULL)
00599 return false;
00600 cmdstack.push(root);
00601 ControlBase * next = cmdstack.top()->activate(display,gui_comm);
00602 if(next==NULL)
00603 cout << "*** WARNING Controller root returned NULL on activate!" << endl;
00604 else if(next!=root)
00605 push(next);
00606 }
00607 return true;
00608 }
00609
00610
00611
00612
00613
00614