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 !!