00001
00002
00003
00004
00005
00006
00007
00008 #include "Joystick.hpp"
00009
00010 #include <telekyb_base/ROS.hpp>
00011
00012
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
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
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
00044 lastYawRateInput = msg->axes[3] * tJoystickYawRateScale->getValue();
00045 }
00046 } else {
00047
00048 lastVelocityInput = Velocity3D::Zero();
00049 lastYawRateInput = 0.0;
00050 }
00051
00052
00053
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
00078
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
00108 }
00109
00110 void Joystick::willBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00111 {
00112
00113 }
00114
00115 void Joystick::didBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00116 {
00117 joySub.shutdown();
00118 }
00119
00120
00121 void Joystick::trajectoryStepCreation(const TKState& currentState, TKTrajectory& generatedTrajInput)
00122 {
00123 generatedTrajInput.setVelocity(lastVelocityInput);
00124 generatedTrajInput.setYawRate(lastYawRateInput);
00125 }
00126
00127
00128 void Joystick::trajectoryStepActive(const TKState& currentState, TKTrajectory& generatedTrajInput)
00129 {
00130
00131
00132
00133
00134 if (tJoystickUsePositionMode->getValue()) {
00135
00136 double timeDiffSec = (Time() - posModeLastInputTime).toDSec();
00137
00138 posModeLastInputTime = Time();
00139 if (tJoystickUseRelativeMode->getValue()) {
00140
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
00146 posModeCurPosition += (lastVelocityInput * timeDiffSec);
00147 }
00148
00149 posModeCurYawAngle += (lastYawRateInput * timeDiffSec);
00150
00151
00152
00153
00154 if (tJoystickUseRelativeMode->getValue()) {
00155
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
00162 generatedTrajInput.setPosition(posModeCurPosition, lastVelocityInput);
00163 generatedTrajInput.setYawAngle(posModeCurYawAngle.dCastPi(0), lastYawRateInput);
00164 }
00165
00166 } else {
00167 if (tJoystickUseRelativeMode->getValue()) {
00168
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
00175 generatedTrajInput.setVelocity(lastVelocityInput);
00176 generatedTrajInput.setYawRate(lastYawRateInput);
00177 }
00178 }
00179 }
00180
00181
00182 void Joystick::trajectoryStepTermination(const TKState& currentState, TKTrajectory& generatedTrajInput)
00183 {
00184 generatedTrajInput.setVelocity( Velocity3D::Zero() );
00185 generatedTrajInput.setYawRate( 0.0 );
00186 }
00187
00188
00189 bool Joystick::isValid(const TKState& currentState) const
00190 {
00191
00192 return valid;
00193 }
00194
00195
00196 }