Martijn Grootens / AS5048

Dependents:   heros_leg_readout_torque_addition heros_leg_readout_torque_addition heros_leg_readout_torque_addition_V3

Embed: (wiki syntax)

« Back to documentation index

As5048 Class Reference

Interfacing with the AMS AS5048A magnetic rotary sensor using SPI protocol AS5048 uses 16-bit transfer; We use two 8-bit transfers for compatibility with 8-bit SPI master devices SPI protocol: Mode = 1: clock polarity = 0 --> clock pulse is high clock phase = 1 --> sample on falling edge of clock pulse Code was succesfully tested on the FRDM KL25Z and K22F. More...

#include <as5048.h>

Public Member Functions

 As5048 (PinName mosi, PinName miso, PinName sck, PinName cs, int num_sensors=1)
 Creates an object of num_sensors daisy chained AS5048 sensors; default number of sensors in chain is 1.
 ~As5048 ()
 Destructor, memory deallocation.
void UpdateAngleBuffer ()
 Update the buffer with angular measurements NOTE 1: If the last command sent through Transfer was *not* AS_CMD_ANGLE then we need an additional Transfer; this takes more time! This should not occur, since Transfer is not *yet* used elsewhere.
const uint16_t * get_read_buffer ()
const uint16_t * get_angle_buffer ()
const uint16_t * get_angle_offset ()
const bool * get_directions_ ()
int getAngle (int i_sensor=0)
 You get the angles from two UpdateAngleBuffer() calls before.
float getAngleRatio (int i_sensor=0)
 You get the angles from two UpdateAngleBuffer() calls before.
float getAngleDegrees (int i_sensor=0)
 You get the angles from two UpdateAngleBuffer() calls before.
float getAngleRadians (int i_sensor=0)
 You get the angles from two UpdateAngleBuffer() calls before.
bool setDirection (int i_sensor, bool dir)
 Set direction for a sensor.
bool setDirection (bool dir)
 Set direction for the first sensor.
bool setOffset (int i_sensor, uint16_t offset)
 Set offset for a sensor.
bool setOffset (uint16_t offset)
 Set offset for the first sensor.
bool setOffsetRatio (int i_sensor, float offset_ratio)
 Set offset for a sensor.
bool setOffsetRatio (float offset_ratio)
 Set offset for the first sensor.
bool setOffsetDegrees (int i_sensor, float offset_degrees)
 Set offset for a sensor.
bool setOffsetDegrees (float offset_degrees)
 Set offset for the first sensor.
bool setOffsetRadians (int i_sensor, float offset_radians)
 Set offset for a sensor.
bool setOffsetRadians (float offset_radians)
 Set offset for the first sensor.

Static Public Member Functions

static bool CheckParity (int n)
 Parity check.

Protected Member Functions

void SelectChip ()
 Select (low) chip, and wait 1 us (at least 350 ns)
void DeselectChip ()
 Deselect (high) chip, and wait 1 us (at least 350 ns)
void Transfer (As5048Command cmd)
 SPI transfer between each of the daisy chained sensors.

Detailed Description

Interfacing with the AMS AS5048A magnetic rotary sensor using SPI protocol AS5048 uses 16-bit transfer; We use two 8-bit transfers for compatibility with 8-bit SPI master devices SPI protocol: Mode = 1: clock polarity = 0 --> clock pulse is high clock phase = 1 --> sample on falling edge of clock pulse Code was succesfully tested on the FRDM KL25Z and K22F.

The same code fails on the K64F for some reason. Sampling using a logic analyzer does however show the same results for al three boards.

Definition at line 17 of file as5048.h.


Constructor & Destructor Documentation

As5048 ( PinName  mosi,
PinName  miso,
PinName  sck,
PinName  cs,
int  num_sensors = 1 
)

Creates an object of num_sensors daisy chained AS5048 sensors; default number of sensors in chain is 1.

Parameters:
mosi,:pinname of the mosi pin of the spi communication
miso,:pinname of the miso pin of the spi communication
sck,:pinname of the clock pin of the spi communication
cs,:pinname of the chip select pin of the spi communication
num_sensors= 1: number of sensors in daisy chain

Definition at line 65 of file as5048.h.

~As5048 (  )

Destructor, memory deallocation.

Definition at line 94 of file as5048.h.


Member Function Documentation

static bool CheckParity ( int  n ) [static]

Parity check.

Parameters:
n,:integer to check
Returns:
: true if ok

Definition at line 107 of file as5048.h.

void DeselectChip (  ) [protected]

Deselect (high) chip, and wait 1 us (at least 350 ns)

Definition at line 322 of file as5048.h.

const uint16_t* get_angle_buffer (  )
Returns:
: pointer to angle_buffer_

Definition at line 156 of file as5048.h.

const uint16_t* get_angle_offset (  )
Returns:
: pointer to angle_offet_

Definition at line 161 of file as5048.h.

const bool* get_directions_ (  )
Returns:
: pointer to directions_

Definition at line 166 of file as5048.h.

const uint16_t* get_read_buffer (  )
Returns:
: pointer to read_buffer_

Definition at line 151 of file as5048.h.

int getAngle ( int  i_sensor = 0 )

You get the angles from two UpdateAngleBuffer() calls before.

Returns:
: 14 bits absolute position

Definition at line 172 of file as5048.h.

float getAngleDegrees ( int  i_sensor = 0 )

You get the angles from two UpdateAngleBuffer() calls before.

Returns:
: angle in degrees

Definition at line 188 of file as5048.h.

float getAngleRadians ( int  i_sensor = 0 )

You get the angles from two UpdateAngleBuffer() calls before.

Returns:
: angle in radians

Definition at line 194 of file as5048.h.

float getAngleRatio ( int  i_sensor = 0 )

You get the angles from two UpdateAngleBuffer() calls before.

Returns:
: revolution ratio in [0,1]

Definition at line 182 of file as5048.h.

void SelectChip (  ) [protected]

Select (low) chip, and wait 1 us (at least 350 ns)

Definition at line 317 of file as5048.h.

bool setDirection ( int  i_sensor,
bool  dir 
)

Set direction for a sensor.

Parameters:
i_sensor,:id of sensor for which the offset is to be set
dir,:true positive, false negative
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 202 of file as5048.h.

bool setDirection ( bool  dir )

Set direction for the first sensor.

Parameters:
dir,:true positive, false negative
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 216 of file as5048.h.

bool setOffset ( int  i_sensor,
uint16_t  offset 
)

Set offset for a sensor.

Parameters:
i_sensor,:id of sensor for which the offset is to be set
offset,:offset in counts [0,2**14-1]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 228 of file as5048.h.

bool setOffset ( uint16_t  offset )

Set offset for the first sensor.

Parameters:
offset,:offset in counts [0,2**14-1]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 242 of file as5048.h.

bool setOffsetDegrees ( int  i_sensor,
float  offset_degrees 
)

Set offset for a sensor.

Parameters:
i_sensor,:id of sensor for which the offset is to be set
offset_degrees,:offset in degrees in [0,360]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 271 of file as5048.h.

bool setOffsetDegrees ( float  offset_degrees )

Set offset for the first sensor.

Parameters:
offset_degrees,:offset in degrees in [0,360]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 281 of file as5048.h.

bool setOffsetRadians ( int  i_sensor,
float  offset_radians 
)

Set offset for a sensor.

Parameters:
i_sensor,:id of sensor for which the offset is to be set
offset_radians,:offset in radians in [0,2*pi]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 292 of file as5048.h.

bool setOffsetRadians ( float  offset_radians )

Set offset for the first sensor.

Parameters:
offset_radians,:offset in radians in [0,2*pi]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 302 of file as5048.h.

bool setOffsetRatio ( int  i_sensor,
float  offset_ratio 
)

Set offset for a sensor.

Parameters:
i_sensor,:id of sensor for which the offset is to be set
offset_ratio,:offset in ratio in [0,1]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 250 of file as5048.h.

bool setOffsetRatio ( float  offset_ratio )

Set offset for the first sensor.

Parameters:
offset_ratio,:offset in ratio in [0,1]
Returns:
: true if i_sensor in [0,kNumSensor_)

Definition at line 260 of file as5048.h.

void Transfer ( As5048Command  cmd ) [protected]

SPI transfer between each of the daisy chained sensors.

Parameters:
cmd,:Command to send

Definition at line 328 of file as5048.h.

void UpdateAngleBuffer (  )

Update the buffer with angular measurements NOTE 1: If the last command sent through Transfer was *not* AS_CMD_ANGLE then we need an additional Transfer; this takes more time! This should not occur, since Transfer is not *yet* used elsewhere.

NOTE 2: We run a parity check on the results from the transfer. We only update the angle_buffer_ with values that pass the parity check. Measurement using Timer on K64F for last_command_ == AS_CMD_ANGLE shows this function takes 87 or 88 us.

Definition at line 129 of file as5048.h.