#include "fuel_learning_table.h" #include "SD.h" #include "maps.h" FuelLearningTable::FuelLearningTable() { reset(); } void FuelLearningTable::reset() { for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { corrections[i][j] = 1.0; cellHits[i][j] = 0; } } } bool FuelLearningTable::save() { File file = SD.open("/fuel_learning.bin", FILE_WRITE); if (!file) return false; file.write((uint8_t*)corrections, sizeof(corrections)); file.write((uint8_t*)cellHits, sizeof(cellHits)); file.close(); return true; } bool FuelLearningTable::load() { File file = SD.open("/fuel_learning.bin", FILE_READ); if (!file) return false; file.read((uint8_t*)corrections, sizeof(corrections)); file.read((uint8_t*)cellHits, sizeof(cellHits)); file.close(); return true; } float FuelLearningTable::getCorrection(float load, float rpm) { int loadIdx = constrain(mapf(load, 0, 100, 0, 15), 0, 15); int rpmIdx = constrain(mapf(rpm, 0, 7000, 0, 15), 0, 15); return corrections[loadIdx][rpmIdx]; } void FuelLearningTable::update(float load, float rpm, float correction) { int loadIdx = constrain(mapf(load, 0, 100, 0, 15), 0, 15); int rpmIdx = constrain(mapf(rpm, 0, 7000, 0, 15), 0, 15); float currentCorr = corrections[loadIdx][rpmIdx]; float learningRate = 0.1f / (1.0f + cellHits[loadIdx][rpmIdx]); corrections[loadIdx][rpmIdx] = currentCorr + (correction - currentCorr) * learningRate; cellHits[loadIdx][rpmIdx]++; } float FuelLearningTable::getProgress() const { int totalCells = 16 * 16; int learnedCells = 0; for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { if (cellHits[i][j] > 0) learnedCells++; } } return (float)learnedCells / totalCells; }