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 python

Description: This tutorial teaches you how to create ROS Packages based on BRIDE models

Tutorial 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 ~/git/bride_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. Continue to the next page and enter your project's name. 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.

New Project wizard

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.

Creating a new BRIDE Model

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 TriggerPublish 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:

Screenshot of talker node in BRIDE

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:

Package properties

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:

Publisher and ActionServer properties

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:

Project Explorer after Code Generation

To implement the actual capability code for your ROS node you have adapt the file talker.py in the src directory. You can open the file by double clicking 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 first have to add the import statement in the customHeader section and afterwards to several adjustments to the talker_impl class:

   1 # protected region customHeaders on begin #
   2 import time
   3 # protected region customHeaders end #
   4 
   5 from std_msgs.msg import String
   6 from bride_tutorials.msg import TriggerPublishAction, TriggerPublishFeedback, TriggerPublishResult
   7 
   8     class talker_impl:
   9         out_pub = String()
  10 
  11         def __init__(self):
  12                 self.config_word = rospy.get_param('~word', "Test")
  13 
  14 
  15 
  16                 # protected region initCode on begin #
  17                 self.counter = 0
  18                 self.running = False
  19                 # protected region initCode end #
  20                 pass
  21 
  22         def configure(self):
  23                 # protected region configureCode on begin #
  24                 # protected region configureCode end #
  25                 pass
  26 
  27         def update(self):
  28                 # protected region updateCode on begin #
  29                 if self.running:
  30                         print 'Send', self.config_word, self.counter
  31                         self.counter += 1
  32                         msg = '%s : %s' % ( self.config_word, self.counter )
  33                         self.out_pub.data = msg
  34                 if self.counter > 10:
  35                         self.running = False
  36                         self.out_pub.data = ''
  37                 # protected region updateCode end #
  38                 pass
  39 
  40 
  41         def execute_TriggerPublish_cb(self, goal):
  42                 # Examples: self.as_TriggerPublish.set_succeeded(_result)
  43                 #           self.as_TriggerPublish.set_aborted()
  44                 #           self.as_TriggerPublish.publish_feedback(_feedback)
  45                 _feedback = TriggerPublishFeedback()
  46                 _result = TriggerPublishResult()
  47                 # protected region user implementation of action callback for TriggerPublish on begin #
  48                 self.counter = 0
  49                 self.running = True
  50                 print 'Running'
  51                 while self.running:
  52                         time.sleep( 0.1 )
  53                 self.as_TriggerPublish.set_succeeded()
  54                 # protected region user implementation of action callback for TriggerPublish end #
  55                 pass

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:

Listener package properties

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:

Screenshot of listener node in BRIDE

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.py file in the src directory:

   1     class listener_impl:
   2         in_sub = String()
   3 
   4         def __init__(self):
   5 
   6 
   7 
   8                 # protected region initCode on begin #
   9                 # protected region initCode end #
  10                 pass
  11 
  12         def configure(self):
  13                 # protected region configureCode on begin #
  14                 # protected region configureCode end #
  15                 pass
  16 
  17         def update(self):
  18                 # protected region updateCode on begin #
  19                 if self.in_sub.data:
  20                         print 'Received: %s' % self.in_sub.data
  21                 # protected region updateCode end #
  22                 pass
  23 
  24         def callback_TestService(self, req):
  25                 # protected region user implementation of service callback for TestService on begin #
  26                 # protected region user implementation of service callback for TestService end #
  27                 pass

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.

Wiki: bride/Tutorials/Creating-nodes-with-BRIDE-and-python (last edited 2014-07-22 13:43:04 by SimonEbner)