Gaëtan Andrieu / AS5048
Committer:
Gaetan_Andrieu
Date:
Fri Jun 14 09:02:15 2019 +0000
Revision:
5:12aba2c743f7
Parent:
4:06b89a41109e
Robot joueur de tennis

Who changed what in which revision?

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