mesure de l'angle sur AS5048 par SPI

Dependencies:   mbed

Committer:
michelmoulin
Date:
Tue Nov 30 13:37:45 2021 +0000
Revision:
1:d92ff88de240
Parent:
0:5a1830df0b66
Lecture de l'angle depuis l'AS5048 en SPI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PC2ROBOT 0:5a1830df0b66 1 #include "as5048spi.h"
PC2ROBOT 0:5a1830df0b66 2
PC2ROBOT 0:5a1830df0b66 3 As5048Spi::As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int ndevices) :
PC2ROBOT 0:5a1830df0b66 4 _nDevices(ndevices),
PC2ROBOT 0:5a1830df0b66 5 _chipSelectN(chipselect),
PC2ROBOT 0:5a1830df0b66 6 _spi(mosi, miso, sclk)
PC2ROBOT 0:5a1830df0b66 7 {
PC2ROBOT 0:5a1830df0b66 8 _chipSelectN.write(1);
PC2ROBOT 0:5a1830df0b66 9 // AS5048 needs 16-bits for is commands
PC2ROBOT 0:5a1830df0b66 10 // Mode = 1:
PC2ROBOT 0:5a1830df0b66 11 // clock polarity = 0 --> clock pulse is high
PC2ROBOT 0:5a1830df0b66 12 // clock phase = 1 --> sample on falling edge of clock pulse
PC2ROBOT 0:5a1830df0b66 13 _spi.format(16, 1);
PC2ROBOT 0:5a1830df0b66 14
PC2ROBOT 0:5a1830df0b66 15 // Set clock frequency to 1 MHz (max is 10Mhz)
PC2ROBOT 0:5a1830df0b66 16 _spi.frequency(1000000);
PC2ROBOT 0:5a1830df0b66 17
PC2ROBOT 0:5a1830df0b66 18 _readBuffer = new int[ndevices];
PC2ROBOT 0:5a1830df0b66 19 }
PC2ROBOT 0:5a1830df0b66 20
PC2ROBOT 0:5a1830df0b66 21 As5048Spi::~As5048Spi()
PC2ROBOT 0:5a1830df0b66 22 {
PC2ROBOT 0:5a1830df0b66 23 delete [] _readBuffer;
PC2ROBOT 0:5a1830df0b66 24 }
PC2ROBOT 0:5a1830df0b66 25
PC2ROBOT 0:5a1830df0b66 26 int As5048Spi::degrees(int sensor_result)
PC2ROBOT 0:5a1830df0b66 27 {
PC2ROBOT 0:5a1830df0b66 28 return mask(sensor_result) * 36000 / 0x4000;
PC2ROBOT 0:5a1830df0b66 29 }
PC2ROBOT 0:5a1830df0b66 30
PC2ROBOT 0:5a1830df0b66 31
PC2ROBOT 0:5a1830df0b66 32 int As5048Spi::radian(int sensor_result)
PC2ROBOT 0:5a1830df0b66 33 {
PC2ROBOT 0:5a1830df0b66 34 return mask(sensor_result) * 62832 / 0x4000;
PC2ROBOT 0:5a1830df0b66 35 }
PC2ROBOT 0:5a1830df0b66 36
PC2ROBOT 0:5a1830df0b66 37 bool As5048Spi::error(int device)
PC2ROBOT 0:5a1830df0b66 38 {
PC2ROBOT 0:5a1830df0b66 39 if( device == -1 ) {
PC2ROBOT 0:5a1830df0b66 40 for(int i = 0; i < _nDevices; ++i) {
PC2ROBOT 0:5a1830df0b66 41 if( _readBuffer[i] & 0x4000 ) {
PC2ROBOT 0:5a1830df0b66 42 return true;
PC2ROBOT 0:5a1830df0b66 43 }
PC2ROBOT 0:5a1830df0b66 44 }
PC2ROBOT 0:5a1830df0b66 45 } else if( device < _nDevices ) {
PC2ROBOT 0:5a1830df0b66 46 return (_readBuffer[device] & 0x4000) == 0x4000;
PC2ROBOT 0:5a1830df0b66 47 }
PC2ROBOT 0:5a1830df0b66 48 return false;
PC2ROBOT 0:5a1830df0b66 49 }
PC2ROBOT 0:5a1830df0b66 50
PC2ROBOT 0:5a1830df0b66 51
PC2ROBOT 0:5a1830df0b66 52 void As5048Spi::frequency(int hz)
PC2ROBOT 0:5a1830df0b66 53 {
PC2ROBOT 0:5a1830df0b66 54 _spi.frequency(hz);
PC2ROBOT 0:5a1830df0b66 55 }
PC2ROBOT 0:5a1830df0b66 56
PC2ROBOT 0:5a1830df0b66 57 int As5048Spi::mask(int sensor_result)
PC2ROBOT 0:5a1830df0b66 58 {
PC2ROBOT 0:5a1830df0b66 59 return sensor_result & 0x3FFF; // return lowest 14-bits
PC2ROBOT 0:5a1830df0b66 60 }
PC2ROBOT 0:5a1830df0b66 61
PC2ROBOT 0:5a1830df0b66 62
PC2ROBOT 0:5a1830df0b66 63 void As5048Spi::mask(int* sensor_results, int n)
PC2ROBOT 0:5a1830df0b66 64 {
PC2ROBOT 0:5a1830df0b66 65 for(int i = 0; i < n; ++i) {
PC2ROBOT 0:5a1830df0b66 66 sensor_results[i] &= 0x3FFF;
PC2ROBOT 0:5a1830df0b66 67 }
PC2ROBOT 0:5a1830df0b66 68 }
PC2ROBOT 0:5a1830df0b66 69
PC2ROBOT 0:5a1830df0b66 70
PC2ROBOT 0:5a1830df0b66 71 bool As5048Spi::parity_check(int sensor_result)
PC2ROBOT 0:5a1830df0b66 72 {
PC2ROBOT 0:5a1830df0b66 73 // Use the LSb of result to keep track of parity (0 = even, 1 = odd)
PC2ROBOT 0:5a1830df0b66 74 int result = sensor_result;
PC2ROBOT 0:5a1830df0b66 75
PC2ROBOT 0:5a1830df0b66 76 for(int i = 1; i <= 15; ++i) {
PC2ROBOT 0:5a1830df0b66 77 sensor_result >>= 1;
PC2ROBOT 0:5a1830df0b66 78 result ^= sensor_result;
PC2ROBOT 0:5a1830df0b66 79 }
PC2ROBOT 0:5a1830df0b66 80 // Parity should be even
PC2ROBOT 0:5a1830df0b66 81 return (result & 0x0001) == 0;
PC2ROBOT 0:5a1830df0b66 82 }
PC2ROBOT 0:5a1830df0b66 83
PC2ROBOT 0:5a1830df0b66 84 const int* As5048Spi::read(As5048Command command)
PC2ROBOT 0:5a1830df0b66 85 {
PC2ROBOT 0:5a1830df0b66 86 _read(command); // Send command to device(s)
PC2ROBOT 0:5a1830df0b66 87 return _read(AS_CMD_NOP); // Read-out device(s)
PC2ROBOT 0:5a1830df0b66 88 }
PC2ROBOT 0:5a1830df0b66 89
PC2ROBOT 0:5a1830df0b66 90 const int* As5048Spi::read_sequential(As5048Command command)
PC2ROBOT 0:5a1830df0b66 91 {
PC2ROBOT 0:5a1830df0b66 92 return _read(command);
PC2ROBOT 0:5a1830df0b66 93 }
PC2ROBOT 0:5a1830df0b66 94
PC2ROBOT 0:5a1830df0b66 95 const int* As5048Spi::read_angle()
PC2ROBOT 0:5a1830df0b66 96 {
PC2ROBOT 0:5a1830df0b66 97 _read(AS_CMD_ANGLE); // Send command to device(s)
PC2ROBOT 0:5a1830df0b66 98 return _read(AS_CMD_NOP); // Read-out device(s)
PC2ROBOT 0:5a1830df0b66 99 }
PC2ROBOT 0:5a1830df0b66 100
PC2ROBOT 0:5a1830df0b66 101 const int* As5048Spi::read_angle_sequential()
PC2ROBOT 0:5a1830df0b66 102 {
PC2ROBOT 0:5a1830df0b66 103 return _read(AS_CMD_ANGLE);
PC2ROBOT 0:5a1830df0b66 104 }
PC2ROBOT 0:5a1830df0b66 105
PC2ROBOT 0:5a1830df0b66 106
PC2ROBOT 0:5a1830df0b66 107 int* As5048Spi::_read(As5048Command command)
PC2ROBOT 0:5a1830df0b66 108 {
PC2ROBOT 0:5a1830df0b66 109 if(_nDevices == 1)
PC2ROBOT 0:5a1830df0b66 110 {
PC2ROBOT 0:5a1830df0b66 111 // Give command to start reading the angle
PC2ROBOT 0:5a1830df0b66 112 _chipSelectN.write(0);
PC2ROBOT 0:5a1830df0b66 113 wait_us(1); // Wait at least 350ns after chip select
PC2ROBOT 0:5a1830df0b66 114 _readBuffer[0] = _spi.write(command);
PC2ROBOT 0:5a1830df0b66 115 _chipSelectN.write(1);
PC2ROBOT 0:5a1830df0b66 116 wait_us(1); // Wait at least 350ns after chip select
PC2ROBOT 0:5a1830df0b66 117 } else
PC2ROBOT 0:5a1830df0b66 118 {
PC2ROBOT 0:5a1830df0b66 119 // Enable the sensor on the chain
PC2ROBOT 0:5a1830df0b66 120 _chipSelectN.write(0);
PC2ROBOT 0:5a1830df0b66 121 wait_us(1); // Wait at least 350ns after chip select
PC2ROBOT 0:5a1830df0b66 122 for(int i = 0; i < _nDevices; ++i)
PC2ROBOT 0:5a1830df0b66 123 {
PC2ROBOT 0:5a1830df0b66 124 _readBuffer[i] = _spi.write(command);
PC2ROBOT 0:5a1830df0b66 125 }
PC2ROBOT 0:5a1830df0b66 126 _chipSelectN.write(1);
PC2ROBOT 0:5a1830df0b66 127 wait_us(1); // Wait at least 350ns after chip select
PC2ROBOT 0:5a1830df0b66 128 }
PC2ROBOT 0:5a1830df0b66 129 return _readBuffer;
PC2ROBOT 0:5a1830df0b66 130 }
PC2ROBOT 0:5a1830df0b66 131
PC2ROBOT 0:5a1830df0b66 132