61
ROS Crash-Course, Part II ROS Design Patterns, C++ APIs, and Best Practices Jonathan Bohren With some information and figures adapted from http: // www. ros. org

ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Crash-Course, Part IIROS Design Patterns, C++ APIs, and Best Practices

Jonathan BohrenWith some information and figures adapted from http: // www. ros. org

Page 2: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Outline

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 2 / 20

Page 3: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Outline

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 2 / 20

Page 4: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Outline

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design Patterns

Datagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 2 / 20

Page 5: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Outline

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 2 / 20

Page 6: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Outline

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 2 / 20

Page 7: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Outline (revisted)

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 3 / 20

Page 8: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Reasons to Factor CodeSplitting code into more packages

Modularity / Re-usabilityOften, code can be useful in contexts other than those for which itwas built. Without splitting code the right way, the dependency graphwill have cycles and be invalid.

Dependency MinimizationIt is best to separate out the smallest unit of code that might be usedas a dependency. This pattern is often used for separating interfacedefinitions like .msg and .srv files into their own packages.Wrapper PackagesROS’s package system can be used as a lightweight way of integratingthird-party software that cannot be acquired through a system’spackage manager.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 4 / 20

Page 9: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Reasons to Factor CodeSplitting code into more packages

Modularity / Re-usabilityOften, code can be useful in contexts other than those for which itwas built. Without splitting code the right way, the dependency graphwill have cycles and be invalid.Dependency MinimizationIt is best to separate out the smallest unit of code that might be usedas a dependency. This pattern is often used for separating interfacedefinitions like .msg and .srv files into their own packages.

Wrapper PackagesROS’s package system can be used as a lightweight way of integratingthird-party software that cannot be acquired through a system’spackage manager.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 4 / 20

Page 10: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Reasons to Factor CodeSplitting code into more packages

Modularity / Re-usabilityOften, code can be useful in contexts other than those for which itwas built. Without splitting code the right way, the dependency graphwill have cycles and be invalid.Dependency MinimizationIt is best to separate out the smallest unit of code that might be usedas a dependency. This pattern is often used for separating interfacedefinitions like .msg and .srv files into their own packages.Wrapper PackagesROS’s package system can be used as a lightweight way of integratingthird-party software that cannot be acquired through a system’spackage manager.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 4 / 20

Page 11: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Reasons to Unify CodeCollecting code into fewer packages

Application-specific CodeIf a collection of packages are so application-specific that they cannotbe used separately.

Rapid Development for Experimenting and PrototypingSometimes experimental or “hacked”-together code shouldn’t beover-engineered into a number of packages during initial development.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 5 / 20

Page 12: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Reasons to Unify CodeCollecting code into fewer packages

Application-specific CodeIf a collection of packages are so application-specific that they cannotbe used separately.Rapid Development for Experimenting and PrototypingSometimes experimental or “hacked”-together code shouldn’t beover-engineered into a number of packages during initial development.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 5 / 20

Page 13: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

When to Make a StackPackaging code for distribution

ROS stacks are best made when there are a collection of packages that,while not necessarily depending on each-other, are useful for a commonpurpose, act as companions to some other package, or make up thecomponents of an application.

Some examples of stacks include:

laser_pipeline - packages for grabbing and manipulating laser datarobot_model - packages for modeling a rigid robotros_comm - packages for the ROS middleware & toolspr2_plugs - the PR2 autonomous recharge application

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 6 / 20

Page 14: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

When to Make a StackPackaging code for distribution

ROS stacks are best made when there are a collection of packages that,while not necessarily depending on each-other, are useful for a commonpurpose, act as companions to some other package, or make up thecomponents of an application.

Some examples of stacks include:laser_pipeline - packages for grabbing and manipulating laser data

robot_model - packages for modeling a rigid robotros_comm - packages for the ROS middleware & toolspr2_plugs - the PR2 autonomous recharge application

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 6 / 20

Page 15: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

When to Make a StackPackaging code for distribution

ROS stacks are best made when there are a collection of packages that,while not necessarily depending on each-other, are useful for a commonpurpose, act as companions to some other package, or make up thecomponents of an application.

Some examples of stacks include:laser_pipeline - packages for grabbing and manipulating laser datarobot_model - packages for modeling a rigid robot

ros_comm - packages for the ROS middleware & toolspr2_plugs - the PR2 autonomous recharge application

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 6 / 20

Page 16: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

When to Make a StackPackaging code for distribution

ROS stacks are best made when there are a collection of packages that,while not necessarily depending on each-other, are useful for a commonpurpose, act as companions to some other package, or make up thecomponents of an application.

Some examples of stacks include:laser_pipeline - packages for grabbing and manipulating laser datarobot_model - packages for modeling a rigid robotros_comm - packages for the ROS middleware & tools

pr2_plugs - the PR2 autonomous recharge application

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 6 / 20

Page 17: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

When to Make a StackPackaging code for distribution

ROS stacks are best made when there are a collection of packages that,while not necessarily depending on each-other, are useful for a commonpurpose, act as companions to some other package, or make up thecomponents of an application.

Some examples of stacks include:laser_pipeline - packages for grabbing and manipulating laser datarobot_model - packages for modeling a rigid robotros_comm - packages for the ROS middleware & toolspr2_plugs - the PR2 autonomous recharge application

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 6 / 20

Page 18: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS Package & Stack Design

Naming ConventionsROS Style Guidelines

under_scored CamelCase camelCase ALL_CAPSstack_namepackage_namefile_namenamespace_namenode_nametopic_nameservice_nameliblibrary_namevariable_name

MessageTypeServiceTypeClassName

funcName() CONSTANT_NAME

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 7 / 20

Page 19: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Integrating ROS With Other Systems

Outline (revisted)

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 8 / 20

Page 20: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Integrating ROS With Other Systems

Adding ROS InterfacesLightweight addition with minimal changes to a codebase

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 9 / 20

Page 21: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Integrating ROS With Other Systems

Adding ROS InterfacesLightweight addition with minimal changes to a codebase

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 9 / 20

01100001011011000110111101110100

ALOT OF CODE

"the alot" (c) hyperboleandahalf.blogspot.com

Page 22: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Integrating ROS With Other Systems

Adding ROS InterfacesLightweight addition with minimal changes to a codebase

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 9 / 20

01100001011011000110111101110100

ALOT OF CODE

"the alot" (c) hyperboleandahalf.blogspot.com

Transparent ROSpackage wrapper

Page 23: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Integrating ROS With Other Systems

Adding ROS InterfacesLightweight addition with minimal changes to a codebase

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 9 / 20

01100001011011000110111101110100

ALOT OF CODE

"the alot" (c) hyperboleandahalf.blogspot.com

Transparent ROSpackage wrapper

Maybe anadditional thread

Page 24: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Integrating ROS With Other Systems

Adding ROS InterfacesLightweight addition with minimal changes to a codebase

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 9 / 20

01100001011011000110111101110100

ALOT OF CODE

"the alot" (c) hyperboleandahalf.blogspot.com

Transparent ROSpackage wrapper

Maybe anadditional thread

WITH ROS

Page 25: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns

Outline (revisted)

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 10 / 20

Page 26: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

.msg Design

ROS .msg design can be summarized in a few basic principles:Try to prevent .msg proliferation (ie: try to use existing messagesfirst)

Complex messages are built through compositionMessage design should come after node designTry to avoid building messages that tend to not get completely filledout

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 11 / 20

Page 27: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

.msg Design

ROS .msg design can be summarized in a few basic principles:Try to prevent .msg proliferation (ie: try to use existing messagesfirst)Complex messages are built through composition

Message design should come after node designTry to avoid building messages that tend to not get completely filledout

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 11 / 20

Page 28: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

.msg Design

ROS .msg design can be summarized in a few basic principles:Try to prevent .msg proliferation (ie: try to use existing messagesfirst)Complex messages are built through compositionMessage design should come after node design

Try to avoid building messages that tend to not get completely filledout

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 11 / 20

Page 29: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

.msg Design

ROS .msg design can be summarized in a few basic principles:Try to prevent .msg proliferation (ie: try to use existing messagesfirst)Complex messages are built through compositionMessage design should come after node designTry to avoid building messages that tend to not get completely filledout

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 11 / 20

Page 30: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

NamespacesLike Jersey barriers, but for ROS graph resources

ROS nodes, topics, services, and parameters, can all be created innamespaces, to better organize the collection of names in the ROS graph.Any ROS resoource which is named in a launchfile can be created in agiven namespace, using the <group> tag and the ns attribute.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 12 / 20

Page 31: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Namespaces for Designating TopicsAn illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 13 / 20

viewer

sensor-##1

Page 32: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Namespaces for Designating TopicsAn illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 13 / 20

viewer

sensor-##1

/data

Page 33: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Namespaces for Designating TopicsAn illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 13 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 34: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Namespaces for Designating TopicsAn illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 13 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 35: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Namespaces for Designating TopicsAn illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 13 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 36: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Namespaces for Designating TopicsAn illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 13 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 37: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”Powerful abstraction interface

One of the most-used and under-documented patterns in ROScommunication is resource remapping. While remapping might first appearto simply be another way of reducing naming collisions, it is actually apowerful design tool.

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 14 / 20

Page 38: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”An illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 15 / 20

viewer

sensor-##1

Page 39: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”An illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 15 / 20

viewer

sensor-##1

/data

Page 40: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”An illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 15 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 41: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”An illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 15 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 42: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”An illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 15 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 43: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

Communication Design Patterns Datagram Design

Resource Name “Remapping”An illustration

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 15 / 20

viewer

sensor-##1

/data

/datasensor-##2

Page 44: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Outline (revisted)

1 ROS Package & Stack Design

2 Integrating ROS With Other Systems

3 Communication Design PatternsDatagram Design

4 ROS (C++) APIs

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 16 / 20

Page 45: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

InitializationThe first thing you will ever do with ROS

Any progam that uses the ROS APIs must intialize the ROS runtime. Thisis done with a single call to ros::init(). This function generallyconforms to the format:

void ros::init(argc,argc,std::string node_name,uint32_t options);

roslaunch passes arguments into ros via argc, argv so these mustbe passed into your ros::init() in order for launchfile settings totake effect.Note that if using roslaunch, the node_name may be over-writtenoptions is a bitfield for advanced use (see online documentation)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 17 / 20

Page 46: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

“Spinning”Where all the magic happens

Before, it was claimed that ROS is a lightweight architecture, and doesnot “take over” your program execution. Processing ROS messages andcallbacks can be done in a few ways:

Single-Threaded Programs

ros::spin()Block and process ROS messages and callbacksros::spinonce()Block and process only currently waiting ROS messages and callbacks(for programs that already have a main loop)

Multi-Threaded Programs

ros::MultiThreadedSpinnerSimilar to ros::spinonce(), but services callbacks in multiple threadsros::AsynchSpinnerNon-blocking, multi-threaded service of callbacks (tends to be the mostuseful for multi-threaded programs)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 18 / 20

Page 47: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

“Spinning”Where all the magic happens

Before, it was claimed that ROS is a lightweight architecture, and doesnot “take over” your program execution. Processing ROS messages andcallbacks can be done in a few ways:

Single-Threaded Programsros::spin()Block and process ROS messages and callbacks

ros::spinonce()Block and process only currently waiting ROS messages and callbacks(for programs that already have a main loop)

Multi-Threaded Programs

ros::MultiThreadedSpinnerSimilar to ros::spinonce(), but services callbacks in multiple threadsros::AsynchSpinnerNon-blocking, multi-threaded service of callbacks (tends to be the mostuseful for multi-threaded programs)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 18 / 20

Page 48: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

“Spinning”Where all the magic happens

Before, it was claimed that ROS is a lightweight architecture, and doesnot “take over” your program execution. Processing ROS messages andcallbacks can be done in a few ways:

Single-Threaded Programsros::spin()Block and process ROS messages and callbacksros::spinonce()Block and process only currently waiting ROS messages and callbacks(for programs that already have a main loop)

Multi-Threaded Programs

ros::MultiThreadedSpinnerSimilar to ros::spinonce(), but services callbacks in multiple threadsros::AsynchSpinnerNon-blocking, multi-threaded service of callbacks (tends to be the mostuseful for multi-threaded programs)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 18 / 20

Page 49: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

“Spinning”Where all the magic happens

Before, it was claimed that ROS is a lightweight architecture, and doesnot “take over” your program execution. Processing ROS messages andcallbacks can be done in a few ways:

Single-Threaded Programsros::spin()Block and process ROS messages and callbacksros::spinonce()Block and process only currently waiting ROS messages and callbacks(for programs that already have a main loop)

Multi-Threaded Programs

ros::MultiThreadedSpinnerSimilar to ros::spinonce(), but services callbacks in multiple threadsros::AsynchSpinnerNon-blocking, multi-threaded service of callbacks (tends to be the mostuseful for multi-threaded programs)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 18 / 20

Page 50: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

“Spinning”Where all the magic happens

Before, it was claimed that ROS is a lightweight architecture, and doesnot “take over” your program execution. Processing ROS messages andcallbacks can be done in a few ways:

Single-Threaded Programsros::spin()Block and process ROS messages and callbacksros::spinonce()Block and process only currently waiting ROS messages and callbacks(for programs that already have a main loop)

Multi-Threaded Programsros::MultiThreadedSpinnerSimilar to ros::spinonce(), but services callbacks in multiple threads

ros::AsynchSpinnerNon-blocking, multi-threaded service of callbacks (tends to be the mostuseful for multi-threaded programs)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 18 / 20

Page 51: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

“Spinning”Where all the magic happens

Before, it was claimed that ROS is a lightweight architecture, and doesnot “take over” your program execution. Processing ROS messages andcallbacks can be done in a few ways:

Single-Threaded Programsros::spin()Block and process ROS messages and callbacksros::spinonce()Block and process only currently waiting ROS messages and callbacks(for programs that already have a main loop)

Multi-Threaded Programsros::MultiThreadedSpinnerSimilar to ros::spinonce(), but services callbacks in multiple threadsros::AsynchSpinnerNon-blocking, multi-threaded service of callbacks (tends to be the mostuseful for multi-threaded programs)

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 18 / 20

Page 52: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Shutting DownThe last thing you will ever do with ROS

ROS programs prefer to terminate cleanly. Doing so is supported throughthe following mechanisms, which can be called at any time:

ros::shutdown()Triggers shutdown of all ROS interfaces in this node

SIGINT (Ctrl-C from CLI)Same effect as ros::shutdown()ros::ok()Returns false once this node’s ROS interfaces have completely shutdownros::isShuttingDown()Discouraged except in advanced cases, returns true onceros::shutdown() has been called

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 19 / 20

Page 53: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Shutting DownThe last thing you will ever do with ROS

ROS programs prefer to terminate cleanly. Doing so is supported throughthe following mechanisms, which can be called at any time:

ros::shutdown()Triggers shutdown of all ROS interfaces in this nodeSIGINT (Ctrl-C from CLI)Same effect as ros::shutdown()

ros::ok()Returns false once this node’s ROS interfaces have completely shutdownros::isShuttingDown()Discouraged except in advanced cases, returns true onceros::shutdown() has been called

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 19 / 20

Page 54: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Shutting DownThe last thing you will ever do with ROS

ROS programs prefer to terminate cleanly. Doing so is supported throughthe following mechanisms, which can be called at any time:

ros::shutdown()Triggers shutdown of all ROS interfaces in this nodeSIGINT (Ctrl-C from CLI)Same effect as ros::shutdown()ros::ok()Returns false once this node’s ROS interfaces have completely shutdown

ros::isShuttingDown()Discouraged except in advanced cases, returns true onceros::shutdown() has been called

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 19 / 20

Page 55: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Shutting DownThe last thing you will ever do with ROS

ROS programs prefer to terminate cleanly. Doing so is supported throughthe following mechanisms, which can be called at any time:

ros::shutdown()Triggers shutdown of all ROS interfaces in this nodeSIGINT (Ctrl-C from CLI)Same effect as ros::shutdown()ros::ok()Returns false once this node’s ROS interfaces have completely shutdownros::isShuttingDown()Discouraged except in advanced cases, returns true onceros::shutdown() has been called

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 19 / 20

Page 56: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Time & The ROS ClockFIXME: 2038 A.D.

ROS provides a time abstraction interface to provide a mechanism tospoof the wall clock for simulation. ROS time is preferred over a system’sclock routines (for non-realtime systems). The ros::Time API has thefollowing features:

ros::Time - (int32 sec, int32 µsec) time point

ros::Time::now() - the current timeros::Duration - (int32 sec, int32 µsec) time durationros::Duration::sleep() - sleep for this durationros::[Time/Duration]::toSec() - (double) in secondsros::Rate - fixed-rate duration for sleeping

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 20 / 20

Page 57: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Time & The ROS ClockFIXME: 2038 A.D.

ROS provides a time abstraction interface to provide a mechanism tospoof the wall clock for simulation. ROS time is preferred over a system’sclock routines (for non-realtime systems). The ros::Time API has thefollowing features:

ros::Time - (int32 sec, int32 µsec) time pointros::Time::now() - the current time

ros::Duration - (int32 sec, int32 µsec) time durationros::Duration::sleep() - sleep for this durationros::[Time/Duration]::toSec() - (double) in secondsros::Rate - fixed-rate duration for sleeping

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 20 / 20

Page 58: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Time & The ROS ClockFIXME: 2038 A.D.

ROS provides a time abstraction interface to provide a mechanism tospoof the wall clock for simulation. ROS time is preferred over a system’sclock routines (for non-realtime systems). The ros::Time API has thefollowing features:

ros::Time - (int32 sec, int32 µsec) time pointros::Time::now() - the current timeros::Duration - (int32 sec, int32 µsec) time duration

ros::Duration::sleep() - sleep for this durationros::[Time/Duration]::toSec() - (double) in secondsros::Rate - fixed-rate duration for sleeping

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 20 / 20

Page 59: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Time & The ROS ClockFIXME: 2038 A.D.

ROS provides a time abstraction interface to provide a mechanism tospoof the wall clock for simulation. ROS time is preferred over a system’sclock routines (for non-realtime systems). The ros::Time API has thefollowing features:

ros::Time - (int32 sec, int32 µsec) time pointros::Time::now() - the current timeros::Duration - (int32 sec, int32 µsec) time durationros::Duration::sleep() - sleep for this duration

ros::[Time/Duration]::toSec() - (double) in secondsros::Rate - fixed-rate duration for sleeping

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 20 / 20

Page 60: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Time & The ROS ClockFIXME: 2038 A.D.

ROS provides a time abstraction interface to provide a mechanism tospoof the wall clock for simulation. ROS time is preferred over a system’sclock routines (for non-realtime systems). The ros::Time API has thefollowing features:

ros::Time - (int32 sec, int32 µsec) time pointros::Time::now() - the current timeros::Duration - (int32 sec, int32 µsec) time durationros::Duration::sleep() - sleep for this durationros::[Time/Duration]::toSec() - (double) in seconds

ros::Rate - fixed-rate duration for sleeping

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 20 / 20

Page 61: ROSCrash-Course,PartII€¦ · Outline 1 ROSPackage&StackDesign 2 IntegratingROSWithOtherSystems 3 CommunicationDesignPatterns DatagramDesign 4 ROS(C++)APIs Jonathan Bohren (JHU LCSR)ROS

ROS (C++) APIs

Time & The ROS ClockFIXME: 2038 A.D.

ROS provides a time abstraction interface to provide a mechanism tospoof the wall clock for simulation. ROS time is preferred over a system’sclock routines (for non-realtime systems). The ros::Time API has thefollowing features:

ros::Time - (int32 sec, int32 µsec) time pointros::Time::now() - the current timeros::Duration - (int32 sec, int32 µsec) time durationros::Duration::sleep() - sleep for this durationros::[Time/Duration]::toSec() - (double) in secondsros::Rate - fixed-rate duration for sleeping

Jonathan Bohren (JHU LCSR) ROS Crash-Course, Part II: Patterns 20 / 20