Motion Master
Loading...
Searching...
No Matches
standalone_autotuning.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef _WIN32
4// <windows.h> pulls in the original WinSock.h unless this is defined first,
5// which then collides with the WinSock2.h that Boost.Asio needs: including this
6// header before Boost.Asio in the same translation unit fails to compile with
7// "WinSock.h has already been included". The library target defines the macro
8// as well, for a consumer that reaches <windows.h> by some other route first.
9#ifndef WIN32_LEAN_AND_MEAN
10#define WIN32_LEAN_AND_MEAN
11#endif
12#include <windows.h>
13#endif
14#ifdef __linux__
15#include <sys/types.h>
16#endif
17
18#include <cstdint>
19#include <string>
20#include <vector>
21
22#include <nlohmann/json.hpp>
23
32 public:
36 std::string message;
37};
38
39NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(StandaloneAutotuningError, message)
40
41
48class PIDGains {
49 public:
51 double kp;
52
54 double ki;
55
57 double kd;
58};
59
60NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(PIDGains, kp, ki, kd)
61
62
77
78NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ControllerGains, position, velocity)
79
80
91
92NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AutoTuningGainsResult, gains)
93
94
103 public:
105 double gm;
106
108 double pm;
109
111 double msens;
112
114 double act_bw;
115
117 double vel_cl;
118
121
123 double max_KS;
124};
125
126NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(VelocityAutoTuningState, gm, pm, msens,
127 act_bw, vel_cl, noise_resp, max_KS)
128
129
138 public:
140 double gm_p;
141
143 double pm_p;
144
146 double msens_p;
147
149 double gm_v;
150
152 double pm_v;
153
155 double msens_v;
156
158 double noise_resp;
159
161 double max_KS;
162
164 double bw_p;
165
167 double bw_v;
168
170 double act_st;
171
173 double pos_cl;
174};
175
176NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(PositionAutoTuningState, gm_p, pm_p, msens_p,
177 gm_v, pm_v, msens_v, noise_resp, max_KS,
178 bw_p, bw_v, act_st, pos_cl)
179
180
190 public:
192 ControllerGains gains;
193
195 double demanded_bw;
196
201 double demanded_zeta;
202
205};
206
207NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FullVelocityAutoTuningResult, gains,
208 demanded_bw, demanded_zeta, state)
209
210
220 public:
222 ControllerGains gains;
223
225 double demanded_st;
226
231 double demanded_zeta;
232
235};
236
237NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FullPositionAutoTuningResult, gains,
238 demanded_st, demanded_zeta, state)
239
240
248class PlantModelResult {
249 public:
251 std::vector<double> numerators;
252
254 std::vector<double> denominators;
255
262 std::string bode_file_content;
263};
264
270 public:
274 std::string bode_file_content;
275};
276
290 private:
292 std::string programPath;
293
295 std::string host;
296
298 uint16_t port;
299
300#ifdef _WIN32
302 PROCESS_INFORMATION pi;
303#endif
304#ifdef __linux__
306 pid_t pid;
307#endif
308
324 nlohmann::json request(const std::string& method, const std::string& target,
325 const std::string& body,
326 unsigned int timeoutMilliseconds);
327
334 bool isProcessRunning();
335
336 public:
338 static constexpr uint16_t kDefaultPort = 63528;
339
346 static constexpr unsigned int kRequestTimeoutMilliseconds = 120000;
347
357 static constexpr unsigned int kFitRequestTimeoutMilliseconds = 900000;
358
360 static constexpr unsigned int kHealthTimeoutMilliseconds = 1000;
361
366 static constexpr unsigned int kReadyTimeoutMilliseconds = 30000;
367
379 explicit StandaloneAutotuning(const std::string& programPath,
380 const std::string& host = "127.0.0.1");
381
390
415 bool start(uint16_t port = kDefaultPort);
416
423 void stop();
424
432 bool isHealthy(unsigned int timeoutMilliseconds = kHealthTimeoutMilliseconds);
433
452 nlohmann::json exchange(
453 const nlohmann::json& jsonObj,
454 unsigned int timeoutMilliseconds = kRequestTimeoutMilliseconds);
455
474 ControllerGains auto_tune_velocity_controller(const nlohmann::json& data);
475
494 ControllerGains auto_tune_position_controller(const nlohmann::json& data);
495
516 const nlohmann::json& data);
517
538 const nlohmann::json& data);
539
560 ControllerGains compute_position_controller_gains(const nlohmann::json& data);
561
578 PlantModelResult identify_plant_model(const nlohmann::json& data);
579
592 const nlohmann::json& data);
593};
Holds the gains resulting from the autotuning process.
Definition standalone_autotuning.h:86
ControllerGains gains
The resulting controller gains for velocity and position.
Definition standalone_autotuning.h:89
Holds position and velocity PID gains for a controller.
Definition standalone_autotuning.h:69
PIDGains velocity
PID gains for the velocity controller.
Definition standalone_autotuning.h:75
PIDGains position
PID gains for the position controller.
Definition standalone_autotuning.h:72
Represents the result of a full position autotuning process.
Definition standalone_autotuning.h:207
Represents the result of a full velocity autotuning process.
Definition standalone_autotuning.h:176
Represents the bode plot data computed for a given plant model.
Definition standalone_autotuning.h:269
std::string bode_file_content
Bode plot data in CSV form, with a header row naming the columns.
Definition standalone_autotuning.h:274
Represents the gains for a PID controller.
Definition standalone_autotuning.h:48
double ki
Integral gain.
Definition standalone_autotuning.h:54
double kd
Derivative gain.
Definition standalone_autotuning.h:57
double kp
Proportional gain.
Definition standalone_autotuning.h:51
Represents the result of a plant model identification process.
Definition standalone_autotuning.h:237
Represents the state of the position autotuning process.
Definition standalone_autotuning.h:126
Represents an error that occurs during standalone autotuning.
Definition standalone_autotuning.h:31
std::string message
Descriptive error message.
Definition standalone_autotuning.h:36
FullVelocityAutoTuningResult full_auto_tune_velocity_controller(const nlohmann::json &data)
Performs a full velocity controller autotuning sequence using external autotuning program.
Definition standalone_autotuning.cc:588
static constexpr unsigned int kFitRequestTimeoutMilliseconds
Timeout applied to the functions that fit a model, in milliseconds.
Definition standalone_autotuning.h:357
ControllerGains auto_tune_position_controller(const nlohmann::json &data)
Performs position controller autotuning using external autotuning program.
Definition standalone_autotuning.cc:571
nlohmann::json exchange(const nlohmann::json &jsonObj, unsigned int timeoutMilliseconds=kRequestTimeoutMilliseconds)
Runs a function on the autotuning server and returns its result.
Definition standalone_autotuning.cc:513
PlantModelResult identify_plant_model(const nlohmann::json &data)
Identifies the plant model based on the input data.
Definition standalone_autotuning.cc:627
~StandaloneAutotuning()
Destroys the StandaloneAutotuning instance.
Definition standalone_autotuning.cc:197
ControllerGains compute_position_controller_gains(const nlohmann::json &data)
Computes the position controller gains without performing full autotuning.
Definition standalone_autotuning.cc:611
static constexpr unsigned int kRequestTimeoutMilliseconds
Timeout applied to a function call, in milliseconds.
Definition standalone_autotuning.h:346
void stop()
Stops the external autotuning process.
Definition standalone_autotuning.cc:346
static constexpr uint16_t kDefaultPort
Port the autotuning HTTP server listens on by default.
Definition standalone_autotuning.h:338
static constexpr unsigned int kHealthTimeoutMilliseconds
Timeout applied to a health request, in milliseconds.
Definition standalone_autotuning.h:360
FullPositionAutoTuningResult full_auto_tune_position_controller(const nlohmann::json &data)
Performs a full position controller autotuning sequence using external autotuning program.
Definition standalone_autotuning.cc:600
bool isHealthy(unsigned int timeoutMilliseconds=kHealthTimeoutMilliseconds)
Reports whether the autotuning server is accepting requests.
Definition standalone_autotuning.cc:422
ControllerGains auto_tune_velocity_controller(const nlohmann::json &data)
Performs velocity controller autotuning using external autotuning program.
Definition standalone_autotuning.cc:555
StandaloneAutotuning(const std::string &programPath, const std::string &host="127.0.0.1")
Constructs the StandaloneAutotuning instance.
Definition standalone_autotuning.cc:186
GeneratePlantBodeFileResult generate_plant_bode_file(const nlohmann::json &data)
Computes the bode plot data for the given plant model.
Definition standalone_autotuning.cc:649
bool start(uint16_t port=kDefaultPort)
Starts the external autotuning program and waits for its HTTP server to accept requests.
Definition standalone_autotuning.cc:199
static constexpr unsigned int kReadyTimeoutMilliseconds
How long start() waits for the server to accept requests, in milliseconds.
Definition standalone_autotuning.h:366
Represents the state of the velocity autotuning process.
Definition standalone_autotuning.h:102
double vel_cl
Closed-loop velocity value of the system.
Definition standalone_autotuning.h:117
double noise_resp
Noise response in the velocity autotuning process.
Definition standalone_autotuning.h:120
double pm
Phase margin of the velocity system.
Definition standalone_autotuning.h:108
double act_bw
Active bandwidth of the system.
Definition standalone_autotuning.h:114
double max_KS
Maximum allowed gain scale factor.
Definition standalone_autotuning.h:123
double gm
Gain margin of the velocity system.
Definition standalone_autotuning.h:105
double msens
Measurement sensitivity of the velocity system.
Definition standalone_autotuning.h:111