XBox360RemoteControlI.C
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
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00050
00051 {
00052 addSubComponent(itsJS);
00053 itsCalibrated = false;
00054 itsSetAxis = -1;
00055
00056
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
00066 }
00067
00068
00069 XBox360RemoteControlI::~XBox360RemoteControlI()
00070 {
00071 }
00072
00073 void XBox360RemoteControlI::registerTopics()
00074 {
00075
00076
00077 this->registerPublisher("XBox360RemoteControlMessageTopic");
00078
00079 lis2.dynCastFrom(lis);
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
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 for(it = itsActionIds.begin(); it != itsActionIds.end(); it++)
00114 {
00115
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
00133 setAxis = itsSetAxis;
00134 }
00135 else
00136 {
00137
00138 cout << "That axis is already mapped to a function." << endl;
00139 itsSetAxis = ControlAxis::NONE;
00140 }
00141 }
00142
00143 itsSetAxisMutex.unlock();
00144 }
00145
00146
00147
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
00155
00156 }
00157
00158 itsActionIds = tempIdMap;
00159 itsAxisIndices = Keys::populateInverseMapping(itsActionIds);
00160
00161
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
00176 return;
00177 }
00178 itsSetBtnMutex.lock();
00179 itsSetBtn = btn;
00180 updateSetAxis(-btn - 1);
00181 itsSetBtnMutex.unlock();
00182 selectionType = ControlAxis::BTN;
00183 }
00184
00185 void XBox360RemoteControlI::updateSetAxis(int axis)
00186 {
00187
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
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
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
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