Skip to content

Commit abce132

Browse files
authored
Merge branch debugging-devel to master (#10)
* estimator-update sync, state & ref needed for controller *remove thread-spawn for state update, make syncd *fix launch arg string type * force move ctor in statev passing, increase debug verbosity *avoid race condition with forced move ctor *big update to controller debug, increase logging
1 parent c108fdb commit abce132

File tree

7 files changed

+45
-39
lines changed

7 files changed

+45
-39
lines changed

freyja_controller.launch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
<!-- advanced settings -->
2323
<arg name="use_velctrl_only" default="false" />
24-
<arg name="bias_compensation" default="false" />
2524
<arg name="enable_flatness_ff" default="false" />
25+
<arg name="bias_compensation" default="auto" /> <!--auto, on, off-->
2626

2727
<!-- Used for examples -->
2828
<arg name="use_examples" default="false"/>

freyja_msgs/msg/lqr_ctrl/ControllerDebug.msg

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
# Often useful for observers, lqr_u is the 4-axis acceleration vector.
33
# State can be invalid due to time delay, or a dead source.
44

5+
# flags:
6+
uint8 CTRL_OK = 1
7+
uint8 BIAS_EN = 2
8+
uint8 FLAT_FF = 4
9+
uint8 MASS_CR = 8
10+
511
Header header
6-
float64[4] lqr_u
7-
float64 thrust
8-
float64 roll
9-
float64 pitch
10-
float64 yaw
11-
uint8 state_valid
12+
float32[4] lqr_u
13+
float32[4] biasv
14+
float32[7] errv
15+
uint8 flags

lqg_controller/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 2.8.3)
22
#cmake_minimum_required(VERSION 3.1.0)
33
project(lqg_control)
4-
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -Wall" )
4+
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O3 -ffast-math -Wall" )
55

66

77
find_package(catkin REQUIRED COMPONENTS

lqg_controller/include/lqr_control_bias.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class LQRController
8383
void initLqrSystem();
8484

8585
ros::Subscriber state_sub_;
86-
void stateCallback( const freyja_msgs::CurrentState::ConstPtr & );
86+
void stateCallback( const freyja_msgs::CurrentState::ConstPtr & ) __attribute__((hot));
8787

8888
ros::ServiceServer bias_enable_serv_;
8989
bool biasEnableServer( BoolServReq&, BoolServRsp& );
@@ -92,13 +92,13 @@ class LQRController
9292
ros::Publisher controller_debug_pub_;
9393

9494
ros::Timer controller_timer_;
95-
void computeFeedback( const ros::TimerEvent & );
95+
void computeFeedback( const ros::TimerEvent & ) __attribute__((optimize("unroll-loops")));
9696

9797
ros::Subscriber reference_sub_;
9898
void trajectoryReferenceCallback( const TrajRef::ConstPtr & );
9999

100100
/* helper function to calculate yaw error */
101-
inline double calcYawError( const double&, const double& ) __attribute__((always_inline));
101+
static constexpr inline double calcYawError( const double&, const double& ) __attribute__((always_inline));
102102

103103
/* estimate actual mass in flight */
104104
void estimateMass( const Eigen::Matrix<double, 4, 1> &, ros::Time & );

lqg_controller/src/bias_estimator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ void BiasEstimator::setMeasurement( const Eigen::Matrix<double, 6, 1> &m )
195195
{
196196
measurement_z_ = m;
197197
n_stprops_since_update_ = 0;
198-
st_upd_thread_ = std::thread( &BiasEstimator::state_updation, this );
199-
st_upd_thread_.detach();
198+
state_updation();
200199
}
201200

202201
void BiasEstimator::setControlInput( const Eigen::Matrix<double, 4, 1> &c )

lqg_controller/src/lqr_control_bias.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ bool LQRController::biasEnableServer( BoolServReq& rq, BoolServRsp& rp )
138138
return true; // service successful
139139
}
140140

141-
double LQRController::calcYawError( const double &a, const double &b )
141+
constexpr double LQRController::calcYawError( const double &a, const double &b )
142142
{
143143
double yd = fmod( a - b + pi, 2*pi );
144144
yd = yd < 0 ? yd+2*pi : yd;
@@ -149,6 +149,7 @@ void LQRController::stateCallback( const freyja_msgs::CurrentState::ConstPtr &ms
149149
{
150150
/* Parse message to obtain state and reduced state information */
151151
static Eigen::Matrix<double, 7, 1> current_state;
152+
static Eigen::Matrix<double, 7, 1> state_err;
152153

153154
float yaw = msg->state_vector[8];
154155
rot_yaw_ << std::cos(yaw), std::sin(yaw), 0,
@@ -168,15 +169,15 @@ void LQRController::stateCallback( const freyja_msgs::CurrentState::ConstPtr &ms
168169
if( have_reference_update_ )
169170
{
170171
rsmtx.lock();
171-
reduced_state_.head<6>() = current_state.head<6>() - reference_state_.head<6>();
172+
state_err.head<6>() = current_state.head<6>() - reference_state_.head<6>();
172173
/* yaw-error is done differently */
173-
reduced_state_(6) = calcYawError( current_state(6), reference_state_(6) );
174+
state_err(6) = calcYawError( current_state(6), reference_state_(6) );
174175
rsmtx.unlock();
176+
reduced_state_ = std::move( state_err );
177+
have_state_update_ = true;
175178
}
176179

177-
have_state_update_ = true;
178180
last_state_update_t_ = ros::Time::now();
179-
180181
}
181182

182183
void LQRController::trajectoryReferenceCallback( const TrajRef::ConstPtr &msg )
@@ -195,6 +196,7 @@ void LQRController::trajectoryReferenceCallback( const TrajRef::ConstPtr &msg )
195196
have_reference_update_ = true;
196197
}
197198

199+
__attribute__((optimize("unroll-loops")))
198200
void LQRController::computeFeedback( const ros::TimerEvent &event )
199201
{
200202
/* Wait for atleast one update, or architect the code better */
@@ -203,7 +205,8 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
203205

204206
float roll, pitch, yaw;
205207
double T;
206-
Eigen::Matrix<double, 4, 1> control_input;
208+
static Eigen::Matrix<double, 4, 1> control_input;
209+
static Eigen::Matrix<double, 7, 1> state_err;
207210

208211
bool state_valid = true;
209212
auto tnow = ros::Time::now();
@@ -221,7 +224,8 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
221224
else
222225
{
223226
/* Compute control inputs (accelerations, in this case) */
224-
control_input = -1 * lqr_K_ * reduced_state_
227+
state_err = std::move( reduced_state_ );
228+
control_input = -1 * lqr_K_ * state_err
225229
+ static_cast<double>(enable_flatness_ff_) * reference_ff_;
226230

227231
/* Force saturation on downward acceleration */
@@ -245,20 +249,6 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
245249
yaw = control_input(3);
246250
}
247251

248-
/* Debug information */
249-
freyja_msgs::ControllerDebug debug_msg;
250-
debug_msg.header.stamp = tnow;
251-
debug_msg.lqr_u[0] = control_input(0);
252-
debug_msg.lqr_u[1] = control_input(1);
253-
debug_msg.lqr_u[2] = control_input(2);
254-
debug_msg.lqr_u[3] = control_input(3);
255-
debug_msg.thrust = T;
256-
debug_msg.roll = roll;
257-
debug_msg.pitch = pitch;
258-
debug_msg.yaw = yaw;
259-
debug_msg.state_valid = state_valid;
260-
controller_debug_pub_.publish( debug_msg );
261-
262252
/* Actual commanded input */
263253
freyja_msgs::CtrlCommand ctrl_cmd;
264254
ctrl_cmd.roll = roll;
@@ -268,13 +258,30 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
268258
ctrl_cmd.ctrl_mode = 0b00001111;
269259
atti_cmd_pub_.publish( ctrl_cmd );
270260

261+
271262
/* Tell bias estimator about new control input */
272263
if( bias_compensation_req_ )
273264
bias_est_.setControlInput( control_input );
274265

275266
/* Update the total flying mass if requested */
276267
if( enable_dyn_mass_estimation_ )
277268
estimateMass( control_input, tnow );
269+
270+
/* Debug information */
271+
static freyja_msgs::ControllerDebug debug_msg;
272+
debug_msg.header.stamp = tnow;
273+
for( uint8_t idx=0; idx<4; idx++ )
274+
debug_msg.lqr_u[idx] = static_cast<float>(control_input(idx));
275+
for( uint8_t idx=0; idx<3; idx++ )
276+
debug_msg.biasv[idx] = static_cast<float>(f_biases_(idx));
277+
for( uint8_t idx=0; idx<7; idx++ )
278+
debug_msg.errv[idx] = static_cast<float>(state_err(idx));
279+
280+
debug_msg.flags = (debug_msg.BIAS_EN * bias_compensation_req_) |
281+
(debug_msg.MASS_CR * enable_dyn_mass_correction_ ) |
282+
(debug_msg.FLAT_FF * enable_flatness_ff_ ) |
283+
(debug_msg.CTRL_OK * state_valid );
284+
controller_debug_pub_.publish( debug_msg );
278285
}
279286

280287
void LQRController::estimateMass( const Eigen::Matrix<double, 4, 1> &c, ros::Time &t )

lqg_controller/src/lqr_vel_control.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,7 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
166166
debug_msg.lqr_u[1] = control_input(1);
167167
debug_msg.lqr_u[2] = control_input(2);
168168
debug_msg.lqr_u[3] = control_input(3);
169-
debug_msg.thrust = T;
170-
debug_msg.roll = roll;
171-
debug_msg.pitch = pitch;
172-
debug_msg.yaw = yaw;
173-
debug_msg.state_valid = state_valid;
169+
debug_msg.flags = state_valid;
174170
controller_debug_pub_.publish( debug_msg );
175171

176172
/* Actual commanded input */

0 commit comments

Comments
 (0)