Skip to content

Commit

Permalink
Add publisher-subscriber example
Browse files Browse the repository at this point in the history
  • Loading branch information
lassilaiho committed Nov 3, 2023
1 parent 69417c1 commit c65f2bc
Show file tree
Hide file tree
Showing 11 changed files with 761 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ distributed as Debian packages.

API documentation is available at [pkg.go.dev][docs].

An example module with a publisher and a subscriber can be found in
[examples/publisher_subscriber](examples/publisher_subscriber).

### ROS 2 interface bindings

rclgo requires Go bindings of all the ROS 2 interfaces to exist. rclgo-gen is
Expand All @@ -36,12 +39,12 @@ import _ "github.com/tiiuae/rclgo/cmd/rclgo-gen"
```
Then run `go mod tidy`. This version of rclgo-gen can be used by running

go run github.com/tiiuae/rclgo/cmd/rclgo-gen generate -d msgs
go run github.com/tiiuae/rclgo/cmd/rclgo-gen generate -d msgs --include-go-package-deps ./...

in the project directory. The command can be added as a `go generate` comment to
one of the source files in the project, such as `main.go`, as follows:
```go
//go:generate go run github.com/tiiuae/rclgo/cmd/rclgo-gen generate -d msgs
//go:generate go run github.com/tiiuae/rclgo/cmd/rclgo-gen generate -d msgs --include-go-package-deps ./...
```

### Developing with custom interface types
Expand Down
46 changes: 39 additions & 7 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ func skipIfShort(t *testing.T) {
}
}

const shellPrelude = `
set -eo pipefail
function kill-jobs {
kill $(jobs -p)
}
trap 'kill-jobs' SIGINT SIGTERM EXIT
`

func shell(t *testing.T, script string) {
t.Helper()
cmd := exec.Command("bash", "-c", script)
cmd := exec.Command("bash", "-c", shellPrelude+script)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
Expand All @@ -26,12 +34,6 @@ func shell(t *testing.T, script string) {
func TestCustomMessagePackage(t *testing.T) {
skipIfShort(t)
shell(t, `
set -e
function kill-jobs {
kill $(jobs -p)
}
trap 'kill-jobs' SIGINT SIGTERM EXIT
cd custom_message_package/greeting_msgs
rm -rf build install log
colcon build
Expand All @@ -47,3 +49,33 @@ msg=$(ros2 topic echo --once /greeter/hello greeting_msgs/msg/Hello)
test "$msg" == $'name: gopher\n---'
`)
}

func TestPublisherSubscriber(t *testing.T) {
skipIfShort(t)
shell(t, `
cd publisher_subscriber
rm -rf msgs pub sub pipe
go generate
go build -o pub ./publisher
go build -o sub ./subscriber
./pub &
mkfifo pipe
./sub >pipe 2>&1 &
function check_output {
received_msg='Received: &std_msgs_msg.String{Data:"gopher"}'
while read -r line; do
if [[ "$line" == *"$received_msg"* ]]; then
exit 0
fi
done <pipe
exit 1
}
export -f check_output
timeout 3 bash -c 'check_output'
`)
}
4 changes: 4 additions & 0 deletions examples/publisher_subscriber/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/msgs
/pub
/sub
/pipe
12 changes: 12 additions & 0 deletions examples/publisher_subscriber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Publisher-subscriber example

This is an example of a publisher and subscriber.

To run the example, generate Go bindings by running

go generate

Then you can run the publisher and subscriber in separate terminals:

go run ./publisher
go run ./subscriber
2 changes: 2 additions & 0 deletions examples/publisher_subscriber/cgo-flags.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export CGO_CFLAGS='-I/opt/ros/humble/include/action_msgs -I/opt/ros/humble/include/builtin_interfaces -I/opt/ros/humble/include/rcl -I/opt/ros/humble/include/rcl_action -I/opt/ros/humble/include/rcl_yaml_param_parser -I/opt/ros/humble/include/rcutils -I/opt/ros/humble/include/rmw -I/opt/ros/humble/include/rosidl_runtime_c -I/opt/ros/humble/include/rosidl_typesupport_interface -I/opt/ros/humble/include/std_msgs -I/opt/ros/humble/include/unique_identifier_msgs '
export CGO_LDFLAGS='-L/opt/ros/humble/lib -Wl,-rpath=/opt/ros/humble/lib '
3 changes: 3 additions & 0 deletions examples/publisher_subscriber/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate go run github.com/tiiuae/rclgo/cmd/rclgo-gen generate -d msgs --include-go-package-deps ./...
36 changes: 36 additions & 0 deletions examples/publisher_subscriber/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module github.com/tiiuae/rclgo/examples/publisher_subscriber

go 1.20

// This is needed to keep the version of rclgo automatically in sync with the
// rest of the repository. It is in general not needed for modules outside
// this repository.
replace github.com/tiiuae/rclgo v0.0.0 => ../..

require github.com/tiiuae/rclgo v0.0.0

require (
github.com/alessio/shellescape v1.4.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kivilahtio/go-re v0.1.8 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit c65f2bc

Please sign in to comment.