1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-23 00:05:33 +03:00

CF/BF - First cut of Current/Voltage/Battery cleanup.

many refactorings, bugs squished, concerns separated,
single-responsibility violations fixed and performance optimizations.
This commit is contained in:
Hydra 2017-03-12 11:26:30 +00:00 committed by Dominic Clifton
parent b46e0fe46d
commit 067d3c0ac2
69 changed files with 1178 additions and 514 deletions

View file

@ -54,7 +54,10 @@ extern "C" {
#include "telemetry/telemetry.h"
bool airMode;
uint16_t vbat;
uint16_t testBatteryVoltage = 0;
int32_t testAmperage = 0;
serialPort_t *telemetrySharedPort;
PG_REGISTER(batteryConfig_t, batteryConfig, PG_BATTERY_CONFIG, 0);
PG_REGISTER(telemetryConfig_t, telemetryConfig, PG_TELEMETRY_CONFIG, 0);
@ -138,7 +141,7 @@ TEST(TelemetryCrsfTest, TestBattery)
{
uint8_t frame[CRSF_FRAME_SIZE_MAX];
vbat = 0; // 0.1V units
testBatteryVoltage = 0; // 0.1V units
int frameLen = getCrsfFrame(frame, CRSF_FRAME_BATTERY_SENSOR);
EXPECT_EQ(CRSF_FRAME_BATTERY_SENSOR_PAYLOAD_SIZE + FRAME_HEADER_FOOTER_LEN, frameLen);
EXPECT_EQ(CRSF_ADDRESS_BROADCAST, frame[0]); // address
@ -154,8 +157,8 @@ TEST(TelemetryCrsfTest, TestBattery)
EXPECT_EQ(67, remaining);
EXPECT_EQ(crfsCrc(frame, frameLen), frame[11]);
vbat = 33; // 3.3V = 3300 mv
amperage = 2960; // = 29.60A = 29600mA - amperage is in 0.01A steps
testBatteryVoltage = 33; // 3.3V = 3300 mv
testAmperage = 2960; // = 29.60A = 29600mA - amperage is in 0.01A steps
batteryConfigMutable()->batteryCapacity = 1234;
frameLen = getCrsfFrame(frame, CRSF_FRAME_BATTERY_SENSOR);
voltage = frame[3] << 8 | frame[4]; // mV * 100
@ -283,9 +286,6 @@ uint16_t GPS_altitude; // altitude in m
uint16_t GPS_speed; // speed in 0.1m/s
uint16_t GPS_ground_course = 0; // degrees * 10
int32_t amperage;
int32_t mAhDrawn;
void beeperConfirmationBeeps(uint8_t beepCount) {UNUSED(beepCount);}
uint32_t micros(void) {return 0;}
@ -308,11 +308,23 @@ bool telemetryCheckRxPortShared(const serialPortConfig_t *) {return true;}
portSharing_e determinePortSharing(const serialPortConfig_t *, serialPortFunction_e) {return PORTSHARING_NOT_SHARED;}
uint8_t batteryCapacityRemainingPercentage(void) {return 67;}
uint8_t calculateBatteryCapacityRemainingPercentage(void) {return 67;}
uint8_t calculateBatteryPercentage(void) {return 67;}
batteryState_e getBatteryState(void) {return BATTERY_OK;}
bool isAirmodeActive(void) {return airMode;}
uint16_t getVbat(void) { return vbat; }
int32_t getAmperage(void) {
return testAmperage;
}
uint16_t getBatteryVoltage(void) {
return testBatteryVoltage;
}
batteryState_e getBatteryState(void) {
return BATTERY_OK;
}
uint8_t calculateBatteryPercentageRemaining(void) {
return 67;
}
}

View file

@ -51,6 +51,11 @@ extern "C" {
#include "telemetry/hott.h"
PG_REGISTER(telemetryConfig_t, telemetryConfig, PG_TELEMETRY_CONFIG, 0);
uint16_t testBatteryVoltage = 0;
int32_t testAmperage = 0;
int32_t testMAhDrawn = 0;
}
#include "unittest_macros.h"
@ -172,10 +177,7 @@ uint16_t GPS_speed; // speed in 0.1m/s
uint16_t GPS_distanceToHome; // distance to home point in meters
uint16_t GPS_altitude; // altitude in 0.1m
int16_t GPS_directionToHome; // direction to home or hol point in degrees
uint16_t vbat;
int32_t amperage;
int32_t mAhDrawn;
uint32_t fixedMillis = 0;
@ -264,8 +266,17 @@ batteryState_e getBatteryState(void)
return BATTERY_OK;
}
uint16_t getVbat(void)
uint16_t getBatteryVoltage(void)
{
return vbat;
return testBatteryVoltage;
}
int32_t getAmperage(void) {
return testAmperage;
}
int32_t getMAhDrawn(void) {
return testMAhDrawn;
}
}

View file

@ -37,7 +37,7 @@ extern "C" {
extern "C" {
uint8_t batteryCellCount = 3;
uint8_t testBatteryCellCount =3;
int16_t rcCommand[4] = {0, 0, 0, 0};
telemetryConfig_t telemetryConfig_System;
}
@ -62,6 +62,16 @@ typedef struct serialPortStub_s {
} serialPortStub_t;
static uint16_t testBatteryVoltage = 100;
uint16_t getBatteryVoltage(void)
{
return testBatteryVoltage;
}
uint8_t getBatteryCellCount(void) {
return testBatteryCellCount;
}
static serialPortStub_t serialWriteStub;
static serialPortStub_t serialReadStub;
@ -367,7 +377,7 @@ TEST_F(IbusTelemteryProtocolUnitTest, Test_IbusRespondToGetMeasurementVbattZero)
{
//Given ibus command: Sensor at address 1, please send your measurement
//then we respond with: I'm reading 0 volts
vbat = 0;
testBatteryVoltage = 0;
checkResponseToCommand("\x04\xA1\x5a\xff", 4, "\x06\xA1\x00\x00\x58\xFF", 6);
}
@ -377,14 +387,14 @@ TEST_F(IbusTelemteryProtocolUnitTest, Test_IbusRespondToGetMeasurementVbattCellV
//Given ibus command: Sensor at address 1, please send your measurement
//then we respond with: I'm reading 0.1 volts
batteryCellCount = 3;
vbat = 30;
testBatteryCellCount =3;
testBatteryVoltage = 30;
checkResponseToCommand("\x04\xA1\x5a\xff", 4, "\x06\xA1\x64\x00\xf4\xFe", 6);
//Given ibus command: Sensor at address 1, please send your measurement
//then we respond with: I'm reading 0.1 volts
batteryCellCount = 1;
vbat = 10;
testBatteryCellCount =1;
testBatteryVoltage = 10;
checkResponseToCommand("\x04\xA1\x5a\xff", 4, "\x06\xA1\x64\x00\xf4\xFe", 6);
}
@ -394,14 +404,14 @@ TEST_F(IbusTelemteryProtocolUnitTest, Test_IbusRespondToGetMeasurementVbattPackV
//Given ibus command: Sensor at address 1, please send your measurement
//then we respond with: I'm reading 0.1 volts
batteryCellCount = 3;
vbat = 10;
testBatteryCellCount =3;
testBatteryVoltage = 10;
checkResponseToCommand("\x04\xA1\x5a\xff", 4, "\x06\xA1\x64\x00\xf4\xFe", 6);
//Given ibus command: Sensor at address 1, please send your measurement
//then we respond with: I'm reading 0.1 volts
batteryCellCount = 1;
vbat = 10;
testBatteryCellCount =1;
testBatteryVoltage = 10;
checkResponseToCommand("\x04\xA1\x5a\xff", 4, "\x06\xA1\x64\x00\xf4\xFe", 6);
}
@ -488,8 +498,8 @@ TEST_F(IbusTelemteryProtocolUnitTestDaisyChained, Test_IbusRespondToGetMeasureme
{
//Given ibus command: Sensor at address 3, please send your measurement
//then we respond with: I'm reading 0.1 volts
batteryCellCount = 1;
vbat = 10;
testBatteryCellCount = 1;
testBatteryVoltage = 10;
checkResponseToCommand("\x04\xA3\x58\xff", 4, "\x06\xA3\x64\x00\xf2\xfe", 6);
//Given ibus command: Sensor at address 4, please send your measurement