SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
axp2602_gauge.ino
Go to the documentation of this file.
1
30#include <GaugeDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 3
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 2
38#endif
39
41
42void setup()
43{
44 Serial.begin(115200);
45 // while (!Serial);
46
47 if (!gauge.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
48 Serial.println("Failed to AXP2602 - check your wiring!");
49 while (1) {
50 delay(1000);
51 }
52 }
53 Serial.println("Init AXP2602 Sensor success!");
54
55 /*
56 * Gauge Work mode:
57 * // It has relatively high power consumption, but can be used for both charging and discharging.
58 * - OPERATING_MODE_HIGH_PRECISION
59 *
60 * // In default mode, it can be used for both charging and discharging.
61 * - OPERATING_MODE_NORMAL
62 *
63 * // With relatively low power consumption, it can only be used in low-power scenarios during discharge.
64 * // In other scenarios, software switching to normal mode or high-precision mode is required.
65 * - OPERATING_MODE_LOW_POWER
66 */
67 // gauge.setOperatingMode(GaugeAXP2602::OPERATING_MODE_HIGH_PRECISION);
68
69 /*
70 * The sampling resistor size is 10 milliohms by default.
71 * SENSE_RESISTOR_10_MOHM
72 * SENSE_RESISTOR_5_MOHM
73 * SENSE_RESISTOR_20_MOHM
74 * SENSE_RESISTOR_40_MOHM
75 */
76 // gauge.setCurrentSenseResistor(GaugeAXP2602::SENSE_RESISTOR_10_MOHM);
77
78 // gauge.setBatteryDetection(true); // Enabled by default
79
80 // gauge.setCurrentMeasurement(true); // Enabled by default
81
82 // gauge.setThermalDieMeasurement(true); // Enabled by default
83
84 // gauge.setLowBatterySOCThreshold(60); // Set low battery SOC threshold to 60%
85
88
89 /*
90
91 Replace with the battery curve you want to use.
92
93 static uint8_t BATTER_PARAMS[] = {
94 0x01, 0xf5, 0x40, 0x00, 0x1b, 0x1e, 0x28, 0x0f, 0x0c, 0x1e, 0x32, 0x02, 0x14, 0x05, 0x0a, 0x04,
95 0x74, 0xfc, 0xf4, 0x0d, 0x43, 0x10, 0x52, 0xfb, 0xa6, 0x01, 0xea, 0x04, 0x64, 0x06, 0x52, 0x06,
96 0x18, 0x0a, 0xe7, 0x0f, 0x9f, 0x0f, 0x51, 0x09, 0xf7, 0x0e, 0x89, 0x0e, 0x71, 0x04, 0x58, 0x04,
97 0x43, 0x09, 0x32, 0x0e, 0x1c, 0x0e, 0x14, 0x09, 0x04, 0x0d, 0xe9, 0x0d, 0xde, 0x03, 0xc8, 0x03,
98 0xb3, 0x08, 0x9d, 0x0d, 0x79, 0x0d, 0x3a, 0x07, 0xf5, 0x9e, 0x56, 0x47, 0x36, 0x20, 0x24, 0x17,
99 0xc5, 0x98, 0x7e, 0x66, 0x4e, 0x44, 0x38, 0x1a, 0x12, 0x0a, 0xf6, 0x00, 0x00, 0xf6, 0x00, 0xf6,
100 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xf6, 0x00, 0x00, 0xf6, 0x00, 0xf6,
101 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xf6, 0x00, 0x00, 0xf6, 0x00, 0xf6
102 };
103
104
105 if (gauge.writeGaugeData(BATTER_PARAMS, sizeof(BATTER_PARAMS))) {
106 Serial.println("Battery calibration data write success");
107 } else {
108 Serial.println("Battery calibration data write failed");
109 }
110
111 */
112}
113
114
115void loop()
116{
117 uint32_t startMeasTime = millis();
118
119 if (gauge.refresh()) {
120
121 uint32_t endMesTime = millis();
122
123 Serial.print("Polling time: "); Serial.print(endMesTime - startMeasTime); Serial.println(" ms");
124 Serial.print("\t- Sleep: "); Serial.print(gauge.isSleepModeEnabled() ? "Enabled" : "Disabled"); Serial.println();
125 Serial.print("\t- Temperature:"); Serial.print(gauge.getTemperature()); Serial.println(" ℃");
126 Serial.print("\t- BatteryVoltage:"); Serial.print(gauge.getVoltage()); Serial.println(" mV");
127 Serial.print("\t- InstantaneousCurrent:"); Serial.print(gauge.getCurrent()); Serial.println(" mAh");
128 Serial.print("\t- InstantaneousCurrentRaw:"); Serial.print(gauge.getCurrentRaw()); Serial.println(" RAW");
129 Serial.print("\t- ThermalDieTemperature:"); Serial.print(gauge.getThermalDieTemperature()); Serial.println(" ℃");
130 Serial.print("\t- ThermalDieTemperatureRaw:"); Serial.print(gauge.getThermalDieTemperatureRaw()); Serial.println(" RAW");
131 Serial.print("\t- ChargingPower:"); Serial.print(gauge.getChargingPower()); Serial.println(" W");
132 Serial.print("\t- DischargingPower:"); Serial.print(gauge.getDischargingPower()); Serial.println(" W");
133 Serial.print("\t- AbsolutePower:"); Serial.print(gauge.getAbsolutePower()); Serial.println(" W");
134 Serial.print("\t- isCharging:"); Serial.print(gauge.isCharging() ? "Yes" : "No"); Serial.println();
135 Serial.print("\t- isDischarging:"); Serial.print(gauge.isDischarging() ? "Yes" : "No"); Serial.println();
136 Serial.print("\t- TimeToEmpty:"); Serial.print(gauge.getTimeToEmpty()); Serial.println(" minutes");
137 Serial.print("\t- TimeToFull:"); Serial.print(gauge.getTimeToFull()); Serial.println(" minutes");
138 Serial.print("\t- StateOfCharge:"); Serial.print(gauge.getStateOfCharge()); Serial.println(" %");
139 Serial.print("\t- Battery Status:"); Serial.print(gauge.getBatteryStatus()); Serial.println(" RAW");
140
142
143 switch (irqStatus) {
145 Serial.println("Low battery detected!");
146 Serial.println("Low battery detected!");
147 break;
149 Serial.println("Watchdog timer timeout!");
150 Serial.println("Watchdog timer timeout!");
151 break;
153 Serial.println("Over temperature detected!");
154 Serial.println("Over temperature detected!");
155 break;
157 Serial.println("State of Charge updated!");
158 Serial.println("State of Charge updated!");
159 break;
160 default:
161 break;
162 }
163
164 if (irqStatus != GaugeAXP2602::IRQ_STATUS_NONE) {
166 }
167
168 Serial.println("===============================================");
169
170 }
171 delay(3000);
172}
173
174
175
@license MIT License
GaugeAXP2602 gauge
void setup()
#define SENSOR_SCL
#define SENSOR_SDA
void loop()
uint16_t getStateOfCharge() override
Get the battery percentage.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback)
Begin with custom callback functions.
uint16_t getTimeToFull() override
Get the battery time to full.
bool isSleepModeEnabled()
Check if the AXP2602 chip is in sleep mode.
uint16_t getTimeToEmpty() override
Get the battery time to empty.
bool isDischarging() override
Checks if the battery is discharging.
float getTemperature() override
Get the temperature.
uint16_t getVoltage() override
Get the battery voltage.
float getCurrent() override
Get the actual current value (considering the sampling resistor)
uint8_t getBatteryStatus()
Get the battery health status.
float getThermalDieTemperature()
Get the actual die temperature.
uint16_t getThermalDieTemperatureRaw()
Get the thermal die temperature.
float getChargingPower()
Get charging power (positive or 0)
int16_t getCurrentRaw()
Get the battery current.
float getDischargingPower()
Get discharging power (positive or 0)
bool refresh() override
Refresh the AXP2602 chip data.
float getAbsolutePower()
Get the absolute power (always positive)
IRQStatus getIRQStatus()
Get the IRQ status.
void clearIRQStatus()
Clear the IRQ status.
bool isCharging() override
Checks if the battery is charging.