Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3074 and some other minor issues, improves battery level #3163

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
21886cf
heltec tracker Screen timeout bug
Gabrielerusso Feb 1, 2024
c206aec
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 1, 2024
d0db7fb
Update protobufs
Gabrielerusso Feb 1, 2024
39878cf
fixed screen offsets and i2c pins
Gabrielerusso Feb 2, 2024
95ba7b0
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 2, 2024
1e2f4c9
fixes heltec tracker width/heigth labels
Gabrielerusso Feb 2, 2024
bd0cfad
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 6, 2024
e9f8675
Added battery level lookup table
Gabrielerusso Feb 6, 2024
714108a
Fixed formatting for trunk
Gabrielerusso Feb 6, 2024
edd5bd7
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 8, 2024
116ffaf
Added LiFePO4 and PB battery table
Gabrielerusso Feb 8, 2024
3cce370
Fixes possible position leak when location is disabled
Gabrielerusso Feb 9, 2024
0def5cd
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 9, 2024
bc53fcc
Version name corrections
Gabrielerusso Feb 9, 2024
b2084c0
Removed delay, added voltage filter
Gabrielerusso Feb 9, 2024
e7947a6
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 11, 2024
aabc979
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 11, 2024
58acd28
Fixed ADC_CTRL , Checks for valid ADC readings
Gabrielerusso Feb 11, 2024
11d78be
Fixed ESP32 ADC resolution bug introduced by #3184
Gabrielerusso Feb 11, 2024
9d83e52
Trunk fmt
Gabrielerusso Feb 11, 2024
940897a
added possibility to completely disable DEBUG LOG
Gabrielerusso Feb 11, 2024
cb59600
Merge remote-tracking branch 'upstream/master'
Gabrielerusso Feb 12, 2024
10205d2
Update protobufs
Gabrielerusso Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class AnalogBatteryLevel : public HasBatteryLevel

#ifndef BATTERY_SENSE_SAMPLES
#define BATTERY_SENSE_SAMPLES \
30 // Set the number of samples, it has an effect of increasing sensitivity in complex electromagnetic environment.
15 // Set the number of samples, it has an effect of increasing sensitivity in complex electromagnetic environment.
#endif

#ifdef BATTERY_PIN
Expand All @@ -208,12 +208,10 @@ class AnalogBatteryLevel : public HasBatteryLevel
raw = raw / BATTERY_SENSE_SAMPLES;
scaled = operativeAdcMultiplier * ((1000 * AREF_VOLTAGE) / pow(2, BATTERY_SENSE_RESOLUTION_BITS)) * raw;
#endif
// LOG_DEBUG("battery gpio %d raw val=%u scaled=%u\n", BATTERY_PIN, raw, (uint32_t)(scaled));
last_read_value += (scaled - last_read_value) * 0.5; // Virtual LPF
return scaled;
} else {
return last_read_value;
//LOG_DEBUG("battery gpio %d raw val=%u scaled=%u filtered=%u\n", BATTERY_PIN, raw, (uint32_t)(scaled), (uint32_t) (last_read_value));
}
return last_read_value;
#endif // BATTERY_PIN
return 0;
}
Expand All @@ -226,19 +224,24 @@ class AnalogBatteryLevel : public HasBatteryLevel
{

uint32_t raw = 0;
uint8_t raw_c = 0; //raw reading counter

#ifndef BAT_MEASURE_ADC_UNIT // ADC1
#ifdef ADC_CTRL
#ifdef ADC_CTRL //enable adc voltage divider when we need to read
pinMode(ADC_CTRL, OUTPUT);
digitalWrite(ADC_CTRL, HIGH);
digitalWrite(ADC_CTRL, ADC_CTRL_ENABLED);
delay(10);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there aren't filter caps with the resistor value i've seen the fet should saturate in less than 3ms

#endif
for (int i = 0; i < BATTERY_SENSE_SAMPLES; i++) {
raw += adc1_get_raw(adc_channel);
int val_ = adc1_get_raw(adc_channel);
if(val_ >= 0){ //save only valid readings
Copy link
Contributor Author

@Gabrielerusso Gabrielerusso Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for valid readings, invalid should give -1 but could be usefull to use strictly greather than 0 instead of >= ?

raw += val_;
raw_c ++;
}
// delayMicroseconds(100);
}
#ifdef ADC_CTRL
digitalWrite(ADC_CTRL, LOW);
#ifdef ADC_CTRL //disable adc voltage divider when we need to read
digitalWrite(ADC_CTRL, !ADC_CTRL_ENABLED);
#endif
#else // ADC2
int32_t adc_buf = 0;
Expand All @@ -249,10 +252,10 @@ class AnalogBatteryLevel : public HasBatteryLevel
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf);
raw += adc_buf;
raw_c ++;
}
#endif // BAT_MEASURE_ADC_UNIT
raw = raw / BATTERY_SENSE_SAMPLES;
return raw;
return (raw / (raw_c < 1 ? 1 : raw_c));
}
#endif

Expand Down Expand Up @@ -383,10 +386,6 @@ bool Power::analogInit()
} else {
LOG_INFO("ADCmod: ADC characterization based on default reference voltage\n");
}
#if defined(HELTEC_V3) || defined(HELTEC_WSL_V3)
pinMode(37, OUTPUT); // needed for P channel mosfet to work
digitalWrite(37, LOW);
#endif
#endif // ARCH_ESP32

#ifdef ARCH_NRF52
Expand Down
3 changes: 2 additions & 1 deletion variants/heltec_v2.1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#define LORA_DIO1 35 // https://www.thethingsnetwork.org/forum/t/big-esp32-sx127x-topic-part-3/18436
#define LORA_DIO2 34 // Not really used

#define ADC_MULTIPLIER 3.8
#define ADC_MULTIPLIER 3.2 //220k + 100k (320k/100k=3.2)
//#define ADC_WIDTH ADC_WIDTH_BIT_10

#define BATTERY_PIN 37 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define ADC_CHANNEL ADC1_GPIO37_CHANNEL
Expand Down
2 changes: 2 additions & 0 deletions variants/heltec_v3/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define VEXT_ENABLE Vext // active low, powers the oled display and the lora antenna boost
#define BUTTON_PIN 0

#define ADC_CTRL 37
#define ADC_CTRL_ENABLED LOW
#define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define ADC_CHANNEL ADC1_GPIO1_CHANNEL
#define ADC_ATTENUATION ADC_ATTEN_DB_2_5 // lower dB for high resistance voltage divider
Expand Down
1 change: 1 addition & 0 deletions variants/heltec_wireless_tracker_V1_1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define ADC_ATTENUATION ADC_ATTEN_DB_2_5 // lower dB for high resistance voltage divider
#define ADC_MULTIPLIER 4.9
#define ADC_CTRL 2 // active HIGH, powers the voltage divider. Only on 1.1
#define ADC_CTRL_ENABLED HIGH

#undef GPS_RX_PIN
#undef GPS_TX_PIN
Expand Down
2 changes: 2 additions & 0 deletions variants/heltec_wsl_v3/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define VEXT_ENABLE Vext // active low, powers the oled display and the lora antenna boost
#define BUTTON_PIN 0

#define ADC_CTRL 37
#define ADC_CTRL_ENABLED LOW
#define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define ADC_CHANNEL ADC1_GPIO1_CHANNEL
#define ADC_ATTENUATION ADC_ATTEN_DB_2_5 // lower dB for high resistance voltage divider
Expand Down