Library for SPI communication with the AMS AS5048 rotary sensor
Dependents: heros_leg_readout_torque_addition heros_leg_readout_torque_addition heros_leg_readout_torque_addition_V3
Diff: As5048.h
- Revision:
- 3:579e12eda4e6
- Parent:
- 2:111641f7e672
- Child:
- 4:56d59ce73270
diff -r 111641f7e672 -r 579e12eda4e6 As5048.h --- a/As5048.h Tue Aug 23 15:01:38 2016 +0000 +++ b/As5048.h Tue Aug 23 15:09:35 2016 +0000 @@ -2,44 +2,6 @@ #define _AS5048_H_ #include "mbed.h" - -const int kNumSensorBits = 14; // 14-bits sensor -const uint16_t kCountsPerRev = 0x4000; // 2**NUM_SENSOR_BITS -const uint16_t kMask = 0x3FFF; // 2**NUM_SENSOR_BITS - 1 -const int kParity = 1; // even parity - -const int kSpiFrequency = 1000000; // AS5048 max 10 MHz -const int kSpiBitsPerTransfer = 8; -const int kSpiMode = 1; - -const float kDegPerRev = 360.0f; // 360 degrees/rev -const float kRadPerRev = 6.28318530718f; // 2*pi rad/rev - -// AS5048 flags -typedef enum { - AS_FLAG_PARITY = 0x8000, - AS_FLAG_READ = 0x4000, -} As5048Flag; - -// AS5048 commands -typedef enum { - AS_CMD_NOP = 0x0000, - AS_CMD_ERROR = 0x0001 | AS_FLAG_READ, // Reads error register of sensor and clear error flags - AS_CMD_DIAGNOSTICS = 0x3FFD | AS_FLAG_READ, // Reads automatic gain control and diagnostics info - AS_CMD_MAGNITUDE = 0x3FFE | AS_FLAG_READ, - AS_CMD_ANGLE = 0x3FFF | AS_FLAG_PARITY | AS_FLAG_READ, -} As5048Command; - -// AS5048 diagnostics -typedef enum { - AS_DIAG_CORDIC_OVERFLOW = 0x0200, - AS_DIAG_HIGH_MAGNETIC = 0x0400, - AS_DIAG_LOW_MAGNETIC = 0x0800, -} As5048Diagnostics; - - - - /** * Interfacing with the AMS AS5048A magnetic rotary sensor using SPI protocol * AS5048 uses 16-bit transfer; @@ -54,8 +16,31 @@ */ class As5048 { + public: + // AS5048 flags + typedef enum { + AS_FLAG_PARITY = 0x8000, + AS_FLAG_READ = 0x4000, + } As5048Flag; + + // AS5048 commands + typedef enum { + AS_CMD_NOP = 0x0000, + AS_CMD_ERROR = 0x0001 | AS_FLAG_READ, // Reads error register of sensor and clear error flags + AS_CMD_DIAGNOSTICS = 0x3FFD | AS_FLAG_READ, // Reads automatic gain control and diagnostics info + AS_CMD_MAGNITUDE = 0x3FFE | AS_FLAG_READ, + AS_CMD_ANGLE = 0x3FFF | AS_FLAG_PARITY | AS_FLAG_READ, + } As5048Command; + + // AS5048 diagnostics + typedef enum { + AS_DIAG_CORDIC_OVERFLOW = 0x0200, + AS_DIAG_HIGH_MAGNETIC = 0x0400, + AS_DIAG_LOW_MAGNETIC = 0x0800, + } As5048Diagnostics; + /** * Creates an object of num_sensors daisy chained AS5048 sensors; * default number of sensors in chain is 1 @@ -312,6 +297,18 @@ protected: + static const int kNumSensorBits = 14; // 14-bits sensor + static const uint16_t kCountsPerRev = 0x4000; // 2**NUM_SENSOR_BITS + static const uint16_t kMask = 0x3FFF; // 2**NUM_SENSOR_BITS - 1 + static const int kParity = 1; // even parity + + static const int kSpiFrequency = 1000000; // AS5048 max 10 MHz + static const int kSpiBitsPerTransfer = 8; + static const int kSpiMode = 1; + + static const float kDegPerRev = 360.0f; // 360 degrees/rev + static const float kRadPerRev = 6.28318530718f; // 2*pi rad/rev + /** * Select (low) chip, and wait 1 us (at least 350 ns) */