MAOS
Multithreaded Adaptive Optics Simulator
Loading...
Searching...
No Matches
servo.h File Reference

Data Structures

struct  servo_t
struct  sho_t
union  servo_t.__unnamed0__
union  servo_t.__unnamed1__

Functions

dcell * servo_optim (real dt, long dtrat, real al, real pmargin, real f0, real zeta, int servo_type, const dmat *psdin, const dmat *sigma2n)
real servo_optim_margin (real dt, long dtrat, real al, real pmargin, real f0, real zeta)
dmat * servo_cl2ol (const dmat *psdcl, real dt, long dtrat, real al, real gain, real sigma2n)
real servo_residual (real *noise_amp, const dmat *psdin, real dt, long dtrat, real al, const dmat *gain, int servo_type)
servo_t * servo_new (anyarray merr, const dmat *ap, real al, real dt, const dmat *ep)
servo_t * servo_new_scalar (anyarray merr, real ap, real al, real dt, real ep)
servo_t * servo_new_sho (anyarray merr, const dmat *ap, real al, real dt, const dmat *ep, real f0, real zeta)
int servo_filter (servo_t *st, const anyarray merr)
void servo_add (servo_t *st, const anyarray madj, real alpha)
void servo_output (const servo_t *st, panyarray out)
dmat * servo_test (dmat *mideal, real dt, int dtrat, dmat *sigma2n, dmat *gain)
void servo_reset (servo_t *st)
void servo_free (servo_t *st)
sho_t * sho_new (real f0, real zeta)
void sho_free (sho_t *sho)
void sho_step (panyarray xout, sho_t *sho, anyarray xi, real dt, int average)
void sho_reset (sho_t *sho)
dmat * sho_filter (const dmat *xi, real dt, real f0, real zeta, int average)

Detailed Description

Routines for servo optimization, filtering, etc.


Data Structure Documentation

◆ servo_t

struct servo_t

Struct for servo filtering

Collaboration diagram for servo_t:
Data Fields
union servo_t.__unnamed0__ __unnamed__
union servo_t.__unnamed1__ __unnamed__
cell * mlead

lead filter temporary storage

cell * merrlast

recorded errro signal from last step

cell * merrhist

Keep a short history of merr

int initialized

is this data initialized

int alint

Integral part of latency

real alfrac

Fractional latency

dmat * ap
dmat * ep
real dt
sho_t * sho

◆ sho_t

struct sho_t

struct for state space modeling of a second order harnomic oscillator with resonance frequency of f0 and damping ratio of zeta.

Collaboration diagram for sho_t:
Data Fields
real dt
real c1
real c2
cell * dx
cell * x
cell * ddx
cell * ytmp

◆ servo_t.__unnamed0__

union servo_t.__unnamed0__
Collaboration diagram for servo_t.__unnamed0__:
Data Fields
cell * mint

second integrator. It is array to accomodate multiple ap's

dccell * mintc
dcell * mintd

◆ servo_t.__unnamed1__

union servo_t.__unnamed1__
Collaboration diagram for servo_t.__unnamed1__:
Data Fields
cell * mpreint

first integrator or other value.

dcell * mpreintc
dmat * mpreintd

Function Documentation

◆ servo_optim()

dcell * servo_optim ( real dt,
long dtrat,
real al,
real pmargin,
real f0,
real zeta,
int servo_type,
const dmat * psdin,
const dmat * sigma2n )

Optimize the servo gains by balancing errors due to noise and signal propagation.

Written: 2010-06-11

Tested OK: 2010-06-11

2011-01-17: The optimization process is quite slow, but the result is only dependent on the sigma2n and fs. psdin does not change during the simulation. Built a lookup table in skyc using various sigma2n and interpolate to get ress, resn, and gain.

2012-03-28: Cleaned up this routine. groupped similar calculations together. Change g2=a from g=sqrt(a)

2013-06-27: The maximum gain should not be limited to 0.5 beause it is later scaled by sqrt(a);

The upper end of frequency must be nyquist freq so that noise transfer can be computed. But we need to capture the turbulence PSD beyond nyquist freq, which are uncorrectable.

2022-03-04: added SHO response parameter.

Parameters
[in]dtSimulation time step (s).
[in]dtratNumber of time steps per loop update.
[in]alAdditional latency in units of dt.
[in]pmarginPhase margin (radian). 0 means pi/4.
[in]f0SHO resonance frequency (Hz). Not used if not positive.
[in]zetaSHO damping ratio.
[in]servo_type1 for integrator, 2 for type II with lead filter.
[in]psdinInput PSD with frequency in the first column. If NULL, only optimize for phase margin.
[in]sigma2nArray of all wanted noise variance. May be NULL.
Returns
Newly allocated dcell with one dmat per sigma2n entry, containing [g, res_sig, res_n] for type I or [g, a, T, res_sig, res_n] for type II. If psdin is NULL, contains only the gain. The caller frees it.

◆ servo_optim_margin()

real servo_optim_margin ( real dt,
long dtrat,
real al,
real pmargin,
real f0,
real zeta )

Optimize integrator control with optional SHO response (output) to have correct phase margin when abs(Hol)==1.

Parameters
[in]dtSimulation time step (s).
[in]dtratNumber of time steps per loop update.
[in]alAdditional latency in units of dt.
[in]pmarginPhase margin (radian). 0 means pi/4.
[in]f0SHO resonance frequency (Hz). Not used if not positive.
[in]zetaSHO damping ratio.
Returns
Integrator gain.

◆ servo_cl2ol()

dmat * servo_cl2ol ( const dmat * psdcl,
real dt,
long dtrat,
real al,
real gain,
real sigma2n )

Convert Closed loop residual PSD back to OL psd using rejection transfer function: PSD_OL=PSD_CL/Hrej-sigma2n/F_nyquist; Only implemented for integrator.

Parameters
[in]psdclClosed loop PSD with frequency in the first column and one or more PSD columns.
[in]dtSimulation time step (s).
[in]dtratNumber of time steps per loop update.
[in]alAdditional latency in units of dt.
[in]gainIntegrator gain. If 0, psdcl is copied.
[in]sigma2nNoise variance.
Returns
Newly allocated open loop PSD with frequency in the first column. Negative values are clipped to 0. The caller frees it.

◆ servo_residual()

real servo_residual ( real * noise_amp,
const dmat * psdin,
real dt,
long dtrat,
real al,
const dmat * gain,
int servo_type )

Compute the residual error after servo rejection of a type I or type II servo given the gain.

Written: 2010-06-11

Tested OK: 2010-06-11

Parameters
[out]noise_ampNoise amplification factor.
[in]psdinInput PSD with frequency in the first column and PSD in the second.
[in]dtSimulation time step (s).
[in]dtratNumber of time steps per loop update.
[in]alAdditional latency in units of dt.
[in]gainServo gain: [g] for type I or [g, a, T] for type II.
[in]servo_type1 for integrator, 2 for type II.
Returns
Residual signal error after servo rejection.

◆ servo_new()

servo_t * servo_new ( anyarray merr,
const dmat * ap,
real al,
real dt,
const dmat * ep )

Initialize servo_t.

Parameters
[in]merrTemplate measurement error used to allocate memory. May be empty.
[in]apCoefficients of the modified integrator. NULL means a pure integrator.
[in]alAdditional latency in units of dt.
[in]dtSimulation time step (s), not the loop update time.
[in]epServo gain. May be NULL if gain is applied externally.
Returns
Newly allocated servo_t. Free with servo_free().

◆ servo_new_scalar()

servo_t * servo_new_scalar ( anyarray merr,
real ap,
real al,
real dt,
real ep )

Initialize servo_t with scalar ap and ep

Parameters
[in]merrTemplate measurement error used to allocate memory. May be empty.
[in]apCoefficient of the modified integrator.
[in]alAdditional latency in units of dt.
[in]dtSimulation time step (s).
[in]epServo gain. 0 means gain is applied externally.
Returns
Newly allocated servo_t. Free with servo_free().

◆ servo_new_sho()

servo_t * servo_new_sho ( anyarray merr,
const dmat * ap,
real al,
real dt,
const dmat * ep,
real f0,
real zeta )

Initialize servo_t with an sho

Parameters
[in]merrTemplate measurement error used to allocate memory. May be empty.
[in]apCoefficients of the modified integrator. NULL means a pure integrator.
[in]alAdditional latency in units of dt.
[in]dtSimulation time step (s).
[in]epServo gain.
[in]f0SHO resonance frequency (Hz). SHO is not used if not positive or infinite.
[in]zetaSHO damping ratio.
Returns
Newly allocated servo_t. Free with servo_free().

◆ servo_filter()

int servo_filter ( servo_t * st,
const anyarray _merr )

Applies type I or type II filter based on number of entries in gain.

Parameters
[in,out]stServo struct.
[in]_merrMeasurement error. May be empty.
Returns
1 if the integrator is updated, 0 if there is no (delayed) measurement error yet.

◆ servo_add()

void servo_add ( servo_t * st,
const anyarray madj,
real alpha )

Adjust integrator content without shift.

Parameters
[in,out]stServo struct.
[in]madjAdjustment to add to the integrator.
[in]alphaScaling factor of madj.

◆ servo_output()

void servo_output ( const servo_t * st,
panyarray out_ )

Create servo output. It handles st->alfrac. and outputs the averaged position over the integration period.

Parameters
[in]stServo struct. Its SHO state, if any, is updated.
[out]out_Output. Allocated if NULL.

◆ servo_test()

dmat * servo_test ( dmat * input,
real dt,
int dtrat,
dmat * sigma2n,
dmat * gain )

test type I/II filter with ideal measurement to make sure it is implemented correctly.

Parameters
[in,out]inputInput time series. Each column is a time step. Reshaped in place if it has a single column.
[in]dtSimulation time step (s).
[in]dtratNumber of time steps per loop update.
[in]sigma2nNoise covariance. Noise is not added if its norm is 0.
[in]gainServo gain.
Returns
Newly allocated residual time series. The caller frees it.

◆ servo_reset()

void servo_reset ( servo_t * st)

reset the data to 0.

Parameters
[in,out]stServo struct.

◆ servo_free()

void servo_free ( servo_t * st)

Free servo_t struct

Parameters
[in]stServo struct to free. May be NULL.

◆ sho_new()

sho_t * sho_new ( real f0,
real zeta )

Second harmonic oscillator. Initialization. Equation is is d^x/dt^2+c1*dx/dt+c2*x=c2*xi State space model is |x'|' | -c1 -c2 | |x'| |xi| |x | = | 1 0 | | x| + c2 |0 | where x' is dx/dt and written as dx in struct. we use c2*xi so that it is in the same unit and scale as x.

Parameters
[in]f0Resonance frequency (Hz).
[in]zetaDamping ratio.
Returns
Newly allocated sho_t, or NULL if f0 is 0 or infinite. Free with sho_free().

◆ sho_free()

void sho_free ( sho_t * sho)

Free sho_t (second order harmonic oscillator).

Parameters
[in]shoSHO struct to free. May be NULL.

◆ sho_step()

void sho_step ( panyarray xout_,
sho_t * sho,
anyarray xi_,
real dt,
int avg )

Second harmonic oscillator state space update. xi is driving position. Second harmonic oscillator state space update. xi is driving position. Update the position after time dt. Also compute the average position between 0 and dt (for WFS integration) if avg is set.

sho->x +=dt*sho->dx;//update position ddx=sho->dx*(-sho->c1)+sho->x*(-sho->c2)+xi;//update speed sho->dx+=dt*ddx; //update speed

Parameters
[out]xout_Output position (or average position if avg is set). Allocated if NULL.
[in,out]shoSHO state. If NULL, xi is copied to xout_.
[in]xi_Driving position.
[in]dtTime to advance (s).
[in]avgWhether to output the average position over dt instead of the final position.

◆ sho_reset()

void sho_reset ( sho_t * sho)

Second harmonic oscillator. Reset.

Parameters
[in,out]shoSHO state.

◆ sho_filter()

dmat * sho_filter ( const dmat * x,
real dt,
real f0,
real zeta,
int avg )

Second harmonic oscillator. Filter a time series for testing.

Parameters
[in]xInput time series. Each column is filtered independently. A row vector is treated as a column.
[in]dtInput time series sampling (s).
[in]f0Resonance frequency (Hz).
[in]zetaDamping ratio.
[in]avgAverage or not.
Returns
Newly allocated filtered time series. The caller frees it.