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

Tekkotsu v3.0
Generated Wed Oct 4 00:03:43 2006 by Doxygen 1.4.7