Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

HeadController.cc

Go to the documentation of this file.
00001 #include "Shared/RobotInfo.h"
00002 #ifdef TGT_HAS_HEAD
00003 
00004 #include "HeadController.h"
00005 #include "Behaviors/Controller.h"
00006 #include "Motion/MMAccessor.h"
00007 #include "Motion/HeadPointerMC.h"
00008 #include "Shared/ERS7Info.h"
00009 #include "Shared/ERS210Info.h"
00010 #include "IPC/SharedObject.h"
00011 
00012 REGISTER_BEHAVIOR_MENU_OPT(HeadController,"TekkotsuMon",BEH_NONEXCLUSIVE);
00013 
00014 HeadController* HeadController::theOne = NULL;
00015 
00016 void HeadController::runCommand(unsigned char *command) {
00017   // Extract the command parameter
00018   float param;
00019   unsigned char *paramp = (unsigned char *) &param;
00020 
00021 #if defined(BYTE_ORDER) && BYTE_ORDER==BIG_ENDIAN
00022   paramp[0] = command[4];
00023   paramp[1] = command[3];
00024   paramp[2] = command[2];
00025   paramp[3] = command[1];
00026 #else
00027   paramp[0] = command[1];
00028   paramp[1] = command[2];
00029   paramp[2] = command[3];
00030   paramp[3] = command[4];
00031 #endif
00032   
00033   // Find out what type of command this is
00034 #ifdef TGT_IS_AIBO
00035   switch(command[0]) {
00036   case CMD_tilt:
00037     t = std::abs(param)*outputRanges[HeadOffset+TiltOffset][param>0?MaxRange:MinRange];
00038     break;
00039   case CMD_pan:
00040     p = std::abs(param)*outputRanges[HeadOffset+PanOffset][param>0?MaxRange:MinRange];
00041     break;
00042   case CMD_roll:
00043     r = std::abs(param)*outputRanges[HeadOffset+RollOffset][param>0?MaxRange:MinRange];
00044     break;
00045   default:
00046     std::cout << "MECHA: unknown command " << command[0] << std::endl;
00047   }
00048 #else
00049   switch(command[0]) {
00050     case CMD_tilt: {
00051       const char* n = ERS7Info::outputNames[ERS7Info::HeadOffset+ERS7Info::TiltOffset];
00052       unsigned int i = capabilities.findOutputOffset(n);
00053       if(i!=-1U)
00054         t = std::abs(param)*outputRanges[i][param>0?MaxRange:MinRange];
00055     } break;
00056     case CMD_pan: {
00057       const char* n = ERS7Info::outputNames[ERS7Info::HeadOffset+ERS7Info::PanOffset];
00058       unsigned int i = capabilities.findOutputOffset(n);
00059       if(i!=-1U)
00060         p = std::abs(param)*outputRanges[i][param>0?MaxRange:MinRange];
00061     } break;
00062     case CMD_roll: {
00063       const char* n = ERS7Info::outputNames[ERS7Info::HeadOffset+ERS7Info::NodOffset];
00064       unsigned int i = capabilities.findOutputOffset(n);
00065       if(i!=-1U)
00066         r = std::abs(param)*outputRanges[i][param>0?MaxRange:MinRange];
00067       else {
00068         n = ERS210Info::outputNames[ERS210Info::HeadOffset+ERS210Info::RollOffset];
00069         i = capabilities.findOutputOffset(n);
00070         if(i!=-1U)
00071           r = std::abs(param)*outputRanges[i][param>0?MaxRange:MinRange];
00072       }
00073     } break;
00074     default:
00075       std::cout << "MECHA: unknown command " << command[0] << std::endl;
00076   }
00077 #endif
00078 
00079   // If the command was a new motion command, apply the
00080   // new motion parameters:
00081   switch(command[0]) {
00082   case CMD_tilt:
00083   case CMD_pan:
00084   case CMD_roll:
00085     {
00086       MMAccessor<HeadPointerMC> head(head_id);
00087 #ifdef TGT_HAS_KINECT
00088       head->setMaxSpeed(0, 1.0f);
00089       head->setMaxSpeed(1, 1.0f);
00090 #endif
00091       head->setJoints(t,p,r);
00092     }
00093   }
00094 }
00095 
00096 void HeadController::doStart() {
00097   // Behavior startup
00098   BehaviorBase::doStart();
00099   // Enable head control
00100   SharedObject<HeadPointerMC> head;
00101   head->setHold(false);
00102   head_id = motman->addPersistentMotion(head);
00103   // Turn on wireless
00104   theLastOne=theOne;
00105   theOne=this;
00106   cmdsock=wireless->socket(Socket::SOCK_STREAM, 2048, 2048);
00107   wireless->setReceiver(cmdsock->sock, mechacmd_callback);
00108   wireless->setDaemon(cmdsock,true);
00109   wireless->listen(cmdsock->sock, config->main.headControl_port);
00110   // Open the WalkGUI on the desktop
00111   Controller::loadGUI("org.tekkotsu.mon.HeadPointGUI","HeadPointGUI",config->main.headControl_port);
00112 }
00113 
00114 void HeadController::doStop() {
00115   // Close the GUI
00116   Controller::closeGUI("HeadPointGUI");
00117   // Turn off timers
00118   erouter->removeListener(this);
00119   // Close socket; turn wireless off
00120   wireless->setDaemon(cmdsock,false);
00121   wireless->close(cmdsock);
00122   theOne=theLastOne;
00123   // Disable head pointer
00124   motman->removeMotion(head_id);
00125   // Total behavior stop
00126   BehaviorBase::doStop();
00127 }
00128 
00129 // The command packet reassembly mechanism
00130 int HeadController::mechacmd_callback(char *buf, int bytes) {
00131   static char cb_buf[5];
00132   static int cb_buf_filled;
00133 
00134   // If there's an incomplete command in the command buffer, fill
00135   // up as much of the command buffer as we can and then execute it
00136   // if possible
00137   if(cb_buf_filled) {
00138     while((cb_buf_filled < 5) && bytes) {
00139       cb_buf[cb_buf_filled++] = *buf++; // copy incoming buffer byte
00140       --bytes;        // decrement remaining byte ct.
00141     }
00142     // did we fill it? if so, execute! and mark buffer empty.
00143     if(cb_buf_filled == 5) {
00144       if(HeadController::theOne) HeadController::theOne->runCommand((unsigned char*) cb_buf);
00145       cb_buf_filled = 0;
00146     }
00147   }
00148 
00149   // now execute all complete bytes in the incoming buffer
00150   while(bytes >= 5) {
00151     if(HeadController::theOne) HeadController::theOne->runCommand((unsigned char *) buf);
00152     bytes -= 5;
00153     buf += 5;
00154   }
00155 
00156   // finally, store all remaining bytes in the command buffer
00157   while(bytes) {
00158     cb_buf[cb_buf_filled++] = *buf++;
00159     --bytes;
00160   }
00161 
00162   return 0;
00163 }
00164 
00165 #endif
00166 
00167 /*! @file
00168  * @brief Implements HeadController, listens to control commands coming in from the command port for remotely controlling the head
00169  * @author tss (Creator)
00170  */
00171 

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