Robot Operating System (ROS) is a popular open-source framework used for building robotic applications. While it is primarily used with C++ and Python, ROS also supports other programming languages, including Go. In this tutorial, we'll explore how to build ROS applications with Go programming.
Prerequisites
Before we start, you should have the following:
- A basic understanding of ROS
- Go programming language installed on your machine
- ROS installed on your machine
If you are new to ROS, I recommend checking out the official ROS documentation. Also, if you don't have ROS installed on your machine, you can follow the installation instructions provided on the official ROS website.
Setting up your Workspace
The first step to building a ROS application with Go is to create a workspace. A workspace is a directory where you will store all your ROS packages. To create a workspace, follow these steps:
1. Create a directory for your workspace:
$ mkdir -p ~/go-ros/src
2. Initialize the workspace:
$ cd ~/go-ros/src
$ catkin_init_workspace
3. Build the workspace:
$ cd ~/go-ros
$ catkin_make
Creating a ROS Package
Now that we have a workspace set up, let's create a ROS package. A ROS package is a collection of files that serve a specific purpose in a ROS system. To create a package, follow these steps:
1. Create a package:
$ cd ~/go-ros/src
$ catkin_create_pkg my_package
2.Create a directory for your Go code:
$ cd ~/go-ros/src/my_package
$ mkdir -p src/go
3.Create a Go module:
$ cd ~/go-ros/src/my_package/src/go
$ go mod init my_package
Writing a Publisher
Now that we have a package set up, let's write a publisher in Go. A publisher is a node that sends messages to a ROS topic. To create a publisher, follow these steps:
1.Create a file called publisher.go in ~/go-ros/src/my_package/src/go:
package main
import (
"fmt"
"github.com/ros/ros_comm/messages/std_msgs"
"github.com/streadway/amqp"
)
func main() {
// Connect to the ROS master
node, err := ros.NewNode("publisher", "", ros.WithoutRosout())
if err != nil {
panic(err)
}
defer node.Shutdown()
// Create a publisher for the "hello" topic
pub, err := node.NewPublisher("hello", std_msgs.MsgString)
if err != nil {
panic(err)
}
defer pub.Unregister()
// Publish messages to the "hello" topic
for i := 0; i < 10; i++ {
msg := &std_msgs.String{
Data: fmt.Sprintf("Hello, World! %d", i),
}
if err := pub.Publish(msg); err != nil {
panic(err)
}
}
}
2.Add the following to CMakeLists.txt in ~/go-ros/src/my_package:
# Add Go source files
catkin_add_go_executable(my_publisher
src/go/publisher.go)
3.Build the package:
$ cd ~/go-ros
$ catkin_make
Writing a Subscriber
Next, let's write a subscriber in Go. A subscriber is a node that listens to a ROS topic and receives messages. To create a subscriber, follow these steps:
1. Create a file called subscriber.go in ~/go-ros/src/my_package/src/go:
package main
import (
"fmt"
"github.com/ros/ros_comm/messages/std_msgs"
)
func main() {
// Connect to the ROS master
node, err := ros.NewNode("subscriber", "", ros.WithoutRosout())
if err != nil {
panic(err)
}
defer node.Shutdown()
// Create a subscriber for the "hello" topic
sub, err := node.NewSubscriber("hello", std_msgs.MsgString, func(msg *std_msgs.String) {
fmt.Printf("Received message: %s\n", msg.Data)
})
if err != nil {
panic(err)
}
defer sub.Unregister()
// Spin the node to listen for messages
node.Spin()
}
2.Add the following to CMakeLists.txt in ~/go-ros/src/my_package:
# Add Go source files
catkin_add_go_executable(my_subscriber
src/go/subscriber.go)
3.Build the package:
$ cd ~/go-ros
$ catkin_make
Running the Publisher and Subscriber
Now that we have a publisher and subscriber, let's run them and see them in action. To run the publisher and subscriber, follow these steps:
1. Open a new terminal window and source your ROS environment:
$ source /opt/ros/melodic/setup.bash
2.Run the publisher:
$ rosrun my_package my_publisher
3.Open another terminal window and source your ROS environment:
$ source /opt/ros/melodic/setup.bash
4.Run the subscriber:
$ rosrun my_package my_subscriber
5.You should now see the subscriber outputting the messages sent by the publisher:
Received message: Hello, World! 0
Received message: Hello, World! 1
Received message: Hello, World! 2
Received message: Hello, World! 3
Received message: Hello, World! 4
Received message: Hello, World! 5
Received message: Hello, World! 6
Received message: Hello, World! 7
Received message: Hello, World! 8
Received message: Hello, World! 9
Conclusion
In this tutorial, we explored how to build ROS applications with Go programming. We created a ROS package, wrote a publisher and subscriber in Go, and ran them to see them in action. ROS provides a powerful platform for building robotic applications, and Go provides a modern and efficient programming language that can be used to write ROS nodes. With the knowledge gained in this tutorial, you can begin building your own ROS applications with Go