From 830a7baed1aefa3fb0e8b79255eec9875ee99ecf Mon Sep 17 00:00:00 2001 From: Demian Zenkov Date: Thu, 11 Jul 2024 10:46:40 +0300 Subject: [PATCH] feat(motor): formatting done & test added - removed unnecessary CS pin configuration - added SPI bus release on encoder deinitialisation --- .../port/angle_sensor/as5048a.cpp | 27 +++++-------------- .../esp_simplefoc/port/angle_sensor/as5048a.h | 20 +++++++------- .../test_apps/main/test_esp_simplefoc.cpp | 15 +++++++++++ 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/components/motor/esp_simplefoc/port/angle_sensor/as5048a.cpp b/components/motor/esp_simplefoc/port/angle_sensor/as5048a.cpp index 6f7f3e851..81b32d209 100644 --- a/components/motor/esp_simplefoc/port/angle_sensor/as5048a.cpp +++ b/components/motor/esp_simplefoc/port/angle_sensor/as5048a.cpp @@ -2,13 +2,11 @@ #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; @@ -19,7 +17,6 @@ AS5048a::AS5048a(spi_host_device_t spi_host, gpio_num_t sclk_io, gpio_num_t miso is_installed = false; } - AS5048a::~AS5048a() { if (is_installed) { @@ -27,29 +24,20 @@ AS5048a::~AS5048a() } } - 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; - /*!< Set GPIO mode for chip select pin */ - gpio_config_t io_conf = {//gpio_types.h - .pin_bit_mask = (1ULL << _cs_io), - .mode = GPIO_MODE_OUTPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_DISABLE, - }; - gpio_config(&io_conf); - /*!< Configuration for the spi bus */ spi_bus_config_t buscfg = { .mosi_io_num = _mosi_io, @@ -61,7 +49,7 @@ void AS5048a::init() }; 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, @@ -72,14 +60,13 @@ void AS5048a::init() .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; @@ -92,7 +79,6 @@ uint8_t AS5048a::calculateParity(uint16_t value) return count & 0x1; } - uint16_t AS5048a::readRegister(uint16_t reg_address) { uint16_t command = 1 << 14; // PAR=0 R/W=R (Read command) @@ -114,12 +100,11 @@ uint16_t AS5048a::readRegister(uint16_t reg_address) 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); diff --git a/components/motor/esp_simplefoc/port/angle_sensor/as5048a.h b/components/motor/esp_simplefoc/port/angle_sensor/as5048a.h index e560ca6e3..2498f8d1e 100644 --- a/components/motor/esp_simplefoc/port/angle_sensor/as5048a.h +++ b/components/motor/esp_simplefoc/port/angle_sensor/as5048a.h @@ -6,23 +6,21 @@ #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 - */ + /** + * @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 diff --git a/components/motor/esp_simplefoc/test_apps/main/test_esp_simplefoc.cpp b/components/motor/esp_simplefoc/test_apps/main/test_esp_simplefoc.cpp index 72d7e29ac..7d5a7198f 100644 --- a/components/motor/esp_simplefoc/test_apps/main/test_esp_simplefoc.cpp +++ b/components/motor/esp_simplefoc/test_apps/main/test_esp_simplefoc.cpp @@ -42,6 +42,21 @@ TEST_CASE("test mt6701", "[sensor][mt6701]") mt6701.deinit(); } +TEST_CASE("test as5048a", "[sensor][as5048a]") +{ +#if CONFIG_IDF_TARGET_ESP32C3 + AS5048a as5048a = AS5048a(SPI2_HOST, GPIO_NUM_2, GPIO_NUM_1, (gpio_num_t) -1, GPIO_NUM_3); +#else + AS5048a as5048a = AS5048a(SPI2_HOST, GPIO_NUM_2, GPIO_NUM_1, (gpio_num_t) -1, GPIO_NUM_42); +#endif + as5048a.init(); + for (int i = 0; i < 10; ++i) { + ESP_LOGI(TAG, "angle:%.2f", as5048a.getSensorAngle()); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + as5048a.deinit(); +} + TEST_CASE("test esp_simplefoc openloop control", "[single motor][openloop][14pp][ledc][drv8313][c3]") { BLDCMotor motor = BLDCMotor(14);