Fix bat voltage: lookup table, currentSensors: Add snap to zero
display.cpp:
Optimize battery voltage measurement
While calibrating noticed it is very non-linear
- outsource function to scale using lookup table
- add lookup table to batvoltage measurement
inserted many values to lookup table while testing
currentsensors:
Fix current value jumping around between 0 and 0.2 on display
while standstill
- add parameter snapToZeroThreshold
This commit is contained in:
@@ -3,6 +3,7 @@ extern "C" {
|
||||
#include "esp_log.h"
|
||||
}
|
||||
|
||||
#include <math.h>
|
||||
#include "currentsensor.hpp"
|
||||
|
||||
//tag for logging
|
||||
@@ -29,11 +30,12 @@ float getVoltage(adc1_channel_t adc, uint32_t samples){
|
||||
//=============================
|
||||
//======== constructor ========
|
||||
//=============================
|
||||
currentSensor::currentSensor (adc1_channel_t adcChannel_f, float ratedCurrent_f, bool isInverted_f){
|
||||
currentSensor::currentSensor (adc1_channel_t adcChannel_f, float ratedCurrent_f, float snapToZeroThreshold_f, bool isInverted_f){
|
||||
//copy config
|
||||
adcChannel = adcChannel_f;
|
||||
ratedCurrent = ratedCurrent_f;
|
||||
isInverted = isInverted_f;
|
||||
snapToZeroThreshold = snapToZeroThreshold_f;
|
||||
//init adc
|
||||
adc1_config_width(ADC_WIDTH_BIT_12); //max resolution 4096
|
||||
adc1_config_channel_atten(adcChannel, ADC_ATTEN_DB_11); //max voltage
|
||||
@@ -59,8 +61,14 @@ float currentSensor::read(void){
|
||||
current = 0;
|
||||
}
|
||||
|
||||
//invert calculated current if necessary
|
||||
if (isInverted) current = -current;
|
||||
if (fabs(current) < snapToZeroThreshold)
|
||||
{
|
||||
ESP_LOGD(TAG, "current=%.3f < threshold=%.3f -> snap to 0", current, snapToZeroThreshold);
|
||||
current = 0;
|
||||
}
|
||||
// invert calculated current if necessary
|
||||
else if (isInverted)
|
||||
current = -current;
|
||||
|
||||
ESP_LOGI(TAG, "read sensor adc=%d: voltage=%.3fV, centerVoltage=%.3fV => current=%.3fA", (int)adcChannel, voltage, centerVoltage, current);
|
||||
return current;
|
||||
|
||||
@@ -7,13 +7,14 @@
|
||||
|
||||
class currentSensor{
|
||||
public:
|
||||
currentSensor (adc1_channel_t adcChannel_f, float ratedCurrent, bool inverted = false);
|
||||
currentSensor (adc1_channel_t adcChannel_f, float ratedCurrent, float snapToZeroThreshold, bool inverted = false);
|
||||
void calibrateZeroAmpere(void); //set current voltage to voltage representing 0A
|
||||
float read(void); //get current ampere
|
||||
private:
|
||||
adc1_channel_t adcChannel;
|
||||
float ratedCurrent;
|
||||
bool isInverted;
|
||||
float snapToZeroThreshold;
|
||||
uint32_t measure;
|
||||
float voltage;
|
||||
float current;
|
||||
|
||||
@@ -29,7 +29,7 @@ void task_motorctl( void * ptrControlledMotor ){
|
||||
//=============================
|
||||
//constructor, simultaniously initialize instance of motor driver 'motor' and current sensor 'cSensor' with provided config (see below lines after ':')
|
||||
controlledMotor::controlledMotor(motorSetCommandFunc_t setCommandFunc, motorctl_config_t config_control, nvs_handle_t * nvsHandle_f):
|
||||
cSensor(config_control.currentSensor_adc, config_control.currentSensor_ratedCurrent, config_control.currentInverted) {
|
||||
cSensor(config_control.currentSensor_adc, config_control.currentSensor_ratedCurrent, config_control.currentSnapToZeroThreshold, config_control.currentInverted) {
|
||||
//copy parameters for controlling the motor
|
||||
config = config_control;
|
||||
//pointer to update motot dury method
|
||||
|
||||
@@ -49,6 +49,7 @@ typedef struct motorctl_config_t {
|
||||
float currentSensor_ratedCurrent;
|
||||
float currentMax;
|
||||
bool currentInverted;
|
||||
float currentSnapToZeroThreshold;
|
||||
uint32_t deadTimeMs; //time motor stays in IDLE before direction change
|
||||
} motorctl_config_t;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user