Skip to content

Commit 644389b

Browse files
committed
FEAT fix of esp32 driver + added support for per phase lpf for current sensing
1 parent ae31910 commit 644389b

File tree

13 files changed

+87
-59
lines changed

13 files changed

+87
-59
lines changed

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ LPF_current_q KEYWORD2
4949
LPF_current_d KEYWORD2
5050
P_angle KEYWORD2
5151
LPF_angle KEYWORD2
52+
lpf_a KEYWORD2
53+
lpf_b KEYWORD2
54+
lpf_c KEYWORD2
5255

5356
MotionControlType KEYWORD1
5457
TorqueControlType KEYWORD1

src/common/defaults.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@
4343
// align voltage
4444
#define DEF_VOLTAGE_SENSOR_ALIGN 3.0 //!< default voltage for sensor and motor zero alignemt
4545
// low pass filter velocity
46-
#define DEF_VEL_FILTER_Tf 0.005 //!< default velocity filter time constant
46+
#define DEF_VEL_FILTER_Tf 0.005 //!< default velocity filter time constant
47+
48+
// current sense default parameters
49+
#define DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf 0.0f //!< default currnet sense per phase low pass filter time constant

src/current_sense/InlineCurrentSense.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ void InlineCurrentSense::calibrateOffsets(){
5050
// read all three phase currents (if possible 2 or 3)
5151
PhaseCurrent_s InlineCurrentSense::getPhaseCurrents(){
5252
PhaseCurrent_s current;
53-
current.a = (_readADCVoltageInline(pinA) - offset_ia)*gain_a;// amps
54-
current.b = (_readADCVoltageInline(pinB) - offset_ib)*gain_b;// amps
55-
current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageInline(pinC) - offset_ic)*gain_c; // amps
53+
current.a = lpf_a(_readADCVoltageInline(pinA) - offset_ia)*gain_a;// amps
54+
current.b = lpf_b(_readADCVoltageInline(pinB) - offset_ib)*gain_b;// amps
55+
current.c = (!_isset(pinC)) ? 0 : lpf_c(_readADCVoltageInline(pinC) - offset_ic)*gain_c; // amps
5656
return current;
5757
}
5858
// Function synchronizing current sense with motor driver.

src/current_sense/InlineCurrentSense.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include "Arduino.h"
55
#include "../common/foc_utils.h"
66
#include "../common/time_utils.h"
7+
#include "../common/defaults.h"
78
#include "../common/base_classes/CurrentSense.h"
9+
#include "../common/lowpass_filter.h"
810
#include "hardware_api.h"
911

1012

@@ -33,8 +35,13 @@ class InlineCurrentSense: public CurrentSense{
3335
float gain_b; //!< phase B gain
3436
float gain_c; //!< phase C gain
3537

36-
private:
38+
// per phase low pass fileters
39+
LowPassFilter lpf_a{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current A low pass filter
40+
LowPassFilter lpf_b{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current B low pass filter
41+
LowPassFilter lpf_c{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current C low pass filter
3742

43+
private:
44+
3845
// hardware variables
3946
int pinA; //!< pin A analog pin for current measurement
4047
int pinB; //!< pin B analog pin for current measurement

src/current_sense/LowsideCurrentSense.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ void LowsideCurrentSense::calibrateOffsets(){
5252
PhaseCurrent_s LowsideCurrentSense::getPhaseCurrents(){
5353
PhaseCurrent_s current;
5454
_startADC3PinConversionLowSide();
55-
current.a = (_readADCVoltageLowSide(pinA) - offset_ia)*gain_a;// amps
56-
current.b = (_readADCVoltageLowSide(pinB) - offset_ib)*gain_b;// amps
57-
current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageLowSide(pinC) - offset_ic)*gain_c; // amps
55+
current.a = lpf_a(_readADCVoltageLowSide(pinA) - offset_ia)*gain_a;// amps
56+
current.b = lpf_b(_readADCVoltageLowSide(pinB) - offset_ib)*gain_b;// amps
57+
current.c = (!_isset(pinC)) ? 0 : lpf_b(_readADCVoltageLowSide(pinC) - offset_ic)*gain_c; // amps
5858
return current;
5959
}
6060
// Function synchronizing current sense with motor driver.

src/current_sense/LowsideCurrentSense.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include "Arduino.h"
55
#include "../common/foc_utils.h"
66
#include "../common/time_utils.h"
7+
#include "../common/defaults.h"
78
#include "../common/base_classes/CurrentSense.h"
89
#include "../common/base_classes/FOCMotor.h"
10+
#include "../common/lowpass_filter.h"
911
#include "hardware_api.h"
1012

1113

@@ -34,6 +36,11 @@ class LowsideCurrentSense: public CurrentSense{
3436
float gain_b; //!< phase B gain
3537
float gain_c; //!< phase C gain
3638

39+
// per phase low pass fileters
40+
LowPassFilter lpf_a{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current A low pass filter
41+
LowPassFilter lpf_b{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current B low pass filter
42+
LowPassFilter lpf_c{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current C low pass filter
43+
3744
private:
3845

3946
// hardware variables

src/current_sense/hardware_api.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ void _configureADCInline(const int pinA,const int pinB,const int pinC = NOT_SET)
2828
* @param pinC - adc pin C
2929
*/
3030
void _configureADCLowSide(const int pinA,const int pinB,const int pinC = NOT_SET);
31-
32-
void _startADC3PinConversionLowSide();
33-
3431
/**
3532
* function reading an ADC value and returning the read voltage
3633
*
@@ -42,4 +39,8 @@ float _readADCVoltageLowSide(const int pinA);
4239
* function syncing the Driver with the ADC for the LowSide Sensing
4340
*/
4441
void _driverSyncLowSide();
42+
43+
44+
void _startADC3PinConversionLowSide();
45+
4546
#endif

src/current_sense/hardware_specific/esp32_adc_driver.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
1-
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
15-
#if defined(ESP_H)
16-
171
#include "esp32_adc_driver.h"
18-
#include "Arduino.h"
2+
3+
#ifdef ESP_H
194

205
#include "freertos/FreeRTOS.h"
216
#include "freertos/task.h"

src/current_sense/hardware_specific/esp32_adc_driver.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
/*
2-
Arduino.h - Main include file for the Arduino SDK
3-
Copyright (c) 2005-2013 Arduino Team. All right reserved.
4-
5-
This library is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU Lesser General Public
7-
License as published by the Free Software Foundation; either
8-
version 2.1 of the License, or (at your option) any later version.
9-
10-
This library is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13-
Lesser General Public License for more details.
14-
15-
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18-
*/
191

20-
#if !defined(MAIN_ESP32_HAL_ADC_DRIVER_H_) && defined(ESP_H)
21-
#define MAIN_ESP32_HAL_ADC_DRIVER_H_
222

23-
#include "esp32-hal.h"
3+
#ifndef SIMPLEFOC_ESP32_HAL_ADC_DRIVER_H_
4+
#define SIMPLEFOC_ESP32_HAL_ADC_DRIVER_H_
245

6+
#include "Arduino.h"
257

8+
#ifdef ESP_H
269
/*
2710
* Get ADC value for pin
2811
* */
@@ -101,4 +84,5 @@ bool __adcBusy(uint8_t pin);
10184
* */
10285
uint16_t __adcEnd(uint8_t pin);
10386

104-
#endif /* MAIN_ESP32_HAL_ADC_H_ */
87+
#endif /* SIMPLEFOC_ESP32_HAL_ADC_DRIVER_H_ */
88+
#endif /* ESP32 */

src/current_sense/hardware_specific/esp32_mcu.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "../hardware_api.h"
22

3-
#if defined(ESP_H)
3+
#ifdef ESP_H
44

55
#include "driver/mcpwm.h"
66
#include "soc/mcpwm_reg.h"
@@ -11,8 +11,8 @@
1111

1212
#include "esp32_adc_driver.h"
1313

14-
#define _ADC_VOLTAGE 3.3
15-
#define _ADC_RESOLUTION 4095.0
14+
#define _ADC_VOLTAGE 3.3f
15+
#define _ADC_RESOLUTION 4095.0f
1616

1717
static mcpwm_dev_t *MCPWM[2] = {&MCPWM0, &MCPWM1};
1818
int a1, a2, a3; //Current readings from internal current sensor amplifiers
@@ -98,8 +98,9 @@ float _readADCVoltageInline(const int pinA){
9898
// uint32_t raw_adc = analogRead(pinA);
9999
return raw_adc * _ADC_CONV;
100100
}
101+
101102
// function reading an ADC value and returning the read voltage
102-
void _configureADCInline(const int pinA,const int pinB,const int pinC){
103+
void _configureADCInline(const int pinA,const int pinB, const int pinC){
103104
pinMode(pinA, INPUT);
104105
pinMode(pinB, INPUT);
105106
if( _isset(pinC) ) pinMode(pinC, INPUT);

0 commit comments

Comments
 (0)