@@ -138,7 +138,7 @@ bool LQRController::biasEnableServer( BoolServReq& rq, BoolServRsp& rp )
138
138
return true ; // service successful
139
139
}
140
140
141
- double LQRController::calcYawError ( const double &a, const double &b )
141
+ constexpr double LQRController::calcYawError ( const double &a, const double &b )
142
142
{
143
143
double yd = fmod ( a - b + pi, 2 *pi );
144
144
yd = yd < 0 ? yd+2 *pi : yd;
@@ -149,6 +149,7 @@ void LQRController::stateCallback( const freyja_msgs::CurrentState::ConstPtr &ms
149
149
{
150
150
/* Parse message to obtain state and reduced state information */
151
151
static Eigen::Matrix<double , 7 , 1 > current_state;
152
+ static Eigen::Matrix<double , 7 , 1 > state_err;
152
153
153
154
float yaw = msg->state_vector [8 ];
154
155
rot_yaw_ << std::cos (yaw), std::sin (yaw), 0 ,
@@ -168,15 +169,15 @@ void LQRController::stateCallback( const freyja_msgs::CurrentState::ConstPtr &ms
168
169
if ( have_reference_update_ )
169
170
{
170
171
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 >();
172
173
/* 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 ) );
174
175
rsmtx.unlock ();
176
+ reduced_state_ = std::move ( state_err );
177
+ have_state_update_ = true ;
175
178
}
176
179
177
- have_state_update_ = true ;
178
180
last_state_update_t_ = ros::Time::now ();
179
-
180
181
}
181
182
182
183
void LQRController::trajectoryReferenceCallback ( const TrajRef::ConstPtr &msg )
@@ -195,6 +196,7 @@ void LQRController::trajectoryReferenceCallback( const TrajRef::ConstPtr &msg )
195
196
have_reference_update_ = true ;
196
197
}
197
198
199
+ __attribute__ ((optimize(" unroll-loops" )))
198
200
void LQRController::computeFeedback( const ros::TimerEvent &event )
199
201
{
200
202
/* Wait for atleast one update, or architect the code better */
@@ -203,7 +205,8 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
203
205
204
206
float roll, pitch, yaw;
205
207
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;
207
210
208
211
bool state_valid = true ;
209
212
auto tnow = ros::Time::now ();
@@ -221,7 +224,8 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
221
224
else
222
225
{
223
226
/* 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
225
229
+ static_cast <double >(enable_flatness_ff_) * reference_ff_;
226
230
227
231
/* Force saturation on downward acceleration */
@@ -245,20 +249,6 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
245
249
yaw = control_input (3 );
246
250
}
247
251
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
-
262
252
/* Actual commanded input */
263
253
freyja_msgs::CtrlCommand ctrl_cmd;
264
254
ctrl_cmd.roll = roll;
@@ -268,13 +258,30 @@ void LQRController::computeFeedback( const ros::TimerEvent &event )
268
258
ctrl_cmd.ctrl_mode = 0b00001111 ;
269
259
atti_cmd_pub_.publish ( ctrl_cmd );
270
260
261
+
271
262
/* Tell bias estimator about new control input */
272
263
if ( bias_compensation_req_ )
273
264
bias_est_.setControlInput ( control_input );
274
265
275
266
/* Update the total flying mass if requested */
276
267
if ( enable_dyn_mass_estimation_ )
277
268
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 );
278
285
}
279
286
280
287
void LQRController::estimateMass ( const Eigen::Matrix<double , 4 , 1 > &c, ros::Time &t )
0 commit comments