Skip to content

Commit f3c5599

Browse files
committed
AP_DDS: Enable offboard attitude control via ros2
1 parent 5e5fde7 commit f3c5599

19 files changed

+158
-12
lines changed

ArduCopter/AP_ExternalControl_Copter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ bool AP_ExternalControl_Copter::set_linear_velocity_and_yaw_rate(const Vector3f
2929
return true;
3030
}
3131

32+
/*
33+
Sets the target orientation, angular velocity, angular acceleration and desired thrust
34+
in body frame
35+
*/
36+
bool AP_ExternalControl_Copter::set_angular_goals(const Quaternion &orientation, const Vector3f &angular_velocity, const Vector3f &angular_acceleration, const float &thrust)
37+
{
38+
if (!ready_for_external_control()) {
39+
return false;
40+
}
41+
copter.mode_guided.set_angle(orientation, angular_velocity, angular_acceleration, thrust, true);
42+
return true;
43+
}
44+
3245
bool AP_ExternalControl_Copter::ready_for_external_control()
3346
{
3447
return copter.flightmode->in_guided_mode() && copter.motors->armed();

ArduCopter/AP_ExternalControl_Copter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class AP_ExternalControl_Copter : public AP_ExternalControl {
1515
Yaw is in earth frame, NED [rad/s].
1616
*/
1717
bool set_linear_velocity_and_yaw_rate(const Vector3f &linear_velocity, float yaw_rate_rads) override WARN_IF_UNUSED;
18+
19+
/*
20+
Sets the target orientation, angular velocity, angular acceleration and desired thrust
21+
in body frame
22+
*/
23+
bool set_angular_goals(const Quaternion &orientation, const Vector3f &angular_velocity, const Vector3f &angular_acceleration, const float &thrust) override WARN_IF_UNUSED;
24+
1825
private:
1926
/*
2027
Return true if Copter is ready to handle external control data.

ArduCopter/Copter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ bool Copter::set_target_angle_and_climbrate(float roll_deg, float pitch_deg, flo
382382
Quaternion q;
383383
q.from_euler(radians(roll_deg),radians(pitch_deg),radians(yaw_deg));
384384

385-
mode_guided.set_angle(q, Vector3f{}, climb_rate_ms*100, false);
385+
mode_guided.set_angle(q, Vector3f{}, Vector3f{},climb_rate_ms*100, false);
386386
return true;
387387
}
388388
#endif

ArduCopter/GCS_Mavlink.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,8 @@ void GCS_MAVLINK_Copter::handle_message(const mavlink_message_t &msg)
12611261
ang_vel.z = packet.body_yaw_rate;
12621262
}
12631263

1264-
copter.mode_guided.set_angle(attitude_quat, ang_vel,
1264+
Vector3f angular_acceleration{0.0, 0.0, 0.0}; // only for use with custom controllers, hence 0 by default
1265+
copter.mode_guided.set_angle(attitude_quat, ang_vel, angular_acceleration,
12651266
climb_rate_or_thrust, use_thrust);
12661267

12671268
break;

ArduCopter/mode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ class ModeGuided : public Mode {
10371037
// climb_rate_cms_or_thrust: represents either the climb_rate (cm/s) or thrust scaled from [0, 1], unitless
10381038
// use_thrust: IF true: climb_rate_cms_or_thrust represents thrust
10391039
// IF false: climb_rate_cms_or_thrust represents climb_rate (cm/s)
1040-
void set_angle(const Quaternion &attitude_quat, const Vector3f &ang_vel, float climb_rate_cms_or_thrust, bool use_thrust);
1040+
void set_angle(const Quaternion &attitude_quat, const Vector3f &ang_vel, const Vector3f &ang_acceleration ,float climb_rate_cms_or_thrust, bool use_thrust);
10411041

10421042
bool set_destination(const Vector3f& destination, bool use_yaw = false, float yaw_cd = 0.0, bool use_yaw_rate = false, float yaw_rate_cds = 0.0, bool yaw_relative = false, bool terrain_alt = false);
10431043
bool set_destination(const Location& dest_loc, bool use_yaw = false, float yaw_cd = 0.0, bool use_yaw_rate = false, float yaw_rate_cds = 0.0, bool yaw_relative = false);

ArduCopter/mode_guided.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct {
1616
uint32_t update_time_ms;
1717
Quaternion attitude_quat;
1818
Vector3f ang_vel;
19+
Vector3f ang_acceleration; // angular acceleration, not used by main PID controller but can be used in custom controllers
1920
float yaw_rate_cds;
2021
float climb_rate_cms; // climb rate in cms. Used if use_thrust is false
2122
float thrust; // thrust from -1 to 1. Used if use_thrust is true
@@ -315,6 +316,7 @@ void ModeGuided::angle_control_start()
315316
guided_angle_state.update_time_ms = millis();
316317
guided_angle_state.attitude_quat.from_euler(Vector3f(0.0, 0.0, attitude_control->get_att_target_euler_rad().z));
317318
guided_angle_state.ang_vel.zero();
319+
guided_angle_state.ang_acceleration.zero();
318320
guided_angle_state.climb_rate_cms = 0.0f;
319321
guided_angle_state.yaw_rate_cds = 0.0f;
320322
guided_angle_state.use_yaw_rate = false;
@@ -635,7 +637,7 @@ bool ModeGuided::use_wpnav_for_position_control() const
635637
// climb_rate_cms_or_thrust: represents either the climb_rate (cm/s) or thrust scaled from [0, 1], unitless
636638
// use_thrust: IF true: climb_rate_cms_or_thrust represents thrust
637639
// IF false: climb_rate_cms_or_thrust represents climb_rate (cm/s)
638-
void ModeGuided::set_angle(const Quaternion &attitude_quat, const Vector3f &ang_vel, float climb_rate_cms_or_thrust, bool use_thrust)
640+
void ModeGuided::set_angle(const Quaternion &attitude_quat, const Vector3f &ang_vel, const Vector3f &ang_acceleration,float climb_rate_cms_or_thrust, bool use_thrust)
639641
{
640642
// check we are in velocity control mode
641643
if (guided_mode != SubMode::Angle) {
@@ -644,6 +646,7 @@ void ModeGuided::set_angle(const Quaternion &attitude_quat, const Vector3f &ang_
644646

645647
guided_angle_state.attitude_quat = attitude_quat;
646648
guided_angle_state.ang_vel = ang_vel;
649+
guided_angle_state.ang_acceleration = ang_acceleration;
647650

648651
guided_angle_state.use_thrust = use_thrust;
649652
if (use_thrust) {
@@ -978,7 +981,7 @@ void ModeGuided::angle_control_run()
978981
if (guided_angle_state.attitude_quat.is_zero()) {
979982
attitude_control->input_rate_bf_roll_pitch_yaw(ToDeg(guided_angle_state.ang_vel.x) * 100.0f, ToDeg(guided_angle_state.ang_vel.y) * 100.0f, ToDeg(guided_angle_state.ang_vel.z) * 100.0f);
980983
} else {
981-
attitude_control->input_quaternion(guided_angle_state.attitude_quat, guided_angle_state.ang_vel);
984+
attitude_control->input_quaternion(guided_angle_state.attitude_quat, guided_angle_state.ang_vel, guided_angle_state.ang_acceleration);
982985
}
983986

984987
// call position controller

Tools/ros2/ardupilot_msgs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ find_package(rosidl_default_generators REQUIRED)
1414

1515
rosidl_generate_interfaces(${PROJECT_NAME}
1616
"msg/GlobalPosition.msg"
17+
"msg/AngularVelandAccn.msg"
1718
"srv/ArmMotors.srv"
1819
"srv/ModeSwitch.srv"
1920
DEPENDENCIES geometry_msgs std_msgs

libraries/AC_AttitudeControl/AC_AttitudeControl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,11 @@ void AC_AttitudeControl::reset_rate_controller_I_terms_smoothly()
228228

229229
// Command a Quaternion attitude with feedforward and smoothing
230230
// attitude_desired_quat: is updated on each time_step by the integral of the angular velocity
231-
void AC_AttitudeControl::input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_target)
231+
void AC_AttitudeControl::input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_target, Vector3f ang_acceleration_target)
232232
{
233233
Quaternion attitude_error_quat = _attitude_target.inverse() * attitude_desired_quat;
234234
Vector3f attitude_error_angle;
235235
attitude_error_quat.to_axis_angle(attitude_error_angle);
236-
237236
// Limit the angular velocity
238237
ang_vel_limit(ang_vel_target, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), radians(_ang_vel_yaw_max));
239238

@@ -260,7 +259,7 @@ void AC_AttitudeControl::input_quaternion(Quaternion& attitude_desired_quat, Vec
260259
attitude_desired_update.from_axis_angle(ang_vel_target * _dt);
261260
attitude_desired_quat = attitude_desired_quat * attitude_desired_update;
262261
attitude_desired_quat.normalize();
263-
262+
_ang_acceleration_target = ang_acceleration_target;
264263
// Call quaternion attitude controller
265264
attitude_controller_run_quat();
266265
}

libraries/AC_AttitudeControl/AC_AttitudeControl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ class AC_AttitudeControl {
161161

162162
// Command a Quaternion attitude with feedforward and smoothing
163163
// attitude_desired_quat: is updated on each time_step (_dt) by the integral of the angular velocity
164-
virtual void input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_target);
164+
// Angular acceleration target will be ignored, only for use with custom controllers
165+
virtual void input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_target, Vector3f ang_acceleration_target);
165166

166167
// Command an euler roll and pitch angle and an euler yaw rate with angular velocity feedforward and smoothing
167168
virtual void input_euler_angle_roll_pitch_euler_rate_yaw(float euler_roll_angle_cd, float euler_pitch_angle_cd, float euler_yaw_rate_cds);
@@ -491,6 +492,10 @@ class AC_AttitudeControl {
491492
// It is reset to zero immediately after it is used.
492493
Vector3f _sysid_ang_vel_body;
493494

495+
// This is the angular acceleration in radians per second^2 in the body frame. This is not used in
496+
// main attitude controller but can be used in custom attitude controllers.
497+
Vector3f _ang_acceleration_target;
498+
494499
// This is the unitless value added to the output of the PID by the System Identification Mode.
495500
// It is reset to zero immediately after it is used.
496501
Vector3f _actuator_sysid;

libraries/AC_AttitudeControl/AC_AttitudeControl_Multi_6DoF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ void AC_AttitudeControl_Multi_6DoF::input_angle_step_bf_roll_pitch_yaw(float rol
149149
// Command a Quaternion attitude with feedforward and smoothing
150150
// attitude_desired_quat: is updated on each time_step (_dt) by the integral of the angular velocity
151151
// not used anywhere in current code, panic in SITL so this implementation is not overlooked
152-
void AC_AttitudeControl_Multi_6DoF::input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_target) {
152+
void AC_AttitudeControl_Multi_6DoF::input_quaternion(Quaternion& attitude_desired_quat, Vector3f ang_vel_target, Vector3f ang_acceleration_target) {
153153
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
154154
AP_HAL::panic("input_quaternion not implemented AC_AttitudeControl_Multi_6DoF");
155155
#endif
156156

157157
_motors.set_lateral(0.0f);
158158
_motors.set_forward(0.0f);
159159

160-
AC_AttitudeControl_Multi::input_quaternion(attitude_desired_quat, ang_vel_target);
160+
AC_AttitudeControl_Multi::input_quaternion(attitude_desired_quat, ang_vel_target, ang_acceleration_target);
161161
}
162162

163163

0 commit comments

Comments
 (0)