Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WalkController.cc

Go to the documentation of this file.
00001 #include "Motion/WalkMC.h"
00002 #ifdef TGT_HAS_WALK
00003 
00004 #include "WalkController.h"
00005 #include "Behaviors/Controller.h"
00006 #include "Sound/SoundManager.h"
00007 
00008 REGISTER_BEHAVIOR_MENU_OPT(WalkController,"TekkotsuMon",BEH_NONEXCLUSIVE);
00009 
00010 using namespace std;
00011 
00012 WalkController* WalkController::theOne = NULL;
00013 
00014 void WalkController::runCommand(unsigned char *command) {
00015   // First, turn off the stop-if-no-heartbeat timer
00016   erouter->removeTimer(this);
00017 
00018   // Extract the command parameter
00019   float param;
00020   unsigned char *paramp = (unsigned char *) &param;
00021 
00022 #if defined(BYTE_ORDER) && BYTE_ORDER==BIG_ENDIAN
00023   paramp[0] = command[4];
00024   paramp[1] = command[3];
00025   paramp[2] = command[2];
00026   paramp[3] = command[1];
00027 #else
00028   paramp[0] = command[1];
00029   paramp[1] = command[2];
00030   paramp[2] = command[3];
00031   paramp[3] = command[4];
00032 #endif
00033 
00034   // Find out what type of command this is
00035   switch(command[0]) {
00036   case CMD_fwd:
00037     dx = param;
00038     break;
00039   case CMD_roto:
00040     da = param;
00041     break;
00042   case CMD_side:
00043     dy = param;
00044     break;
00045   case CMD_opt0:
00046     {
00047       /*      HeadPointerMC *head =
00048         (HeadPointerMC*)motman->checkoutMotion(head_id);
00049       head->setJoints(0,0,0);
00050       motman->checkinMotion(head_id);*/
00051       break;
00052     }
00053   case CMD_opt1:
00054   case CMD_opt2:
00055   case CMD_opt3:
00056   case CMD_opt4:
00057     cout << "MECHA: hey, reprogram this button!" << endl;
00058     break;
00059   case CMD_opt5:
00060     sndman->playFile("howl.wav");
00061     break;
00062   case CMD_opt6:
00063     sndman->playFile("yap.wav");
00064     break;
00065   case CMD_opt7:
00066     sndman->playFile("whimper.wav");
00067     break;
00068   case CMD_opt8:
00069     sndman->playFile("growl.wav");
00070     break;
00071   case CMD_opt9:
00072     sndman->playFile("barkmed.wav");
00073     break;
00074     // The options button commands.
00075   default:
00076     cout << "MECHA: unknown command " << command[0] << endl;
00077   }
00078 
00079   // If the command was a new motion command, apply the
00080   // new motion parameters:
00081   switch(command[0]) {
00082   case CMD_fwd:
00083   case CMD_roto:
00084   case CMD_side:
00085     {
00086       MMAccessor<WalkMC> walker(getWalkID());
00087 #ifdef TGT_HAS_CMPACKWALK
00088       float tdx=dx*walker->getCP().max_vel[dx>0?WalkMC::CalibrationParam::forward:WalkMC::CalibrationParam::reverse];
00089       float tdy=dy*walker->getCP().max_vel[WalkMC::CalibrationParam::strafe];
00090       float tda=da*walker->getCP().max_vel[WalkMC::CalibrationParam::rotate];
00091 #else
00092       float tdx=dx*walker->getMaxXVel();
00093       float tdy=dy*walker->getMaxYVel();
00094       float tda=da*walker->getMaxAVel();
00095 #endif
00096       walker->setTargetVelocity(tdx,tdy,tda);
00097     }
00098   }
00099 
00100   // Reset the stop-if-no-heartbeat timer -- if we don't
00101   // hear from the mothership in three seconds, stop immediately.
00102   erouter->addTimer(this, TIMER_COMM_TIMEOUT, 3000, false);
00103   erouter->addTimer(this, TIMER_COMM_CHECK, 500, true);
00104 }
00105 
00106 void WalkController::doStart() {
00107   // Behavior startup
00108   BehaviorBase::doStart();
00109   erouter->addListener(this, EventBase::runtimeEGID);
00110   // Enable walker (the MC_ID can be accessed through the shared_walker later)
00111   motman->addPersistentMotion(shared_walker);
00112   // Turn on wireless
00113   theLastOne=theOne;
00114   theOne=this;
00115   cmdsock=wireless->socket(Socket::SOCK_STREAM, 2048, 2048);
00116   sock= cmdsock->sock;
00117   wireless->setReceiver(sock, mechacmd_callback);
00118   wireless->setDaemon(sock,true); 
00119   wireless->listen(sock, config->main.walkControl_port);
00120   // Open the WalkGUI on the desktop
00121   Controller::loadGUI("org.tekkotsu.mon.WalkGUI","WalkGUI",config->main.walkControl_port);
00122 }
00123 
00124 void WalkController::shutdown() {
00125   // Turn off timers
00126   erouter->removeListener(this);
00127   // Close socket; turn wireless off
00128   if(sock>0) {
00129     wireless->setDaemon(sock,false); 
00130     wireless->close(sock);
00131     cmdsock=NULL;
00132     sock=-1;
00133   }
00134   theOne=theLastOne;
00135   // Disable walker
00136   motman->removeMotion(getWalkID());
00137 }
00138 
00139 void WalkController::doStop() {
00140   shutdown();
00141   // Close the GUI
00142   if(!keepGUI)
00143     Controller::closeGUI("WalkGUI");
00144   // Total behavior stop
00145   BehaviorBase::doStop();
00146 }
00147 
00148 // The command packet reassembly mechanism
00149 int WalkController::mechacmd_callback(char *buf, int bytes) {
00150   static char cb_buf[5];
00151   static int cb_buf_filled;
00152 
00153   // If there's an incomplete command in the command buffer, fill
00154   // up as much of the command buffer as we can and then execute it
00155   // if possible
00156   if(cb_buf_filled) {
00157     while((cb_buf_filled < 5) && bytes) {
00158       cb_buf[cb_buf_filled++] = *buf++; // copy incoming buffer byte
00159       --bytes;        // decrement remaining byte ct.
00160     }
00161     // did we fill it? if so, execute! and mark buffer empty.
00162     if(cb_buf_filled == 5) {
00163       if(WalkController::theOne) WalkController::theOne->runCommand((unsigned char*) cb_buf);
00164       cb_buf_filled = 0;
00165     }
00166   }
00167 
00168   // now execute all complete bytes in the incoming buffer
00169   while(bytes >= 5) {
00170     if(WalkController::theOne) WalkController::theOne->runCommand((unsigned char *) buf);
00171     bytes -= 5;
00172     buf += 5;
00173   }
00174 
00175   // finally, store all remaining bytes in the command buffer
00176   while(bytes) {
00177     cb_buf[cb_buf_filled++] = *buf++;
00178     --bytes;
00179   }
00180 
00181   return 0;
00182 }
00183 
00184 #endif
00185 
00186 /*! @file
00187  * @brief Implements WalkController, listens to mecha control commands coming in from the command port for remotely controlling the walk
00188  * @author tss (Creator)
00189  * @author ejt (modifications)
00190  * @author PA Gov. School for the Sciences 2003 Team Project - Haoqian Chen, Yantian Martin, Jon Stahlman (modifications)
00191  */

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3