00001 #include <vigir_pattern_generator/joystick_handler.h>
00002
00003 namespace vigir_footstep_planning
00004 {
00005 JoystickHandler::JoystickHandler(ros::NodeHandle& nh)
00006 : enable_generator(false)
00007 {
00008
00009 nh.param("joystick/thresh_x", thresh_lin.x, 0.0);
00010 nh.param("joystick/thresh_y", thresh_lin.y, 0.0);
00011 nh.param("joystick/thresh_yaw", thresh_rot.z, 0.0);
00012
00013
00014 nh.param("joystick/sensivity_x", sensivity_lin.x, 0.0);
00015 nh.param("joystick/sensivity_y", sensivity_lin.y, 0.0);
00016 nh.param("joystick/sensivity_yaw", sensivity_rot.z, 0.0);
00017
00018
00019 nh.param("joystick/min_vel_x", limits_min_lin_vel.x, 0.0);
00020 nh.param("joystick/max_vel_x", limits_max_lin_vel.x, 0.0);
00021 nh.param("joystick/min_vel_y", limits_min_lin_vel.y, 0.0);
00022 nh.param("joystick/max_vel_y", limits_max_lin_vel.y, 0.0);
00023 nh.param("joystick/min_vel_yaw", limits_min_rot_vel.z, 0.0); limits_min_rot_vel.z *= (M_PI / 180.0);
00024 nh.param("joystick/max_vel_yaw", limits_max_rot_vel.z, 0.0); limits_max_rot_vel.z *= (M_PI / 180.0);
00025
00026
00027 nh.param("joystick/min_acc_x", limits_min_lin_acc.x, 0.0);
00028 nh.param("joystick/max_acc_x", limits_max_lin_acc.x, 0.0);
00029 nh.param("joystick/min_acc_y", limits_min_lin_acc.y, 0.0);
00030 nh.param("joystick/max_acc_y", limits_max_lin_acc.y, 0.0);
00031 nh.param("joystick/min_acc_yaw", limits_min_rot_acc.z, 0.0); limits_min_rot_acc.z *= (M_PI / 180.0);
00032 nh.param("joystick/max_acc_yaw", limits_max_rot_acc.z, 0.0); limits_max_rot_acc.z *= (M_PI / 180.0);
00033
00034 joy_sub = nh.subscribe("/joy", 1,& JoystickHandler::joyCallback, this);
00035 }
00036
00037 JoystickHandler::~JoystickHandler()
00038 {
00039 }
00040
00041 void JoystickHandler::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
00042 {
00043 last_joy_msg = joy;
00044
00045 if (last_joy_msg->buttons.size() < 8)
00046 {
00047 ROS_ERROR_THROTTLE(1.0, "Couldn't read the correct buttons. Maybe wrong controller connected?");
00048 return;
00049 }
00050
00051
00052 if (last_joy_msg->buttons[7])
00053 enable_generator = true;
00054 else if (last_joy_msg->buttons[6])
00055 enable_generator = false;
00056 }
00057
00058 void JoystickHandler::updateJoystickCommands(double elapsed_time_sec, bool& enable, double& d_x, double& d_y, double& d_yaw) const
00059 {
00060 if (!last_joy_msg)
00061 {
00062 ROS_ERROR("Didn't get joystick input sor far. Check if joystick is connected and joy node running!");
00063 return;
00064 }
00065
00066 if (last_joy_msg->axes.size() < 5)
00067 {
00068 ROS_ERROR("Couldn't read the correct axes. Maybe wrong controller connected?");
00069 return;
00070 }
00071
00072 enable = enable_generator;
00073
00074
00075 double joy_x = last_joy_msg->axes[4];
00076 joy_x = std::abs(joy_x) > thresh_lin.x ? joy_x : 0.0;
00077 updateJoystickCommand(elapsed_time_sec, joy_x, d_x, limits_min_lin_vel.x, limits_max_lin_vel.x, limits_min_lin_acc.x, limits_max_lin_acc.x, sensivity_lin.x);
00078
00079
00080 double joy_y = last_joy_msg->axes[0];
00081 joy_y = std::abs(joy_y) > thresh_lin.y ? joy_y : 0.0;
00082 updateJoystickCommand(elapsed_time_sec, joy_y, d_y, limits_min_lin_vel.y, limits_max_lin_vel.y, limits_min_lin_acc.y, limits_max_lin_acc.y, sensivity_lin.y);
00083
00084
00085 double joy_yaw = last_joy_msg->axes[3];
00086 joy_yaw = std::abs(joy_yaw) > thresh_rot.z ? joy_yaw : 0.0;
00087 if (joy_x < 0.0)
00088 joy_yaw = -joy_yaw;
00089 updateJoystickCommand(elapsed_time_sec, joy_yaw, d_yaw, limits_min_rot_vel.z, limits_max_rot_vel.z, limits_min_rot_acc.z, limits_max_rot_acc.z, sensivity_rot.z);
00090
00091
00092 }
00093
00094
00095 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
00096 {
00097
00098 double acc = convertJoyAxisToAcc(elapsed_time_sec, joy_val, val, min_vel, max_vel) * sensivity;
00099 acc = std::min(max_acc, std::max(min_acc, acc));
00100
00101
00102 val += acc;
00103 val = std::min(max_vel, std::max(min_vel, val));
00104 }
00105
00106 double JoystickHandler::convertJoyAxisToAcc(double elapsed_time_sec, double joy_val, double val, double min_vel, double max_vel) const
00107 {
00108
00109 if (val >= 0.0)
00110 return (joy_val*std::abs(max_vel) - val)*elapsed_time_sec;
00111 else
00112 return (joy_val*std::abs(min_vel) - val)*elapsed_time_sec;
00113 }
00114 }