2021-01-18 00:16:08 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/*
|
|
|
|
Gain and phase determination algorithm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
class AC_AutoTune_FreqResp {
|
|
|
|
public:
|
|
|
|
// Constructor
|
|
|
|
AC_AutoTune_FreqResp()
|
|
|
|
{
|
2024-04-09 00:28:10 -03:00
|
|
|
dwell_cycles = 6;
|
2024-06-16 08:21:53 -03:00
|
|
|
meas_peak_info_buffer = NEW_NOTHROW ObjectBuffer<peak_info>(dwell_cycles);
|
|
|
|
tgt_peak_info_buffer = NEW_NOTHROW ObjectBuffer<peak_info>(dwell_cycles);
|
2021-01-18 00:16:08 -04:00
|
|
|
}
|
|
|
|
|
2021-12-01 01:01:34 -04:00
|
|
|
// Enumeration of input type
|
2021-01-18 00:16:08 -04:00
|
|
|
enum InputType {
|
|
|
|
DWELL = 0,
|
|
|
|
SWEEP = 1,
|
|
|
|
};
|
|
|
|
|
2022-01-17 10:37:43 -04:00
|
|
|
// Enumeration of type
|
|
|
|
enum ResponseType {
|
|
|
|
RATE = 0,
|
|
|
|
ANGLE = 1,
|
|
|
|
};
|
|
|
|
|
2021-09-10 00:32:31 -03:00
|
|
|
// Initialize the Frequency Response Object.
|
|
|
|
// Must be called before running dwell or frequency sweep tests
|
2022-01-17 10:37:43 -04:00
|
|
|
void init(InputType input_type, ResponseType response_type);
|
2021-01-18 00:16:08 -04:00
|
|
|
|
2021-12-01 01:01:34 -04:00
|
|
|
// Determines the gain and phase based on angle response for a dwell or sweep
|
2022-01-17 10:37:43 -04:00
|
|
|
void update(float command, float tgt_resp, float meas_resp, float tgt_freq);
|
2021-01-18 00:16:08 -04:00
|
|
|
|
2021-12-01 01:01:34 -04:00
|
|
|
// Enable external query if cycle is complete and freq response data are available
|
2021-01-18 00:16:08 -04:00
|
|
|
bool is_cycle_complete() { return cycle_complete;}
|
|
|
|
|
2021-12-01 01:01:34 -04:00
|
|
|
// Reset cycle_complete flag
|
2021-01-18 00:16:08 -04:00
|
|
|
void reset_cycle_complete() { cycle_complete = false; }
|
|
|
|
|
2024-04-09 00:28:10 -03:00
|
|
|
void set_dwell_cycles(uint8_t cycles);
|
2024-04-08 00:34:47 -03:00
|
|
|
|
|
|
|
uint8_t get_dwell_cycles() { return dwell_cycles;}
|
|
|
|
|
2021-12-01 01:01:34 -04:00
|
|
|
// Frequency response data accessors
|
2021-01-18 00:16:08 -04:00
|
|
|
float get_freq() { return curr_test_freq; }
|
|
|
|
float get_gain() { return curr_test_gain; }
|
|
|
|
float get_phase() { return curr_test_phase; }
|
|
|
|
float get_accel_max() { return max_accel; }
|
|
|
|
|
|
|
|
private:
|
2021-12-01 01:01:34 -04:00
|
|
|
// time of the start of a new target value search. keeps noise from prematurely starting the search of a new target value.
|
|
|
|
uint32_t new_tgt_time_ms;
|
|
|
|
|
|
|
|
// flag for searching for a new target peak
|
2021-01-18 00:16:08 -04:00
|
|
|
bool new_target = false;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// maximum target value
|
|
|
|
float max_target;
|
|
|
|
|
|
|
|
// time of maximum target value in current cycle
|
|
|
|
uint32_t max_tgt_time;
|
|
|
|
|
|
|
|
// counter for target value maximums
|
|
|
|
uint16_t max_target_cnt;
|
|
|
|
|
|
|
|
// holds previously determined maximum target value while current cycle is running
|
|
|
|
float temp_max_target;
|
|
|
|
|
|
|
|
// holds previously determined time of maximum target value while current cycle is running
|
|
|
|
uint32_t temp_max_tgt_time;
|
|
|
|
|
|
|
|
// minimum target value
|
|
|
|
float min_target;
|
|
|
|
|
|
|
|
// counter for target value minimums
|
|
|
|
uint16_t min_target_cnt;
|
|
|
|
|
|
|
|
// holds previously determined minimum target value while current cycle is running
|
|
|
|
float temp_min_target;
|
|
|
|
|
|
|
|
// maximum target value from previous cycle
|
|
|
|
float prev_target;
|
|
|
|
|
2022-01-17 10:37:43 -04:00
|
|
|
// maximum target response from previous cycle
|
|
|
|
float prev_tgt_resp;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// holds target amplitude for gain calculation
|
|
|
|
float temp_tgt_ampl;
|
|
|
|
|
|
|
|
// time of the start of a new measured value search. keeps noise from prematurely starting the search of a new measured value.
|
|
|
|
uint32_t new_meas_time_ms;
|
|
|
|
|
|
|
|
// flag for searching for a new measured peak
|
2021-01-18 00:16:08 -04:00
|
|
|
bool new_meas = false;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// maximum measured value
|
|
|
|
float max_meas;
|
|
|
|
|
|
|
|
// time of maximum measured value in current cycle
|
|
|
|
uint32_t max_meas_time;
|
|
|
|
|
|
|
|
// counter for measured value maximums
|
|
|
|
uint16_t max_meas_cnt;
|
|
|
|
|
|
|
|
// holds previously determined maximum measured value while current cycle is running
|
|
|
|
float temp_max_meas;
|
|
|
|
|
|
|
|
// holds previously determined time of maximum measured value while current cycle is running
|
|
|
|
uint32_t temp_max_meas_time;
|
|
|
|
|
|
|
|
// minimum measured value
|
|
|
|
float min_meas;
|
|
|
|
|
|
|
|
// counter for measured value minimums
|
|
|
|
uint16_t min_meas_cnt;
|
|
|
|
|
|
|
|
// holds previously determined minimum measured value while current cycle is running
|
|
|
|
float temp_min_meas;
|
|
|
|
|
|
|
|
// maximum measured value from previous cycle
|
|
|
|
float prev_meas;
|
|
|
|
|
2022-01-17 10:37:43 -04:00
|
|
|
// maximum measured response from previous cycle
|
|
|
|
float prev_meas_resp;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// holds measured amplitude for gain calculation
|
|
|
|
float temp_meas_ampl;
|
|
|
|
|
|
|
|
// calculated target rate from angle data
|
|
|
|
float target_rate;
|
|
|
|
|
|
|
|
// calculated measured rate from angle data
|
|
|
|
float measured_rate;
|
|
|
|
|
|
|
|
// holds start time of input to track length of time that input in running
|
|
|
|
uint32_t input_start_time_ms;
|
|
|
|
|
|
|
|
// flag indicating when one oscillation cycle is complete
|
2021-01-18 00:16:08 -04:00
|
|
|
bool cycle_complete = false;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
2024-04-08 00:34:47 -03:00
|
|
|
// number of dwell cycles to complete for dwell excitation
|
|
|
|
uint8_t dwell_cycles;
|
|
|
|
|
2021-12-01 01:01:34 -04:00
|
|
|
// current test frequency, gain, and phase
|
|
|
|
float curr_test_freq;
|
|
|
|
float curr_test_gain;
|
|
|
|
float curr_test_phase;
|
|
|
|
|
|
|
|
// maximum measured rate throughout excitation used for max accel calculation
|
|
|
|
float max_meas_rate;
|
|
|
|
|
|
|
|
// maximum command associated with maximum rate used for max accel calculation
|
|
|
|
float max_command;
|
|
|
|
|
|
|
|
// maximum acceleration in cdss determined during test
|
2021-01-18 00:16:08 -04:00
|
|
|
float max_accel;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Input type for frequency response object
|
2021-01-18 00:16:08 -04:00
|
|
|
InputType excitation;
|
|
|
|
|
2022-01-17 10:37:43 -04:00
|
|
|
// Response type for frequency response object
|
|
|
|
ResponseType response;
|
|
|
|
|
2021-01-18 00:16:08 -04:00
|
|
|
// sweep_peak_finding_data tracks the peak data
|
|
|
|
struct sweep_peak_finding_data {
|
|
|
|
uint16_t count_m1;
|
|
|
|
float amplitude_m1;
|
|
|
|
float max_time_m1;
|
|
|
|
};
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Measured data for sweep peak
|
2021-01-18 00:16:08 -04:00
|
|
|
sweep_peak_finding_data sweep_meas;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Target data for sweep peak
|
2021-01-18 00:16:08 -04:00
|
|
|
sweep_peak_finding_data sweep_tgt;
|
|
|
|
|
2021-09-10 00:32:31 -03:00
|
|
|
//store gain data in ring buffer
|
2021-01-18 00:16:08 -04:00
|
|
|
struct peak_info {
|
|
|
|
uint16_t curr_count;
|
|
|
|
float amplitude;
|
|
|
|
uint32_t time_ms;
|
|
|
|
|
|
|
|
};
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Buffer object for measured peak data
|
2024-04-09 00:28:10 -03:00
|
|
|
ObjectBuffer<peak_info> *meas_peak_info_buffer;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Buffer object for target peak data
|
2024-04-09 00:28:10 -03:00
|
|
|
ObjectBuffer<peak_info> *tgt_peak_info_buffer;
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Push data into measured peak data buffer object
|
2021-01-18 00:16:08 -04:00
|
|
|
void push_to_meas_buffer(uint16_t count, float amplitude, uint32_t time_ms);
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Pull data from measured peak data buffer object
|
2021-01-18 00:16:08 -04:00
|
|
|
void pull_from_meas_buffer(uint16_t &count, float &litude, uint32_t &time_ms);
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Push data into target peak data buffer object
|
2021-01-18 00:16:08 -04:00
|
|
|
void push_to_tgt_buffer(uint16_t count, float amplitude, uint32_t time_ms);
|
2021-12-01 01:01:34 -04:00
|
|
|
|
|
|
|
// Pull data from target peak data buffer object
|
2021-01-18 00:16:08 -04:00
|
|
|
void pull_from_tgt_buffer(uint16_t &count, float &litude, uint32_t &time_ms);
|
|
|
|
|
|
|
|
};
|