Pan & Tilt Real Robot Connection Face Tracking

Pan & Tilt Real Robot Connection Face Tracking

About

In this video you will learn:

  • How to connect to the real robot through ROS Development Studio
  • How to execute the face tracking script for the real robot to track your own face.

List of materials

Related courses & tutorials

3D Models

[irp posts=”13312″ name=”Pan & Tilt Face Tracking in ROS”]

[ROS Q&A] 190 – How to subscribe with a C++ subscriber to a custom msg array published with a Python publisher

[ROS Q&A] 190 – How to subscribe with a C++ subscriber to a custom msg array published with a Python publisher

In the post, you will learn how to create a C++ subscriber to a custom msg array published with Python publisher. This answers the following question on the ROS Answers forum: https://answers.ros.org/question/324325/how-to-use-c-subscribe-to-a-custom-msg-array-published-by-python/.

What you will learn

  1. How to create a custom message
  2. How to create a ROS Publisher with Python
  3. How to create a ROS Subscriber with C++

On your marks…get set…let’s go!


Related Resources


Step 0: Get your development environment ready

Either of the following will do:

  1. Use the ROS Development Studio (ROSDS), an online platform for developing for ROS within a PC browser. Easy-peasy. I’m using this option for this post
    1. Once you log in, click on the New ROSject button to create a project that will host your code. Then Click on Open ROSject to launch the development/simulation environment.
    2. To open a “terminal” on ROSDSpick the Shell app from the Tools menu.
    3. You can find the IDE app on the Tools menu.
  2. You have ROS installed on a local PC. Okay, skip to Step 2.

Next step!

Step 1: Create the custom messages

Err, not so fast! We need to create a package first. So, open a terminate, browse to your catkin workspace source directory and create the package and some of the files we will need for the custom messages. Note that when creating the package, we added both rospy and roscpp because we will be working with C++ and Python source codes.

cd ~/catkin_ws/src
catkin_create_pkg cpp_python roscpp rospy
cd cpp_python
mkdir -p msg
touch InfoData.msg
touch InfoDataArray.msg

Now open the IDE and locate the open both message files in the msg subdirectory of the package. In InfoData.msg, paste in the following:

int32 num
string color
string name

Put this in InfoDataArray.msg:

InfoData[] infos

Add the following lines to your CMakeLists.txt and package.xml in order to recognize the custom messages:

CMakeLists.txt:

1. Find the existing add_message_files directive, uncomment it, and change it to the following:

add_message_files(
   FILES
   InfoData.msg
   InfoDataArray.msg
 )

2.  Find the generate_messages directive and uncomment it:

generate_messages(
   DEPENDENCIES
   std_msgs  # Or other packages containing msgs
 )

3. Find the catkin_package directive and uncomment the CATKIN_DEPENDS line:

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES cpp_python
  CATKIN_DEPENDS roscpp rospy
#  DEPENDS system_lib
)

Of course, please save the file!

package.xml:

Add the following lines required to generate the custom messages:

<build_depend>message_generation</build_depend>
<build_export_depend>message_generation</build_export_depend>
<exec_depend>message_runtime</exec_depend>

Save!

Compile the messages:

# make sure you run this from the root of your catkin workspace (e.g catkin_ws)
catkin_make

Source the workspace:

# make sure you run this from the root of your catkin workspace (e.g catkin_ws)
source devel/setup.bash

Step 2: Create the Python publisher node

Create a python file in the src folder and make it executable

roscd cpp_python
cd src
touch infodata_publisher.py
chmod +x infodata_publisher.py

Using the IDE, paste in the following code

#! /usr/bin/env python

import rospy # import the ros Python api
from cpp_python.msg import InfoData, InfoDataArray # import the custom messages

rospy.init_node('infodata_publisher_node') # create a node to run the publisher
pub = rospy.Publisher('/infodata', InfoDataArray, queue_size=1) # create the publisher
rate = rospy.Rate(1) # create a rate (per second) for publishing
infodata = InfoData() # create the infodata message
infodata_array = InfoDataArray() # create the infodata array
# set the variables of the info data message
infodata.num = 5
infodata.color = 'red'
infodata.name = 'data1'

# push several infodata messages into the info data array
for x in range(10):
    infodata_array.infos.append(infodata)

# continously publish the infodata array at the rate specified
# until someone stops the node
while not rospy.is_shutdown():
  pub.publish(infodata_array.infos)
  rate.sleep()

Step 3: Create the C++ subscriber node

Create a C++ file in the src folder

roscd cpp_python
cd src
touch infodata_subscriber.cpp

Locate the file in the IDE and paste in the following code:

// Import the infodata messaages
#include <cpp_python/InfoData.h>
#include <cpp_python/InfoDataArray.h>
#include <ros/ros.h> // import the ros C++ api

// the function that takes the message from the subscriber and does
// something with it
void infodataCallback(const cpp_python::InfoDataArray::ConstPtr &msg) {
  ROS_INFO("%d", msg->infos[0].num);
  ROS_INFO("%s", msg->infos[0].color.c_str());
  ROS_INFO("%s", msg->infos[0].name.c_str());
}

int main(int argc, char **argv) {
  // create a node for running the subscriber
  ros::init(argc, argv, "infodata_subscriber_node");
  ros::NodeHandle nh;

  // create the subscriber
  ros::Subscriber sub = nh.subscribe("/infodata", 1000, infodataCallback);

  // Run the subscriber until someone stops the program with Ctrl + C
  ros::spin();

  return 0;
}

Add the following lines to CMakeLists.txt, under the Build section, so the C++ can be compiled:

add_executable(infodata_subscriber src/infodata_subscriber.cpp)
add_dependencies(infodata_subscriber ${cpp_python_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(infodata_subscriber
   ${catkin_LIBRARIES}
 )

Compile the C++ node:

# make sure you run this from the root of your catkin workspace (e.g catkin_ws)
catkin_make

Step 4: Run the Python publisher node

# make sure you run this from the root of your catkin workspace (e.g catkin_ws)
source devel/setup.bash
nohup roscore &
rosrun cpp_python infodata_publisher.py

Nothing is happening? Nah, wait until we launch the subscriber! So, then, let’s do it!

Step 5: Run the C++ subscriber node

Open another terminal and run:

user:~$ cd catkin_ws/
user:~/catkin_ws$ source devel/setup.bash
user:~/catkin_ws$ rosrun cpp_python infodata_subscriber
[ INFO] [1583243129.975478016]: 5
[ INFO] [1583243129.975588039]: red
[ INFO] [1583243129.975620409]: data1
[ INFO] [1583243130.975436776]: 5
[ INFO] [1583243130.975521758]: red
[ INFO] [1583243130.975555258]: data1
[ INFO] [1583243131.975349645]: 5
# ...

So that was it! That’s how to create a C++ subscriber for a Python publisher node.

Extra: Video of this post

We made a video showing how we solved this challenge. If you prefer “sights and sounds” to “black and white”, here you go:

Feedback

Did you like this post? Do you have any questions about the explanations? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it.


Edited by Bayode Aderinola


Want to play with the complete code right away? You can get a copy of the ROSject at the link below (requires an account on ROSDS).

[ROS Q&A] 189 – How to create a custom action message

[ROS Q&A] 189 – How to create a custom action message

About

Learn how to create and compile a custom action message for ROS Actions from zero.

You’ll learn

  • How to define the message 
  • How to modify the CMakeLists.txt and package.xml files
  • How to compile and import the message for ROS Actions

Below are the Steps to create the project as shown in the video

Step 1

  • Head to ROS Development Studio and create a new project.
  • Provide a suitable project name and some useful description. (We have named the project how_to_make_custom_ros_action)
  • Load/Start the project (this will take a few seconds).
  • Let’s create a new catkin package in our catkin workspace. Open Tools > Shell and run the following commands

$ cd catkin_ws/src
$ catkin_create_pkg turtlebot3_console_controller actionlib_msgs std_msgs

  • We’ll create an action directory to put the action file. We will create an action file named Motor.action inside the action directory

$ cd turtlebot3_console_controller
$ mkdir action
$ touch Motor.action

  • Use Tools > IDE to browse the directory and file we just created. At this point we should have the following directory structure
    .
    ├── ai_ws
    ├── catkin_ws
    │   ├── build
    │   ├── devel
    │   └── src
    │       ├── CMakeLists.txt
    │       └── turtlebot3_console_controller
    │           ├── action
    │           │   └── Motor.action
    │           ├── CMakeLists.txt
    │           └── package.xml
    ├── how_to_make_custom_ros_action.zip
    ├── notebook_ws
    │   ├── default.ipynb
    │   └── images
    └── simulation_ws
      ├── build
      ├── devel
      └── src
    

Step 2

  • Open the Motor.action file in IDE and write the following content to it.
# This is an action definition file, which has three parts: the goal
# the result, and the feedback.
# Part 1: the goal.
#
# The angle in degree the robot to turn, sent by client main
float64 angle
---
# Part 2: the result, sent by action server unpon completion
#
# How much time used
duration time_elapsed
# How many updates thrown in total
uint32 updates_n
---
# Part 3: the feedback,to be sent periodically by server
#
# The amount of time elapsed from the start
duration time_elapsed
  • The contents of Motor.action defines variables of three categories. The categories are separated by lines. The first category of variables are related to GOAL, second is related to RESULT and third are related to the FEEDBACK. Here we have the following variable in different categories: angle for GOAL, time_elapsed, updates_n for RESULT and time_elapsed for FEEDBACK.
  • Now we have to add the directive to compile action file in CMakeLists.txt file. This file is inside our package directory i.e. ~/catkin_ws/src/turtlebot3_console_controller. Open the CMakeLists.txt file in IDE
  • Add the following code block
    add_action_files(
    FILES
    Motor.action
    )
    
  • Also, add this code block
    generate_messages(
    DEPENDENCIES
    actionlib_msgs
    )
    
  • Let’s compile the project. Run following commands in the SHELL

    $ cd ~/catkin_ws

    $ catkin_make

  • To test if the action file works we will create a script name node.py inside a new directory within the package. Use the following commands in SHELL

    $ cd ~/catkin_ws/src/turtlebot3_console_controller

    $ mkdir src

    $ touch node.py

  • To create the directory and file we can use IDE tool as well. Let’s add the following code to the node.py file
from turtlebot3_console_controller.msg import MotorGoal, MotorResult, MotorFeedback 

print("OK, it worked!!!")

Let’s run the above script. Enter the following commands in the SHELL

$ cd ~/catkin_ws/src/turtlebot3_console_controller/src

$ ./node.py

After running the above command you should see the reply OK, it worked!!! in the next line.

And that’s it. We have created a basic action message and verified it works.

Related courses

Pan & Tilt Face Tracking in ROS

Pan & Tilt Face Tracking in ROS

About

Do you want to do a face tracking Pan & Tilt system with ROS? This is the tutorial for you! This video will show you how to do a face tracking system using ROS and all the system we generated in the previous videos.

We will learn:

  • How to integrate icog_face_tracker package in ROS
  • How to use face tracking data to do a simple face tracker for a Pan &  Tilt

List of materials

Related courses & tutorials

3D Models

[irp posts=”13228″ name=”Pan & Tilt Gazebo Simulation”]

Pin It on Pinterest