Motion Master
Loading...
Searching...
No Matches
system_identification.h
Go to the documentation of this file.
1#pragma once
2
3#include <dlib/matrix.h>
4
5#include <boost/numeric/odeint/stepper/controlled_runge_kutta.hpp>
6#include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
7#include <vector>
8
9#include "cia402_drive.h"
10#include "motion-master.pb.h"
11#include "si_unit_velocity.h"
12#include "util.h"
13
15 public:
16 typedef std::vector<double> state_type;
17
19 std::vector<state_type>& m_states;
20 std::vector<double>& m_times;
22 double m_timeStep;
23 size_t m_size;
24
25 push_back_state_and_time(std::vector<state_type>& states,
26 std::vector<double>& times, bool resample = false,
27 double timeStep = 0, size_t size = 0)
29 m_times(times),
30 m_resample(resample),
31 m_timeStep(timeStep),
32 m_size(size) {}
33
34 void operator()(const state_type& x, double t) {
35 if (m_resample) {
36 if (m_times.size() > 0) {
37 double invTS = 1.0 / m_timeStep;
38 double bt = m_times.back();
39 if (t >= bt + m_timeStep && m_times.size() < m_size) {
40 state_type bx = m_states.back();
41 state_type new_x(x.size());
42 // perform linear interpolation
43 for (size_t si = 0; si < x.size(); si++)
44 new_x[si] = x[si] - (x[si] - bx[si]) * (t - bt) * invTS;
45 m_states.push_back(new_x);
46 m_times.push_back(bt + m_timeStep);
47 // this is just preparation for the last value
48 if (m_times.size() == m_size - 1) {
49 m_states.push_back(new_x);
50 m_times.push_back(bt + m_timeStep);
51 }
52 } else if (m_times.size() == m_size) {
53 // the last value can be a problem because it never reaches above
54 // condition so for the last value we just set whatever is the last
55 // value
56 m_states[m_size - 1] = x;
57 }
58 } else {
59 m_states.push_back(x);
60 m_times.push_back(t);
61 }
62 } else {
63 m_states.push_back(x);
64 m_times.push_back(t);
65 }
66 }
67 };
68
70 uint32_t device_address, Cia402Drive& cia402_drive,
71 TuningParameters tuning_params,
72 const std::function<void()>& started_callback = {},
73 const std::function<void(motionmaster::MotionMasterMessage::Status::
74 SystemIdentification::Warning::Code,
75 std::string)>& warning_callback = {},
76 bool next_gen_sys_id = false);
77
79
88 MotionMasterError start();
89
90 private:
91 uint32_t device_address_;
92 Cia402Drive& cia402_drive_;
93
94 TuningParameters tuning_params_;
95 SystemParameters system_params_;
96
97 const std::function<void()>& started_callback_;
98 const std::function<void(motionmaster::MotionMasterMessage::Status::
99 SystemIdentification::Warning::Code,
100 std::string)>& warning_callback_;
101
102 SiUnitVelocity si_unit_velocity_;
103
104 bool next_gen_sys_id_ = false;
105
106 double gear_ratio_ = 1.0; // initialized in init()
107 double feed_constant_ = 1.0; // initialized in init()
108
114 MotionMasterError init();
115
123 static std::vector<int16_t> chirp(TuningParameters tuning_params);
124
132 static double missing_data(std::vector<int32_t>& raw_positions,
133 std::vector<int32_t>& raw_velocities,
134 std::vector<int32_t>& raw_torques);
135
146 static int32_t calculate_linear_interpolation_step_value(
147 std::vector<int32_t> data, uint32_t first_zero_index,
148 uint32_t last_zero_index);
149
158 static void linear_interpolation(std::vector<int32_t>& raw_positions,
159 std::vector<int32_t>& raw_velocities,
160 std::vector<int32_t>& raw_torques);
161
169 MotionMasterError save_plant_model_to_flash(PlantModel m);
170
179 static std::vector<double> convert_position_to_si(
180 std::vector<int32_t> raw_positions, uint32_t singleturn_resolution);
181
192 static std::vector<double> convert_velocity_to_si(
193 const std::vector<int32_t>& raw_velocities, double gear_ratio = 1.0,
194 double velocity_unit_scaling = 1, double feed_constant = 1.0);
195
204 static std::vector<double> convert_torque_to_si(
205 const std::vector<int32_t>& raw_torques, uint32_t nominal_torque);
206
215 MotionMasterError check_input_signal_amplitudes(
216 const std::vector<int32_t>& torques,
217 const std::vector<double>& velocities);
218
229 static MotionMasterError check_input_signal_signs(
230 const std::vector<int16_t>& chirp_target_torques,
231 const std::vector<double>& velocities,
232 const std::vector<int32_t>& raw_positions);
233
239 MotionMasterError set_integral_limits();
240
251 static bool check_model_stability(const PlantModel& m);
252
272 static PlantModel identify_plant_model(std::vector<double> velocities_si,
273 std::vector<double> torques_si,
274 SystemParameters system_params,
275 TuningParameters tuning_params);
276
286 static int32_t find_torque_sign_change(
287 const std::vector<int16_t>& chirp_target_torques);
288
300 static std::tuple<PlantModel, double> get_optimal_model(
301 std::vector<double> velocities_si, std::vector<double> torques_si,
302 SystemParameters system_params, TuningParameters tuning_params,
303 const std::string& fileName = "");
304
316 static double calculate_stability_margin(PlantModel m);
317
334 static double validate_sys_ident(PlantModel m,
335 const std::vector<double>& velocities_si,
336 const std::vector<double>& torques_si,
337 SystemParameters system_params,
338 TuningParameters tuning_params,
339 const std::string& = "");
340
366 static PlantModel do_least_squares(std::vector<double> y,
367 std::vector<double> tau, double time_step,
368 uint8_t order, uint16_t cutoff_frequency);
369
385 static std::vector<double> gpmf_filtering(std::vector<double> data,
386 uint16_t cutoff_frequency,
387 double time_step);
388
402 static std::tuple<dlib::matrix<double>, std::vector<double>,
403 std::vector<double>>
404 create_data_matrix(std::vector<double> y, std::vector<double> tau,
405 double time_step, uint8_t order);
406
415 static std::vector<double> numerical_differentiation(std::vector<double> data,
416 double time_step);
417
418 static void write_sys_id_csv(const std::string& filename,
419 const std::vector<double>& torques_si,
420 const std::vector<double>& velocities_si);
421};
struct TuningParametersClass TuningParameters
struct SystemParametersClass SystemParameters
Definition cia402_drive.h:48
~SystemIdentification()
Definition system_identification.cc:42
SystemIdentification(uint32_t device_address, Cia402Drive &cia402_drive, TuningParameters tuning_params, const std::function< void()> &started_callback={}, const std::function< void(motionmaster::MotionMasterMessage::Status::SystemIdentification::Warning::Code, std::string)> &warning_callback={}, bool next_gen_sys_id=false)
Definition system_identification.cc:26
MotionMasterError start()
Run system identification.
Definition system_identification.cc:44
std::vector< double > state_type
Definition system_identification.h:16
enum e_States states
std::vector< double > & m_times
Definition system_identification.h:20
double m_timeStep
Definition system_identification.h:22
void operator()(const state_type &x, double t)
Definition system_identification.h:34
push_back_state_and_time(std::vector< state_type > &states, std::vector< double > &times, bool resample=false, double timeStep=0, size_t size=0)
Definition system_identification.h:25
bool m_resample
Definition system_identification.h:21
size_t m_size
Definition system_identification.h:23
std::vector< state_type > & m_states
Definition system_identification.h:19