Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AC_AutoTune/AC_AutoTune_FreqResp.h
Views: 1798
#pragma once12/*3Gain and phase determination algorithm4*/56#include <AP_Math/AP_Math.h>78class AC_AutoTune_FreqResp {9public:10// Constructor11AC_AutoTune_FreqResp()12{13}1415// Enumeration of input type16enum InputType {17DWELL = 0,18SWEEP = 1,19};2021// Enumeration of type22enum ResponseType {23RATE = 0,24ANGLE = 1,25};2627// Initialize the Frequency Response Object.28// Must be called before running dwell or frequency sweep tests29void init(InputType input_type, ResponseType response_type, uint8_t cycles);3031// Determines the gain and phase based on angle response for a dwell or sweep32void update(float command, float tgt_resp, float meas_resp, float tgt_freq);3334// Enable external query if cycle is complete and freq response data are available35bool is_cycle_complete() { return cycle_complete;}3637// Reset cycle_complete flag38void reset_cycle_complete() { cycle_complete = false; }3940// Frequency response data accessors41float get_freq() { return curr_test_freq; }42float get_gain() { return curr_test_gain; }43float get_phase() { return curr_test_phase; }44float get_accel_max() { return max_accel; }4546private:47// time of the start of a new target value search. keeps noise from prematurely starting the search of a new target value.48uint32_t new_tgt_time_ms;4950// flag for searching for a new target peak51bool new_target = false;5253// maximum target value54float max_target;5556// time of maximum target value in current cycle57uint32_t max_tgt_time;5859// counter for target value maximums60uint16_t max_target_cnt;6162// holds previously determined maximum target value while current cycle is running63float temp_max_target;6465// holds previously determined time of maximum target value while current cycle is running66uint32_t temp_max_tgt_time;6768// minimum target value69float min_target;7071// counter for target value minimums72uint16_t min_target_cnt;7374// holds previously determined minimum target value while current cycle is running75float temp_min_target;7677// maximum target value from previous cycle78float prev_target;7980// maximum target response from previous cycle81float prev_tgt_resp;8283// holds target amplitude for gain calculation84float temp_tgt_ampl;8586// time of the start of a new measured value search. keeps noise from prematurely starting the search of a new measured value.87uint32_t new_meas_time_ms;8889// flag for searching for a new measured peak90bool new_meas = false;9192// maximum measured value93float max_meas;9495// time of maximum measured value in current cycle96uint32_t max_meas_time;9798// counter for measured value maximums99uint16_t max_meas_cnt;100101// holds previously determined maximum measured value while current cycle is running102float temp_max_meas;103104// holds previously determined time of maximum measured value while current cycle is running105uint32_t temp_max_meas_time;106107// minimum measured value108float min_meas;109110// counter for measured value minimums111uint16_t min_meas_cnt;112113// holds previously determined minimum measured value while current cycle is running114float temp_min_meas;115116// maximum measured value from previous cycle117float prev_meas;118119// maximum measured response from previous cycle120float prev_meas_resp;121122// holds measured amplitude for gain calculation123float temp_meas_ampl;124125// calculated target rate from angle data126float target_rate;127128// calculated measured rate from angle data129float measured_rate;130131// holds start time of input to track length of time that input in running132uint32_t input_start_time_ms;133134// flag indicating when one oscillation cycle is complete135bool cycle_complete = false;136137// number of dwell cycles to complete for dwell excitation138uint8_t dwell_cycles;139140// current test frequency, gain, and phase141float curr_test_freq;142float curr_test_gain;143float curr_test_phase;144145// maximum measured rate throughout excitation used for max accel calculation146float max_meas_rate;147148// maximum command associated with maximum rate used for max accel calculation149float max_command;150151// maximum acceleration in cdss determined during test152float max_accel;153154// Input type for frequency response object155InputType excitation;156157// Response type for frequency response object158ResponseType response;159160// sweep_peak_finding_data tracks the peak data161struct sweep_peak_finding_data {162uint16_t count_m1;163float amplitude_m1;164float max_time_m1;165};166167// Measured data for sweep peak168sweep_peak_finding_data sweep_meas;169170// Target data for sweep peak171sweep_peak_finding_data sweep_tgt;172173//store gain data in ring buffer174struct peak_info {175uint16_t curr_count;176float amplitude;177uint32_t time_ms;178179};180181// Buffer object for measured peak data182ObjectBuffer<peak_info> meas_peak_info_buffer{12};183184// Buffer object for target peak data185ObjectBuffer<peak_info> tgt_peak_info_buffer{12};186187// Push data into measured peak data buffer object188void push_to_meas_buffer(uint16_t count, float amplitude, uint32_t time_ms);189190// Pull data from measured peak data buffer object191void pull_from_meas_buffer(uint16_t &count, float &litude, uint32_t &time_ms);192193// Push data into target peak data buffer object194void push_to_tgt_buffer(uint16_t count, float amplitude, uint32_t time_ms);195196// Pull data from target peak data buffer object197void pull_from_tgt_buffer(uint16_t &count, float &litude, uint32_t &time_ms);198199};200201202