• +4917626969472
  • info@ur-techpartner.de

ROS Basics (12/17) – Define Action

Robot Operating System(ROS1/ROS2) ROS Programming

ROS Basics – Define Action

In previous blog, we have viewed an action definition file named “Fibonacci.action”. Now we will define our own action definition file.

You may remember that the the extension of message definition file was ”.msg”, and the extension of service definition file was “.srv”. Similarly, the extension of action definition file is “.action”.

Also the action files are defined in a directory named “action” of the package.

You may create a new package and create a directory named “action” inside the package. However, since we have already created a package “ros_basics”, we will define action for it.

Create an Action Definition File

Run in terminal

roscd ros_basics

(You will move to the package “ros_basics”)

Run the command

ls

(See the contents to verify if there is no “action” folder yet)

Run the command

mkdir action

(This will create “action” directory)

Now run ls command again

ls

You can see there is “action” directory

To go to action directory, run the command

cd action

Inside the directory, create a file “Timer.action”

Run the command

touch Timer.action

Run following command to give execute permission (It is also fine if you do not give it execute permission. Because, this file will not be executed)

chmod +x Timer.action

Open the file using gedit command

gedit Timer.action

Write following lines into the file

# Part 1: the goal, it will be sent by the client
# It has one variable which is defined for the amount of time we want to wait

duration time_to_wait
---
# Part 2: the result, it will be sent by the server upon completion
# This part has two variables
# One is for How much time we waited

duration time_elapsed
# Second variable is for how many updates we provided along the way

uint32 updates_sent
---
# Part 3: the feedback, it will be sent periodically by the server during
# execution.
# This part has two variables
# One variable is for the amount of time that has elapsed from start

duration time_elapsed
# Second variable is for the amount of time remaining until we're done

duration time_remaining

 

All above executed commands are shown in figure below

The file “Timer.Action” will look like as shown in figure below

Explanation

We can see that different parts of action definition file are separated by three dashes (—). This is similar to service definition file. However, in service definition file, there were two parts (request and response). While, action definition file has three parts (goal, result, and feedback).

The three parts and their variables are explained in comments (using symbal #) within the action file “Timer.action”

Modify CMakeLists.txt and package.xml files for defined Action Compilation

Now we come back to the action file “Timer.Action” that we have just defined. In order to compile this action message file, we need to modify “CMakeLists.txt” file as we have already done in define service blog.

For this go to the package “ros_basics”

Run the command

roscd ros_basics

Run ls command to see the contents

ls

You can see the file “CMakeLists.txt” inside the directory

Modify “CMakeLists.txt”

Run following command to modify the file

gedit CMakeLists.txt

The file will be opened as shown in figure below, you will see most of the code lines are commented. You will have to make changes in following four functions. You do not need to write the function names (they are already there), You only need to find them and uncomment them (if they are commented). The four functions are

1. find_package()
2. add_action_files()
3. generate_messages()
4. catkin_package()

You can notice that three of these functions are same as we modified in define topic message and define service message articles.

Only one function is different which is “add_action_files ()”. While, it was “add_message_files()”, and “add_service_files ()” in case of message compilation, and service compilation, respectively.

You can also see it in figure below.

The above four functions should be modified like this

1.
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs
message_generation #Other packages are already listed
actionlib_msgs # Add “actionlib_msgs” here
)

2.
add_action_files(
DIRECTORY action
FILES
Timer.action
# Add “Timer.action” here, and uncomment function & parenthesis
)

3.
generate_messages(
DEPENDENCIES
std_msgs
actionlib_msgs #Add “actionlib_msgs” here
)

4.
catkin_package(
CATKIN_DEPENDS rospy std_msgs message_runtime
actionlib_msgs #Add “actionlib_msgs” here as well
)

When all four functions are modified accordingly.

Then save the file and close it.

Modify “Package.xml”

Same as in case of defining topic message and service message, we will add build and execute dependencies to package.xml file for defined action message.

Run following command

gedit package.xml

Add following lines into the file

<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>

The file will look like in figure below

After modification, save and close the file.

Build the “catkin_ws” Workspace

Now the last step is to build the “catkin_ws” workspace

Go to the catkin_ws directory

Run command

cd ~/catkin_ws

Run the command

catkin_make

(This will build all packages)

Run the command

source devel/setup.bash

After executing above commands, your Action message is ready to use.

After executing catkin_make command, it will generate four classes which are “TimerAction”, “TimerGoal”, “TimerResult” , and “TimerFeedback”.

Additionally, several message definition files will be produced which are TimerAction.msg, TimerActionGoal.msg, TimerActionResult.msg, TimerActionFeedback.msg, TimerGoal.msg, TimerResult.msg, and TimerFeedback.msg.

These classes and messages will be used for server/client communication.

In next blogs, we will create action server and action client nodes that will use action definition file to communicate with each other.

No comments