joystick_handler.cpp
Go to the documentation of this file.
00001 #include <vigir_pattern_generator/joystick_handler.h>
00002 
00003 
00004 
00005 namespace vigir_footstep_planning
00006 {
00007 JoystickHandler::JoystickHandler(ros::NodeHandle& nh)
00008   : has_joy_msg_(false)
00009 {
00010   XmlRpc::XmlRpcValue params;
00011 
00012   if (nh.getParam("joystick", params))
00013   {
00014     initJoystickInput(params, "x", x_axis_);
00015     initJoystickInput(params, "y", y_axis_);
00016     initJoystickInput(params, "yaw", yaw_axis_);
00017     initJoystickInput(params, "enable", enable_);
00018   }
00019   else
00020     ROS_ERROR("[JoystickHandler] Input parameters missing in config!");
00021 
00022   joy_sub_ = nh.subscribe("joy", 1,& JoystickHandler::joyCallback, this);
00023 }
00024 
00025 JoystickHandler::~JoystickHandler()
00026 {
00027 }
00028 
00029 void JoystickHandler::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
00030 {
00031   if (x_axis_)
00032     x_axis_->joyCallback(joy);
00033 
00034   if (y_axis_)
00035     y_axis_->joyCallback(joy);
00036 
00037   if (yaw_axis_)
00038     yaw_axis_->joyCallback(joy);
00039 
00040   if (enable_)
00041     enable_->joyCallback(joy);
00042 
00043   has_joy_msg_ = true;
00044 }
00045 
00046 void JoystickHandler::getJoystickCommand(geometry_msgs::Twist& twist, bool& enable) const
00047 {
00048   twist = geometry_msgs::Twist();
00049 
00050   if (!has_joy_msg_)
00051   {
00052     enable = false;
00053     return;
00054   }
00055 
00056   if (x_axis_ && x_axis_->isPressed())
00057     twist.linear.x = x_axis_->getValue();
00058 
00059   if (y_axis_ && y_axis_->isPressed())
00060     twist.linear.y = y_axis_->getValue();
00061 
00062   if (yaw_axis_ && yaw_axis_->isPressed())
00063     twist.angular.z = yaw_axis_->getValue();
00064 
00065   if (enable_)
00066     enable = enable_->isPressed();
00067 }
00068 
00069 void JoystickHandler::initJoystickInput(XmlRpc::XmlRpcValue& params, std::string name, JoystickInputHandle::Ptr& handle) const
00070 {
00071   if (params.hasMember(name))
00072   {
00073     XmlRpc::XmlRpcValue& input_params = params[name];
00074 
00075     if (input_params.hasMember("button"))
00076       handle.reset(new JoystickButton(input_params));
00077     else if (input_params.hasMember("axis"))
00078       handle.reset(new JoystickAxis(input_params));
00079     else
00080       ROS_ERROR("[JoystickHandler] Couldn't handle '%s' input type!", name.c_str());
00081   }
00082   else
00083     ROS_ERROR("[JoystickHandler] '%s' input parameter missing in config!", name.c_str());
00084 }
00085 
00086 void JoystickHandler::updateJoystickCommand(double elapsed_time_sec, double joy_val, double& val, double min_vel, double max_vel, double min_acc, double max_acc, double sensivity) const
00087 {
00088   // determine acceleration
00089   double acc = convertJoyAxisToAcc(elapsed_time_sec, joy_val, val, min_vel, max_vel) * sensivity;
00090   acc = std::min(max_acc, std::max(min_acc, acc));
00091 
00092   // update value
00093   val = std::min(max_vel, std::max(min_vel, val+acc));
00094 }
00095 
00096 double JoystickHandler::convertJoyAxisToAcc(double elapsed_time_sec, double joy_val, double val, double min_vel, double max_vel) const
00097 {
00098   // using current val to determine joy_val scale
00099   if (val >= 0.0)
00100     return (joy_val*std::abs(max_vel) - val)*elapsed_time_sec;
00101   else
00102     return (joy_val*std::abs(min_vel) - val)*elapsed_time_sec;
00103 }
00104 }


vigir_pattern_generator
Author(s): Alexander Stumpf
autogenerated on Thu Jun 6 2019 18:38:16