Skip to content

Commit

Permalink
feat(motor): formatting done & test added
Browse files Browse the repository at this point in the history
- removed unnecessary CS pin configuration
- added SPI bus release on encoder deinitialisation
  • Loading branch information
demianzenkov committed Jul 11, 2024
1 parent d9be659 commit 830a7ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
27 changes: 6 additions & 21 deletions components/motor/esp_simplefoc/port/angle_sensor/as5048a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,37 +17,27 @@ 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) {
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;

/*!< 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,
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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);
Expand Down
20 changes: 9 additions & 11 deletions components/motor/esp_simplefoc/port/angle_sensor/as5048a.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 830a7ba

Please sign in to comment.