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_ExternalControl.cpp
Views: 1798
#include "AP_DDS_config.h"12#if AP_DDS_ENABLED34#include "AP_DDS_ExternalControl.h"5#include "AP_DDS_Frames.h"6#include <AP_AHRS/AP_AHRS.h>78#include <AP_ExternalControl/AP_ExternalControl.h>910// These are the Goal Interface constants. Because microxrceddsgen does not expose11// them in the generated code, they are manually maintained12// Position ignore flags13static constexpr uint16_t TYPE_MASK_IGNORE_LATITUDE = 1;14static constexpr uint16_t TYPE_MASK_IGNORE_LONGITUDE = 2;15static constexpr uint16_t TYPE_MASK_IGNORE_ALTITUDE = 4;1617bool AP_DDS_External_Control::handle_global_position_control(ardupilot_msgs_msg_GlobalPosition& cmd_pos)18{19auto *external_control = AP::externalcontrol();20if (external_control == nullptr) {21return false;22}2324if (strcmp(cmd_pos.header.frame_id, MAP_FRAME) == 0) {25// Narrow the altitude26const int32_t alt_cm = static_cast<int32_t>(cmd_pos.altitude * 100);2728Location::AltFrame alt_frame;29if (!convert_alt_frame(cmd_pos.coordinate_frame, alt_frame)) {30return false;31}3233constexpr uint32_t MASK_POS_IGNORE =34TYPE_MASK_IGNORE_LATITUDE |35TYPE_MASK_IGNORE_LONGITUDE |36TYPE_MASK_IGNORE_ALTITUDE;3738if (!(cmd_pos.type_mask & MASK_POS_IGNORE)) {39Location loc(cmd_pos.latitude * 1E7, cmd_pos.longitude * 1E7, alt_cm, alt_frame);40if (!external_control->set_global_position(loc)) {41return false; // Don't try sending other commands if this fails42}43}4445// TODO add velocity and accel handling4647return true;48}4950return false;51}5253bool AP_DDS_External_Control::handle_velocity_control(geometry_msgs_msg_TwistStamped& cmd_vel)54{55auto *external_control = AP::externalcontrol();56if (external_control == nullptr) {57return false;58}5960if (strcmp(cmd_vel.header.frame_id, BASE_LINK_FRAME_ID) == 0) {61// Convert commands from body frame (x-forward, y-left, z-up) to NED.62Vector3f linear_velocity;63Vector3f linear_velocity_base_link {64float(cmd_vel.twist.linear.x),65float(cmd_vel.twist.linear.y),66float(-cmd_vel.twist.linear.z) };67const float yaw_rate = -cmd_vel.twist.angular.z;6869auto &ahrs = AP::ahrs();70linear_velocity = ahrs.body_to_earth(linear_velocity_base_link);71return external_control->set_linear_velocity_and_yaw_rate(linear_velocity, yaw_rate);72}7374else if (strcmp(cmd_vel.header.frame_id, MAP_FRAME) == 0) {75// Convert commands from ENU to NED frame76Vector3f linear_velocity {77float(cmd_vel.twist.linear.y),78float(cmd_vel.twist.linear.x),79float(-cmd_vel.twist.linear.z) };80const float yaw_rate = -cmd_vel.twist.angular.z;81return external_control->set_linear_velocity_and_yaw_rate(linear_velocity, yaw_rate);82}8384return false;85}8687bool AP_DDS_External_Control::arm(AP_Arming::Method method, bool do_arming_checks)88{89auto *external_control = AP::externalcontrol();90if (external_control == nullptr) {91return false;92}9394return external_control->arm(method, do_arming_checks);95}9697bool AP_DDS_External_Control::disarm(AP_Arming::Method method, bool do_disarm_checks)98{99auto *external_control = AP::externalcontrol();100if (external_control == nullptr) {101return false;102}103104return external_control->disarm(method, do_disarm_checks);105}106107bool AP_DDS_External_Control::convert_alt_frame(const uint8_t frame_in, Location::AltFrame& frame_out)108{109110// Specified in ROS REP-147; only some are supported.111switch (frame_in) {112case 5: // FRAME_GLOBAL_INT113frame_out = Location::AltFrame::ABSOLUTE;114break;115case 6: // FRAME_GLOBAL_REL_ALT116frame_out = Location::AltFrame::ABOVE_HOME;117break;118case 11: // FRAME_GLOBAL_TERRAIN_ALT119frame_out = Location::AltFrame::ABOVE_TERRAIN;120break;121default:122return false;123}124return true;125}126127128#endif // AP_DDS_ENABLED129130