t6.rst 2.23 KB
Newer Older
CANNERE Xavier's avatar
6.1.2  
CANNERE Xavier committed
1 2
ROS2 : 1/? Create a visual follower
===================================
CANNERE Xavier's avatar
CANNERE Xavier committed
3

CANNERE Xavier's avatar
6.1  
CANNERE Xavier committed
4
We'll learn how launch ROS1 nodes with ROS2 nodes. In this example we'll see a follower robot with a camera (same concept as the sensorfollower but he will detect target with the color) in ROS2 with a node that allows object recognition.
CANNERE Xavier's avatar
CANNERE Xavier committed
5

CANNERE Xavier's avatar
6.1  
CANNERE Xavier committed
6 7 8 9 10
Step 1 : Create the visual follower package
-------------------------------------------

We'll create a new package who allow the following of a visual target called ``pkg_visu`` and a new node ``visu.cpp``::

CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
11
	ros2 pkg create --build-type ament_cmake pkg_visual --dependencies sensor_msgs geometry_msgs rclcpp OpenCV cv_bridge image_transport --node-name visual
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
12 13 14
	
.. note::
	parameters:
CANNERE Xavier's avatar
6.1.2  
CANNERE Xavier committed
15
		- dependencies : parameter to declare dependencies
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
16 17 18
		- sensor_msgs : library to use sensor, in this tuto we'll use a camera
		- geometry_msgs : library to use variables like vector or poses
		- rclcpp : primary library, allow the use of ROS with c++
CANNERE Xavier's avatar
6.1.2  
CANNERE Xavier committed
19 20 21 22 23 24 25 26 27
		- OpenCV : library allowing the exploitation of images
		- cv_bridge : library bridging opencv and ros
		- image_transport : Optimize and transport images
		- node : parameter to create nodes
		- visual : main node
		- DetectionCnam_codels : ??? (à modifier)
		
Step 2 : 
--------
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
28
			
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
29
Open on QtCreator and edit visu.cpp. You can delete all what is written. First, include the libraries::
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
30
	
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
31 32 33 34 35 36
	#include <cstdio>
	#include "opencv2/opencv.hpp"
	#include "rclcpp/rclcpp.hpp"
	#include "sensor_msgs/msg/image.hpp"
	#include "geometry_msgs/msg/twist.hpp"
	#include "cv_bridge/cv_bridge.h"
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
37
	
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
38
Now paste this universal main code::	
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
39
	
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
40 41 42 43 44 45 46 47 48
	int main(int argc, char ** argv)
	{
	  (void) argc;
	  (void) argv;
	  rclcpp::init(argc, argv);
	  rclcpp::spin(node);
	  rclcpp::shutdown();
	  return 0;
	}	
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
49
	
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
50 51 52 53 54 55 56 57 58 59
- init is the initialization
- spin is a loop of ``node`` which is not even declared
- shutdown is the end of execution

Now we'll create a class ``visual``, we'll declare inside this class	the main algorithm. Paste that between the ``include`` and the ``main()``::

	class Visu : public rclcpp::Node
	{

	};	
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
60
	
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
61 62 63
You can add the following line in the ``main`` before the ``spin``, it will define which node will be launched::

	auto node = std::make_shared<Visual>();
CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
64
	
CANNERE Xavier's avatar
6.1.3  
CANNERE Xavier committed
65

CANNERE Xavier's avatar
6.1.1  
CANNERE Xavier committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

CANNERE Xavier's avatar
6.2  
CANNERE Xavier committed
100