ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Is it possible to use a certain data value of a topic for executing some code?

asked 2016-11-30 08:52:26 -0500

Marcofon gravatar image

updated 2016-12-01 07:29:30 -0500

Hello, i will try to be as clear as possible. I have a ROS topic in which are published only 0 and 1. In another node i have a subscriber that is subscribed to that topic. Then i had an idea to implement a callback function, that as some code to execute inside, that is called by the subscriber. Let's say that every ten data valued 1 a data valued 0 will show up in the topic. So my question is, is it possible to execute the code inside the callback function only when the subscriber reads 0 in the topic? I mean with a sort of if(...) {...} mechanism or something similar. I apologize if it's not clear, i can provide other details or explanations; just let me know. Thank you in advance!

EDIT: here is the correct code of the callback function that i mentioned in the comment:

void orderCallback(const std_msgs::Bool::ConstPtr& msg)
{

if (msg->data == 0)
{
chdir("/home/marco/catkin_ws/src/heart_rate_monitor");

system("get_hrv -R my_data_file.txt >doc.txt");
}
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-11-30 09:18:52 -0500

mgruhler gravatar image

Yes. A callback is just a function. Inside this you can do whatever you want. Just check the data and use an if clause.

edit flag offensive delete link more

Comments

Thank you for your reply! Since i don't know how to check the topic values, can you please try to explain it to me? Below there is the code:

void orderCallback()
{
chdir("/home/marco/catkin_ws/src/heart_rate_monitor");

system("get_hrv -R my_data_file.txt >doc.txt");
}
Marcofon gravatar image Marcofon  ( 2016-11-30 09:28:21 -0500 )edit

@mig This is the subscriber:

ros::Subscriber sub = n.subscribe("semaphore", 1000, orderCallback);

And, as i said in the question, i have only boolean values.

Marcofon gravatar image Marcofon  ( 2016-11-30 09:30:03 -0500 )edit

check out the tutorial: http://wiki.ros.org/ROS/Tutorials/Wri... , your callback is also not correct.

mgruhler gravatar image mgruhler  ( 2016-11-30 10:11:11 -0500 )edit

@mig I have already checked it before posting. Can you help me with the code? If you can, of course. Thank you again.

Marcofon gravatar image Marcofon  ( 2016-11-30 10:18:22 -0500 )edit
1

What kind of message do you have? this needs to go in the callback function definition. See l.34 of the listener.cpp in the tutorial. Then, depending on the fields of the message, you can access this as shown in l.36 msg->data. But note, that the String.msg has a field called data. You need to

mgruhler gravatar image mgruhler  ( 2016-12-01 01:50:17 -0500 )edit

adapt this accordingly for whatever your message you use. Then, implement your logic in whatever way you like...

mgruhler gravatar image mgruhler  ( 2016-12-01 01:50:53 -0500 )edit

Thank you for your reply! I tried something like this:

void orderCallback(const std_msgs::Bool::ConstPtr& msg)
{
bool data;
msg->data;
if (data = 0)
{
chdir("/home/marco/catkin_ws/src/heart_rate_monitor");
system("get_hrv -R my_data_file.txt >doc.txt");
}
}
Marcofon gravatar image Marcofon  ( 2016-12-01 04:51:59 -0500 )edit

...i tried to adapt the callback of the tutorial to my case. Anyway, it doesn't seem to work. What i'm missing? P.S. is data == 0 in my real code, it was only a typo in the comment. Thank you again @mig

Marcofon gravatar image Marcofon  ( 2016-12-01 04:55:21 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-11-30 08:52:26 -0500

Seen: 125 times

Last updated: Dec 01 '16