Skip to content

Commit edfccc8

Browse files
committed
minor refactoring, increased drs km_lim limit
1 parent 4b11bad commit edfccc8

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

cfg/mpc_controller.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ attitude.add("kq_yaw", double_t, 0, "Attitude constant for intrinsic yaw control
2323
mass = gen.add_group("Mass estimator");
2424

2525
mass.add("km", double_t, 0, "Integral constant for mass", 0.0, 0.0, 2.0)
26-
mass.add("km_lim", double_t, 0, "mass integral limit", 0.0, 0.0, 10.0)
26+
mass.add("km_lim", double_t, 0, "mass integral limit", 0.0, 0.0, 50.0)
2727

2828
output = gen.add_group("Output");
2929

cfg/se3_controller.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ attitude.add("kq_yaw", double_t, 0, "Attitude constant for intrinsic yaw control
3232
attitude = gen.add_group("Mass estimator");
3333

3434
attitude.add("km", double_t, 0, "Integral constant for mass", 0.0, 0.0, 2.0)
35-
attitude.add("km_lim", double_t, 0, "Mass integral limit", 0.0, 0.0, 10.0)
35+
attitude.add("km_lim", double_t, 0, "Mass integral limit", 0.0, 0.0, 50.0)
3636

3737
output = gen.add_group("Output");
3838

src/mpc_controller.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ bool MpcController::initialize(const ros::NodeHandle &nh, std::shared_ptr<mrs_ua
344344

345345
_tilt_angle_failsafe_ = M_PI * (_tilt_angle_failsafe_ / 180.0);
346346

347-
if (_tilt_angle_failsafe_enabled_ && fabs(_tilt_angle_failsafe_) < 1e-3) {
347+
if (_tilt_angle_failsafe_enabled_ && std::abs(_tilt_angle_failsafe_) < 1e-3) {
348348
ROS_ERROR("[%s]: constraints/tilt_angle_failsafe/enabled = 'TRUE' but the limit is too low", name_.c_str());
349349
return false;
350350
}
@@ -499,7 +499,7 @@ bool MpcController::activate(const ControlOutput &last_control_output) {
499499
rampup_last_time_ = ros::Time::now();
500500
rampup_throttle_ = throttle_last_controller.value();
501501

502-
rampup_duration_ = fabs(throttle_difference) / _rampup_speed_;
502+
rampup_duration_ = std::abs(throttle_difference) / _rampup_speed_;
503503
}
504504

505505
first_iteration_ = true;
@@ -577,7 +577,7 @@ MpcController::ControlOutput MpcController::updateActive(const mrs_msgs::UavStat
577577

578578
last_update_time_ = uav_state.header.stamp;
579579

580-
if (fabs(dt) < 0.001) {
580+
if (std::abs(dt) < 0.001) {
581581

582582
ROS_DEBUG("[%s]: the last odometry message came too close (%.2f s)!", name_.c_str(), dt);
583583

@@ -848,22 +848,22 @@ void MpcController::MPC(const mrs_msgs::UavState &uav_state, const mrs_msgs::Tra
848848
double velocity;
849849
double coef = 1.5;
850850

851-
if (fabs(uav_state.acceleration.linear.x) < coef * max_acceleration_horizontal) {
851+
if (std::abs(uav_state.acceleration.linear.x) < coef * max_acceleration_horizontal) {
852852
acceleration = uav_state.acceleration.linear.x;
853853
} else {
854854
acceleration = tracker_command.acceleration.x;
855855

856856
ROS_ERROR_THROTTLE(1.0, "[%s]: odometry x acceleration exceeds constraints (%.2f > %.1f * %.2f m), using reference for initial condition", name_.c_str(),
857-
fabs(uav_state.acceleration.linear.x), coef, max_acceleration_horizontal);
857+
std::abs(uav_state.acceleration.linear.x), coef, max_acceleration_horizontal);
858858
}
859859

860-
if (fabs(uav_state.velocity.linear.x) < coef * max_speed_horizontal) {
860+
if (std::abs(uav_state.velocity.linear.x) < coef * max_speed_horizontal) {
861861
velocity = uav_state.velocity.linear.x;
862862
} else {
863863
velocity = tracker_command.velocity.x;
864864

865865
ROS_ERROR_THROTTLE(1.0, "[%s]: odometry x velocity exceeds constraints (%.2f > %0.1f * %.2f m), using reference for initial condition", name_.c_str(),
866-
fabs(uav_state.velocity.linear.x), coef, max_speed_horizontal);
866+
std::abs(uav_state.velocity.linear.x), coef, max_speed_horizontal);
867867
}
868868

869869
initial_x << uav_state.pose.position.x, velocity, acceleration;
@@ -878,22 +878,22 @@ void MpcController::MPC(const mrs_msgs::UavState &uav_state, const mrs_msgs::Tra
878878
double velocity;
879879
double coef = 1.5;
880880

881-
if (fabs(uav_state.acceleration.linear.y) < coef * max_acceleration_horizontal) {
881+
if (std::abs(uav_state.acceleration.linear.y) < coef * max_acceleration_horizontal) {
882882
acceleration = uav_state.acceleration.linear.y;
883883
} else {
884884
acceleration = tracker_command.acceleration.y;
885885

886886
ROS_ERROR_THROTTLE(1.0, "[%s]: odometry y acceleration exceeds constraints (%.2f > %.1f * %.2f m), using reference for initial condition", name_.c_str(),
887-
fabs(uav_state.acceleration.linear.y), coef, max_acceleration_horizontal);
887+
std::abs(uav_state.acceleration.linear.y), coef, max_acceleration_horizontal);
888888
}
889889

890-
if (fabs(uav_state.velocity.linear.y) < coef * max_speed_horizontal) {
890+
if (std::abs(uav_state.velocity.linear.y) < coef * max_speed_horizontal) {
891891
velocity = uav_state.velocity.linear.y;
892892
} else {
893893
velocity = tracker_command.velocity.y;
894894

895895
ROS_ERROR_THROTTLE(1.0, "[%s]: odometry y velocity exceeds constraints (%.2f > %0.1f * %.2f m), using reference for initial condition", name_.c_str(),
896-
fabs(uav_state.velocity.linear.y), coef, max_speed_horizontal);
896+
std::abs(uav_state.velocity.linear.y), coef, max_speed_horizontal);
897897
}
898898

899899
initial_y << uav_state.pose.position.y, velocity, acceleration;
@@ -908,22 +908,22 @@ void MpcController::MPC(const mrs_msgs::UavState &uav_state, const mrs_msgs::Tra
908908
double velocity;
909909
double coef = 1.5;
910910

911-
if (fabs(uav_state.acceleration.linear.z) < coef * max_acceleration_horizontal) {
911+
if (std::abs(uav_state.acceleration.linear.z) < coef * max_acceleration_horizontal) {
912912
acceleration = uav_state.acceleration.linear.z;
913913
} else {
914914
acceleration = tracker_command.acceleration.z;
915915

916916
ROS_ERROR_THROTTLE(1.0, "[%s]: odometry z acceleration exceeds constraints (%.2f > %.1f * %.2f m), using reference for initial condition", name_.c_str(),
917-
fabs(uav_state.acceleration.linear.z), coef, max_acceleration_horizontal);
917+
std::abs(uav_state.acceleration.linear.z), coef, max_acceleration_horizontal);
918918
}
919919

920-
if (fabs(uav_state.velocity.linear.z) < coef * max_speed_vertical) {
920+
if (std::abs(uav_state.velocity.linear.z) < coef * max_speed_vertical) {
921921
velocity = uav_state.velocity.linear.z;
922922
} else {
923923
velocity = tracker_command.velocity.z;
924924

925925
ROS_ERROR_THROTTLE(1.0, "[%s]: odometry z velocity exceeds constraints (%.2f > %0.1f * %.2f m), using reference for initial condition", name_.c_str(),
926-
fabs(uav_state.velocity.linear.z), coef, max_speed_vertical);
926+
std::abs(uav_state.velocity.linear.z), coef, max_speed_vertical);
927927
}
928928

929929
initial_z << uav_state.pose.position.z, velocity, acceleration;
@@ -1393,7 +1393,7 @@ void MpcController::MPC(const mrs_msgs::UavState &uav_state, const mrs_msgs::Tra
13931393
// antiwindup
13941394
double temp_gain = gains.km;
13951395
if (rampup_active_ ||
1396-
(fabs(uav_state.velocity.linear.z) > 0.3 && ((Ep(2) > 0 && uav_state.velocity.linear.z > 0) || (Ep(2) < 0 && uav_state.velocity.linear.z < 0)))) {
1396+
(std::abs(uav_state.velocity.linear.z) > 0.3 && ((Ep(2) > 0 && uav_state.velocity.linear.z > 0) || (Ep(2) < 0 && uav_state.velocity.linear.z < 0)))) {
13971397
temp_gain = 0;
13981398
ROS_DEBUG_THROTTLE(1.0, "[%s]: anti-windup for the mass kicks in", this->name_.c_str());
13991399
}
@@ -1500,7 +1500,7 @@ void MpcController::MPC(const mrs_msgs::UavState &uav_state, const mrs_msgs::Tra
15001500
if (rampup_active_) {
15011501

15021502
// deactivate the rampup when the times up
1503-
if (fabs((ros::Time::now() - rampup_start_time_).toSec()) >= rampup_duration_) {
1503+
if (std::abs((ros::Time::now() - rampup_start_time_).toSec()) >= rampup_duration_) {
15041504

15051505
rampup_active_ = false;
15061506

@@ -2004,7 +2004,11 @@ void MpcController::timerGains(const ros::TimerEvent &event) {
20042004

20052005
mute_gains_ = false;
20062006

2007-
const double dt = (event.current_real - event.last_real).toSec();
2007+
double dt = (event.current_real - event.last_real).toSec();
2008+
2009+
if (!std::isfinite(dt) || (dt <= 0) || (dt > 5 * (1.0 / _gain_filtering_rate_))) {
2010+
return;
2011+
}
20082012

20092013
bool updated = false;
20102014

@@ -2062,29 +2066,29 @@ double MpcController::calculateGainChange(const double dt, const double current_
20622066
double change_in_perc;
20632067
double saturated_change;
20642068

2065-
if (fabs(current_value) < 1e-6) {
2069+
if (std::abs(current_value) < 1e-6) {
20662070
change *= gains_filter_max_change;
20672071
} else {
20682072

20692073
saturated_change = change;
20702074

2071-
change_in_perc = (current_value + saturated_change) / current_value - 1.0;
2075+
change_in_perc = ((current_value + saturated_change) / current_value) - 1.0;
20722076

20732077
if (change_in_perc > gains_filter_max_change) {
20742078
saturated_change = current_value * gains_filter_max_change;
20752079
} else if (change_in_perc < -gains_filter_max_change) {
20762080
saturated_change = current_value * -gains_filter_max_change;
20772081
}
20782082

2079-
if (fabs(saturated_change) < fabs(change) * gains_filter_min_change) {
2083+
if (std::abs(saturated_change) < std::abs(change) * gains_filter_min_change) {
20802084
change *= gains_filter_min_change;
20812085
} else {
20822086
change = saturated_change;
20832087
}
20842088
}
20852089
}
20862090

2087-
if (fabs(change) > 1e-3) {
2091+
if (std::abs(change) > 1e-3) {
20882092
ROS_DEBUG("[%s]: changing gain '%s' from %.2f to %.2f", name_.c_str(), name.c_str(), current_value, desired_value);
20892093
updated = true;
20902094
}

src/se3_controller.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ bool Se3Controller::initialize(const ros::NodeHandle& nh, std::shared_ptr<mrs_ua
280280

281281
_tilt_angle_failsafe_ = M_PI * (_tilt_angle_failsafe_ / 180.0);
282282

283-
if (_tilt_angle_failsafe_enabled_ && fabs(_tilt_angle_failsafe_) < 1e-3) {
283+
if (_tilt_angle_failsafe_enabled_ && std::abs(_tilt_angle_failsafe_) < 1e-3) {
284284
ROS_ERROR("[Se3Controller]: constraints/tilt_angle_failsafe/enabled = 'TRUE' but the limit is too low");
285285
return false;
286286
}
@@ -436,7 +436,7 @@ bool Se3Controller::activate(const ControlOutput& last_control_output) {
436436
rampup_last_time_ = ros::Time::now();
437437
rampup_throttle_ = throttle_last_controller.value();
438438

439-
rampup_duration_ = fabs(throttle_difference) / _rampup_speed_;
439+
rampup_duration_ = std::abs(throttle_difference) / _rampup_speed_;
440440
}
441441

442442
first_iteration_ = true;
@@ -512,7 +512,7 @@ Se3Controller::ControlOutput Se3Controller::updateActive(const mrs_msgs::UavStat
512512

513513
last_update_time_ = uav_state.header.stamp;
514514

515-
if (fabs(dt) < 0.001) {
515+
if (std::abs(dt) < 0.001) {
516516

517517
ROS_DEBUG("[Se3Controller]: the last odometry message came too close (%.2f s)!", dt);
518518

@@ -1252,7 +1252,7 @@ void Se3Controller::SE3Controller(const mrs_msgs::UavState& uav_state, const mrs
12521252
} else if (rampup_active_) {
12531253

12541254
// deactivate the rampup when the times up
1255-
if (fabs((ros::Time::now() - rampup_start_time_).toSec()) >= rampup_duration_) {
1255+
if (std::abs((ros::Time::now() - rampup_start_time_).toSec()) >= rampup_duration_) {
12561256

12571257
rampup_active_ = false;
12581258

@@ -1707,7 +1707,11 @@ void Se3Controller::timerGains(const ros::TimerEvent& event) {
17071707

17081708
mute_gains_ = false;
17091709

1710-
const double dt = (event.current_real - event.last_real).toSec();
1710+
double dt = (event.current_real - event.last_real).toSec();
1711+
1712+
if (!std::isfinite(dt) || (dt <= 0) || (dt > 5 * (1.0 / _gain_filtering_rate_))) {
1713+
return;
1714+
}
17111715

17121716
bool updated = false;
17131717

@@ -1777,29 +1781,29 @@ double Se3Controller::calculateGainChange(const double dt, const double current_
17771781
double change_in_perc;
17781782
double saturated_change;
17791783

1780-
if (fabs(current_value) < 1e-6) {
1784+
if (std::abs(current_value) < 1e-6) {
17811785
change *= gains_filter_max_change;
17821786
} else {
17831787

17841788
saturated_change = change;
17851789

1786-
change_in_perc = (current_value + saturated_change) / current_value - 1.0;
1790+
change_in_perc = ((current_value + saturated_change) / current_value) - 1.0;
17871791

17881792
if (change_in_perc > gains_filter_max_change) {
17891793
saturated_change = current_value * gains_filter_max_change;
17901794
} else if (change_in_perc < -gains_filter_max_change) {
17911795
saturated_change = current_value * -gains_filter_max_change;
17921796
}
17931797

1794-
if (fabs(saturated_change) < fabs(change) * gains_filter_min_change) {
1798+
if (std::abs(saturated_change) < std::abs(change) * gains_filter_min_change) {
17951799
change *= gains_filter_min_change;
17961800
} else {
17971801
change = saturated_change;
17981802
}
17991803
}
18001804
}
18011805

1802-
if (fabs(change) > 1e-3) {
1806+
if (std::abs(change) > 1e-3) {
18031807
ROS_DEBUG("[Se3Controller]: changing gain '%s' from %.2f to %.2f", name.c_str(), current_value, desired_value);
18041808
updated = true;
18051809
}

0 commit comments

Comments
 (0)