ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

Joy_node How do I access buttons?[Solved]

asked 2022-07-29 01:10:24 -0600

TK hobby gravatar image

updated 2022-07-31 22:18:03 -0600

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 ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-29 01:36:18 -0600

ravijoshi gravatar image

updated 2022-07-30 09:49:47 -0600

You are on the right track.

  1. You need to create a subscriber as you have done it already. However, please note that the callback function will be called automatically upon receiving data from your joystick. Therefore, you don't need to call the callback function by yourself.
  2. Next, you can look up the message definition of sensor_msgs/Joy. Finally, the buttons and axes array contains button measurements and axes measurements from a joystick, respectively. In the past, I found the mappings of buttons and axes by pressing each of them one by one while echoing the joy topic.
  3. Once you find the correct mapping, you should trigger the lights on/off, etc.

Update

The message you are receiving is a pointer type. The buttons and axes are array variables. So you can iterate over them to find their mappings. See below the code snippet:

for(const auto & button : joy->buttons){
    // you can print button here
}

Once you find the correct mapping, say you are interested in a button at index 1, use a conditional statement as shown below:

auto button = joy->buttons[1]
if (button == 1){
    // trigger light on here
}
edit flag offensive delete link more

Comments

Had a look and visited that page in my extensive googling. It only tells me that the "int32[] buttons", So.. I should a=int32[0] buttons or do I create an aray/list equel to int32[]buttons

Can you point me to a tutorial where they used buttons?

TK hobby gravatar image TK hobby  ( 2022-07-29 02:18:57 -0600 )edit

I updated my answer. Please see the update section. For tutorials, it is always advised to check official wiki page. Please feel free to look at http://wiki.ros.org/joy and http://wiki.ros.org/joystick_drivers/...

ravijoshi gravatar image ravijoshi  ( 2022-07-30 09:53:27 -0600 )edit

It is advised to click on "up vote" and "accept answer" instead of editing the title saying "[Solved]"

ravijoshi gravatar image ravijoshi  ( 2022-08-01 03:43:28 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-07-29 01:10:24 -0600

Seen: 105 times

Last updated: Jul 31