Skip to content

Commit

Permalink
Merge pull request #4 from Candas1/dev
Browse files Browse the repository at this point in the history
Merge new dev changes
  • Loading branch information
Candas1 authored Oct 31, 2023
2 parents bfa492c + 72e7458 commit 70ab24c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ jobs:
sketch-names: single_full_control_example.ino

- arduino-boards-fqbn: esp32:esp32:esp32s2 # esp32s2
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
sketch-names: bldc_driver_3pwm_standalone.ino, stepper_driver_2pwm_standalone.ino, stepper_driver_4pwm_standalone
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
sketch-names: bldc_driver_3pwm_standalone.ino,stepper_driver_2pwm_standalone.ino,stepper_driver_4pwm_standalone.ino

- arduino-boards-fqbn: esp32:esp32:esp32s3 # esp32s3
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
sketch-names: esp32_position_control.ino, esp32_i2c_dual_bus_example.ino

- arduino-boards-fqbn: esp32:esp32:esp32doit-devkit-v1 # esp32
platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
sketch-names: esp32_position_control.ino, esp32_i2c_dual_bus_example.ino, esp32_current_control_low_side.ino, esp32_spi_alt_example.ino

- arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 # bluepill - hs examples
Expand Down
43 changes: 33 additions & 10 deletions src/common/foc_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ __attribute__((weak)) void _sincos(float a, float* s, float* c){
*c = _cos(a);
}

// fast_atan2 based on https://math.stackexchange.com/a/1105038/81278
// Via Odrive project
// https://github.com/odriverobotics/ODrive/blob/master/Firmware/MotorControl/utils.cpp
// This function is MIT licenced, copyright Oskar Weigl/Odrive Robotics
// The origin for Odrive atan2 is public domain. Thanks to Odrive for making
// it easy to borrow.
__attribute__((weak)) float _atan2(float y, float x) {
// a := min (|x|, |y|) / max (|x|, |y|)
float abs_y = fabsf(y);
float abs_x = fabsf(x);
// inject FLT_MIN in denominator to avoid division by zero
float a = min(abs_x, abs_y) / (max(abs_x, abs_y));
// s := a * a
float s = a * a;
// r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
float r =
((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
// if |y| > |x| then r := 1.57079637 - r
if (abs_y > abs_x) r = 1.57079637f - r;
// if x < 0 then r := 3.14159274 - r
if (x < 0.0f) r = 3.14159274f - r;
// if y < 0 then r := -r
if (y < 0.0f) r = -r;

return r;
}


// normalizing radian angle to [0,2PI]
__attribute__((weak)) float _normalizeAngle(float angle){
Expand All @@ -60,14 +87,10 @@ float _electricalAngle(float shaft_angle, int pole_pairs) {
// https://reprap.org/forum/read.php?147,219210
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
__attribute__((weak)) float _sqrtApprox(float number) {//low in fat
// float x;
// const float f = 1.5F; // better precision

// x = number * 0.5F;
float y = number;
long i = * ( long * ) &y;
i = 0x5f375a86 - ( i >> 1 );
y = * ( float * ) &i;
// y = y * ( f - ( x * y * y ) ); // better precision
return number * y;
union {
float f;
uint32_t i;
} y = { .f = number };
y.i = 0x5f375a86 - ( y.i >> 1 );
return number * y.f;
}
5 changes: 5 additions & 0 deletions src/common/foc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ float _cos(float a);
*/
void _sincos(float a, float* s, float* c);

/**
* Function approximating atan2
*
*/
float _atan2(float y, float x);

/**
* normalizing radian angle to [0,2PI]
Expand Down
7 changes: 1 addition & 6 deletions src/communication/StepDirListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
#include "../common/foc_utils.h"


#if !defined(TARGET_RP2040) && !defined(_SAMD21_) && !defined(_SAMD51_) && !defined(_SAME51_) && !defined(ARDUINO_UNOR4_WIFI) && !defined(ARDUINO_UNOR4_MINIMA) && !defined(NRF52_SERIES) && !defined(ARDUINO_ARCH_MEGAAVR)
#define PinStatus int
#endif


/**
* Step/Dir listenner class for easier interraction with this communication interface.
*/
Expand Down Expand Up @@ -53,7 +48,7 @@ class StepDirListener
int pin_step; //!< step pin
int pin_dir; //!< direction pin
long count; //!< current counter value - should be set to 0 for homing
PinStatus polarity = RISING; //!< polarity of the step pin
decltype(RISING) polarity = RISING; //!< polarity of the step pin

private:
float* attached_variable = nullptr; //!< pointer to the attached variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "freertos/task.h"
#include "rom/ets_sys.h"
#include "esp_attr.h"
#include "esp_intr.h"
//#include "esp_intr.h" deprecated
#include "esp_intr_alloc.h"
#include "soc/rtc_io_reg.h"
#include "soc/rtc_cntl_reg.h"
#include "soc/sens_reg.h"
Expand Down
3 changes: 3 additions & 0 deletions src/current_sense/hardware_specific/esp32/esp32_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void _driverSyncLowSide(void* driver_params, void* cs_params){
mcpwm_isr_register(mcpwm_unit, mcpwm1_isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL); //Set ISR Handler
}

static void IRAM_ATTR mcpwm0_isr_handler(void*) __attribute__ ((unused));

// Read currents when interrupt is triggered
static void IRAM_ATTR mcpwm0_isr_handler(void*){
// // high side
Expand All @@ -139,6 +141,7 @@ static void IRAM_ATTR mcpwm0_isr_handler(void*){
// MCPWM0.int_clr.timer0_tez_int_clr = mcpwm_intr_status_0;
}

static void IRAM_ATTR mcpwm1_isr_handler(void*) __attribute__ ((unused));

// Read currents when interrupt is triggered
static void IRAM_ATTR mcpwm1_isr_handler(void*){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
MX_ADC1_Init(&hadc1);
MX_ADC2_Init(&hadc2);

HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);

MX_DMA1_Init(&hadc1, &hdma_adc1, DMA1_Channel1, DMA_REQUEST_ADC1);
MX_DMA1_Init(&hadc2, &hdma_adc2, DMA1_Channel2, DMA_REQUEST_ADC2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
}
// set the trigger output event
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);

// Start the adc calibration
HAL_ADCEx_Calibration_Start(cs_params->adc_handle);

// start the adc
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);

Expand Down
1 change: 0 additions & 1 deletion src/sensors/HallSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ void HallSensor::updateState() {
hall_state = new_hall_state;

int8_t new_electric_sector = ELECTRIC_SECTORS[hall_state];
static Direction old_direction;
if (new_electric_sector - electric_sector > 3) {
//underflow
direction = Direction::CCW;
Expand Down
1 change: 1 addition & 0 deletions src/sensors/HallSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class HallSensor: public Sensor{

// whether last step was CW (+1) or CCW (-1).
Direction direction;
Direction old_direction;

void attachSectorCallback(void (*onSectorChange)(int a) = nullptr);

Expand Down

0 comments on commit 70ab24c

Please sign in to comment.