Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

UPennWalkControllerBehavior.cc

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

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