@@ -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 }
0 commit comments