Joystick.cpp
Go to the documentation of this file.
00001 /*
00002  * Joystick.cpp
00003  *
00004  *  Created on: Nov 12, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include "Joystick.hpp"
00009 
00010 #include <telekyb_base/ROS.hpp>
00011 
00012 // Declare
00013 PLUGINLIB_DECLARE_CLASS(tk_be_common, Joystick, telekyb_behavior::Joystick, TELEKYB_NAMESPACE::Behavior);
00014 
00015 namespace telekyb_behavior {
00016 
00017 Joystick::Joystick()
00018         : Behavior("tk_be_common/Joystick", BehaviorType::Air)
00019 {
00020 
00021 }
00022 
00023 void Joystick::joystickCB(const sensor_msgs::Joy::ConstPtr& msg)
00024 {
00025 //      ROS_INFO("Received Joystick CB");
00026 
00027         if (msg->axes.size() < 4) {
00028                 ROS_ERROR("We need at least 4 Axes!");
00029                 return;
00030         }
00031 
00032         if (msg->buttons.size() < 2) {
00033                 ROS_ERROR("We need at least 2 Buttons!");
00034                 return;
00035         }
00036 
00037         //dead man button is pressed, or we do not care about the dead man button
00038         if ( msg->buttons[0] || !tJoystickUseDeadManSwitch->getValue()) {
00039                 lastVelocityInput = Velocity3D(msg->axes[0],msg->axes[1],msg->axes[2]);
00040                 if (msg->axes.size() != 4) {
00041                         lastYawRateInput = (msg->axes[4] - msg->axes[3])/2.0 * tJoystickYawRateScale->getValue();
00042                 } else {
00043                         // 4 axes case
00044                         lastYawRateInput = msg->axes[3] * tJoystickYawRateScale->getValue();
00045                 }
00046         } else {
00047                 // apply 0
00048                 lastVelocityInput = Velocity3D::Zero();
00049                 lastYawRateInput = 0.0;
00050         }
00051 
00052 
00053         // invalidate
00054         if ( msg->buttons[1] ) {
00055                 valid = false;
00056         }
00057 }
00058 
00059 void Joystick::initialize()
00060 {
00061         tJoystickTopic = addOption<std::string>("tJoystickTopic",
00062                         "JoystickTopic that published sensor_msgs::Joy",
00063                         "/TeleKyb/tJoy/joy", false, false);
00064 
00065         tJoystickUsePositionMode = addOption("tJoystickUsePositionMode",
00066                         "Integrates Position from Velocity input.", true, false, false);
00067         tJoystickYawRateScale = addOption("tJoystickYawRateScale",
00068                         "Commanded Yaw Rate is scaled to this value", 1.0, false ,false);
00069 
00070         tJoystickUseRelativeMode = addOption("tJoystickUseRelativeMode",
00071                         "Enable this to have all inputs interpreted w.r.t. the local frame.", false, false, true);
00072         tJoystickUseDeadManSwitch = addOption("tJoystickUseDeadManSwitch",
00073                         "Disable this to fly without pressing a special button simultaneously.", true, false, true);
00074 
00075         nodeHandle = ROSModule::Instance().getMainNodeHandle();
00076 
00077         // no Parameters
00078         //parameterInitialized = true;
00079 }
00080 
00081 void Joystick::destroy()
00082 {
00083 
00084 }
00085 
00086 bool Joystick::willBecomeActive(const TKState& currentState, const Behavior& previousBehavior)
00087 {
00088         joySub = nodeHandle.subscribe(tJoystickTopic->getValue()
00089                         , 10, &Joystick::joystickCB, this);
00090 
00091         lastVelocityInput = Velocity3D::Zero();
00092         lastYawRateInput = 0.0;
00093 
00094         if (tJoystickUsePositionMode->getValue()) {
00095                 posModeCurPosition = currentState.position;
00096                 posModeCurYawAngle = currentState.getEulerRPY()(2);
00097                 posModeLastInputTime = Time();
00098         }
00099 
00100         valid = true;
00101 
00102         return true;
00103 }
00104 
00105 void Joystick::didBecomeActive(const TKState& currentState, const Behavior& previousBehavior)
00106 {
00107         // not used
00108 }
00109 
00110 void Joystick::willBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00111 {
00112         // not used
00113 }
00114 
00115 void Joystick::didBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00116 {
00117         joySub.shutdown();
00118 }
00119 
00120 // called everytime a new TKState is available AND it is the NEW Behavior of an active Switch
00121 void Joystick::trajectoryStepCreation(const TKState& currentState, TKTrajectory& generatedTrajInput)
00122 {
00123         generatedTrajInput.setVelocity(lastVelocityInput);
00124         generatedTrajInput.setYawRate(lastYawRateInput);
00125 }
00126 
00127 // called everytime a new TKState is available. Should return false if invalid (swtich to next behavior, or Hover if undef).
00128 void Joystick::trajectoryStepActive(const TKState& currentState, TKTrajectory& generatedTrajInput)
00129 {
00130         //      ROS_INFO_STREAM("InputVel: " << std::endl << lastVelocityInput);
00131         //      ROS_INFO_STREAM("InputYawRate: " << lastYawRateInput);
00132 
00133                 // set Velocity Mode
00134                 if (tJoystickUsePositionMode->getValue()) {
00135                         // integrate Position
00136                         double timeDiffSec = (Time() - posModeLastInputTime).toDSec();
00137         //              ROS_INFO("timeDiffSec: %f", timeDiffSec);
00138                         posModeLastInputTime = Time();
00139                         if (tJoystickUseRelativeMode->getValue()) {
00140                                 //all is w.r.t. local frame
00141                                 Eigen::Vector2d partial = currentState.orientation.toRotationMatrix().block<2,2>(0,0) * lastVelocityInput.block<2,1>(0,0);
00142                                 Eigen::Vector3d newVel(partial(0), partial(1), lastVelocityInput(2));
00143                                 posModeCurPosition += (newVel * timeDiffSec);
00144                         } else {
00145                                 //all is w.r.t. world frame
00146                                 posModeCurPosition += (lastVelocityInput * timeDiffSec);
00147                         }
00148 
00149                         posModeCurYawAngle += (lastYawRateInput * timeDiffSec);
00150 
00151         //              ROS_INFO_STREAM("Position: " << std::endl << posModeCurPosition);
00152         //              ROS_INFO_STREAM("YawAngle: " << posModeCurYawAngle.dCast());
00153 
00154                         if (tJoystickUseRelativeMode->getValue()) {
00155                                 //all is w.r.t. local frame
00156                                 Eigen::Vector2d partial = currentState.orientation.toRotationMatrix().block<2,2>(0,0) * lastVelocityInput.block<2,1>(0,0);
00157                                 Eigen::Vector3d newVel(partial(0), partial(1), lastVelocityInput(2));
00158                                 generatedTrajInput.setPosition(posModeCurPosition,newVel);
00159                                 generatedTrajInput.setYawAngle(posModeCurYawAngle.dCastPi(0), lastYawRateInput);
00160                         } else {
00161                                 //all is w.r.t. world frame
00162                                 generatedTrajInput.setPosition(posModeCurPosition, lastVelocityInput);
00163                                 generatedTrajInput.setYawAngle(posModeCurYawAngle.dCastPi(0), lastYawRateInput);
00164                         }
00165 
00166                 } else {
00167                         if (tJoystickUseRelativeMode->getValue()) {
00168                                 //all is w.r.t. local frame
00169                                 Eigen::Vector2d partial = currentState.orientation.toRotationMatrix().block<2,2>(0,0) * lastVelocityInput.block<2,1>(0,0);
00170                                 Eigen::Vector3d newVel(partial(0), partial(1), lastVelocityInput(2));
00171                                 generatedTrajInput.setVelocity(newVel);
00172                                 generatedTrajInput.setYawRate(lastYawRateInput);
00173                         } else{
00174                                 //all is w.r.t. world frame
00175                                 generatedTrajInput.setVelocity(lastVelocityInput);
00176                                 generatedTrajInput.setYawRate(lastYawRateInput);
00177                         }
00178                 }
00179 }
00180 
00181 // called everytime a new TKState is available AND it is the OLD Behavior of an active Switch
00182 void Joystick::trajectoryStepTermination(const TKState& currentState, TKTrajectory& generatedTrajInput)
00183 {
00184         generatedTrajInput.setVelocity( Velocity3D::Zero() );
00185         generatedTrajInput.setYawRate( 0.0 );
00186 }
00187 
00188 // Return true if the active Behavior is (still) valid. Initiate Switch otherwise
00189 bool Joystick::isValid(const TKState& currentState) const
00190 {
00191         // never turns invalid
00192         return valid;
00193 }
00194 
00195 
00196 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


tk_be_common
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:14:29