WalkController.cc
Go to the documentation of this file.00001 #include "Motion/WalkMC.h"
00002 #ifdef TGT_HAS_WALK
00003
00004 #include "WalkController.h"
00005 #include "Behaviors/Controller.h"
00006 #include "Sound/SoundManager.h"
00007
00008 REGISTER_BEHAVIOR_MENU_OPT(WalkController,"TekkotsuMon",BEH_NONEXCLUSIVE);
00009
00010 using namespace std;
00011
00012 WalkController* WalkController::theOne = NULL;
00013
00014 void WalkController::runCommand(unsigned char *command) {
00015
00016 erouter->removeTimer(this);
00017
00018
00019 float param;
00020 unsigned char *paramp = (unsigned char *) ¶m;
00021
00022 #if defined(BYTE_ORDER) && BYTE_ORDER==BIG_ENDIAN
00023 paramp[0] = command[4];
00024 paramp[1] = command[3];
00025 paramp[2] = command[2];
00026 paramp[3] = command[1];
00027 #else
00028 paramp[0] = command[1];
00029 paramp[1] = command[2];
00030 paramp[2] = command[3];
00031 paramp[3] = command[4];
00032 #endif
00033
00034
00035 switch(command[0]) {
00036 case CMD_fwd:
00037 dx = param;
00038 break;
00039 case CMD_roto:
00040 da = param;
00041 break;
00042 case CMD_side:
00043 dy = param;
00044 break;
00045 case CMD_opt0:
00046 {
00047
00048
00049
00050
00051 break;
00052 }
00053 case CMD_opt1:
00054 case CMD_opt2:
00055 case CMD_opt3:
00056 case CMD_opt4:
00057 cout << "MECHA: hey, reprogram this button!" << endl;
00058 break;
00059 case CMD_opt5:
00060 sndman->playFile("howl.wav");
00061 break;
00062 case CMD_opt6:
00063 sndman->playFile("yap.wav");
00064 break;
00065 case CMD_opt7:
00066 sndman->playFile("whimper.wav");
00067 break;
00068 case CMD_opt8:
00069 sndman->playFile("growl.wav");
00070 break;
00071 case CMD_opt9:
00072 sndman->playFile("barkmed.wav");
00073 break;
00074
00075 default:
00076 cout << "MECHA: unknown command " << command[0] << endl;
00077 }
00078
00079
00080
00081 switch(command[0]) {
00082 case CMD_fwd:
00083 case CMD_roto:
00084 case CMD_side:
00085 {
00086 MMAccessor<WalkMC> walker(getWalkID());
00087 #ifdef TGT_HAS_CMPACKWALK
00088 float tdx=dx*walker->getCP().max_vel[dx>0?WalkMC::CalibrationParam::forward:WalkMC::CalibrationParam::reverse];
00089 float tdy=dy*walker->getCP().max_vel[WalkMC::CalibrationParam::strafe];
00090 float tda=da*walker->getCP().max_vel[WalkMC::CalibrationParam::rotate];
00091 #else
00092 float tdx=dx*walker->getMaxXVel();
00093 float tdy=dy*walker->getMaxYVel();
00094 float tda=da*walker->getMaxAVel();
00095 #endif
00096 walker->setTargetVelocity(tdx,tdy,tda);
00097 }
00098 }
00099
00100
00101
00102 erouter->addTimer(this, TIMER_COMM_TIMEOUT, 3000, false);
00103 erouter->addTimer(this, TIMER_COMM_CHECK, 500, true);
00104 }
00105
00106 void WalkController::doStart() {
00107
00108 BehaviorBase::doStart();
00109 erouter->addListener(this, EventBase::runtimeEGID);
00110
00111 motman->addPersistentMotion(shared_walker);
00112
00113 theLastOne=theOne;
00114 theOne=this;
00115 cmdsock=wireless->socket(Socket::SOCK_STREAM, 2048, 2048);
00116 sock= cmdsock->sock;
00117 wireless->setReceiver(sock, mechacmd_callback);
00118 wireless->setDaemon(sock,true);
00119 wireless->listen(sock, config->main.walkControl_port);
00120
00121 Controller::loadGUI("org.tekkotsu.mon.WalkGUI","WalkGUI",config->main.walkControl_port);
00122 }
00123
00124 void WalkController::shutdown() {
00125
00126 erouter->removeListener(this);
00127
00128 if(sock>0) {
00129 wireless->setDaemon(sock,false);
00130 wireless->close(sock);
00131 cmdsock=NULL;
00132 sock=-1;
00133 }
00134 theOne=theLastOne;
00135
00136 motman->removeMotion(getWalkID());
00137 }
00138
00139 void WalkController::doStop() {
00140 shutdown();
00141
00142 if(!keepGUI)
00143 Controller::closeGUI("WalkGUI");
00144
00145 BehaviorBase::doStop();
00146 }
00147
00148
00149 int WalkController::mechacmd_callback(char *buf, int bytes) {
00150 static char cb_buf[5];
00151 static int cb_buf_filled;
00152
00153
00154
00155
00156 if(cb_buf_filled) {
00157 while((cb_buf_filled < 5) && bytes) {
00158 cb_buf[cb_buf_filled++] = *buf++;
00159 --bytes;
00160 }
00161
00162 if(cb_buf_filled == 5) {
00163 if(WalkController::theOne) WalkController::theOne->runCommand((unsigned char*) cb_buf);
00164 cb_buf_filled = 0;
00165 }
00166 }
00167
00168
00169 while(bytes >= 5) {
00170 if(WalkController::theOne) WalkController::theOne->runCommand((unsigned char *) buf);
00171 bytes -= 5;
00172 buf += 5;
00173 }
00174
00175
00176 while(bytes) {
00177 cb_buf[cb_buf_filled++] = *buf++;
00178 --bytes;
00179 }
00180
00181 return 0;
00182 }
00183
00184 #endif
00185
00186
00187
00188
00189
00190
00191