Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_UROS: add micro-ROS client #25154

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

srmainwaring
Copy link
Contributor

@srmainwaring srmainwaring commented Sep 30, 2023

A draft PR to investigate using the micro-ROS client library directly as an alternative to AP_DDS.

See: #23424 and the comment on the micro-ROS PoC.

Dependencies

The PR adds a module containing pre-built instances of the micro-ROS client library for various target platforms.

This is to separate out the fairly complex process of configuring and building the client libraries. Including this step directly in the waf build would add a dependency on colcon and ROS.

Features

The AP_UROS library is feature compatible with AP_DDS. This means it can be used as a drop in replacement for AP_DDS for external control.

  • publish all topics supported by AP_DDS
  • arm motors and mode switch services enabled
  • external control and odometry enabled

In addition there is a prototype parameter server. This is not yet integrated with ArduPilot's parameter system.

micro-ROS links

The following micro-ROS tutorials describe the process of creating the client libraries:

Tasks

  • Fully document process for building the client libraries for each platform (Ubuntu, macOS, stm32, esp32, ).
  • Remove hardcoded platform target in AP_UROS wscript.
  • Factor out common code with AP_DDS.
  • Remove hardcoded flags from ardupilot_sitl CMakeLists.txt.

Notes

  • The file and type names of the generated bindings and headers for DDS and UROS are not compatible, so only one of --enable-dds or --enable-uros may be selected at a time (you would not want to use them concurrently in any case).
  • The accessors to the message and service interfaces for DDS and UROS are almost compatible, except for the treatment of strings and arrays. This means it should be possible to factor out common code for responding to services requests and publishing and subscribing to topics.

Testing

See the library README for build instructions.

The off-board navigation example described in ArduPilot/ardupilot_ros#11 may be run using AP_UROS instead of AP_DDS.

Figure: MAVProxy console output from SITL running with --enable-uros.
uros-mavproxy-mode-arm

Flash

Flash usage for MatekH743 in different configurations (expand for details)

$ ./waf configure --board MatekH743
$ ./waf copter 

BUILD SUMMARY
Build directory: /Users/rhys/Code/ardupilot/ardupilot/build/MatekH743
Target          Text (B)  Data (B)  BSS (B)  Total Flash Used (B)  Free Flash (B)  External Flash Used (B)
----------------------------------------------------------------------------------------------------------
bin/arducopter   1593784      3628   258648               1597412          106516  Not Applicable      
$ ./waf configure --board MatekH743 --enable-uros
$ ./waf copter 

BUILD SUMMARY
Build directory: /Users/rhys/Code/ardupilot/ardupilot/build/MatekH743
Target          Text (B)  Data (B)  BSS (B)  Total Flash Used (B)  Free Flash (B)  External Flash Used (B)
----------------------------------------------------------------------------------------------------------
bin/arducopter   1686908      9500   252828               1696408            7512  Not Applicable 
$ ./waf configure --board MatekH743 --enable-dds
$ ./waf copter 

BUILD SUMMARY
Build directory: /Users/rhys/Code/ardupilot/ardupilot/build/MatekH743
Target          Text (B)  Data (B)  BSS (B)  Total Flash Used (B)  Free Flash (B)  External Flash Used (B)
----------------------------------------------------------------------------------------------------------
bin/arducopter   1622680      3628   258648               1626308           77620  Not Applicable   

Scripting disabled:

$ ./waf configure --board MatekH743 --disable-scripting
$ ./waf copter 

BUILD SUMMARY
Build directory: /Users/rhys/Code/ardupilot/ardupilot/build/MatekH743
Target          Text (B)  Data (B)  BSS (B)  Total Flash Used (B)  Free Flash (B)  External Flash Used (B)
----------------------------------------------------------------------------------------------------------
bin/arducopter   1449856      3508   258768               1453364          250560  Not Applicable   
$ ./waf configure --board MatekH743 --disable-scripting --enable-uros
$ ./waf copter 

BUILD SUMMARY
Build directory: /Users/rhys/Code/ardupilot/ardupilot/build/MatekH743
Target          Text (B)  Data (B)  BSS (B)  Total Flash Used (B)  Free Flash (B)  External Flash Used (B)
----------------------------------------------------------------------------------------------------------
bin/arducopter   1543492      9384   252940               1552876          151056  Not Applicable  
$ ./waf configure --board MatekH743 --disable-scripting --enable-dds
$ ./waf copter 

BUILD SUMMARY
Build directory: /Users/rhys/Code/ardupilot/ardupilot/build/MatekH743
Target          Text (B)  Data (B)  BSS (B)  Total Flash Used (B)  Free Flash (B)  External Flash Used (B)
----------------------------------------------------------------------------------------------------------
bin/arducopter   1478736      3508   258768               1482244          221680  Not Applicable

@srmainwaring srmainwaring marked this pull request as draft September 30, 2023 13:52
@srmainwaring srmainwaring force-pushed the prs/pr-micro-ros branch 5 times, most recently from 41dd963 to 74efad2 Compare October 2, 2023 21:28
@srmainwaring
Copy link
Contributor Author

srmainwaring commented Oct 3, 2023

Factor out common code

AP_UROS and AP_DDS have common code for publishing topics, handling subscriptions and services, and managing external odometry and control. This can be factored out into a common library AP_ROS which uses templated accessors to handle the different conventions in DDS and micro-ROS for strings and sequences. The AP_DDS and AP_UROS libraries provide template specialisations for the templated accessors and then use the templated versions of the common code to implement functions that would otherwise be duplicated.

This PR demonstrates the approach.

The templates for type access are in AP_ROS_TypeConversions:

// string accessor templates
template <typename S>
const char* string_data(const S* str);

template <typename S>
const char* string_data(const S& str);

// sequence accessor templates
// see: https://stackoverflow.com/questions/15911890/overriding-return-type-in-function-template-specialization

template <typename T>
uint32_t transforms_size(const T& msg);

template <typename T>
struct transforms_type{ typedef T* type; };

template <typename T>
typename transforms_type<T>::type transforms_data(const T& msg);

With example usage in AP_ROS_ExternalOdometry, for seqeunces:

    for (size_t i = 0; i < transforms_size<TFMessage>(msg); i++) {
        const auto& ros_transform_stamped = transforms_data<TFMessage>(msg)[i];
        if (!is_odometry_frame(ros_transform_stamped)) {
            continue;
        }

and for strings:

template <typename TransformStamped>
bool AP_ROS_External_Odom::is_odometry_frame(const TransformStamped& msg)
{
    char odom_parent[] = "odom";
    char odom_child[] = "base_link";
    // Assume the frame ID's are null terminated.
    return (strcmp(string_data(msg.header.frame_id), odom_parent) == 0) &&
           (strcmp(string_data(msg.child_frame_id), odom_child) == 0);
} 

Signed-off-by: Rhys Mainwaring <[email protected]>

modules: update ardupliot_uros

- Added mode_switch service interface for esp32
- Added mode_switch service interface for stm32.

Signed-off-by: Rhys Mainwaring <[email protected]>
- Add option --enable-uros.
- Add define AP_UROS_ENABLED.

Signed-off-by: Rhys Mainwaring <[email protected]>
Signed-off-by: Rhys Mainwaring <[email protected]>

Tools: ros2: disable DSS in colcon build

Signed-off-by: Rhys Mainwaring <[email protected]>

Tools: ros2: update README

Signed-off-by: Rhys Mainwaring <[email protected]>
Signed-off-by: Rhys Mainwaring <[email protected]>
AP_UROS: search for micro-ROS client library in ardupilot_uros module

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add vector3 subscriber example

- PoC - will only work for SITL / POSIX (must add use custom transport)

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: move init and create code into respective functions

- Make rcl objects member variables.
- Update checks macros to report to GCS.
- Move rcl initialisation to init().
- Move subscriber creation to create().

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add custom transport

- Adapt UDP custom transport from AP_DDS.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: enable custom transport

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add publisher example

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add publisher example

- Send message to GCS.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: include additional ROS message types

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add time publisher

- Add time publisher.
- Rename node to 'ardupilot_uros'.
- Change timer period to 1 ms

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add publishers for local pose and twist

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add publishers for battery, clock, nav sat, static tf

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: clarify number of handles calculation

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: implement topic writers using code from AP_DDS

- Strings and sequence values are not populated as memory must be allocated.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add code for static transform from AP_DDS

- Strings and sequence values are not populated as memory must be allocated.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: manage memory for topic strings and sequences

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: only update nav sat fix on new data to match AP_DDS

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add subscriber to sensor_msgs/msg/Joy

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: remove demo subscriber to geometry_msgs/msg/Vector3

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: update @todo formatting

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add function declarations for Ubuntu build

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: disable tf2_msgs for esp32 dev

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: move global variables into AP_UROS_Client class

- Make singleton.
- Make update_topic member function.
- Add trampoline for thread task.
- Move thread to core 1 and adjust priority.
- Customise initialisation.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: fix initialisation of local twist publisher

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: publish geopose, static_tf, and subscribe to tf

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: use trampoline functions for timer and message callbacks

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add arm motors service

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add parameter server

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add README

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: display parameter changes in GCS

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: implementation of custom serial transport

- Note this is ESP32 specific.
- Requires micro-ROS client library to be built for custom transport.
- UART_NUM_2 not managed by AP_HAL
- Return if publisher or subscriber initialisation fails

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: call uros_port->begin(0) to ensure thread owns port

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: support build for stm32 - disable param server

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: support both esp32 and stm32

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add compile option for param server

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: update macros for debug and error messages

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: fix bug introduced into entity handle count

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: update print format specifier for TF message

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: enable UDP transport for SITL

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: update print format specifier for TF message

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add microros library distro options to wscript

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: fix library paths in wscript

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add stubs for mode switch service

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: implement arming service

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: implement mode switch service

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add stub for velocity control subscriber

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: add classes adapted from AP_DDS to manage external control

- Functions are stubs only.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: adapt external control functions to micro-ROS message types

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: enable external odom in uros client

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: update GCS messages for services

- Add UROS: prefix.

Signed-off-by: Rhys Mainwaring <[email protected]>

AP_UROS: update README

Signed-off-by: Rhys Mainwaring <[email protected]>
Signed-off-by: Rhys Mainwaring <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🏗 In progress
Development

Successfully merging this pull request may close these issues.

1 participant