0
$\begingroup$

Rosanswers logo

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");
}
}

Originally posted by Marcofon on ROS Answers with karma: 17 on 2016-11-30

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by mgruhler with karma: 12390 on 2016-11-30

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Marcofon on 2016-11-30:
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");
}

Comment by Marcofon on 2016-11-30:
@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.

Comment by mgruhler on 2016-11-30:
check out the tutorial: http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber(c%2B%2B), your callback is also not correct.

Comment by Marcofon on 2016-11-30:
@mig I have already checked it before posting. Can you help me with the code? If you can, of course. Thank you again.

Comment by mgruhler on 2016-12-01:
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

Comment by mgruhler on 2016-12-01:
adapt this accordingly for whatever your message you use. Then, implement your logic in whatever way you like...

Comment by Marcofon on 2016-12-01:
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");
}
}

Comment by Marcofon on 2016-12-01:
...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

Comment by mgruhler on 2016-12-01:
try if (msg->data == 0).

You are just creating an uninitalized bool data but don't assign msg->data to it.

Comment by Marcofon on 2016-12-01:
Yes, i was writing to you right now! I made the exact change that you suggested to me before seeing your new comment! Thank you very much indeed! I'll post the solution as an edit to my question. Thank you again @mig !!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.