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
00010 float param;
00011 unsigned char *paramp = (unsigned char *) ¶m;
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
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
00041
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
00055 BehaviorBase::DoStart();
00056
00057 head_id = motman->addPersistentMotion(SharedObject<HeadPointerMC>());
00058
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
00066 Controller::loadGUI("org.tekkotsu.mon.HeadPointGUI","HeadPointGUI",config->main.headControl_port);
00067 }
00068
00069 void HeadPointControllerBehavior::DoStop() {
00070
00071 Controller::closeGUI("HeadPointGUI");
00072
00073 erouter->removeListener(this);
00074
00075 wireless->setDaemon(cmdsock,false);
00076 wireless->close(cmdsock);
00077 theOne=theLastOne;
00078
00079 motman->removeMotion(head_id);
00080
00081 BehaviorBase::DoStop();
00082 }
00083
00084
00085 int HeadPointControllerBehavior::mechacmd_callback(char *buf, int bytes) {
00086 static char cb_buf[5];
00087 static int cb_buf_filled;
00088
00089
00090
00091
00092 if(cb_buf_filled) {
00093 while((cb_buf_filled < 5) && bytes) {
00094 cb_buf[cb_buf_filled++] = *buf++;
00095 --bytes;
00096 }
00097
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
00105 while(bytes >= 5) {
00106 if(HeadPointControllerBehavior::theOne) HeadPointControllerBehavior::theOne->runCommand((unsigned char *) buf);
00107 bytes -= 5;
00108 buf += 5;
00109 }
00110
00111
00112 while(bytes) {
00113 cb_buf[cb_buf_filled++] = *buf++;
00114 --bytes;
00115 }
00116
00117 return 0;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130