00001 #include <phJoystickControl.h> 00002 #include <phGravisGamePadProData.h> 00003 #include <phSideWinderPrecisionProData.h> 00004 #include <phPlayerUtil.h> 00005 #include <phbase.h> 00006 00007 /* ------------------------------------------------------------------------ */ 00008 phJoystickControl::phJoystickControl( phLinuxJoystick *joystick, 00009 phPlayerPosition *position, 00010 phPlayerSonar *sonar, 00011 phPlayerPtz *ptz 00012 ) 00013 { 00014 phFUNCTION("phJoystickControl::phJoystickControl") 00015 int locked = 0; 00016 00017 phTHIS_LOOSE_LOCK(locked); 00018 00019 this->setName("phJoystickControl"); 00020 this->m_joystick = joystick; 00021 this->m_sonar = sonar; 00022 this->m_position = position; 00023 this->m_ptz = ptz; 00024 00025 phTHIS_LOOSE_UNLOCK(locked); 00026 } 00027 00028 /* ------------------------------------------------------------------------ */ 00029 int phJoystickControl::run( ) 00030 { 00031 phFUNCTION("phJoystickControl::run") 00032 00033 int lock_position = 0; 00034 int position_locked = 0; 00035 double translate = MAX_TRANSLATE; 00036 double rotate = 0.0; 00037 int exit_now = 0; 00038 00039 int enabled = 0; 00040 00041 int joy_id = 0; 00042 int32_t go = 0; 00043 00044 phJoystickData *joy_data = NULL; 00045 00046 phCHECK_PTR(this->m_joystick,NULL,"this->m_joystick == NULL"); 00047 00048 if ((strlen(this->m_joystick->getName()) == 0) || 00049 (strcmp(this->m_joystick->getName(), 00050 "Microsoft Microsoft SideWinder Precision Pro (USB)") == 0)) 00051 { 00052 phPRINT("Microsoft: %s\n",this->m_joystick->getName()); 00053 00054 joy_data = new phSideWinderPrecisionProData(); 00055 joy_id = 1; 00056 } 00057 else 00058 { 00059 phPRINT("Gravis: %s\n",this->m_joystick->getName()); 00060 00061 joy_data = new phGravisGamePadProData(); 00062 joy_id = 0; 00063 } 00064 00065 rc = joy_data->connect(this->m_joystick->getSource()); 00066 phCHECK_RC(rc,NULL,"joy_data->connect(this->m_joystick->getSource())"); 00067 00068 rc = this->signal_running(); 00069 phCHECK_RC(rc,NULL,"this->signal_running"); 00070 00071 while (this->isRunning() && (exit_now == 0)) 00072 { 00073 /* Joystick handling */ 00074 rc = joy_data->update(phLiveObjectWAIT); 00075 phPRINT_RC(rc,NULL,"joy_data->update(WAIT)"); 00076 00077 if (rc == phSUCCESS) 00078 { 00079 int32_t red = 0; 00080 int32_t green = 0; 00081 int32_t blue = 0; 00082 int32_t yellow = 0; 00083 int32_t x = 0; 00084 int32_t y = 0; 00085 double rotate_scale = 0.0; 00086 double translate_scale = 0.0; 00087 int home = 0; 00088 int32_t pan = 0; 00089 int32_t tilt = 0; 00090 int32_t zoom = 0; 00091 00092 x = joy_data->x(); 00093 y = joy_data->y(); 00094 red = joy_data->get("red"); 00095 green = joy_data->get("green"); 00096 blue = joy_data->get("blue"); 00097 yellow = joy_data->get("yellow"); 00098 00099 if (joy_id == 0) 00100 { 00101 x = (red ? -1 : (green ? 1 : 0 )); 00102 y = (blue ? -1 : (yellow ? 1 : 0 )); 00103 go = joy_data->get("R1"); 00104 exit_now = (joy_data->get("start") && joy_data->get("L2")) ? 1 : 0; 00105 00106 /* TODO add PTZ */ 00107 } 00108 else 00109 { 00110 int32_t up = joy_data->get("up"); 00111 int32_t down = joy_data->get("down"); 00112 int32_t home_button = joy_data->get("select"); 00113 00114 go = joy_data->get("trigger"); 00115 exit_now = (joy_data->get("arrow") ? 1 : 0); 00116 pan = joy_data->get("hat_roll"); 00117 tilt = joy_data->get("hat_pitch"); 00118 00119 if ((up && down && home_button) || 00120 (up && down) || 00121 (up && home_button) || 00122 (down && home_button)) 00123 { 00124 zoom = home = 0; 00125 } 00126 else 00127 { 00128 if (home_button) home = 1; 00129 if (up) 00130 zoom = 1; 00131 else if (down) 00132 zoom = -1; 00133 } 00134 } 00135 00136 //phPROGRESS("%d:%d:%d:%d\n",pan,tilt,home,zoom); 00137 /* PTZ */ 00138 if ((this->m_ptz != NULL) && 00139 (this->m_ptz->isSubscribed()) && 00140 ((pan || tilt || home || zoom))) 00141 { 00142 double pan_adjust = 0.5; 00143 double tilt_adjust = 0.5; 00144 double zoom_adjust = 0.5; 00145 00146 double pan_val = 0.0; 00147 double tilt_val = 0.0; 00148 double zoom_val = 0.0; 00149 00150 //phPROGRESS("\n"); 00151 00152 rc = this->m_ptz->lock(); 00153 phPRINT_RC(rc,NULL,"this->m_ptz->lock()"); 00154 00155 if (rc == phSUCCESS) 00156 { 00157 if (home) 00158 { 00159 pan_val = tilt_val = zoom_val = 0.0; 00160 } 00161 else 00162 { 00163 pan_val = this->m_ptz->getPan(); 00164 tilt_val = this->m_ptz->getTilt(); 00165 zoom_val = this->m_ptz->getZoom(); 00166 } 00167 if (pan > 0) /* pan right */ 00168 pan_val += pan_adjust; 00169 else if (pan < 0) /* pan left */ 00170 pan_val += pan_adjust * -1.0; 00171 00172 if (tilt < 0) /* tilt up */ 00173 tilt_val += tilt_adjust; 00174 else if (tilt > 0) /* tilt down */ 00175 tilt_val += tilt_adjust * -1.0; 00176 00177 if (zoom > 0) /* zoom in */ 00178 zoom_val += zoom_adjust; 00179 else if (zoom < 0) /* zoom out */ 00180 zoom_val += zoom_adjust * -1.0; 00181 00182 00183 //phPROGRESS("%0.4f:%0.4f:%0.4f\n",pan_val,tilt_val,zoom_val); 00184 rc = this->m_ptz->panTiltZoom(pan_val,tilt_val,zoom_val); 00185 phPRINT_RC(rc,NULL,"this->m_ptz->panTiltZoom"); 00186 00187 rc = this->m_ptz->unlock(); 00188 phPRINT_RC(rc,NULL,"this->m_ptz->unlock()"); 00189 } 00190 } 00191 00192 /* MOVEMENT */ 00193 00194 rotate_scale = (double)x / (double)joy_data->valueMax(); 00195 translate_scale = (double)y / (double)joy_data->valueMax(); 00196 00197 //joy_data->print_data(); 00198 00199 rotate = MAX_ROTATE * -1.0 * rotate_scale; 00200 translate = MAX_ROTATE * -1.0 * translate_scale; 00201 00202 /* 00203 if (go) 00204 { 00205 enabled = (enabled == 0 ? 1 : 0); 00206 phPROGRESS("enabled:%d\n",enabled); 00207 rc = this->m_position->enable(enabled); 00208 phPRINT_RC(rc,NULL,"m_position->enable(enabled);"); 00209 if (enabled == 0) 00210 { 00211 this->m_position->stop(); 00212 phPRINT_RC(rc,NULL,"this->m_position->stop()"); 00213 } 00214 } 00215 */ 00216 if (lock_position) 00217 { 00218 if ((!position_locked) && (x || y)) 00219 { 00220 phPRINT("locked\n"); 00221 phMUTEX_LOCK((*(this->m_position)),position_locked); 00222 } 00223 00224 if ((position_locked) && (!x & !y)) 00225 { 00226 phPRINT("unlocked\n"); 00227 phMUTEX_UNLOCK((*(this->m_position)),position_locked); 00228 } 00229 00230 phPRINT("(joy)enabled:%d trans:%0.8f rot:%0.8f\n",enabled,translate,rotate); 00231 } 00232 } 00233 00234 if (this->m_position->isEnabled() && (!(go > 0))) 00235 { 00236 rc = this->m_position->stop(); 00237 phPRINT_RC(rc,NULL,"this->m_position->stop()"); 00238 00239 rc = this->m_position->enable(0); 00240 phPRINT_RC(rc,NULL,"m_position->enable(0);"); 00241 } 00242 else if ((!(this->m_position->isEnabled())) && (go > 0)) 00243 { 00244 rc = this->m_position->enable(1); 00245 phPRINT_RC(rc,NULL,"m_position->enable(1);"); 00246 } 00247 00248 rc = this->m_position->move(translate,rotate); 00249 phPRINT_RC(rc,NULL,"move"); 00250 } 00251 00252 rc = joy_data->disconnect(); 00253 phCHECK_RC(rc,NULL,"joy_data->disconnect()"); 00254 00255 phMUTEX_UNLOCK((*(this->m_position)),position_locked); 00256 00257 return phSUCCESS; 00258 00259 error: 00260 rc = this->signal_error(); 00261 phPRINT_RC(rc,NULL,"this->signal_error"); 00262 00263 phMUTEX_ERROR_UNLOCK((*(this->m_position)),position_locked); 00264 00265 return phFAIL; 00266 } 00267 00268
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |