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