microbit-dal/inc/MicroBitAccelerometer.h

268 lines
8 KiB
C
Raw Normal View History

#ifndef MICROBIT_ACCELEROMETER_H
#define MICROBIT_ACCELEROMETER_H
#include "mbed.h"
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
#include "MicroBitComponent.h"
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
#include "MicroBitCoordinateSystem.h"
/**
* Relevant pin assignments
*/
#define MICROBIT_PIN_ACCEL_DATA_READY P0_28
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
/**
* Status flags
*/
#define MICROBIT_ACCEL_PITCH_ROLL_VALID 0x01
/*
* I2C constants
*/
#define MMA8653_DEFAULT_ADDR 0x3A
/*
* MMA8653 Register map (partial)
*/
#define MMA8653_STATUS 0x00
#define MMA8653_OUT_X_MSB 0x01
#define MMA8653_WHOAMI 0x0D
#define MMA8653_XYZ_DATA_CFG 0x0E
#define MMA8653_CTRL_REG1 0x2A
#define MMA8653_CTRL_REG2 0x2B
#define MMA8653_CTRL_REG3 0x2C
#define MMA8653_CTRL_REG4 0x2D
#define MMA8653_CTRL_REG5 0x2E
/**
* MMA8653 constants
*/
#define MMA8653_WHOAMI_VAL 0x5A
#define MMA8653_SAMPLE_RANGES 3
#define MMA8653_SAMPLE_RATES 8
/*
* Accelerometer events
*/
#define MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE 1
struct MMA8653Sample
{
int16_t x;
int16_t y;
int16_t z;
};
struct MMA8653SampleRateConfig
{
uint32_t sample_period;
uint8_t ctrl_reg1;
};
struct MMA8653SampleRangeConfig
{
uint8_t sample_range;
uint8_t xyz_data_cfg;
};
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
extern const MMA8653SampleRangeConfig MMA8653SampleRange[];
extern const MMA8653SampleRateConfig MMA8653SampleRate[];
/**
* Class definition for MicroBit Accelerometer.
*
* Represents an implementation of the Freescale MMA8653 3 axis accelerometer
* Also includes basic data caching and on demand activation.
*/
class MicroBitAccelerometer : public MicroBitComponent
{
/**
* Unique, enumerated ID for this component.
* Used to track asynchronous events in the event bus.
*/
uint16_t address; // I2C address of this accelerometer.
uint16_t samplePeriod; // The time between samples, in milliseconds.
uint8_t sampleRange; // The sample range of the accelerometer in g.
MMA8653Sample sample; // The last sample read.
DigitalIn int1; // Data ready interrupt.
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
float pitch; // Pitch of the device, in radians.
float roll; // Roll of the device, in radians.
public:
/**
* Constructor.
* Create an accelerometer representation with the given ID.
* @param id the ID of the new object.
* @param address the default base address of the accelerometer.
*
* Example:
* @code
* accelerometer(MICROBIT_ID_ACCELEROMETER, MMA8653_DEFAULT_ADDR)
* @endcode
*/
MicroBitAccelerometer(uint16_t id, uint16_t address);
/**
* Configures the accelerometer for G range and sample rate defined
* in this object. The nearest values are chosen to those defined
* that are supported by the hardware. The instance variables are then
* updated to reflect reality.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured.
*/
int configure();
/**
* Reads the acceleration data from the accelerometer, and stores it in our buffer.
* This is called by the tick() member function, if the interrupt is set.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the read request fails.
*/
int update();
/**
* Attempts to set the sample rate of the accelerometer to the specified value (in ms).
* n.b. the requested rate may not be possible on the hardware. In this case, the
* nearest lower rate is chosen.
* @param period the requested time between samples, in milliseconds.
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
*/
int setPeriod(int period);
/**
* Reads the currently configured sample rate of the accelerometer.
* @return The time between samples, in milliseconds.
*/
int getPeriod();
/**
* Attempts to set the sample range of the accelerometer to the specified value (in g).
* n.b. the requested range may not be possible on the hardware. In this case, the
* nearest lower rate is chosen.
* @param range The requested sample range of samples, in g.
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
*/
int setRange(int range);
/**
* Reads the currently configured sample range of the accelerometer.
* @return The sample range, in g.
*/
int getRange();
/**
* Attempts to determine the 8 bit ID from the accelerometer.
* @return the 8 bit ID returned by the accelerometer, or MICROBIT_I2C_ERROR if the request fails.
*
* Example:
* @code
* uBit.accelerometer.whoAmI();
* @endcode
*/
int whoAmI();
/**
* Reads the X axis value of the latest update from the accelerometer.
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
* @param system The coordinate system to use. By default, a simple cartesian system is provided.
* @return The force measured in the X axis, in milli-g.
*
* Example:
* @code
* uBit.accelerometer.getX();
* @endcode
*/
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Reads the Y axis value of the latest update from the accelerometer.
* @return The force measured in the Y axis, in milli-g.
*
* Example:
* @code
* uBit.accelerometer.getY();
* @endcode
*/
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Reads the Z axis value of the latest update from the accelerometer.
* @return The force measured in the Z axis, in milli-g.
*
* Example:
* @code
* uBit.accelerometer.getZ();
* @endcode
*/
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
/**
* Provides a rotation compensated pitch of the device, based on the latest update from the accelerometer.
* @return The pitch of the device, in degrees.
*
* Example:
* @code
* uBit.accelerometer.getPitch();
* @endcode
*/
int getPitch();
float getPitchRadians();
/**
* Provides a rotation compensated roll of the device, based on the latest update from the accelerometer.
* @return The roll of the device, in degrees.
*
* Example:
* @code
* uBit.accelerometer.getRoll();
* @endcode
*/
int getRoll();
float getRollRadians();
/**
* periodic callback from MicroBit idle thread.
* Check if any data is ready for reading by checking the interrupt flag on the accelerometer
*/
virtual void idleTick();
/**
* Returns 0 or 1. 1 indicates data is waiting to be read, zero means data is not ready to be read.
*/
virtual int isIdleCallbackNeeded();
private:
/**
* Issues a standard, 2 byte I2C command write to the accelerometer.
* Blocks the calling thread until complete.
*
* @param reg The address of the register to write to.
* @param value The value to write.
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed.
*/
int writeCommand(uint8_t reg, uint8_t value);
/**
* Issues a read command into the specified buffer.
* Blocks the calling thread until complete.
*
* @param reg The address of the register to access.
* @param buffer Memory area to read the data into.
* @param length The number of bytes to read.
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed.
*/
int readCommand(uint8_t reg, uint8_t* buffer, int length);
microbit: Added support for compass tilt compensation An e-compass solution requires knowwlede two pieces of data to provide an accurate heading: - Accurate calibration of the magnetometer hardware so that reliable measurements can be taken. - Knowledge of the pitch and roll of of device, so that the correct components of the X/Y and Z axis sensors of the magnetomer can be used to sense the magnetic field in a horizontal plane regardless of the tilt of the device. This commit represent changes to the MicroBitAccelerometer and MicroBitCompass classes to implemen tthese goals. More specifically, this commit provides: - The introduciton of an interactive calibration 'game', that can rapidly gather all the data required to calibrate the compass. - An improved calibration algorithm based on a Least Mean Squares approach of compass samples, as documened in Freescale Application Note AN4248. - The inclusion of a simple Matrix4 class to enable efficient Least Mean Squares implementation. - A change from asynchronous to synchronous calibration of the compass when first used. This is in repsonse to a feature request for this from users and high level languages using microbit-dal. - Support for detemrining tilt and roll angle in MicroBitAccelerometer - Support for multiple co-ordinate spaces in MicroBitAccelerometer and MicroBitCompass. Data can now be read in either RAW (unaltered) data. MICORBIT_SIMPLE_CARTESIAN (as used previously) or NORTH_EAST_DOWN (the industry convention in mobile phones, tablets and aviation) - Implementation of a tilt compensated algorithm, used when determining device heading.
2015-12-17 14:08:30 +00:00
/**
* Recalculate roll and pitch values for the current sample.
* We only do this at most once per sample, as the necessary trigonemteric functions are rather
* heavyweight for a CPU without a floating point unit...
*/
void recalculatePitchRoll();
};
#endif