Loading files
fae49d278This file is pinned to the selected release. Download the original to open it in your design tool.
#include "Characterisation.h"
#include "gui.h"
#include "pushpull.h"
#include "Battery.h"
static const uint16_t imagedata[1024] = {
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x5ac9,0xcdb2,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdb2,0x5ac9,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6aa5,0xedc9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xdde9,0xedc9,0x6aa5,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xd5a8,0x8dc8,0x85c7,0x85c7,0x85c7,0x85a7,0x85a7,0x85a7,0x85a7,0x85a7,0x7d87,0x85c7,0x8dc8,0xd5a8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xcda8,0x7dc7,0x75a7,0x6d87,0x42c8,0x4b87,0x4b87,0x4b87,0x4b87,0x4b67,0x6506,0x75a7,0x7dc7,0xcda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xcda8,0x7da7,0x4b87,0x4b87,0x4ba7,0x7587,0x75a7,0x75a7,0x6d66,0x4b67,0x4b87,0x4b87,0x7d87,0xcda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xcda8,0x7d87,0x4ba8,0x75a7,0x75c7,0x75c7,0x75c7,0x75c7,0x4b87,0x6d46,0x75a7,0x4ba8,0x7d87,0xcda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xcda8,0x7dc7,0x75c7,0x75c7,0x75c7,0x75c7,0x75c7,0x6d87,0x4b87,0x75a7,0x75c7,0x75c7,0x7dc7,0xcda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x15c0,0x15c0,0x15c0,0x15c0,0x15c0,0x15c0,0x15c0,0x6a83,0xcda8,0x7dc7,0x75c7,0x75c7,0x75c7,0x75a7,0x6d46,0x4b67,0x6d46,0x75c7,0x75c7,0x75c7,0x7dc7,0xcda8,0x6a83,0xb800,0xb800,0xb800,0xb800,0xb800,0xb800,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xcda8,0x8dc7,0x7dc7,0x7dc7,0x7dc7,0x7526,0x3aa8,0x6464,0x7526,0x7dc7,0x7dc7,0x7dc7,0x8dc7,0xcda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xeda8,0xd5c8,0xcda8,0xd5c8,0xd5c8,0xcd88,0xc547,0xc547,0xcd88,0xd5c8,0xd5c8,0xcda8,0xd5c8,0xeda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xeda8,0xbca8,0x3229,0xd528,0xf5c8,0xe588,0xac48,0x3229,0xe588,0xf5c8,0xbcc8,0x3229,0xcd08,0xeda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xdd48,0x3229,0x3229,0x3229,0xeda8,0xc4e9,0x3229,0x3229,0x3229,0xe568,0x3229,0x3229,0x3229,0xe588,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6a83,0xe568,0x8bc9,0x3229,0xac68,0xf5c8,0xd529,0x6b49,0x3229,0xcd08,0xed88,0x8ba8,0x3229,0xac8a,0xeda8,0x6a83,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x6aa5,0xedca,0xe589,0xdd6a,0xedc9,0xf5e9,0xf5e9,0xdd69,0xdd69,0xf5c9,0xf5e9,0xe589,0xd549,0xedc9,0xf5ca,0x6aa5,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x5ac9,0xcdb2,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdd1,0xcdb2,0x5ac9,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0841,0x2104,0x2104,0x2123,0x2a44,0x3264,0x3a64,0x4264,0x4244,0x4a44,0x4a24,0x5224,0x5a04,0x59c4,0x51a4,0x4964,0x3924,0x10a2,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x4a69,0x7bcf,0x528a,0x530a,0x8e8c,0x968b,0xa68b,0xae6b,0xbe4b,0xce0b,0xd5eb,0xe5ab,0xed6a,0xecca,0xdc2b,0xcb8b,0xb3ce,0x7bcf,0x0000,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x0000,0x4a49,0x632c,0x4208,0x4ac8,0x8eac,0x8669,0x9649,0xa629,0xb609,0xc5c9,0xd588,0xdd48,0xece8,0xe448,0xd388,0xc2c8,0xbb8d,0x738d,0x0861,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x10a2,0x2965,0x39c7,0x1062,0x1982,0x7688,0x6623,0x7e03,0x8dc2,0xa582,0xb522,0xc4c2,0xd461,0xe401,0xdb21,0xca41,0xb941,0xb268,0x3986,0x0841,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x4208,0x39e7,0x39e7,0x1082,0x21c2,0x7ec9,0x6e43,0x7e23,0x95c3,0xa583,0xb522,0xc4e2,0xd481,0xe401,0xe321,0xca42,0xb942,0xbaa9,0x3986,0x0841,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x0000,0x0000,0x0000,0x0000,0x18c3,0x2945,0x4208,0x1082,0x21e3,0x7eea,0x6e64,0x8623,0x95e3,0xa583,0xb542,0xc4e2,0xd482,0xe401,0xe321,0xd242,0xc142,0xbaa9,0x3986,0x0861,0x0000,0x0000,0x0000,0x0000,0xb800,0x0000,
0x0000,0x15c0,0x15c0,0x15c0,0x15c0,0x15c0,0x2124,0x2945,0x41e8,0x1082,0x21e3,0x870a,0x6e84,0x8643,0x95e3,0xa5a3,0xb522,0xc4c2,0xd461,0xe401,0xe321,0xd222,0xc922,0xc289,0x3165,0x0861,0xb800,0xb800,0xb800,0xb800,0xb800,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0020,0x6b4d,0x6b4d,0x630c,0x4a49,0x5349,0x9f4f,0x96ea,0x9eaa,0xae6a,0xbe29,0xc5e9,0xd589,0xdd48,0xece8,0xec48,0xe389,0xdac9,0xd3ce,0x62eb,0x10a2,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x738e,0x9cf3,0x8410,0x6b6d,0x742d,0xb792,0xaf2f,0xb70f,0xbecf,0xce8e,0xd64e,0xde0e,0xe5cd,0xed8d,0xed0d,0xec6e,0xe3ce,0xdcb2,0x9cb2,0x18e3,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1082,0x9492,0x94b2,0x7bef,0x848f,0xbf94,0xb751,0xbf31,0xc6f0,0xced0,0xd690,0xde50,0xe60f,0xedcf,0xf56f,0xecf0,0xec50,0xe4f3,0xbdd7,0x18e3,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x9492,0xa534,0x8c51,0x8cf1,0xc7b5,0xbf73,0xc753,0xcf13,0xd6f2,0xdeb2,0xe692,0xee51,0xf631,0xf5d2,0xf552,0xecd2,0xe555,0xd6ba,0x18c3,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x4a49,0x5acb,0x39c7,0x4287,0x976d,0x974a,0x9f0a,0xaea9,0xbe49,0xc608,0xd5a8,0xe547,0xed07,0xf447,0xf388,0xf2a9,0xcb0c,0x6b2c,0x0841,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1082,0x2965,0x2945,0x2965,0x3ac6,0x42e6,0x42c6,0x4aa5,0x4a85,0x5285,0x5265,0x5a45,0x5a25,0x5a05,0x59c5,0x5985,0x4965,0x2104,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
};
static Image_t icon = { .width = 32, .height = 32, .data = imagedata };
/* Forward declaration of actual app task */
static void Characterisation(void *unused);
/* Creates the app task, called from desktop when user start the app */
static void Characterisation_Start(){
xTaskCreate(Characterisation, "Characterisation", 1000, NULL, 3, NULL);
}
/* Register this app */
void Characterisation_Init() {
App_Register("Characterisation", "Measure battery characteristics", Characterisation_Start, icon);
}
static uint8_t start = 0;
static TaskHandle_t handle;
#define SAMPLES 275
static int32_t vResponse[SAMPLES];
static int32_t cResponse[SAMPLES];
static uint32_t sampleCnt;
static volatile bool samplingDone;
static int analyzeResponse(int32_t *voltage, int32_t *current, uint16_t npoints,
BatDataPoint_t *result) {
printf("Response:\n");
uint16_t i;
char buf[12];
for(i=0;i<npoints;i++) {
printf("%ld;%ld\n", current[i], voltage[i]);
}
printf("Analyzing response...\n");
const int32_t startCurrent = current[0], stopCurrent = current[npoints - 1];
common_StringFromValue(buf, 10, startCurrent, &Unit_Current);
printf("Start current: %s\n", buf);
common_StringFromValue(buf, 10, stopCurrent, &Unit_Current);
printf("Stop current: %s\n", buf);
/* Calculate start voltage */
int64_t vsum = 0;
uint16_t currentChangeIndex = 0;
for (i = 0; i < npoints; i++) {
/* Check if current is closer to stopCurrent than to startCurrent */
if (startCurrent > stopCurrent) {
if (current[i] < (startCurrent + stopCurrent) / 2)
break;
} else {
if (current[i] > (startCurrent + stopCurrent) / 2)
break;
}
vsum += voltage[i];
}
currentChangeIndex = i;
printf("Change index: %d\n", i);
if (i < 2) {
/* not enough samples */
return -1;
}
/* remove the last sample as it might already be affected by the current change */
i--;
vsum -= voltage[i];
/* Get average idle voltage */
int32_t startVoltage = vsum / i;
common_StringFromValue(buf, 10, startVoltage, &Unit_Voltage);
printf("Start voltage: %s\n", buf);
/* Calculate stop voltage */
vsum = 0;
for (i = 0; i < (npoints - currentChangeIndex) / 2; i++) {
vsum += voltage[npoints - i - 1];
}
int32_t stopVoltage = vsum / i;
common_StringFromValue(buf, 10, voltage[currentChangeIndex + 1], &Unit_Voltage);
printf("Change voltage: %s\n", buf);
common_StringFromValue(buf, 10, stopVoltage, &Unit_Voltage);
printf("Stop voltage: %s\n", buf);
/* Calculate R1 and R2 */
int32_t R1drop = voltage[currentChangeIndex + 1] - startVoltage;
int32_t R2drop = stopVoltage - voltage[currentChangeIndex + 1];
result->R1 = (int64_t) R1drop * 1000000 / (stopCurrent - startCurrent);
result->R2 = (int64_t) R2drop * 1000000 / (stopCurrent - startCurrent);
common_StringFromValue(buf, 10, result->R1, &Unit_Resistance);
printf("R1: %s\n", buf);
common_StringFromValue(buf, 10, result->R2, &Unit_Resistance);
printf("R2: %s\n", buf);
// TODO complete
return 0;
}
static void pushpullCallback(PushPull_State_t *state) {
if (sampleCnt < SAMPLES) {
vResponse[sampleCnt] = state->voltage;
cResponse[sampleCnt] = state->current;
sampleCnt++;
} else {
pushpull_SetCallback(NULL);
BaseType_t yield;
samplingDone = true;
portYIELD_FROM_ISR(yield);
}
}
static void Characterisation(void *unused) {
handle = xTaskGetCurrentTaskHandle();
/* Create GUI elements */
Container *c= new Container(COORDS(280, 240));
c->setPosition(COORDS(40, 0));
int32_t current = 0;
Label *lCurrent = new Label("Test current:", Font_Big);
Entry *eCurrent = new Entry(¤t, &Limits.maxCurrent, &null, Font_Big, 6, &Unit_Current);
Graph *gvResponse = new Graph(vResponse, SAMPLES, 85, COLOR_DARKGREEN, &Unit_Voltage);
Graph *gcResponse = new Graph(cResponse, SAMPLES, 85, COLOR_RED, &Unit_Current);
Button *bStart = new Button("START", Font_Big, [](Widget &w) {
start = 1;
xTaskNotify(handle, SIGNAL_WAKEUP, eSetBits);
}, 260);
c->attach(lCurrent, COORDS(5, 7));
c->attach(eCurrent, COORDS(180, 5));
c->attach(gvResponse, COORDS(2, 30));
c->attach(gcResponse, COORDS(2, 120));
c->attach(bStart, COORDS(10, 210));
/* Notify desktop of started app */
desktop_AppStarted(Characterisation_Start, c);
pushpull_AcquireControl();
pushpull_SetAveraging(300);
samplingDone = false;
while(1) {
uint32_t signal;
if (App_Handler(&signal, 300)) {
/* Handle app signals */
if(start) {
sampleCnt = 0;
pushpull_SetEnabled(1);
vTaskDelay(100);
pushpull_SetCallback(pushpullCallback);
HAL_Delay(10);
pushpull_SetSinkCurrent(current);
while(!samplingDone) {
HAL_Delay(100);
}
pushpull_SetDefault();
BatDataPoint_t res;
analyzeResponse(vResponse, cResponse, SAMPLES, &res);
Dialog::MessageBox("Done", Font_Medium, "Sampling finished",
Dialog::MsgBox::OK, NULL, true);
gvResponse->requestRedrawFull();
gcResponse->requestRedrawFull();
start = 0;
}
}
}
}