Homepage Demos Overview Downloads Tutorials Reference
Credits

StartupBehavior.cc

Go to the documentation of this file.
00001 #include "StartupBehavior.h"
00002 
00003 #include "Behaviors/Controller.h"
00004 #include "Behaviors/Controls/BatteryCheckControl.h"
00005 #include "Behaviors/Controls/ControlBase.h"
00006 #include "Behaviors/Controls/HelpControl.h"
00007 #include "Behaviors/Controls/RebootControl.h"
00008 #include "Behaviors/Controls/ShutdownControl.h"
00009 
00010 #include "Motion/MotionCommand.h"
00011 #include "Motion/PostureMC.h"
00012 #include "Motion/EmergencyStopMC.h"
00013 #include "Motion/PIDMC.h"
00014 #include "Motion/MotionSequenceMC.h"
00015 #include "Motion/MMAccessor.h"
00016 
00017 #include "Vision/Vision.h"
00018 
00019 #include "SoundPlay/SoundManager.h"
00020 
00021 #include "Shared/ERS210Info.h"
00022 
00023 StartupBehavior theStartup; //!< used to initialize the global ::startupBehavior, used by MMCombo
00024 BehaviorBase& startupBehavior=theStartup; //!< used by MMCombo as the init behavior
00025 
00026 StartupBehavior::StartupBehavior()
00027   : BehaviorBase(), spawned(),setup(),
00028     stop_id(MotionManager::invalid_MC_ID),
00029     pid_id(MotionManager::invalid_MC_ID)
00030 {
00031   AddReference(); // this is a global, so there's a global reference
00032 }
00033 
00034 StartupBehavior::~StartupBehavior() {cout << "Goodbye" << endl;}
00035 
00036 void StartupBehavior::DoStart() {
00037   BehaviorBase::DoStart();
00038   
00039   //This will "fade" in the PIDs so the joints don't jerk to full power, also looks cooler
00040   pid_id=motman->addMotion(SharedObject<PIDMC>(0),MotionManager::kEmergencyPriority+1,false);
00041   //also, pause before we start fading in, PIDs take effect right away, before the emergencystop is picked up
00042   erouter->addTimer(this,0,4*FrameTime*NumFrames,true);
00043 
00044   //This is the default emergency stop
00045   const SharedObject<EmergencyStopMC> stop;
00046   stop->LoadFile("/ms/data/motion/liedown.pos"); //This *should* be replaced by the current position, but just in case, better than setting everything to 0's
00047   stop->setStopped(true,false); //if you want to start off paused
00048   //sndman->StopPlay(); //so it doesn't play the halt sound the first time (but the false in previous line does that now)
00049   stop_id=motman->addMotion(stop,MotionManager::kEmergencyPriority);
00050   
00051   //This displays the current battery conditions
00052   BatteryCheckControl batchk;
00053   batchk.activate(MotionManager::invalid_MC_ID,NULL);
00054   //  const SharedObject<LedMC> led; //! @todo LedMC's don't support autopruning yet, it should for uses like this, or could the one in Controller
00055   //  batchk.activate(motman->addMotion(led,true));
00056   batchk.deactivate();
00057 
00058   //This is for the menu system
00059   Controller * controller=new Controller;
00060   controller->DoStart();
00061   controller->setEStopID(stop_id);
00062   controller->setRoot(SetupMenus());
00063   wireless->setReceiver(sout, Controller::console_callback);
00064   spawned.push_back(controller);
00065   
00066   sndman->PlayFile("roar.wav");
00067 
00068   //This will close the mouth so it doesn't look stupid
00069   //Now done by setting the emergency stop directly in processEvent, but left as demo code:
00070   /*  const SharedObject<PostureMC> closemouth;
00071       closemouth->setJointCmd(MouthOffset,outputRanges[MouthOffset][MaxRange],1);
00072       motman->addMotion(closemouth,MotionCommand::kEmergencyPriority+2,true);
00073   */
00074 }
00075 
00076 void StartupBehavior::DoStop() {
00077   for(std::vector<BehaviorBase*>::iterator it=spawned.begin(); it!=spawned.end(); it++)
00078     (*it)->DoStop();
00079   motman->removeMotion(stop_id);
00080   BehaviorBase::DoStop();
00081 }
00082 
00083 /*!Uses a few timer events at the beginning to fade in the PID values, and closes the mouth too*/
00084 void StartupBehavior::processEvent(const EventBase&) {
00085   static unsigned int start_time=-1U;
00086   const unsigned int tot_time=2047; //if this is set to 2048, i sometimes get funny errors: [oid:80000034,prio:1] AGRMSDriver::SetGain() : 0x0A IS USED FOR GAIN SHIFT VALUE.
00087   if(start_time==-1U) { //first time
00088     start_time=get_time();
00089     MMAccessor<EmergencyStopMC>(stop_id)->takeSnapshot(); //take new snapshot with hopefully valid data
00090   } else {
00091     float power=(get_time()-start_time)/(float)tot_time;
00092     if(power>1)
00093       power=1;
00094     { MMAccessor<PIDMC>(pid_id)->setAllPowerLevel(power); }
00095     if(state->robotDesign & WorldState::ERS210Mask)
00096       { MMAccessor<EmergencyStopMC>(stop_id)->setOutputCmd(ERS210Info::MouthOffset,outputRanges[ERS210Info::MouthOffset][MaxRange]); }
00097   }
00098   if((get_time()-start_time)>=tot_time) {
00099     erouter->removeTimer(this);
00100     motman->removeMotion(pid_id);
00101     pid_id=MotionManager::invalid_MC_ID;
00102   }
00103 }
00104 
00105 /*class WalkToPinkBallFactory : public Factory<WalkToTargetMachine> {
00106   public:
00107   static WalkToTargetMachine* construct() { return new WalkToTargetMachine(VisionEventNS::PinkBallSID); }
00108   };
00109 */
00110 
00111 ControlBase*
00112 StartupBehavior::SetupMenus() {
00113   std::cout << "CONTROLLER-INIT..." << std::flush;
00114   ControlBase* root=new ControlBase("Root Control");
00115   setup.push(root);
00116 
00117   // additional controls will be submenus of root:
00118   {
00119     SetupModeSwitch();
00120     SetupBackgroundBehaviors();
00121     SetupTekkotsuMon();
00122     SetupStatusReports();
00123     SetupFileAccess();
00124     SetupWalkEdit();
00125     addItem(new ControlBase("Shutdown?"));
00126     startSubMenu();
00127     { 
00128       addItem(new ShutdownControl());
00129       addItem(new RebootControl());
00130     }
00131     endSubMenu();
00132     addItem(new HelpControl(root));
00133   }
00134   
00135   if(endSubMenu()!=root)
00136     cout << "\n*** WARNING *** menu setup stack corrupted" << endl;
00137   cout << "DONE" << endl;
00138   return root;
00139 }
00140 
00141 void
00142 StartupBehavior::startSubMenu() {
00143   setup.push(setup.top()->getSlots().back());
00144 }
00145 
00146 void
00147 StartupBehavior::addItem(ControlBase * control) {
00148   setup.top()->pushSlot(control);
00149 }
00150 
00151 ControlBase*
00152 StartupBehavior::endSubMenu() {
00153   ControlBase * tmp=setup.top();
00154   setup.pop();
00155   return tmp;
00156 }

Tekkotsu v1.5
Generated Fri Oct 10 15:52:00 2003 by Doxygen 1.3.4