First Commit

This commit is contained in:
2024-12-02 15:11:30 +01:00
commit 031f6004de
4688 changed files with 441558 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
[[ROS2]] is a bit tedious to debug, because it is inherently asynchronous and multi threaded.
A good way is to use VSCode for example like this:
https://gist.github.com/JADC362/a4425c2d05cdaadaaa71b697b674425f
As always the nav2 library is a good place for resources: [get backtrace explanation](https://navigation.ros.org/tutorials/docs/get_backtrace.html).
## Requirements
In order to run this we need to install gdbserver: `sudo apt install gdbserver`
## Launch Files
In the launch file, when adding a Node, add a `prefix="gdbserver localhost:3000"`:
```python
Node(
package='nav3_controller',
executable='controller_server',
output='screen',
respawn=use_respawn,
respawn_delay=2.0,
parameters=[configured_params],
prefix='gdbserver localhost:3000',
arguments=['--ros-args', '--log-level', log_level],
remappings=remappings + [('offboard_cmd', 'offboard_velocity_cmd')]
)
```
This will create gdb as a server. Now we need to configure VSCode to attach to this debug session (as explained [here](https://answers.ros.org/question/267261/how-can-i-run-ros2-nodes-in-a-debugger-eg-gdb/)) add a new launch configuration like this:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debugger",
"request": "launch",
"type": "cppdbg",
"miDebuggerServerAddress": "localhost:3000",
"cwd": "/",
"program": "[build-path-executable]"
}
]
}
```

View File

@@ -0,0 +1,31 @@
Also known as managed Nodes in [[ROS2]]. The [[ROS2 - NAV2 Library|NAV2]] library makes good use of it.
From [ROS2 Design](https://design.ros2.org/articles/node_lifecycle.html):
>A managed life cycle for nodes allows greater control over the state of ROS system. It will allow roslaunch to ensure that all components have been instantiated correctly before it allows any component to begin executing its behaviour. It will also allow nodes to be restarted or replaced on-line.
>The most important concept of this document is that a managed node presents a known interface, executes according to a known life cycle state machine, and otherwise can be considered a black box. This allows freedom to the node developer on how they provide the managed life cycle functionality, while also ensuring that any tools created for managing nodes can work with any compliant node.
There are 4 primary states: *unconfigured, inactive, active, finalized*
There are 7 transitions: *create, configure, cleanup, activate, deactivate, shutdown and destroy*
## States
All nodes start with the **unconfigured** state, which is kind of like an empty state where everything starts but it might also end there
More important is the **inactive** state. its purpose is to breath life into a node. It allows the user to read parameters, add subscriptions and publications and (re)configure it such that it can fulfill its job. This is done while the node is not running. While a node is in this state it will not receive any data from other processes.
## Transition Callbacks
The main functions to implement for a custom node in the lifecycle scheme are:
### onConfigure()
Here the things are implemented that are executed only once in the Node's lifetime, such as obtaining permanent memory buffers and setting up topic publications/subscriptions that do not change.
### onCleanup()
This is the transition function that is called when a Node is being taken out of service (essentially the oposite of *onConfigure()*. Essentially it leaves the node without a state, such that there is no difference between a node that got cleaned up and another that was just created.
### onActivate()
This callback is responsible to implement any final preparations before the node is executing its main purpose. Examples are acquiring resources needed for execution such as access to hardware (it should return fast without a lengthy hardware startup).
### onDeactivate()
This callback should undo anything that *onActivate()* did.
## Management Interface
This is a common interface to allow a managing node to manage the different lifecycle nodes accordingly.
## Managing Node
This is the node that loads the different lifecycle nodes and is responsible to bring them from one state into the next and handle any error they feed back.

View File

@@ -0,0 +1,40 @@
---
aliases:
- NAV2
- Navigation
- ROS2
---
- How is the NAV2 library structured?
- What are the core concepts?
- How can we utilize it to improve our own code?
# ROS2 and NAV2
Because navigation is usually a long running task, [[ROS2 - NAV2 Library|NAV2]] uses [[ROS2]] actions (servers and clients) to implement and execute navigation tasks. Actions servers implement the actual execution in a separate thread and thus can be running in a blocking manner (a [shared Future object](https://en.wikipedia.org/wiki/Futures_and_promises) is used to communicate Feedback and results). The feedback can be shared both synchronously through callbacks and asynchronously through requesting information from the shared future object. In any case we spinning the client node is required.
## Lifecycle Nodes (aka. Mangaged Nodes)
NAV2 relies heavily on [[ROS2 - Lifecycle Nodes]], because it helps to structure the program in reasonable ways for commercial uses and debugging.
All servers in NAV2 use the lifecycle nodes and it is the best convention for all ROS systems to use lifecycle nodes if possible.
# Behavior Trees
[[Behaviour Trees]] are used as the main concept to implement complex tasks and the application logic. In order to do that behaviors are broken down into primitives (very basic behavior).
From [NAV2 documentation:](https://navigation.ros.org/concepts/index.html)
>For this project, we use [BehaviorTree CPP V3](https://www.behaviortree.dev/) as the behavior tree library. We create node plugins which can be constructed into a tree, inside the `BT Navigator`. The node plugins are loaded into the BT and when the XML file of the tree is parsed, the registered names are associated. At this point, we can march through the behavior tree to navigate.
# Source Code Walk Through
The version of nav2 I used to write our own nav3 library was: 7009ffba on october 16th 2023.
## NAV2 Common
This package contains only launchfiles that implement classes to facilitate writing other launch files. Functions like rewriting part of a file, replacing strings, check if node parameters are available, etc.
# Navigation Servers
## Planners
## Controllers
There is a single GoalChecker Plugin that implements the GoalChecker interface. It is called SimpleGoalChecker
# Navigation illumination Video
[Vimeo Video](https://vimeo.com/106994708)
<div style="padding:56.25% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/106994708?h=5972d2a502" style="position:absolute;top:0;left:0;width:100%;height:100%;" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>
Controller (Local Planner)
![[Pasted image 20231019145329.png]]

View File

@@ -0,0 +1,83 @@
The Pluginlib is a library for [[ROS2]] that allows a very modular development. It is heavily used in the [[ROS2 - NAV2 Library|NAV2]] Library.
From the [pluginlib tutorial]():
>`pluginlib` is a C++ library for loading and unloading plugins from within a ROS package. Plugins are dynamically loadable classes that are loaded from a runtime library (i.e. shared object, dynamically linked library). With pluginlib, you do not have to explicitly link your application against the library containing the classes instead `pluginlib` can open a library containing exported classes at any point without the application having any prior awareness of the library or the header file containing the class definition. Plugins are useful for extending/modifying application behavior without needing the application source code.
Basically it allows to define an abstract base class that defines the interface of the plugin. It defines what functions (`virtual`) need to be overwritten and what variables are there. You can then derive multiple packages with different implementations of this plugin base class which is used by an executor function. Those plugins can then be loaded at runtime without prior knowledge about them because they follow the same structure.
Requirements:
1. Constructor without parameters -> use initialization function instead.
2. Make header available to other classes
1. Add the following snippet to `CMakeLists.txt`:
```cmake
install(
DIRECTORY include/
DESTINATION include
)
...
ament_export_include_directories(
include
)
```
3. In the c++ file where you define your plugins you need to add the following macro at the very end. This creates the plugin instances when the corresponding library is loaded.
```cpp
#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon)
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon)
```
4. The plugin loader needs some information to find the library and to know what to reference in the libary. Thus an xml-file needs to be written as well as an export line in the package.xml file. With those 2 additions ROS knows everything it needs to know in order to use the plugins. In the following snippets we have the two Plugins: Square and Triangle defined in a plugin.xml file.
```xml
<library path="polygon_plugins">
<class type="polygon_plugins::Square" base_class_type="polygon_base::RegularPolygon">
<description>This is a square plugin.</description>
</class>
<class type="polygon_plugins::Triangle" base_class_type="polygon_base::RegularPolygon">
<description>This is a triangle plugin.</description>
</class>
</library>
```
```cmake
# polygon_base: package with base class
# plugins.xml: relative path to plugin file defined above
pluginlib_export_plugin_description_file(polygon_base plugins.xml)
```
How to use the plugins
The plugins can be used in any package that you want
```cpp
#include <pluginlib/class_loader.hpp>
#include <polygon_base/regular_polygon.hpp>
int main(int argc, char** argv)
{
// To avoid unused parameter warnings
(void) argc;
(void) argv;
pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("polygon_base", "polygon_base::RegularPolygon");
try
{
std::shared_ptr<polygon_base::RegularPolygon> triangle = poly_loader.createSharedInstance("polygon_plugins::Triangle");
triangle->initialize(10.0);
std::shared_ptr<polygon_base::RegularPolygon> square = poly_loader.createSharedInstance("polygon_plugins::Square");
square->initialize(10.0);
printf("Triangle area: %.2f\n", triangle->area());
printf("Square area: %.2f\n", square->area());
}
catch(pluginlib::PluginlibException& ex)
{
printf("The plugin failed to load for some reason. Error: %s\n", ex.what());
}
return 0;
}
```
>Important note: the `polygon_base` package in which this node is defined does NOT depend on the `polygon_plugins` class. The plugins will be loaded dynamically without any dependency needing to be declared. Furthermore, were instantiating the classes with hardcoded plugin names, but you can also do so dynamically with parameters, etc.

View File

@@ -0,0 +1,6 @@
- [ ] #todo/b Write overview of what ros2 does in my own words. Advantages / disadvantages
# Build System
The ROS2 [build system](https://docs.ros.org/en/humble/Concepts/Advanced/About-Build-System.html)is a challenging part, because packages written in different languages such as [[C++]] or [[Python]] need to be built together in order to form a unit.
To achieve this ROS2 relies heavily on the [[Colcon]] build system, which under the hood uses [[CMake]] for C++ packages and setuptools for Python. In order to define dependencies across the different packages and languages, ROS2 packages always contain a `package.xml` file also known as manifest file that contains essential metadata about the package, such as dependencies and others.