-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'contrib/github_pr_382' into 'master'
AS5048A encoder support (GitHub PR) Closes AEGHB-725 See merge request ae_group/esp-iot-solution!1049
- Loading branch information
Showing
7 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: 0.1.1 | ||
version: 0.2.0 | ||
targets: | ||
- esp32 | ||
- esp32s2 | ||
|
123 changes: 123 additions & 0 deletions
123
components/motor/esp_simplefoc/port/angle_sensor/as5048a.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "as5048a.h" | ||
#include "esp_log.h" | ||
#include "esp_check.h" | ||
|
||
#define AS5048A_REG_ANGLE 0x3FFF | ||
#define PI 3.14159265358979f | ||
|
||
static const char *TAG = "AS5048a"; | ||
|
||
AS5048a::AS5048a(spi_host_device_t spi_host, gpio_num_t sclk_io, gpio_num_t miso_io, gpio_num_t mosi_io, gpio_num_t cs_io) | ||
{ | ||
_spi_host = spi_host; | ||
_sclk_io = sclk_io; | ||
_miso_io = miso_io; | ||
_mosi_io = mosi_io; | ||
_cs_io = cs_io; | ||
is_installed = false; | ||
} | ||
|
||
AS5048a::~AS5048a() | ||
{ | ||
if (is_installed) { | ||
deinit(); | ||
} | ||
} | ||
|
||
void AS5048a::deinit() | ||
{ | ||
esp_err_t ret; | ||
ret = spi_bus_remove_device(spi_device); | ||
ESP_RETURN_ON_FALSE(ret == ESP_OK,, TAG, "SPI remove device fail"); | ||
ret = spi_bus_free(_spi_host); | ||
ESP_RETURN_ON_FALSE(ret == ESP_OK,, TAG, "SPI free fail"); | ||
is_installed = false; | ||
} | ||
|
||
void AS5048a::init() | ||
{ | ||
esp_err_t ret; | ||
|
||
/*!< Configuration for the spi bus */ | ||
spi_bus_config_t buscfg = { | ||
.mosi_io_num = _mosi_io, | ||
.miso_io_num = _miso_io, | ||
.sclk_io_num = _sclk_io, | ||
.quadwp_io_num = -1, | ||
.quadhd_io_num = -1, | ||
.max_transfer_sz = 1000, | ||
}; | ||
ret = spi_bus_initialize(_spi_host, &buscfg, SPI_DMA_CH_AUTO); | ||
ESP_RETURN_ON_FALSE(ret == ESP_OK,, TAG, "SPI bus init fail"); | ||
|
||
spi_device_interface_config_t devcfg = { | ||
.command_bits = 0, | ||
.address_bits = 0, | ||
.dummy_bits = 0, | ||
.mode = 1, | ||
.cs_ena_pretrans = 1, | ||
.clock_speed_hz = 4 * 1000 * 1000, | ||
.spics_io_num = _cs_io, | ||
.queue_size = 1, | ||
}; | ||
|
||
ret = spi_bus_add_device(_spi_host, &devcfg, &spi_device); | ||
ESP_RETURN_ON_FALSE(ret == ESP_OK,, TAG, "SPI bus add device fail"); | ||
|
||
is_installed = true; | ||
} | ||
|
||
uint8_t AS5048a::calculateParity(uint16_t value) | ||
{ | ||
uint8_t count = 0; | ||
for (int i = 0; i < 16; i++) { | ||
if (value & 0x1) { | ||
count++; | ||
} | ||
value >>= 1; | ||
} | ||
return count & 0x1; | ||
} | ||
|
||
uint16_t AS5048a::readRegister(uint16_t reg_address) | ||
{ | ||
uint16_t command = 1 << 14; // PAR=0 R/W=R (Read command) | ||
command |= reg_address; // Command to read angle | ||
command |= ((uint16_t)calculateParity(command) << 15); // Adding parity bit | ||
|
||
uint8_t cmd_high = (command >> 8) & 0xFF; | ||
uint8_t cmd_low = command & 0xFF; | ||
uint8_t tx_buffer[2] = {cmd_high, cmd_low}; | ||
spi_transaction_t t = {}; | ||
t.length = 16; // 16 bits | ||
t.tx_buffer = tx_buffer; | ||
t.rx_buffer = NULL; | ||
spi_device_transmit(spi_device, &t); | ||
|
||
uint8_t rx_buffer[2] = {}; | ||
t.length = 16; // 16 bits | ||
t.tx_buffer = NULL; | ||
t.rxlength = 16; // 16 bits | ||
t.rx_buffer = rx_buffer; | ||
spi_device_transmit(spi_device, &t); | ||
|
||
uint16_t reg_value = (rx_buffer[0] << 8) | rx_buffer[1]; | ||
return reg_value & 0x3FFF; // Masking to get 14 bits angle value | ||
} | ||
|
||
uint16_t AS5048a::readRawPosition() | ||
{ | ||
return readRegister(AS5048A_REG_ANGLE); | ||
} | ||
|
||
float AS5048a::getSensorAngle() | ||
{ | ||
uint16_t raw_angle = readRawPosition(); | ||
float angle = (((int)raw_angle & 0b0011111111111111) * 360.0f / 16383.0f) * (PI / 180.0f); | ||
return angle; | ||
} |
61 changes: 61 additions & 0 deletions
61
components/motor/esp_simplefoc/port/angle_sensor/as5048a.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "common/base_classes/Sensor.h" | ||
#include "driver/spi_master.h" | ||
#include "driver/gpio.h" | ||
|
||
class AS5048a : public Sensor { | ||
public: | ||
/** | ||
* @brief Construct a new AS5048a object | ||
* | ||
* @param spi_host | ||
* @param sclk_io | ||
* @param miso_io | ||
* @param mosi_io | ||
* @param cs_io | ||
*/ | ||
AS5048a(spi_host_device_t spi_host, gpio_num_t sclk_io, gpio_num_t miso_io, gpio_num_t mosi_io, gpio_num_t cs_io); | ||
/** | ||
* @brief Destroy the AS5048a object | ||
* | ||
*/ | ||
~AS5048a(); | ||
/** | ||
* @brief Init spi for AS5048a | ||
* | ||
*/ | ||
void init(); | ||
/** | ||
* @brief Deinit spi for AS5048a | ||
* | ||
*/ | ||
void deinit(); | ||
/** | ||
* @brief Get the output of AS5048a | ||
* | ||
* @return float | ||
*/ | ||
float getSensorAngle(); | ||
|
||
private: | ||
uint16_t readRegister(uint16_t reg_address); | ||
uint16_t readRawPosition(); | ||
static uint8_t calculateParity(uint16_t value); | ||
|
||
private: | ||
spi_host_device_t _spi_host; | ||
gpio_num_t _sclk_io; | ||
gpio_num_t _miso_io; | ||
gpio_num_t _mosi_io; | ||
gpio_num_t _cs_io; | ||
spi_device_handle_t spi_device; | ||
bool is_installed; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters