2023-08-08 21:41:46 -03:00
|
|
|
#if AP_DDS_ENABLED
|
|
|
|
|
|
|
|
#include "AP_DDS_ExternalControl.h"
|
|
|
|
#include "AP_DDS_Frames.h"
|
2023-09-03 21:04:58 -03:00
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2023-08-08 21:41:46 -03:00
|
|
|
|
|
|
|
#include <AP_ExternalControl/AP_ExternalControl.h>
|
|
|
|
|
|
|
|
bool AP_DDS_External_Control::handle_velocity_control(geometry_msgs_msg_TwistStamped& cmd_vel)
|
|
|
|
{
|
|
|
|
auto *external_control = AP::externalcontrol();
|
|
|
|
if (external_control == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-03 21:04:58 -03:00
|
|
|
|
|
|
|
if (strcmp(cmd_vel.header.frame_id, BASE_LINK_FRAME_ID) == 0) {
|
|
|
|
// Convert commands from body frame (x-forward, y-left, z-up) to NED.
|
|
|
|
Vector3f linear_velocity;
|
|
|
|
Vector3f linear_velocity_base_link {
|
|
|
|
float(cmd_vel.twist.linear.x),
|
|
|
|
float(cmd_vel.twist.linear.y),
|
|
|
|
float(-cmd_vel.twist.linear.z) };
|
|
|
|
const float yaw_rate = -cmd_vel.twist.angular.z;
|
|
|
|
|
|
|
|
auto &ahrs = AP::ahrs();
|
|
|
|
linear_velocity = ahrs.body_to_earth(linear_velocity_base_link);
|
|
|
|
return external_control->set_linear_velocity_and_yaw_rate(linear_velocity, yaw_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strcmp(cmd_vel.header.frame_id, MAP_FRAME) == 0) {
|
|
|
|
// Convert commands from ENU to NED frame
|
|
|
|
Vector3f linear_velocity {
|
|
|
|
float(cmd_vel.twist.linear.y),
|
|
|
|
float(cmd_vel.twist.linear.x),
|
|
|
|
float(-cmd_vel.twist.linear.z) };
|
|
|
|
const float yaw_rate = -cmd_vel.twist.angular.z;
|
|
|
|
return external_control->set_linear_velocity_and_yaw_rate(linear_velocity, yaw_rate);
|
2023-08-08 21:41:46 -03:00
|
|
|
}
|
|
|
|
|
2023-09-03 21:04:58 -03:00
|
|
|
return false;
|
2023-08-08 21:41:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // AP_DDS_ENABLED
|