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

Design advice for executing sequence of ros msg interactions

asked 2017-11-03 13:06:55 -0500

knxa gravatar image

updated 2017-11-03 14:01:51 -0500

I am struggling with a good design for nodes that when receiving a callback must perform a sequence of interactions with one or more other ros nodes.

So far I ended up mixing spin and spinOnce but something tells me that this is far from optimal. Here is some pseudo code of what I am trying to achieve:

void actionCallback() {

    // want to write some logic in sequential style,
    // but the sequence involves multiple ros message send/receive:
    result1 = rosService1();
    result2 = rosService2();
    result3 = waitForSomeMessageOnTopic();

    // ...
}

Result rosService1() {

    // This my attempt of simulating a synchronous call to a ros service
    ... call service 1

    while (response from service1 not received) {
        spin_once();
        ... sleep a bit
    }

    ... return result from service1 response
}

int main() {
    ...
    startActionServer();
    spin();
}

I guess this design challenge has some better well-proven solutions. Please advice.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-11-04 05:53:59 -0500

gvdhoorn gravatar image

updated 2017-11-04 06:24:02 -0500

So far I ended up mixing spin and spinOnce but something tells me that this is far from optimal.

Yes. Please try to not do that.

I am struggling with a good design for nodes that when receiving a callback must perform a sequence of interactions with one or more other ros nodes. [..] I guess this design challenge has some better well-proven solutions

I would say this is coordination, for which one of the accepted approaches is using state machines (or state charts, behaviour trees, or similar formalisms). State machines are really well suited for modelling "sequence[s] of interactions" based on state (ie: events).

See #q273811 for a similar question (and the answer to that).

edit flag offensive delete link more

Comments

Yes. Please try to not do that

But really, why not? I find it easy to read and debug. Is there anything technically wrong with calling spin_once in a callback, using it as a "yield"? I am using SMACH for coordination at the higher "task" level, but at this level, SMACH seems like overkill.

knxa gravatar image knxa  ( 2017-11-04 10:39:40 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-03 13:06:55 -0500

Seen: 347 times

Last updated: Nov 04 '17