Michel MOULIN
/
as5048spi_moulin
mesure de l'angle sur AS5048 par SPI
as5048spi.h@0:5a1830df0b66, 2019-10-08 (annotated)
- Committer:
- PC2ROBOT
- Date:
- Tue Oct 08 14:59:11 2019 +0000
- Revision:
- 0:5a1830df0b66
as5048
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PC2ROBOT | 0:5a1830df0b66 | 1 | #include "mbed.h" |
PC2ROBOT | 0:5a1830df0b66 | 2 | typedef enum { |
PC2ROBOT | 0:5a1830df0b66 | 3 | AS_FLAG_PARITY = 0x8000, |
PC2ROBOT | 0:5a1830df0b66 | 4 | AS_FLAG_READ = 0x4000, |
PC2ROBOT | 0:5a1830df0b66 | 5 | } As5048Flag; |
PC2ROBOT | 0:5a1830df0b66 | 6 | |
PC2ROBOT | 0:5a1830df0b66 | 7 | typedef enum { |
PC2ROBOT | 0:5a1830df0b66 | 8 | AS_CMD_NOP = 0x0000, |
PC2ROBOT | 0:5a1830df0b66 | 9 | AS_CMD_ERROR = 0x0001 | AS_FLAG_READ, // Reads error register of sensor and clear error flags |
PC2ROBOT | 0:5a1830df0b66 | 10 | AS_CMD_DIAGNOSTICS = 0x3FFD | AS_FLAG_READ, // Reads automatic gain control and diagnostics info |
PC2ROBOT | 0:5a1830df0b66 | 11 | AS_CMD_MAGNITUDE = 0x3FFE | AS_FLAG_READ, |
PC2ROBOT | 0:5a1830df0b66 | 12 | |
PC2ROBOT | 0:5a1830df0b66 | 13 | AS_CMD_ANGLE = 0x3FFF| AS_FLAG_PARITY | AS_FLAG_READ, |
PC2ROBOT | 0:5a1830df0b66 | 14 | } As5048Command; |
PC2ROBOT | 0:5a1830df0b66 | 15 | |
PC2ROBOT | 0:5a1830df0b66 | 16 | // Masks for bits in the result of the AS_CMD_DIAGNOSTICS command |
PC2ROBOT | 0:5a1830df0b66 | 17 | typedef enum { |
PC2ROBOT | 0:5a1830df0b66 | 18 | AS_DIAG_CORDIC_OVERFLOW = 0x0200, |
PC2ROBOT | 0:5a1830df0b66 | 19 | AS_DIAG_HIGH_MAGNETIC = 0x0400, |
PC2ROBOT | 0:5a1830df0b66 | 20 | AS_DIAG_LOW_MAGNETIC = 0x0800, |
PC2ROBOT | 0:5a1830df0b66 | 21 | } As5048Diagnostics; |
PC2ROBOT | 0:5a1830df0b66 | 22 | |
PC2ROBOT | 0:5a1830df0b66 | 23 | |
PC2ROBOT | 0:5a1830df0b66 | 24 | //! Class for interfacing with the AMS AS5048A magnetic rotary sensor over the SPI-interface. |
PC2ROBOT | 0:5a1830df0b66 | 25 | class As5048Spi |
PC2ROBOT | 0:5a1830df0b66 | 26 | { |
PC2ROBOT | 0:5a1830df0b66 | 27 | public: |
PC2ROBOT | 0:5a1830df0b66 | 28 | As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int nDevices = 1); |
PC2ROBOT | 0:5a1830df0b66 | 29 | ~As5048Spi(); |
PC2ROBOT | 0:5a1830df0b66 | 30 | |
PC2ROBOT | 0:5a1830df0b66 | 31 | bool error(int device = -1); |
PC2ROBOT | 0:5a1830df0b66 | 32 | |
PC2ROBOT | 0:5a1830df0b66 | 33 | /// Sets the SPI clock frequency in Hz. Maximum tested frequency is 10MHz. |
PC2ROBOT | 0:5a1830df0b66 | 34 | void frequency(int frequency = 1000000); |
PC2ROBOT | 0:5a1830df0b66 | 35 | |
PC2ROBOT | 0:5a1830df0b66 | 36 | /// Sends a read command to the sensor. |
PC2ROBOT | 0:5a1830df0b66 | 37 | const int* read(As5048Command command); |
PC2ROBOT | 0:5a1830df0b66 | 38 | |
PC2ROBOT | 0:5a1830df0b66 | 39 | /// Sends a read command to the sensor. |
PC2ROBOT | 0:5a1830df0b66 | 40 | /// A call to this function will not directly return the requested value. The |
PC2ROBOT | 0:5a1830df0b66 | 41 | /// requested value will be returned in a next read_sequential call. |
PC2ROBOT | 0:5a1830df0b66 | 42 | /// Use this function to read sensor values with minimum speed impact on SPI-bus |
PC2ROBOT | 0:5a1830df0b66 | 43 | /// and microcontroller. |
PC2ROBOT | 0:5a1830df0b66 | 44 | const int* read_sequential(As5048Command command); |
PC2ROBOT | 0:5a1830df0b66 | 45 | |
PC2ROBOT | 0:5a1830df0b66 | 46 | /// Performs a single angle measurement on all sensors |
PC2ROBOT | 0:5a1830df0b66 | 47 | /// @return Array of raw angle data. To get the 14-bit value representing |
PC2ROBOT | 0:5a1830df0b66 | 48 | /// the angle, apply the mask() to the result. |
PC2ROBOT | 0:5a1830df0b66 | 49 | const int* read_angle(); |
PC2ROBOT | 0:5a1830df0b66 | 50 | |
PC2ROBOT | 0:5a1830df0b66 | 51 | /// Performs sequential angle measurements on all sensors. The first time this |
PC2ROBOT | 0:5a1830df0b66 | 52 | /// method is called the result is not usefull, the measurement data of the call |
PC2ROBOT | 0:5a1830df0b66 | 53 | /// will be returned by the next call to this method. |
PC2ROBOT | 0:5a1830df0b66 | 54 | /// @return Array of raw angle data. To get the 14-bit value representing |
PC2ROBOT | 0:5a1830df0b66 | 55 | /// the angle, apply the mask() to the result. |
PC2ROBOT | 0:5a1830df0b66 | 56 | const int* read_angle_sequential(); |
PC2ROBOT | 0:5a1830df0b66 | 57 | |
PC2ROBOT | 0:5a1830df0b66 | 58 | /// Returns lowest 14-bits |
PC2ROBOT | 0:5a1830df0b66 | 59 | static int mask(int sensor_result); |
PC2ROBOT | 0:5a1830df0b66 | 60 | |
PC2ROBOT | 0:5a1830df0b66 | 61 | /// Applies the mask to the first n bytes in the read buffer (for daisychained sensors). |
PC2ROBOT | 0:5a1830df0b66 | 62 | static void mask(int* sensor_results, int n); |
PC2ROBOT | 0:5a1830df0b66 | 63 | |
PC2ROBOT | 0:5a1830df0b66 | 64 | /// Checks if the return value from the sensor has the right parity |
PC2ROBOT | 0:5a1830df0b66 | 65 | /// @return true if ok |
PC2ROBOT | 0:5a1830df0b66 | 66 | static bool parity_check(int sensor_result); |
PC2ROBOT | 0:5a1830df0b66 | 67 | |
PC2ROBOT | 0:5a1830df0b66 | 68 | /// Returns an angle from 0 to 36000 (degrees times 100). |
PC2ROBOT | 0:5a1830df0b66 | 69 | /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential |
PC2ROBOT | 0:5a1830df0b66 | 70 | static int degrees(int sensor_result); |
PC2ROBOT | 0:5a1830df0b66 | 71 | |
PC2ROBOT | 0:5a1830df0b66 | 72 | /// Returns an angle from 0 to 2*PI*100 |
PC2ROBOT | 0:5a1830df0b66 | 73 | /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential |
PC2ROBOT | 0:5a1830df0b66 | 74 | static int radian(int sensor_result); |
PC2ROBOT | 0:5a1830df0b66 | 75 | |
PC2ROBOT | 0:5a1830df0b66 | 76 | |
PC2ROBOT | 0:5a1830df0b66 | 77 | protected: |
PC2ROBOT | 0:5a1830df0b66 | 78 | int _nDevices; |
PC2ROBOT | 0:5a1830df0b66 | 79 | DigitalOut _chipSelectN; |
PC2ROBOT | 0:5a1830df0b66 | 80 | SPI _spi; |
PC2ROBOT | 0:5a1830df0b66 | 81 | |
PC2ROBOT | 0:5a1830df0b66 | 82 | int* _readBuffer; // Stores the results of the last sequential read |
PC2ROBOT | 0:5a1830df0b66 | 83 | |
PC2ROBOT | 0:5a1830df0b66 | 84 | int* _read(As5048Command command); |
PC2ROBOT | 0:5a1830df0b66 | 85 | }; |