Note: This tutorial assumes that you have completed the previous tutorials: Installing BRIDE. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Creating nodes with BRIDE and C++
Description: This tutorial teaches you how to create ROS Packages based on BRIDE modelsTutorial Level: BEGINNER
Next Tutorial: Creating ROS coordinators with BRIDE
In this small tutorial you will learn how to create ROS packages with BRIDE.
Preparation and starting BRIDE
Preparing a catkin workspace
Before you start eclipse for the first time, make sure you have a catkin workspace at hand. In the following we will assume this workspace to be located at ~/catkin_ws. However, you are free to choose any other path, but be sure to adapt all the paths accordingly. To create a new catkin workspace at ~/catkin_ws, run the following commands:
$ mkdir -p ~/catkin_ws/src $ cd ~/catkin_ws/src $ catkin_init_workspace $ cd .. $ catkin_make
You are now prepared to create new catkin projects so we can proceed to start eclipse. Before actually starting the application, make sure you have all necessary ROS settings set up. If you followed the installation tutorial installing BRIDE, you can start eclipse in the following way:
$ source ~/catkin_ws/devel/setup.bash $ rosrun bride eclipse
After the start of BRIDE you will be asked for a workspace directory. Accept the default setting (~/workspace) and continue.
Creating your first project
You can now create a new project by selecting File -> New -> Project and then choose ROS -> ROSPackageWizard. Before you continue to the next page and enter your project's name do the following: In this tutorial we will call our project talker. After setting your project's name be sure to update the project's location. This is important since our project must be placed inside our previously created catkin workspace. Untick the box Use default location and update the project's location to /home/your_username/catkin_ws/src/talker. Be sure to replace your_username by your actual username. Note that you cannot use the abbreviation tilde (~) in the path location. In case you use a different project name make sure you adapt the last part of the location path accordingly.
Now finish the wizard and close the welcome page.
Create the model for the talker node
On the left hand side of your eclipse environment, you should now see the Package explorer containing a blue folder called talker. Expand the folder and right click on the folder model. Add a new capability model by clicking on New -> Other... or by pressing Ctrl+N if the model folder is highlighted. Select ROS Package Diagram in the BRIDE section.
Confirm your choice by clicking on Next. You can now set the diagram model's filename. Use your desired node name plus the extension ros_package_diagram as filename. In this tutorial we will call our node talker, so we use the filename talker.ros_package_diagram. Proceed to the next page to set the filename for your domain model. This time use your node's name plus by the extension ros_package. Following this scheme we choose talker.ros_package.
You now should have 2 new files inside the model directory and the graphical editor for your new model should be opened. Choose the Capability Developer perspective on the upper right area of eclipse. (the "Open Perspective" icon).
With the palette on the right you can now add a node to the graphical plane. First single-click on Node in the Palette-View followed by a click on the white canvas. Name the node talker. Now you can add a Publisher called pub, an ActionServer called TriggerPublisher and a Parameter called word to your node. You can do so by selecting the respective type in the palette and clicking on your graphical node representation afterwards. Set the properties for that parameter as follows: Set Type and Value to string and Empty respectively. You should finally see the following screen:
Now we have to do a few setting for the package and the nodes. Select the properties pane in the lower area of the screen. When you click in the white area of the screen you should see the package properties below. Fill them out as in the screenshot below:
Now select the actionserver and the publisher you created. In the property pane you now see the settings for each of the two. The options you see in the msg drop-down menu are based on the dependencies you set in the package settings. You should therefore see the types of std_msgs in here e.g. std::msgs::Bool or std::msgs::String. Fill out the settings of the publisher and actionserver as in the following screen:
Now we update the Frequency in Hz of the node by selecting the node and setting the Loop Rate property to 10.0.
You are now done creating the model for the talker ros node. After you save your changes switch to the panel Problems. If you properly followed each step in this tutorial, you shouldn't be able to see any error. However, if any problems are present, go back to the corresponding step in this tutorial and make sure you didn't miss any detail.
Generating source and finishing the implementation of the node
You can now generate the source code of the node automatically based on the model you created. You do this by selecting BRIDE -> ROS -> Generate C++ Code from the menu. Be sure you saved your diagram before generating. If the building fails, make sure that there is not a single error/warning/info in the problems box. If there is and you cannot resolve the issue, delete the corresponding warning/info (right click on the message followed by a click on delete). If the problems box is completely empty, try to generate the code once again.
When you refresh your project in the project explorer by right-clicking on the project and selecting Refresh or hit F5 there. You should now see the complete ROS package structure in the project explorer as in the screenshot below:
To implement the actual capability code for your ROS node you have to only focus on the file talker_common.cpp in the common directory. You can open the file by double clicking it in the project explorer. The file contains protected regions you can use for your code that will not be overwritten once you regenerate the node by the tool chain.
For this example you can now go to the talker_impl class and edit the code as follows:
1 class talker_impl
2 {
3 /* protected region user member variables on begin */
4 int counter;
5 bool running;
6 /* protected region user member variables end */
7
8 public:
9 talker_impl()
10 {
11 /* protected region user constructor on begin */
12 counter = 0;
13 running = false;
14 /* protected region user constructor end */
15 }
16 void configure(talker_config config)
17 {
18 /* protected region user configure on begin */
19 /* protected region user configure end */
20 }
21 void update(talker_data &data, talker_config config)
22 {
23 /* protected region user update on begin */
24 if(running == true)
25 {
26 counter ++;
27 std::stringstream ss;
28 ss << config.word << " : " << counter;
29 data.out_pub.data = ss.str();
30 }
31
32 if(counter > 65535)
33 running = false;
34 /* protected region user update end */
35 }
36 void callback_TriggerPublisher_(const bride_tutorials::TriggerPublishGoalConstPtr &goal, actionlib::SimpleActionServer<bride_tutorials::TriggerPublishAction> *as_)
37 {
38 /* protected region user implementation of action callback for TriggerPublish on begin */
39 counter = 0;
40 running = true;
41 while(running == true)
42 sleep(0.1);
43 as_->setSucceeded();
44 /* protected region user implementation of action callback for TriggerPublish end */
45 }
46 };
You can now build the node by right clicking the project in the Project Explorer and choosing Build Project. You can see the output of the triggered catkin_make in the Console Pane in the lower area of BRIDE.
Congratulation you just created your first model-based ROS node.
Create a corresponding listener node with a service port
To create another node that can listen to the talker node you should create a new ROS Project as above and call it listener. Be sure to properly set the project's path to /home/your_username/catkin_ws/src/listener this time. Like in the talker example create a new Package Diagram via New -> Other and select ROS Package Diagram from the BRIDE section in the following popup. Enter listener.ros_package_diagram and listener.ros_package as filenames respectively. Fill out the package property as follows:
Create a new node like in the talker example and name it listener. Now add a subscriber sub to the new node and set its message type to std_msgs::String in the properties panel. Also add a new ServiceServer, name it TestService and set the message type to std_srvs::Empty. Your canvas should now look like this:
Save your model, make sure there are no errors and generate the code for the listener example following the steps for code generating in the talker example. After generating the code for the model you can alter the update function of the listener_impl class of the common/src/listener_common.cpp file as follows:
You will also see a generated "callback_TestService" function you can alter as follow:
You can now build the node by right clicking the project in the Project Explorer and choosing Build Project. Afterwards you have finished creating the second node.
If you like you can now continue to the coordinator creation tutorial.