-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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_ExternalControl: initial implementation of external control library #24549
Changes from all commits
e72467c
d409f5d
386b057
58b3319
5f46526
31a92c6
b9afe37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
external control library for copter | ||
*/ | ||
|
||
|
||
#include "AP_ExternalControl_Copter.h" | ||
#if AP_EXTERNAL_CONTROL_ENABLED | ||
|
||
#include "Copter.h" | ||
|
||
/* | ||
set linear velocity and yaw rate. Pass NaN for yaw_rate_rads to not control yaw | ||
velocity is in earth frame, NED, m/s | ||
*/ | ||
bool AP_ExternalControl_Copter::set_linear_velocity_and_yaw_rate(const Vector3f &linear_velocity, float yaw_rate_rads) | ||
{ | ||
if (!ready_for_external_control()) { | ||
return false; | ||
} | ||
const float yaw_rate_cds = isnan(yaw_rate_rads)? 0: degrees(yaw_rate_rads)*100; | ||
|
||
// Copter velocity is positive if aicraft is moving up which is opposite the incoming NED frame. | ||
Vector3f velocity_NEU_ms { | ||
linear_velocity.x, | ||
linear_velocity.y, | ||
-linear_velocity.z }; | ||
Vector3f velocity_up_cms = velocity_NEU_ms * 100; | ||
copter.mode_guided.set_velocity(velocity_up_cms, false, 0, !isnan(yaw_rate_rads), yaw_rate_cds); | ||
return true; | ||
} | ||
|
||
bool AP_ExternalControl_Copter::ready_for_external_control() | ||
{ | ||
return copter.flightmode->in_guided_mode() && copter.motors->armed(); | ||
} | ||
|
||
#endif // AP_EXTERNAL_CONTROL_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
external control library for copter | ||
*/ | ||
#pragma once | ||
|
||
#include <AP_ExternalControl/AP_ExternalControl.h> | ||
|
||
#if AP_EXTERNAL_CONTROL_ENABLED | ||
|
||
class AP_ExternalControl_Copter : public AP_ExternalControl { | ||
public: | ||
/* | ||
Set linear velocity and yaw rate. Pass NaN for yaw_rate_rads to not control yaw. | ||
Velocity is in earth frame, NED [m/s]. | ||
Yaw is in earth frame, NED [rad/s]. | ||
*/ | ||
bool set_linear_velocity_and_yaw_rate(const Vector3f &linear_velocity, float yaw_rate_rads) override; | ||
private: | ||
/* | ||
Return true if Copter is ready to handle external control data. | ||
Currently checks mode and arm states. | ||
*/ | ||
bool ready_for_external_control(); | ||
}; | ||
|
||
#endif // AP_EXTERNAL_CONTROL_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#if AP_DDS_ENABLED | ||
|
||
#include "AP_DDS_ExternalControl.h" | ||
#include "AP_DDS_Frames.h" | ||
|
||
#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; | ||
} | ||
if (strcmp(cmd_vel.header.frame_id, MAP_FRAME) != 0) { | ||
// Although REP-147 says cmd_vel should be in body frame, all the AP math is done in earth frame. | ||
// This is because accounting for the gravity vector. | ||
// Although the ROS 2 interface could support body-frame velocity control in the future, | ||
// it is currently not supported. | ||
return false; | ||
} | ||
|
||
// 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); | ||
} | ||
|
||
|
||
#endif // AP_DDS_ENABLED | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#if AP_DDS_ENABLED | ||
#include "geometry_msgs/msg/TwistStamped.h" | ||
|
||
class AP_DDS_External_Control | ||
{ | ||
public: | ||
static bool handle_velocity_control(geometry_msgs_msg_TwistStamped& cmd_vel); | ||
}; | ||
#endif // AP_DDS_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
static constexpr char WGS_84_FRAME_ID[] = "WGS-84"; | ||
// https://www.ros.org/reps/rep-0105.html#base-link | ||
static constexpr char BASE_LINK_FRAME_ID[] = "base_link"; | ||
// https://www.ros.org/reps/rep-0105.html#map | ||
static constexpr char MAP_FRAME[] = "map"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "AP_ExternalControl.h" | ||
|
||
#if AP_EXTERNAL_CONTROL_ENABLED | ||
|
||
// singleton instance | ||
AP_ExternalControl *AP_ExternalControl::singleton; | ||
|
||
AP_ExternalControl::AP_ExternalControl() | ||
{ | ||
singleton = this; | ||
} | ||
|
||
|
||
namespace AP | ||
{ | ||
|
||
AP_ExternalControl *externalcontrol() | ||
{ | ||
return AP_ExternalControl::get_singleton(); | ||
} | ||
|
||
}; | ||
|
||
#endif // AP_EXTERNAL_CONTROL_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
external control library for MAVLink, DDS and scripting | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "AP_ExternalControl_config.h" | ||
|
||
#if AP_EXTERNAL_CONTROL_ENABLED | ||
|
||
#include <AP_Math/AP_Math.h> | ||
|
||
class AP_ExternalControl | ||
{ | ||
public: | ||
|
||
AP_ExternalControl(); | ||
/* | ||
Set linear velocity and yaw rate. Pass NaN for yaw_rate_rads to not control yaw. | ||
Velocity is in earth frame, NED [m/s]. | ||
Yaw is in earth frame, NED [rad/s]. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should also say the frame convention for yaw rate? Even @lthall has realized the mavlink API's are not quite cut and dry. Although I don't recommend reading too much of this thread, this comment is useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeh, I am going to put a PR in to fix this mistake. Once I saw it I wonder why I missed it the first time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Please tag me, so we can follow the same convention here. |
||
virtual bool set_linear_velocity_and_yaw_rate(const Vector3f &linear_velocity, float yaw_rate_rads) | ||
{ | ||
return false; | ||
} | ||
|
||
static AP_ExternalControl *get_singleton(void) | ||
{ | ||
return singleton; | ||
} | ||
|
||
private: | ||
static AP_ExternalControl *singleton; | ||
}; | ||
|
||
|
||
namespace AP | ||
{ | ||
AP_ExternalControl *externalcontrol(); | ||
}; | ||
|
||
#endif // AP_EXTERNAL_CONTROL_ENABLED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <AP_Common/AP_Common.h> | ||
#include <AP_HAL/AP_HAL_Boards.h> | ||
|
||
#ifndef AP_EXTERNAL_CONTROL_ENABLED | ||
#define AP_EXTERNAL_CONTROL_ENABLED 1 | ||
#endif | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline.