Skip to content

Commit 70ab24c

Browse files
authored
Merge pull request #4 from Candas1/dev
Merge new dev changes
2 parents bfa492c + 72e7458 commit 70ab24c

File tree

10 files changed

+56
-22
lines changed

10 files changed

+56
-22
lines changed

.github/workflows/ccpp.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ jobs:
5858
sketch-names: single_full_control_example.ino
5959

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

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

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

7272
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 # bluepill - hs examples

src/common/foc_utils.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ __attribute__((weak)) void _sincos(float a, float* s, float* c){
4444
*c = _cos(a);
4545
}
4646

47+
// fast_atan2 based on https://math.stackexchange.com/a/1105038/81278
48+
// Via Odrive project
49+
// https://github.com/odriverobotics/ODrive/blob/master/Firmware/MotorControl/utils.cpp
50+
// This function is MIT licenced, copyright Oskar Weigl/Odrive Robotics
51+
// The origin for Odrive atan2 is public domain. Thanks to Odrive for making
52+
// it easy to borrow.
53+
__attribute__((weak)) float _atan2(float y, float x) {
54+
// a := min (|x|, |y|) / max (|x|, |y|)
55+
float abs_y = fabsf(y);
56+
float abs_x = fabsf(x);
57+
// inject FLT_MIN in denominator to avoid division by zero
58+
float a = min(abs_x, abs_y) / (max(abs_x, abs_y));
59+
// s := a * a
60+
float s = a * a;
61+
// r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
62+
float r =
63+
((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
64+
// if |y| > |x| then r := 1.57079637 - r
65+
if (abs_y > abs_x) r = 1.57079637f - r;
66+
// if x < 0 then r := 3.14159274 - r
67+
if (x < 0.0f) r = 3.14159274f - r;
68+
// if y < 0 then r := -r
69+
if (y < 0.0f) r = -r;
70+
71+
return r;
72+
}
73+
4774

4875
// normalizing radian angle to [0,2PI]
4976
__attribute__((weak)) float _normalizeAngle(float angle){
@@ -60,14 +87,10 @@ float _electricalAngle(float shaft_angle, int pole_pairs) {
6087
// https://reprap.org/forum/read.php?147,219210
6188
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
6289
__attribute__((weak)) float _sqrtApprox(float number) {//low in fat
63-
// float x;
64-
// const float f = 1.5F; // better precision
65-
66-
// x = number * 0.5F;
67-
float y = number;
68-
long i = * ( long * ) &y;
69-
i = 0x5f375a86 - ( i >> 1 );
70-
y = * ( float * ) &i;
71-
// y = y * ( f - ( x * y * y ) ); // better precision
72-
return number * y;
90+
union {
91+
float f;
92+
uint32_t i;
93+
} y = { .f = number };
94+
y.i = 0x5f375a86 - ( y.i >> 1 );
95+
return number * y.f;
7396
}

src/common/foc_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ float _cos(float a);
7979
*/
8080
void _sincos(float a, float* s, float* c);
8181

82+
/**
83+
* Function approximating atan2
84+
*
85+
*/
86+
float _atan2(float y, float x);
8287

8388
/**
8489
* normalizing radian angle to [0,2PI]

src/communication/StepDirListener.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
#include "../common/foc_utils.h"
66

77

8-
#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)
9-
#define PinStatus int
10-
#endif
11-
12-
138
/**
149
* Step/Dir listenner class for easier interraction with this communication interface.
1510
*/
@@ -53,7 +48,7 @@ class StepDirListener
5348
int pin_step; //!< step pin
5449
int pin_dir; //!< direction pin
5550
long count; //!< current counter value - should be set to 0 for homing
56-
PinStatus polarity = RISING; //!< polarity of the step pin
51+
decltype(RISING) polarity = RISING; //!< polarity of the step pin
5752

5853
private:
5954
float* attached_variable = nullptr; //!< pointer to the attached variable

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "freertos/task.h"
77
#include "rom/ets_sys.h"
88
#include "esp_attr.h"
9-
#include "esp_intr.h"
9+
//#include "esp_intr.h" deprecated
10+
#include "esp_intr_alloc.h"
1011
#include "soc/rtc_io_reg.h"
1112
#include "soc/rtc_cntl_reg.h"
1213
#include "soc/sens_reg.h"

src/current_sense/hardware_specific/esp32/esp32_mcu.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ void _driverSyncLowSide(void* driver_params, void* cs_params){
121121
mcpwm_isr_register(mcpwm_unit, mcpwm1_isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL); //Set ISR Handler
122122
}
123123

124+
static void IRAM_ATTR mcpwm0_isr_handler(void*) __attribute__ ((unused));
125+
124126
// Read currents when interrupt is triggered
125127
static void IRAM_ATTR mcpwm0_isr_handler(void*){
126128
// // high side
@@ -139,6 +141,7 @@ static void IRAM_ATTR mcpwm0_isr_handler(void*){
139141
// MCPWM0.int_clr.timer0_tez_int_clr = mcpwm_intr_status_0;
140142
}
141143

144+
static void IRAM_ATTR mcpwm1_isr_handler(void*) __attribute__ ((unused));
142145

143146
// Read currents when interrupt is triggered
144147
static void IRAM_ATTR mcpwm1_isr_handler(void*){

src/current_sense/hardware_specific/stm32/b_g431/b_g431_mcu.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
110110
MX_ADC1_Init(&hadc1);
111111
MX_ADC2_Init(&hadc2);
112112

113+
HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
114+
HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);
115+
113116
MX_DMA1_Init(&hadc1, &hdma_adc1, DMA1_Channel1, DMA_REQUEST_ADC1);
114117
MX_DMA1_Init(&hadc2, &hdma_adc2, DMA1_Channel2, DMA_REQUEST_ADC2);
115118

src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_mcu.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
6666
}
6767
// set the trigger output event
6868
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
69+
70+
// Start the adc calibration
71+
HAL_ADCEx_Calibration_Start(cs_params->adc_handle);
72+
6973
// start the adc
7074
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
7175

src/sensors/HallSensor.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void HallSensor::updateState() {
5353
hall_state = new_hall_state;
5454

5555
int8_t new_electric_sector = ELECTRIC_SECTORS[hall_state];
56-
static Direction old_direction;
5756
if (new_electric_sector - electric_sector > 3) {
5857
//underflow
5958
direction = Direction::CCW;

src/sensors/HallSensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class HallSensor: public Sensor{
6262

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

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

0 commit comments

Comments
 (0)