Utilities Module

This module contains certain utilities and constants used internally by the library.

From all its functionality, the user might find useful the following resources:

  • Implementation of a Moving Average Filter.
  • Time definitions for proper time management.

How to use

  1. First, add all the SOMANET Motion Control modules to your app Makefile.
USED_MODULES = module_utils module_hall module_pwm module_adc module_controllers lib_bldc_torque_control module_profiles module_gpio module_incremental_encoder module_watchdog module_board-support

Note

Not all modules will be required, but when using a library it is recommended to include always all the contained modules. This will help solving internal dependency issues.

Moving Average Filter

  1. Include the filter header in your app.
#include <filter_blocks.h>
  1. Now you can just use filter API.
int filter_buffer[8] = {0};
int index = 0;

init_filter(filter_buffer, index, 8);
filter(filter_buffer, index, 8, NEW_SAMPLE_OF_THE_SIGNAL_TO_FILTER);

Time Definitions

  1. Include the header in your app.
#include <refclk.h>
  1. If you need to set 1 ms time in your code, and your code is running on tile with a 100MHz Reference Freq.:
timer t;
unsigned ts;

t :> ts;
t when timerafter(ts + MSEC_STD) :> ts;
  1. However, if you need to set 1 ms time in your code, but your code is running on tile with a 250MHz Reference Freq. (e.g. IF2 tile):
timer t;
unsigned ts;

t :> ts;
t when timerafter(ts + MSEC_FAST) :> ts;

API

Time definitions

USEC_STD
MSEC_STD

Defines the amount of ticks of a 100MHz timer (standard system reference frequency) for 1 ms.

SEC_STD

Defines the amount of ticks of a 100MHz timer (standard system reference frequency) for 1 s.

USEC_FAST
MSEC_FAST

Defines the amount of ticks of a 250MHz timer (Motorcontrol TILE reference frequency) for 1 ms.

SEC_FAST

Defines the amount of ticks of a 250MHz timer (Motorcontrol TILE reference frequency) for 1 s.

Moving Average Filter

void init_filter(int filter_buffer[], int &index, int filter_length)

Initialize Moving Average Filter Configuration.

Parameters:
  • filter_buffer – Reference to the samples array to initialize.
  • index – Reference to the index variable to initialize.
  • filter_length – Defines the length of the filter.
Returns:

void

int filter(int filter_buffer[], int &index, int filter_length, int input)

Get moving average filtered output.

Parameters:
  • filter_buffer – Samples to filter.
  • index – Index of the filter.
  • filter_length – Defines the length of the filter.
  • input – New sample.
Returns:

Filtered output.