Skip to content

Commit 78fb0e7

Browse files
markusgftGiftthaler Markus (CR/PJ-AI-R31)
authored andcommitted
handing over a shared_ptr to ct system model rather than a reference
1 parent fd7f546 commit 78fb0e7

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

ct_optcon/examples/KalmanDisturbanceFiltering.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,13 @@ int main(int argc, char** argv)
183183
ct::core::loadMatrix(configFile, "kalman_weights.Qaug", Qaug);
184184
ct::core::loadMatrix(configFile, "kalman_weights.R", R);
185185

186-
dFdv.setIdentity(); // todo tune me
186+
dFdv.setIdentity();
187187

188188
// create a sensitivity approximator to obtain discrete-time dynamics matrices
189189
std::shared_ptr<ct::core::SystemLinearizer<state_dim + dist_dim, control_dim>> linearizer(
190190
new ct::core::SystemLinearizer<state_dim + dist_dim, control_dim>(inputDisturbedSystem));
191-
ct::core::SensitivityApproximation<state_dim + dist_dim, control_dim> sensApprox(dt, linearizer);
191+
std::shared_ptr<ct::core::SensitivityApproximation<state_dim + dist_dim, control_dim>> sensApprox(
192+
new ct::core::SensitivityApproximation<state_dim + dist_dim, control_dim>(dt, linearizer));
192193

193194
// set up the system model
194195
std::shared_ptr<ct::optcon::CTSystemModel<state_dim + dist_dim, control_dim>> sysModel(

ct_optcon/examples/KalmanFiltering.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main(int argc, char** argv)
8383
// load Kalman Filter weighting matrices from file
8484
ct::core::StateMatrix<state_dim> Q, dFdv;
8585
ct::core::OutputMatrix<output_dim> R;
86-
dFdv.setIdentity(); // todo tune me!
86+
dFdv.setIdentity();
8787
ct::core::loadMatrix(settingsFile, "kalman_weights.Q", Q);
8888
ct::core::loadMatrix(settingsFile, "kalman_weights.R", R);
8989
std::cout << "Loaded Kalman R as " << std::endl << R << std::endl;
@@ -92,7 +92,9 @@ int main(int argc, char** argv)
9292
// create a sensitivity approximator to compute A and B matrices
9393
std::shared_ptr<ct::core::SystemLinearizer<state_dim, control_dim>> linearizer(
9494
new ct::core::SystemLinearizer<state_dim, control_dim>(oscillator));
95-
ct::core::SensitivityApproximation<state_dim, control_dim> sensApprox(dt, linearizer);
95+
96+
std::shared_ptr<ct::core::SensitivityApproximation<state_dim, control_dim>> sensApprox(
97+
new ct::core::SensitivityApproximation<state_dim, control_dim>(dt, linearizer));
9698

9799

98100
// the observer is supplied with a dynamic model identical to the one used above for data generation
@@ -103,8 +105,10 @@ int main(int argc, char** argv)
103105
new ct::optcon::CTSystemModel<state_dim, control_dim>(oscillator_observer_model, sensApprox, dFdv));
104106

105107
// set up the measurement model
108+
ct::core::OutputStateMatrix<output_dim, state_dim> dHdw;
109+
dHdw.setIdentity();
106110
std::shared_ptr<ct::optcon::LinearMeasurementModel<output_dim, state_dim>> measModel(
107-
new ct::optcon::LTIMeasurementModel<output_dim, state_dim>(C));
111+
new ct::optcon::LTIMeasurementModel<output_dim, state_dim>(C, dHdw));
108112

109113
// set up Filter, e.g. extended Kalman or Unscented Kalman filter
110114
ct::optcon::ExtendedKalmanFilter<state_dim, control_dim, output_dim> filter(
@@ -129,7 +133,7 @@ int main(int argc, char** argv)
129133
// compute an observation
130134
states_meas[i] = states[i];
131135

132-
// todo this is technically not correct, the noise enters not on the state but on the output!!!
136+
// note that this is technically not correct, the noise enters not on the state but on the output!!!
133137
position_measurement_noise.noisify(states_meas[i](0)); // Position noise.
134138
velocity_measurement_noise.noisify(states_meas[i](1)); // Velocity noise.
135139
ct::core::OutputVector<output_dim> y = C * states_meas[i];

ct_optcon/include/ct/optcon/filter/CTSystemModel-impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ namespace optcon {
1111
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
1212
CTSystemModel<STATE_DIM, CONTROL_DIM, SCALAR>::CTSystemModel(
1313
std::shared_ptr<ct::core::ControlledSystem<STATE_DIM, CONTROL_DIM, SCALAR>> system,
14-
const SensitivityApprox_t& sensApprox,
14+
std::shared_ptr<SensitivityApprox_t> sensApprox,
1515
const state_matrix_t& dFdv,
1616
const ct::core::IntegrationType& intType)
17-
: system_(system->clone()),
17+
: system_(system),
1818
constantController_(new ct::core::ConstantController<STATE_DIM, CONTROL_DIM, SCALAR>()),
1919
sensApprox_(sensApprox),
2020
dFdv_(dFdv),
@@ -49,9 +49,9 @@ auto CTSystemModel<STATE_DIM, CONTROL_DIM, SCALAR>::computeDerivativeState(const
4949
state_matrix_t A;
5050
ct::core::StateControlMatrix<STATE_DIM, CONTROL_DIM, SCALAR> Btemp;
5151

52-
sensApprox_.setTimeDiscretization(dt);
52+
sensApprox_->setTimeDiscretization(dt);
5353

54-
sensApprox_.getAandB(state, u, state, int(t / dt), 1, A, Btemp);
54+
sensApprox_->getAandB(state, u, state, int(t / dt), 1, A, Btemp);
5555
return A;
5656
}
5757

ct_optcon/include/ct/optcon/filter/CTSystemModel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CTSystemModel final : public SystemModelBase<STATE_DIM, CONTROL_DIM, SCALA
3939

4040
//! Constructor. Takes in the system with defined controller, and sens approximator for computing the derivatives
4141
CTSystemModel(std::shared_ptr<ct::core::ControlledSystem<STATE_DIM, CONTROL_DIM, SCALAR>> system,
42-
const SensitivityApprox_t& sensApprox,
42+
std::shared_ptr<SensitivityApprox_t> sensApprox,
4343
const state_matrix_t& dFdv,
4444
const ct::core::IntegrationType& intType = ct::core::IntegrationType::EULERCT);
4545

@@ -69,7 +69,7 @@ class CTSystemModel final : public SystemModelBase<STATE_DIM, CONTROL_DIM, SCALA
6969
std::shared_ptr<ct::core::ConstantController<STATE_DIM, CONTROL_DIM, SCALAR>> constantController_;
7070

7171
//! The sensitivity approximator
72-
SensitivityApprox_t sensApprox_;
72+
std::shared_ptr<SensitivityApprox_t> sensApprox_;
7373

7474
//! Derivative w.r.t. noise.
7575
state_matrix_t dFdv_;

0 commit comments

Comments
 (0)