Homepage Demos Overview Downloads Tutorials Reference
Credits

WalkControllerBehavior.cc

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

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