<< >>

API

Configuration Defines

The file dsc_config.h must be provided in the application source code. This file can set the following defines:

PWM_DEAD_TIME

This is the period, in 10ns intervals, which is not counted towards the PWM time as the PWM is output.

PWM_MAX_VALUE

The PWM input is clamped to this value

LOCK_ADC_TO_PWM

If this is defined, the PWM outputs synchronization information to a channel and dummy port, allowing the ADC module to synchronize the ADC measurement to the dead time when all PWM channels are off.

NUMBER_OF_POLES

This is the number of poles in the motor. It is therefore ratio of the number of electrical rotations to each physical rotation. If a motor has a single winding per coil, then it is called a 2 pole motor. Two sets of windings per coil makes a four pole motor, and so on.

USE_CAN

When defined, the CAN controller in included in the executable. This option is mutually exclusive with Ethernet.

USE_ETH

When defined, the Ethernet controller is included in the executable. This option is mutually exclusive with CAN.

TCP_CONTROL_PORT

When the Ethernet controller is included, this is the TCP port that the server listens on, for receiving control information.

MIN_RPM

The minimum RPM that the controllers can set.

MAX_RPM

The maximum RPM that the controllers can set.

External modules

For documentation on the Ethernet, CAN and PWM modules, see the relevent XMOS software module documentation.

ADC

Client functions

void do_adc_calibration(streaming chanend c_adc)

ADC calibration sequence.

This switches the ADC server into calibration mode. After a number (512) of samples the server reverts to non-calibration mode, and subsequent calls to the function get_adc_vals_calibrated_int16 will take the measured average of the calibration period as an offset

Parameters:
  • c_adc – the control channel to the ADC server
{int, int, int} g get_adc_vals_calibrated_int16(streaming chanend c_adc)

Get values converted from 14 bit unsigned to 16 bit signed and calibrated.

Read a set of current values from the motor, and convert them into a standardized 16 bit scale

Parameters:
  • c_adc – the control channel to the ADC server

Server functions

void adc_7265_triggered(streaming chanend c_adc[ADC_NUMBER_OF_TRIGGERS], chanend c_trig[ADC_NUMBER_OF_TRIGGERS], clock clk, out port SCLK, port CNVST, in buffered port:32 DATA_A, in buffered port:32 DATA_B, port out MUX)

Implements the AD7265 triggered ADC service.

This implements the AD hardware interface to the 7265 ADC device. It has two ports to allow reading two simultaneous current readings for a single motor.

Parameters:
  • c_adc – the array of ADC server control channels
  • c_trig – the array of channels to recieve triggers from the PWM modules
  • clk – an XCORE clock to provide clocking to the ADC
  • SCLK – the external clock pin on the ADC
  • CNVST – the convert strobe on the ADC
  • DATA_A – the first data port on the ADC
  • DATA_B – the second data port on the ADC
  • MUX – a port to allow the selection of the analogue MUX input
void adc_ltc1408_triggered(chanend c_adc[], chanend c_trig[], clock clk, port out SCLK, buffered out port:32 CNVST, in buffered port:32 DATA)

Execute the triggered ADC server.

This is the server thread implementation for the LTC1408 ADC device.

Parameters:
  • c_adc – the array of ADC control channels
  • c_trig – the array of channels to receive triggers from the PWM modules
  • clk – the clock for the ADC device serial port
  • SCLK – the port which feeds the ADC serial clock
  • CNVST – the ADC convert strobe
  • DATA – the ADC data port
void run_adc_max1379(chanend c_adc[], clock clk, port out SCLK, port out CNVST, port out SEL, in buffered port:32 DATA)

The server thread for the MAX1379 ADC device.

Implements the server thread for the MAX1379 ADC device

Parameters:
  • c_adc – the array of ADC control channels
  • clk – the clock for the ADC device serial port
  • SCLK – the port which feeds the ADC serial clock
  • CNVST – the ADC convert strobe
  • SEL – the chip select for the ADC device
  • DATA – the ADC data port

QEI

Client functions

{ unsigned, unsigned, unsigned } g get_qei_data(streaming chanend c_qei)

Get the position from the QEI server.

Parameters:
  • c_qei – The control channel for the QEI server
Returns:

the speed, position and valid state

Server functions

void do_qei(streaming chanend c_qei, port in p_qei)

Implementation of the QEI server thread.

Parameters:
  • c_qei – The control channel used by the client
  • p_qei – The hardware port where the quadrature encoder is located
void do_multiple_qei(streaming chanend c_qei[], port in p_qei[])

Implementation of the QEI server thread that services multiple QEI devices.

Parameters:
  • c_qei – The control channels used by the client
  • p_qei – The hardware ports where the quadrature encoder is located

Hall sensors

Client functions

{unsigned, unsigned, unsigned} g get_hall_pos_speed_delta(chanend c_hall)

Get position, speed and delta from a hall server.

The client library function for a hall sensor server

Parameters:
  • c_hall – the channel for communicating with the hall server
void do_hall(unsigned &hall_state, unsigned &cur_pin_state, port in p_hall)

A blocking read of the hall port.

Parameters:
  • hall_state – the output hall state
  • cur_pin_state – the last value read from the hall encoder port
  • p_hall – the hall port
select do_hall_select(unsigned &hall_state, unsigned &cur_pin_state, port in p_hall)

A selectable read of the hall pins.

This selectable function becomes ready when the hall pins change state

Parameters:
  • hall_state – the output hall state
  • cur_pin_state – the last value read from the hall encoder port
  • p_hall – the hall port

Server functions

void run_hall(chanend c_hall, port in p_hall)

A basic hall encoder server.

This implements the basic hall sensor server

Parameters:
  • c_hall – the control channel for reading hall position
  • p_hall – the port for reading the hall sensor data
void run_hall_speed(chanend c_hall, chanend c_speed, port in p_hall)

A hall encoder server that also calculates motor speed.

This implements the hall sensor server

Parameters:
  • c_hall – the control channel for reading hall position
  • c_speed – the control channel for reading the rotor speed
  • p_hall – the port for reading the hall sensor data
void run_hall_speed_timed_avg(chanend c_hall, chanend c_speed, port in p_hall)

A hall encoder server that also calculates motor speed.

This implements the hall sensor server, where the speed is calculated using a timed average of many values.

Parameters:
  • c_hall – the control channel for reading hall position
  • c_speed – the control channel for reading the rotor speed
  • p_hall – the port for reading the hall sensor data
void run_hall_speed_timed(chanend c_hall, chanend c_speed, port in p_hall, chanend ?c_logging_0, chanend ?c_logging_1)

A hall encoder server that also calculates motor speed.

This implements the hall sensor server, where the speed is calculated using a timed average of many values.

Parameters:
  • c_hall – the control channel for reading hall position
  • c_speed – the control channel for reading the rotor speed
  • p_hall – the port for reading the hall sensor data
  • c_logging_0 – an optional channel for logging the hall data on port 0
  • c_logging_1 – an optional channel for logging the hall data on port 1

Computational Blocks

void park_transform(int &Id, int &Iq, int I_alpha, int I_beta, unsigned theta)

Perform a Park transform.

A Park transform is a 2D to 2D transform which takes the radial and tangential components of a measurement (for instance the magnetic flux or total coil currents) and converts them to a rotating frame of reference. Typically this is the rotating frame of reference attached to the spinning rotor.

Parameters:
  • Id – the output tangential component
  • Iq – the output radial component
  • I_alpha – the input tangential component
  • I_beta – the input radial component
  • theta – the angle between the fixed and rotating frames of reference
void inverse_park_transform(int &I_alpha, int &I_beta, int Id, int Iq, unsigned theta)

Perform an inverse Park transform.

A Park transform is a 2D to 2D transform which takes the radial and tangential components of a measurement (for instance the magnetic flux or total coil currents) and converts them to a rotating frame of reference. Typically this is the rotating frame of reference attached to the spinning rotor.

Parameters:
  • I_alpha – the output tangential component
  • I_beta – the output radial component
  • Id – the input tangential component
  • Iq – the intput radial component
  • theta – the angle between the fixed and rotating frames of reference
void clarke_transform(int Ia, int Ib, int Ic, int &I_alpha, int &I_beta)

Perform a clarke transform.

A Clarke transform is a 3D to 2D transformation where the 3D components have only 2 degrees of freedom. It is used to convert the three current values in the 120 degree separation coils into a radial and tangential component values.

Parameters:
  • Ia – the parameter from coil A
  • Ib – the parameter from coil B
  • Ic – the parameter from coil C
  • I_alpha – the output tangential component
  • I_beta – the output radial component
void inverse_clarke_transform(int &Ia, int &Ib, int &Ic, int alpha, int beta)

Perform an inverse clarke transform.

The inverse Clarke transform is a 2D to 3D transformation where the 3D components have only 2 degrees of freedom. It is used to convert radial and tangential components of the current vector into the three coil currents.

Parameters:
  • Ia – the output parameter for coil A
  • Ib – the output parameter for coil B
  • Ic – the output parameter for coil C
  • alpha – the input tangential component
  • beta – the input radial component
int sine(unsigned angle)

Look up the fixed point sine value.

This looks up the sine of a value. The value is the index into the sine table, rather than a particular angular measurement. The sine table has 256 entries, so each index is 1.4 degrees.

A table of 256 entries is suitable for an encoder angle measured in 1024 steps, attached to a 4 pole motor. Each encoder step is 360/1024 = 0.35 physical degrees, but this is worth 4 times as many electrical degrees, or 1.4 electrical degrees.

The result is in fixed point 18.14 format.

Parameters:
  • angle – the index of the sine value to look up
Returns:

the 18.14 fixed point sine value

int cosine(unsigned angle)

Look up the fixed point cosine value.

This looks up the cosine of a value. The value is the index into the sine table, rather than a particular angular measurement.

Parameters:
  • angle – the index of the cosine value to look up
Returns:

the 18.14 fixed point cosine value

Watchdog Timer

void do_wd(chanend c_wd, out port wd)

Run the watchdog timer server.

The watchdog timer needs a constant stream of pulses to prevent it from shutting down the motor. This is a thread server which implements the watchdog timer output.

The watchdog control port should have two bits attached to the watchdog circuitry. Bit zero will get a rising edge whenever the watchdog is to be reset, and bit one will have the pulse train.

Parameters:
  • c_wd – the control channel for controlling the watchdog
  • wd – the control port for the watchdog device

High level communications

Ethernet control

void do_comms_eth(chanend c_commands[], chanend tcp_svr)

Implement the high level Ethernet control server.

This control the motors based on commands from the ethernet/TCP stack

Parameters:
  • c_commands – Array of command channels for motors
  • tcp_svr – channel to the TCP/IP thread

CAN control

void do_comms_can(chanend c_commands[], chanend rxChan, chanend txChan)

This is a thread which performs the higher level control for the CAN interface.

Use it in conjunction with the thread ‘canPhyRxTx’ from the module module_can.

Parameters:
  • c_commands – Channel array for interfacing to the motors
  • rxChan – Connect to the rxChan port on the canPhyRxTx
  • txChan – Connect to the txChan port on the canPhyRxTx

LCD display and PHY reset

LCD

lcd_interface_t

The control structure for the LCD ports.

The display uses an I2C interface with an extra control signal to support selection between a data read/write and a control read/write. This extra signal is bit zero of the p_core1_shared member - which contains no other signals despite the name.

Structure Members:

out port p_lcd_sclk

i2c serial clock

out port p_lcd_mosi

i2c serial data

out port p_lcd_cs_n

i2c chip select

out port p_core1_shared

Display data/control select.

void reverse(char s[])

Reverse the order of bytes in the array.

Parameters:
  • s – the byte array to reverse
void itoa(int n, char s[])

Convert an integer into a base 10 ASCII string.

Parameters:
  • n – the integer to represent in string form
  • s – the output byte array to contain the number
void lcd_ports_init(lcd_interface_t &p)

Initialise the LCD device.

Parameters:
  • p – the LCD interface description
void lcd_byte_out(lcd_interface_t &p, unsigned char i, int is_data)

Write a byte to the LCD.

Parameters:
  • p – the LCD interface description
  • i – the byte to write
  • is_data – a boolean indicating if the write is data or control information
void lcd_clear(lcd_interface_t &p)

Clear the LCD.

Parameters:
  • p – the LCD interface description
void lcd_draw_image(const unsigned char image[], lcd_interface_t &p)

Draw an image on the LCD.

Draw an image on the LCD. The image is assumed to cover the complete LCD. The size of the LCD is 128 wide by 32 high.

Parameters:
  • image – a byte array containing the image data.
  • p – the LCD interface description
void lcd_draw_text_row(const char string[], int lcd_row, lcd_interface_t &p)

Write text to a row on the LCD.

Display a row of text. The LCD columns beyond the end of the string will be cleared.

Parameters:
  • string – the ASCII string to display on the LCD
  • lcd_row – the character row on which to display the string
  • p – the LCD interface description