Joy_node How do I access buttons?[Solved]
Hi I have x-box controller connected and topic echo's just fine.
I can even drive my bot around > teleop_twist_joy. So now I want to use the buttons on joy stick to move a servo and switch lights on/off.
I started a node witch compiles just fine but I am stuck on subscribing to joy sensor mseg. If I can get to the buttons info I would like to publish to "servo" topic to move servo (value from 0-180) ---later
For know just need to access joy_node.
#include <ros/ros.h>
#include <sensor_msgs/Joy.h> // use to subscribe joy topic
#include <std_msgs/UInt8.h> //use to publish to servo topic
void joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{
// if (msg.buttons[0] == 1) {
// ROS_INFO("Button 0 pressed...");
//}
//std::cout<<"axes: ["<< joy->axes[0] <<","<<joy->axes[1] <<","<<joy->axes[2] <<"<<"]"<<std::endl;
//std::cout<<"buttons: ["<< joy->buttons[0] <<","<< joy->buttons[1] <<ns[ <<"]"<<std::endl;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "servo_controle_node");
ros::NodeHandle n("~");
ros::Rate loop_rate(50);
ros::Subscriber sub = n.subscribe("joy", 10, joyCallback);
ROS_INFO_ONCE("print out test");
//joyCallback(); //read joystick buttons
while (ros::ok())
{
ros::spinOnce();
loop_rate.sleep();
}
}
So if I can get the this= ros::Subscriber sub = n.subscribe("joy", 10, joyCallback); write then maybe I can access the buttons in the joyCallback function. Trolled the net with google and the "remote teleop node" or "simple plublisher/sebscriber node" not helping much
Update: Thanks Ravi Joshi your help apreciated. I solved it before I got back to update this post. so the new code....
#include <ros/ros.h>
#include <sensor_msgs/Joy.h> // use to subscribe joy topic
#include <std_msgs/UInt8.h> //use to publish to servo topic
/*void stickCallback (const sensor_msgs::Joy::ConstPtr& msg)
{
for (unsigned i = 0; i < msg->axes.size(); ++i)
{
ROS_INFO("Axis %d is now at position %f", i, msg->axes[i]);
}
}*/
/*void buttonCallback (const sensor_msgs::Joy::ConstPtr& msg)
{
for (unsigned i = 0; i < msg->buttons.size(); ++i) {
ROS_INFO("button %d is now at position %d", i, msg->buttons[i]);
}
}*/
void servo_moveCallback(const sensor_msgs::Joy::ConstPtr &msg) //while loop dont play nice
{
//int a = msg->buttons[2];
int b = msg->buttons[1];
if (msg->buttons[2] == 1)
{
ROS_INFO("panning servo left publish");
}
else if ( b == 1 )
{
ROS_INFO("panning servo right publish");
}
}
/***********************************************************************************
created while sorting out left and right > keeping it around for up/down tilt
**********************************************************************************/
void servo2_moveCallback(const sensor_msgs::Joy::ConstPtr &msg)
{
if (msg->buttons[3] == 1)
{
ROS_INFO("tilting servo up publish");
}
else if ( msg->buttons[0]== 1 )
{
ROS_INFO("tilting servo down publish");
}
}
/**********************************************************************************/
int main(int argc, char **argv)
{
ros::init(argc, argv, "servo_controle_node");
ros::NodeHandle nh;
ros::Rate loop_rate(50);
// ros::Subscriber sub = nh.subscribe("joy",10, stickCallback); //test function to access joy sticks
// ros::Subscriber sub2 = nh.subscribe("joy",10, buttonCallback); //test function to access buttons
ros::Subscriber sub3 = nh.subscribe("joy", 10, servo_moveCallback);
ros::Subscriber sub4 = nh.subscribe("joy",10, servo2_moveCallback);
ROS_INFO_ONCE("Servo_controle_node Started");
while (ros::ok())
{
ros::spinOnce();
loop_rate.sleep();
}
}
Ok, so first two functions (commented out ...