00001 #include "Robots/SeaBeeIII/XBox360RemoteControlI.H" 00002 00003 #include "Component/ModelParam.H" 00004 #include "Component/ModelOptionDef.H" 00005 00006 using namespace XBox360RemoteControl; 00007 00008 std::map<int, std::string> Keys::Actions::toString = Keys::Actions::populateMappingToString(); 00009 std::map<std::string, int> Keys::Actions::toInt = Keys::populateInverseMapping(Keys::Actions::toString); 00010 00011 /*const string Keys::Btns::L_BUMPER = "L_BUMPER_BTN"; 00012 const string Keys::Btns::R_BUMPER = "R_BUMPER_BTN"; 00013 const string Keys::Btns::BACK = "BACK_BTN"; 00014 const string Keys::Btns::START = "START_BTN"; 00015 const string Keys::Btns::CENTER = "CENTER_BTN"; 00016 const string Keys::Btns::L_JOYSTICK = "L_JOYSTICK_BTN"; 00017 const string Keys::Btns::R_JOYSTICK = "R_JOYSTICK_BTN"; 00018 const string Keys::Btns::X = "X_BTN"; 00019 const string Keys::Btns::Y = "Y_BTN"; 00020 const string Keys::Btns::A = "A_BTN"; 00021 const string Keys::Btns::B = "B_BTN"; 00022 00023 const string Keys::Axes::L_TRIGGER = "L_TRIGGER_AXIS"; 00024 const string Keys::Axes::R_TRIGGER = "R_TRIGGER_AXIS"; 00025 const string Keys::Axes::L_JOYSTICK_X = "L_JOYSTICK_X_AXIS"; 00026 const string Keys::Axes::L_JOYSTICK_Y = "L_JOYSTICK_Y_AXIS"; 00027 const string Keys::Axes::R_JOYSTICK_X = "R_JOYSTICK_X_AXIS"; 00028 const string Keys::Axes::R_JOYSTICK_Y = "R_JOYSTICK_Y_AXIS"; 00029 const string Keys::Axes::DPAD_X = "DPAD_X_AXIS"; 00030 const string Keys::Axes::DPAD_Y = "DPAD_Y_AXIS";*/ 00031 00032 using namespace std; 00033 00034 const ModelOptionCateg MOC_SeaBee3GUI = { MOC_SORTPRI_3, 00035 "SeaBee3GUI Related Options" }; 00036 00037 const ModelOptionDef OPT_JoystickDisable = { MODOPT_ARG(int), 00038 "JoystickDisable", &MOC_SeaBee3GUI, OPTEXP_CORE, 00039 "Whether or not to disable the joystick control with GUI", 00040 "disable-joystick", '\0', "<int>", "0" }; 00041 00042 #ifdef HAVE_LINUX_JOYSTICK_H 00043 // ###################################################################### 00044 XBox360RemoteControlI::XBox360RemoteControlI(int id, OptionManager& mgr, 00045 const std::string& descrName, const std::string& tagName) : 00046 RobotBrainComponent(mgr, descrName, tagName), 00047 itsJS(new JoyStick(mgr)), 00048 lis(new TestJoyStickListener(rutz::shared_ptr<XBox360RemoteControlI>(this)))//, 00049 // itsJoystickDisable(&OPT_JoystickDisable, this, 0) 00050 00051 { 00052 addSubComponent(itsJS); 00053 itsCalibrated = false; 00054 itsSetAxis = -1; 00055 00056 //the numbering here only determines the order in which the axes are requested during configuration 00057 itsActionIds[0] = Keys::Actions::AXIS_INVERT; 00058 itsActionIds[1] = Keys::Actions::STRAFE; 00059 itsActionIds[2] = Keys::Actions::SPEED; 00060 itsActionIds[3] = Keys::Actions::HEADING; 00061 itsActionIds[4] = Keys::Actions::DIVE; 00062 itsActionIds[5] = Keys::Actions::SURFACE; 00063 itsActionIds[6] = Keys::Actions::ARM_NEXT_DEV; 00064 itsActionIds[8] = Keys::Actions::FIRE_DEV; 00065 //0,1,3,5,2 00066 } 00067 00068 // ###################################################################### 00069 XBox360RemoteControlI::~XBox360RemoteControlI() 00070 { 00071 } 00072 00073 void XBox360RemoteControlI::registerTopics() 00074 { 00075 /* if(!itsJoystickDisable.getVal()) 00076 {*/ 00077 this->registerPublisher("XBox360RemoteControlMessageTopic"); 00078 00079 lis2.dynCastFrom(lis); // cast down 00080 itsJS->setListener(lis2); 00081 // } 00082 } 00083 00084 void XBox360RemoteControlI::evolve() 00085 { 00086 if(!itsCalibrated && isEnabled()) 00087 { 00088 calibrate(); 00089 itsCalibrated = true; 00090 } 00091 } 00092 00093 void XBox360RemoteControlI::calibrate() 00094 { 00095 map<int, int>::iterator it; 00096 map<int, int> tempIdMap; 00097 map<int, int> tempInversionMap; 00098 00099 /*cout << "Press " << Keys::Actions::toString[Keys::Actions::AXIS_INVERT] << endl; 00100 updateSetBtn(-1); 00101 int setBtn = -1; 00102 while(setBtn < 0) 00103 { 00104 sleep(0.5); 00105 itsSetBtnMutex.lock(); 00106 setBtn = itsSetBtn; 00107 itsSetBtnMutex.unlock(); 00108 } 00109 itsAxisInversionBtn = setBtn; 00110 00111 cout << "set to " << itsAxisInversionBtn << endl;*/ 00112 00113 for(it = itsActionIds.begin(); it != itsActionIds.end(); it++) //cycling through actions 00114 { 00115 //LINFO("Set %s...\n",Keys::Actions::mMappings[it->first].c_str()); 00116 cout << "Set " << Keys::Actions::toString[it->second] << "..." << endl; 00117 00118 itsSetAxis = ControlAxis::NONE; 00119 00120 int setAxis = ControlAxis::NONE; 00121 selectionType = ControlAxis::NONE; 00122 00123 while(setAxis == ControlAxis::NONE) 00124 { 00125 sleep(0.5); 00126 00127 itsSetAxisMutex.lock(); 00128 if(itsSetAxis != ControlAxis::NONE) 00129 { 00130 if(tempIdMap.find(itsSetAxis) == tempIdMap.end()) 00131 { 00132 //cout << itsSetAxis << "," << selectionType << "," << itsTypeMappings[itsSetAxis] << endl; 00133 setAxis = itsSetAxis; 00134 } 00135 else 00136 { 00137 //cout << itsSetAxis << "," << selectionType << "," << itsTypeMappings[itsSetAxis] << endl; 00138 cout << "That axis is already mapped to a function." << endl; 00139 itsSetAxis = ControlAxis::NONE; 00140 } 00141 } 00142 00143 itsSetAxisMutex.unlock(); 00144 } 00145 00146 //cout << it->second << " now bound to " << setAxis << endl; 00147 //LINFO("%s now bound to %d\n\n",Keys::Actions::mMappings[it->first], setAxis); 00148 cout << Keys::Actions::toString[it->second] << " now bound to " << selectionType << ":" << setAxis << endl; 00149 00150 tempIdMap[setAxis] = it->second; 00151 itsTypeMappings[setAxis] = selectionType; 00152 itsInversionMappings[setAxis] = 1; 00153 00154 //sleep(5); 00155 00156 } 00157 00158 itsActionIds = tempIdMap; 00159 itsAxisIndices = Keys::populateInverseMapping(itsActionIds); 00160 00161 //default axes to be inverted (so we don't have to do this every time) 00162 itsInversionMappings[itsAxisIndices[Keys::Actions::STRAFE]] = -1; 00163 itsInversionMappings[itsAxisIndices[Keys::Actions::HEADING]] = -1; 00164 itsInversionMappings[itsAxisIndices[Keys::Actions::DIVE]] = -1; 00165 itsInversionMappings[itsAxisIndices[Keys::Actions::SURFACE]] = -1; 00166 00167 cout << "Calibration complete!" << endl; 00168 } 00169 00170 void XBox360RemoteControlI::updateSetBtn(int btn) 00171 { 00172 if(btn == ControlAxis::NONE) 00173 { 00174 itsSetBtn = -1; 00175 //itsSetAxis = ControlAxis::NONE; 00176 return; 00177 } 00178 itsSetBtnMutex.lock(); 00179 itsSetBtn = btn; 00180 updateSetAxis(-btn - 1); //btn[n] --> axis[-n-1] 00181 itsSetBtnMutex.unlock(); 00182 selectionType = ControlAxis::BTN; 00183 } 00184 00185 void XBox360RemoteControlI::updateSetAxis(int axis) 00186 { 00187 //LINFO("%d",axis); 00188 itsSetAxisMutex.lock(); 00189 if(itsCalibrated) 00190 { 00191 if(itsActionIds.find(axis) != itsActionIds.end() && itsActionIds[axis] != Keys::Actions::AXIS_INVERT) 00192 { 00193 cout << Keys::Actions::toString[itsActionIds[axis]] << " selected; inversion settings: " << itsInversionMappings[axis] << endl; 00194 itsSetAxis = axis; 00195 } 00196 else if(itsActionIds.find(axis) != itsActionIds.end() && itsActionIds[axis] == Keys::Actions::AXIS_INVERT) 00197 { 00198 //do nothing, this just saves some typing 00199 } 00200 else if(axis >= 0) 00201 { 00202 cout << "Axis (joystick code: " << axis << ") not bound" << endl; 00203 } 00204 else 00205 { 00206 cout << "Axis (button code: " << axis << ") not bound" << endl; 00207 } 00208 } 00209 else 00210 { 00211 itsSetAxis = axis; 00212 } 00213 itsSetAxisMutex.unlock(); 00214 selectionType = ControlAxis::JSTK; 00215 } 00216 00217 void XBox360RemoteControlI::updateSetAxisInversion() 00218 { 00219 itsSetAxisInvertMutex.lock(); 00220 itsSetAxisMutex.lock(); 00221 00222 if(itsSetAxis != ControlAxis::NONE) 00223 { 00224 itsInversionMappings[itsSetAxis] *= -1; 00225 cout << "Inverted " << Keys::Actions::toString[itsActionIds[itsSetAxis]] << ": " << itsInversionMappings[itsSetAxis] << endl; 00226 } 00227 00228 itsSetAxisMutex.unlock(); 00229 itsSetAxisInvertMutex.unlock(); 00230 00231 updateSetBtn(ControlAxis::NONE); 00232 } 00233 00234 // ###################################################################### 00235 void XBox360RemoteControlI::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg, 00236 const Ice::Current&) 00237 { 00238 00239 } 00240 #else 00241 //No linux/joystick defined.. 00242 00243 XBox360RemoteControlI::XBox360RemoteControlI(int id, OptionManager& mgr, 00244 const std::string& descrName, const std::string& tagName) : 00245 RobotBrainComponent(mgr, descrName, tagName) 00246 { 00247 ; 00248 } 00249 00250 XBox360RemoteControlI::~XBox360RemoteControlI() 00251 { 00252 } 00253 00254 void XBox360RemoteControlI::evolve() 00255 { 00256 ; 00257 } 00258 00259 //!Get a message 00260 void XBox360RemoteControlI::updateMessage( 00261 const RobotSimEvents::EventMessagePtr& eMsg, const Ice::Current&) 00262 { 00263 ; 00264 } 00265 00266 void XBox360RemoteControlI::registerTopics() 00267 { 00268 ; 00269 } 00270 00271 void XBox360RemoteControlI::calibrate() 00272 { 00273 ; 00274 } 00275 00276 void XBox360RemoteControlI::updateSetBtn(int btn) 00277 { 00278 ; 00279 } 00280 00281 void XBox360RemoteControlI::updateSetAxis(int axis) 00282 { 00283 ; 00284 } 00285 00286 void XBox360RemoteControlI::updateSetAxisInversion() 00287 { 00288 ; 00289 } 00290 00291 #endif 00292