Homepage Demos Overview Downloads Tutorials Reference
Credits

HeadPointControllerBehavior.cc

Go to the documentation of this file.
00001 #include "HeadPointControllerBehavior.h"
00002 #include "Behaviors/Controller.h"
00003 #include "Motion/MMAccessor.h"
00004 
00005 HeadPointControllerBehavior* HeadPointControllerBehavior::theOne = NULL;
00006 
00007 void HeadPointControllerBehavior::runCommand(unsigned char *command) {
00008   // First, turn off the stop-if-no-heartbeat timer
00009   erouter->removeTimer(this);
00010 
00011   // Extract the command parameter
00012   float param;
00013   unsigned char *paramp = (unsigned char *) &param;
00014 
00015   paramp[0] = command[1];
00016   paramp[1] = command[2];
00017   paramp[2] = command[3];
00018   paramp[3] = command[4];
00019 
00020   // Find out what type of command this is
00021   switch(command[0]) {
00022   case CMD_tilt:
00023     t = param*outputRanges[HeadOffset+TiltOffset][MaxRange];
00024     break;
00025   case CMD_pan:
00026     p = param*outputRanges[HeadOffset+PanOffset][MaxRange];
00027     break;
00028   case CMD_roll:
00029     r = param*outputRanges[HeadOffset+RollOffset][MaxRange];
00030     break;
00031   default:
00032     cout << "MECHA: unknown command " << command[0] << endl;
00033   }
00034 
00035   // If the command was a new motion command, apply the
00036   // new motion parameters:
00037   switch(command[0]) {
00038   case CMD_tilt:
00039   case CMD_pan:
00040   case CMD_roll:
00041     {
00042       MMAccessor<HeadPointerMC> head(head_id);
00043       head->setJoints(t,p,r);
00044     }
00045   }
00046 
00047   // Reset the stop-if-no-heartbeat timer -- if we don't
00048   // hear from the mothership in three seconds, stop immediately.
00049   erouter->addTimer(this, 0, 3000, false);
00050 }
00051 
00052 void HeadPointControllerBehavior::DoStart() {
00053   // Behavior startup
00054   BehaviorBase::DoStart();
00055   // We listen to timers
00056   erouter->addListener(this, EventBase::timerEGID);
00057   // Enable head control
00058   head_id = motman->addMotion(SharedObject<HeadPointerMC>());
00059   // Turn on wireless
00060   theLastOne=theOne;
00061   theOne=this;
00062   wireless->setReceiver(cmdsock->sock, mechacmd_callback);
00063   wireless->listen(cmdsock->sock, config->main.headControl_port);
00064   // Open the WalkGUI on the desktop
00065   Controller::loadGUI("org.tekkotsu.mon.HeadPointGUI","HeadPointGUI",config->main.headControl_port);
00066 }
00067 
00068 void HeadPointControllerBehavior::DoStop() {
00069   // Close the GUI
00070   Controller::closeGUI("HeadPointGUI");
00071   // Turn off timers
00072   erouter->forgetListener(this);
00073   // Close socket; turn wireless off
00074   wireless->close(cmdsock);
00075   theOne=theLastOne;
00076   // Disable head pointer
00077   motman->removeMotion(head_id);
00078   // Total behavior stop
00079   BehaviorBase::DoStop();
00080 }
00081 
00082 // The command packet reassembly mechanism
00083 int HeadPointControllerBehavior::mechacmd_callback(char *buf, int bytes) {
00084   static char cb_buf[5];
00085   static int cb_buf_filled;
00086 
00087   // If there's an incomplete command in the command buffer, fill
00088   // up as much of the command buffer as we can and then execute it
00089   // if possible
00090   if(cb_buf_filled) {
00091     while((cb_buf_filled < 5) && bytes) {
00092       cb_buf[cb_buf_filled++] = *buf++; // copy incoming buffer byte
00093       --bytes;        // decrement remaining byte ct.
00094     }
00095     // did we fill it? if so, execute! and mark buffer empty.
00096     if(cb_buf_filled == 5) {
00097       if(HeadPointControllerBehavior::theOne) HeadPointControllerBehavior::theOne->runCommand((unsigned char*) cb_buf);
00098       cb_buf_filled = 0;
00099     }
00100   }
00101 
00102   // now execute all complete bytes in the incoming buffer
00103   while(bytes >= 5) {
00104     if(HeadPointControllerBehavior::theOne) HeadPointControllerBehavior::theOne->runCommand((unsigned char *) buf);
00105     bytes -= 5;
00106     buf += 5;
00107   }
00108 
00109   // finally, store all remaining bytes in the command buffer
00110   while(bytes) {
00111     cb_buf[cb_buf_filled++] = *buf++;
00112     --bytes;
00113   }
00114 
00115   return 0;
00116 }
00117 
00118 /*! @file
00119  * @brief Implements HeadPointControllerBehavior, listens to control commands coming in from the command port for remotely controlling the head
00120  * @author tss (Creator)
00121  * 
00122  * $Author: ejt $
00123  * $Name: tekkotsu-1_5 $
00124  * $Revision: 1.1 $
00125  * $State: Rel $
00126  * $Date: 2003/10/10 00:45:01 $
00127  */
00128 

Tekkotsu v1.5
Generated Fri Oct 10 15:51:59 2003 by Doxygen 1.3.4