How do i use the variables from the joyCallback function in the OpenCVCallback?

asked 2017-11-30 02:01:12 -0500

ldw30 gravatar image

updated 2017-12-05 00:16:28 -0500

I'm not too familiar with programming and C++ so i apologise in advance for any misinformation. Using the turtlesim Teleoperation Node with joystick from http://wiki.ros.org/joy/Tutorials/Wri... , I intend to change the movements of the turtlesim based on the value of msg->data from the OpenCVCallback function. The msg->data is a integer value.

The intended outcome is such that when msg->data is more than 200, the turtlesim will not be able to move using the joystick control.

Is there a way where i can make use of the variables like twist.angular.z from the joyCallback function inside the OpenCVCallback function? The code will be something like this :

void TeleopTurtle::OpenCVCallback(const std_msgs::Int16::ConstPtr& msg)
{


  ROS_INFO("I heard [%d]", msg->data );
  if( (msg->data) >=200 )
{   

    twist.angular.z = 0;
    twist.linear.x = 0;
//the turtlesim will not be able to move using the joystick

}

Code

#nclude <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/Joy.h>
#include <std_msgs/Int16.h>


int a;
class TeleopTurtle
{
public:
  TeleopTurtle();

private:
  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
  void OpenCVCallback(const std_msgs::Int16::ConstPtr& msg);

  ros::NodeHandle nh_;

  int linear_, angular_;
  double l_scale_, a_scale_;
  ros::Publisher vel_pub_;
  ros::Subscriber joy_sub_;
  ros::Subscriber opencv_sub_;
  ros::Subscriber twist_sub_;


};


TeleopTurtle::TeleopTurtle():   
  linear_(1),
  angular_(2)
{

  nh_.param("axis_linear", linear_, linear_);
  nh_.param("axis_angular", angular_, angular_);
  nh_.param("scale_angular", a_scale_, a_scale_);
  nh_.param("scale_linear", l_scale_, l_scale_);


  vel_pub_ = nh_.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1);


  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopTurtle::joyCallback, this);

  opencv_sub_ = nh_.subscribe<std_msgs::Int16>("chatter", 10, &TeleopTurtle::OpenCVCallback, this);





}

void TeleopTurtle::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{  
  geometry_msgs::Twist twist;
  twist.angular.z = a_scale_*joy->axes[angular_];
  twist.linear.x = l_scale_*joy->axes[linear_];
  a = twist.angular.z ;
  vel_pub_.publish(twist);
}

void TeleopTurtle::OpenCVCallback(const std_msgs::Int16::ConstPtr& msg)
{


  ROS_INFO("I heard [%d]", msg->data );
  if( (msg->data) >=200 )
{   

//the turtlesim will not be able to move using the joystick

}

}


int main(int argc, char** argv)
{
  ros::init(argc, argv, "teleop_turtle");
  TeleopTurtle teleop_turtle;



  ros::spin();
}

Here's what i tried:

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/Joy.h>
#include <std_msgs/Int16.h>



class TeleopTurtle
{
public:
  TeleopTurtle();

private:
  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
  void OpenCVCallback(const std_msgs::Int16::ConstPtr& msg);

  ros::NodeHandle nh_;

  int linear_, angular_;
  double l_scale_, a_scale_;
  ros::Publisher vel_pub_;
  ros::Subscriber joy_sub_;
  ros::Subscriber opencv_sub_;
  ros::Subscriber twist_sub_;


};


TeleopTurtle::TeleopTurtle():   
  linear_(1),
  angular_(2)
{

  nh_.param("axis_linear", linear_, linear_);
  nh_.param("axis_angular", angular_, angular_);
  nh_.param("scale_angular", a_scale_, a_scale_);
  nh_.param("scale_linear", l_scale_, l_scale_);


  vel_pub_ = nh_.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1);


  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopTurtle::joyCallback, this);

  opencv_sub_ = nh_.subscribe<std_msgs::Int16>("chatter", 10, &TeleopTurtle::OpenCVCallback, this);





}

void TeleopTurtle::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{  
  geometry_msgs::Twist twist;
  TeleopTurtle::twist.angular.z = a_scale_*joy->axes[angular_];
  TeleopTurtle::twist.linear.x = l_scale_*joy->axes[linear_];
  vel_pub_.publish(twist);
}

void TeleopTurtle::OpenCVCallback(const std_msgs::Int16::ConstPtr& msg)
{


  ROS_INFO("I heard [%d]", msg->data ...
(more)
edit retag flag offensive close merge delete

Comments

If you want to use data from one callback in another callback then you should use a member variable. Set the member variable in one callback and use it in the other.

jayess gravatar image jayess  ( 2017-12-02 14:07:35 -0500 )edit

Hi, sorry for the late response. I tried using the member variable as you mentioned but it did not work. Is it possible if you can maybe show me an example of how it's done with callback functions? I'm sorry if it's not allowed here as i have tried googling but to no avail.

ldw30 gravatar image ldw30  ( 2017-12-04 10:09:08 -0500 )edit

Please update your question with what you have tried. Maybe we can tweak it.

jayess gravatar image jayess  ( 2017-12-04 11:03:48 -0500 )edit

I've edited my post on what I've tried. Is this how you make use of the member variable? I googled and found out that there's a few ways you can do it although the examples are not using callback functions.

ldw30 gravatar image ldw30  ( 2017-12-05 00:17:21 -0500 )edit