Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_DDS/AP_DDS_External_Odom.cpp
Views: 1798
12#include "AP_DDS_External_Odom.h"3#include "AP_DDS_Type_Conversions.h"45#if AP_DDS_VISUALODOM_ENABLED67#include <AP_VisualOdom/AP_VisualOdom.h>8#include <GCS_MAVLink/GCS.h>910void AP_DDS_External_Odom::handle_external_odom(const tf2_msgs_msg_TFMessage& msg)11{12auto *visual_odom = AP::visualodom();13if (visual_odom == nullptr) {14return;15}1617for (size_t i = 0; i < msg.transforms_size; i++) {18const auto& ros_transform_stamped = msg.transforms[i];19if (!is_odometry_frame(ros_transform_stamped)) {20continue;21}22const uint64_t remote_time_us {AP_DDS_Type_Conversions::time_u64_micros(ros_transform_stamped.header.stamp)};2324Vector3f ap_position;25Quaternion ap_rotation;2627convert_transform(ros_transform_stamped.transform, ap_position, ap_rotation);28// Although ROS convention states quaternions in ROS messages should be normalized, it's not guaranteed.29// Before propagating a potentially inaccurate quaternion to the rest of AP, normalize it here.30// TODO what if the quaternion is NaN?31ap_rotation.normalize();3233// No error is available in TF, trust the data as-is34const float posErr {0.0};35const float angErr {0.0};36// The odom to base_link transform used is locally consistent per ROS REP-105.37// https://www.ros.org/reps/rep-0105.html#id1638// Thus, there will not be any resets.39const uint8_t reset_counter {0};40// TODO implement jitter correction similar to GCS_MAVLINK::correct_offboard_timestamp_usec_to_ms(remote_time_us, sizeof(msg));41const uint32_t time_ms {static_cast<uint32_t>(remote_time_us * 1E-3)};42visual_odom->handle_pose_estimate(remote_time_us, time_ms, ap_position.x, ap_position.y, ap_position.z, ap_rotation, posErr, angErr, reset_counter, 0);4344}45}4647bool AP_DDS_External_Odom::is_odometry_frame(const geometry_msgs_msg_TransformStamped& msg)48{49char odom_parent[] = "odom";50char odom_child[] = "base_link";51// Assume the frame ID's are null terminated.52return (strcmp(msg.header.frame_id, odom_parent) == 0) &&53(strcmp(msg.child_frame_id, odom_child) == 0);54}5556void AP_DDS_External_Odom::convert_transform(const geometry_msgs_msg_Transform& ros_transform, Vector3f& translation, Quaternion& rotation)57{58// convert from x-forward, y-left, z-up to NED59// https://github.com/mavlink/mavros/issues/49#issuecomment-5161413060translation = {61static_cast<float>(ros_transform.translation.x),62static_cast<float>(-ros_transform.translation.y),63static_cast<float>(-ros_transform.translation.z)64};6566// In AP, q1 is the quaternion's scalar component.67// In ROS, w is the quaternion's scalar component.68// https://docs.ros.org/en/humble/Tutorials/Intermediate/Tf2/Quaternion-Fundamentals.html#components-of-a-quaternion69rotation.q1 = ros_transform.rotation.w;70rotation.q2 = ros_transform.rotation.x;71rotation.q3 = -ros_transform.rotation.y;72rotation.q4 = -ros_transform.rotation.z;73}7475#endif // AP_DDS_VISUALODOM_ENABLED767778