From 43b6b9a9222052b53d707c965d56cabb615f6cdd Mon Sep 17 00:00:00 2001 From: FDSoftware Date: Thu, 20 Jan 2022 02:43:54 -0300 Subject: [PATCH] added ignition interrupt --- Inc/templates/CHT_1_6_.h | 1 + Inc/variables.h | 15 ++++---- Src/ignition/include/ignition.hpp | 22 ++++++------ Src/ignition/src/ignition.cpp | 60 +++++++++++++++++++------------ Src/main.cpp | 4 +-- Src/memory/include/tables.hpp | 4 +-- Src/memory/src/tables.cpp | 19 +++++++--- 7 files changed, 76 insertions(+), 49 deletions(-) diff --git a/Inc/templates/CHT_1_6_.h b/Inc/templates/CHT_1_6_.h index 177f384d..c30cf25b 100644 --- a/Inc/templates/CHT_1_6_.h +++ b/Inc/templates/CHT_1_6_.h @@ -15,6 +15,7 @@ #undef PMSI #undef AVCI #undef ECNT +#undef MAX_RPM /*-----( Globales )-----*/ diff --git a/Inc/variables.h b/Inc/variables.h index 53eda9de..4d61a189 100644 --- a/Inc/variables.h +++ b/Inc/variables.h @@ -1,21 +1,20 @@ /*-----( Variables Globales )-----*/ // estas variables las puede usar cualquieeer funcion -#include +#include #include #include -#include +#include #ifndef VARIABLES #define VARIABLES extern uint32_t _RPM, // las rpm :V - _POS, // posicion del cigueñal (en dientes) (el contador de rpm la resetea a cada rato) - _POS_AE, // posicion arbol de levas - _AE; // avance de encendido + _POS, // posicion del cigueñal (en dientes) (el contador de rpm la resetea a cada rato) + _POS_AE; // posicion arbol de levas +extern int32_t _AE; // avance de encendido -extern bool - MOTOR_ENABLE, // cache de "can_turn_on" - SINC; // indica si se sincronizo el PMS +extern bool MOTOR_ENABLE, // cache de "can_turn_on" + SINC; // indica si se sincronizo el PMS /*-----( Variables RPM )-----*/ diff --git a/Src/ignition/include/ignition.hpp b/Src/ignition/include/ignition.hpp index 31981339..36e05ba3 100644 --- a/Src/ignition/include/ignition.hpp +++ b/Src/ignition/include/ignition.hpp @@ -6,20 +6,22 @@ #define IGNITION_HPP #include -#include #include +#include #include "aliases/memory.hpp" #include "aliases/sensors.hpp" -namespace ignition -{ - void interrupt(); - void setup(); - - // tabla de avance, por TPS y RPM - extern TABLEDATA avc_tps_rpm; - extern int16_t avc_rpm[13]; - extern int16_t avc_tps[13]; +#include "variables.h" + +namespace ignition { +void interrupt(); +void setup(); + +// tabla de avance, por TPS y RPM +extern TABLEDATA avc_tps_rpm; +extern int16_t avc_rpm[13]; +extern int16_t avc_tps[13]; +extern bool loaded; } // namespace ignition diff --git a/Src/ignition/src/ignition.cpp b/Src/ignition/src/ignition.cpp index 0d2d2e66..b95e24e5 100644 --- a/Src/ignition/src/ignition.cpp +++ b/Src/ignition/src/ignition.cpp @@ -10,38 +10,52 @@ usando sensors => map y rpm nomas */ #include "../include/ignition.hpp" -TABLEDATA avc_tps_rpm; +TABLEDATA ignition::avc_tps_rpm; +bool ignition::loaded = false; +int32_t _AE = 0; -void ignition::interrupt() -{ +void ignition::interrupt() { + if (!ignition::loaded) + return; + int32_t load = sensors::values._MAP; + + int16_t load_value = tables::find_nearest_neighbor( + tables::col_to_row(ignition::avc_tps_rpm, 0), load); + + int16_t rpm_value = + tables::find_nearest_neighbor(ignition::avc_tps_rpm[0], load); + + _AE = avc_tps_rpm[load_value][rpm_value]; } -void ignition::setup() -{ - table_ref ignition_table = TABLES_IGNITION_TPS_SETTINGS; - avc_tps_rpm = tables::read_all(ignition_table); +void ignition::setup() { + table_ref ignition_table = TABLES_IGNITION_TPS_SETTINGS; + ignition::avc_tps_rpm = tables::read_all(ignition_table); } /** Ejemplo tablita: * load(tps)/rpm - * [ * ] [550 ] [ 950] [1200] [1650] [2200] [2800] [3400] [3900] [4400] [4900] [5400] [7200] - * [ 100] (13.5) (13.0) (14.0) (14.0) (18.6) (24.0) (31.0) (33.0) (33.2) (33.4) (33.6) (34.3) - * [ 90 ] (13.8) (13.3) (14.0) (14.2) (17.4) (24.5) (31.2) (33.3) (33.6) (33.8) (34.1) (34.9) - * [ 80 ] (14.2) (13.6) (13.9) (14.4) (17.8) (25.0) (31.5) (33.7) (34.0) (34.2) (34.5) (35.5) - * [ 70 ] (14.5) (13.9) (13.9) (14.6) (18.3) (25.5) (31.7) (34.0) (34.4) (34.7) (35.0) (36.1) - * [ 60 ] (14.9) (14.2) (13.8) (14.8) (18.7) (25.9) (32.0) (34.4) (34.7) (35.1) (35.4) (36.7) - * [ 50 ] (15.3) (14.5) (13.8) (15.0) (19.1) (26.4) (32.2) (34.4) (35.1) (35.5) (35.9) (37.3) - * [ 40 ] (15.7) (14.8) (13.8) (15.2) (19.5) (26.9) (32.5) (34.4) (35.5) (35.9) (36.4) (37.9) - * [ 30 ] (16.1) (15.2) (14.4) (15.4) (19.9) (27.4) (32.7) (34.4) (35.9) (36.4) (36.8) (38.5) - * [ 20 ] (16.4) (15.5) (15.1) (15.7) (20.4) (27.4) (34.9) (34.4) (36.3) (36.8) (37.3) (39.1) - * [ 15 ] (16.6) (15.7) (15.4) (15.8) (20.6) (28.3) (36.0) (34.4) (34.7) (37.0) (37.5) (39.4) - * [ 10 ] (16.8) (16.3) (15.7) (15.9) (20.8) (28.4) (36.0) (34.4) (34.7) (37.2) (37.8) (39.7) - * [ 5 ] (17.0) (16.5) (16.0) (16.0) (21.0) (28.5) (36.0) (34.4) (34.7) (37.4) (38.0) (40.0) -*/ + * [ * ] [550 ] [ 950] [1200] [1650] [2200] [2800] [3400] [3900] [4400] [4900] + * [5400] [7200] [ 100] (13.5) (13.0) (14.0) (14.0) (18.6) (24.0) (31.0) (33.0) + * (33.2) (33.4) (33.6) (34.3) [ 90 ] (13.8) (13.3) (14.0) (14.2) (17.4) (24.5) + * (31.2) (33.3) (33.6) (33.8) (34.1) (34.9) [ 80 ] (14.2) (13.6) (13.9) (14.4) + * (17.8) (25.0) (31.5) (33.7) (34.0) (34.2) (34.5) (35.5) [ 70 ] (14.5) (13.9) + * (13.9) (14.6) (18.3) (25.5) (31.7) (34.0) (34.4) (34.7) (35.0) (36.1) [ 60 ] + * (14.9) (14.2) (13.8) (14.8) (18.7) (25.9) (32.0) (34.4) (34.7) (35.1) (35.4) + * (36.7) [ 50 ] (15.3) (14.5) (13.8) (15.0) (19.1) (26.4) (32.2) (34.4) (35.1) + * (35.5) (35.9) (37.3) [ 40 ] (15.7) (14.8) (13.8) (15.2) (19.5) (26.9) (32.5) + * (34.4) (35.5) (35.9) (36.4) (37.9) [ 30 ] (16.1) (15.2) (14.4) (15.4) (19.9) + * (27.4) (32.7) (34.4) (35.9) (36.4) (36.8) (38.5) [ 20 ] (16.4) (15.5) (15.1) + * (15.7) (20.4) (27.4) (34.9) (34.4) (36.3) (36.8) (37.3) (39.1) [ 15 ] (16.6) + * (15.7) (15.4) (15.8) (20.6) (28.3) (36.0) (34.4) (34.7) (37.0) (37.5) (39.4) + * [ 10 ] (16.8) (16.3) (15.7) (15.9) (20.8) (28.4) (36.0) (34.4) (34.7) (37.2) + * (37.8) (39.7) [ 5 ] (17.0) (16.5) (16.0) (16.0) (21.0) (28.5) (36.0) (34.4) + * (34.7) (37.4) (38.0) (40.0) + */ /** - * + * * * [ 20 ] 164, 155, 151, 157, 204, 274, 349, 344, 363, 368, 373, 391, * [ 15 ] 166, 157, 154, 158, 206, 283, 360, 344, 347, 370, 375, 394, * [ 10 ] 168, 163, 157, 159, 208, 284, 360, 344, 347, 372, 378, 397, * [ 5 ] 170, 165, 160, 160, 210, 285, 360, 344, 347, 374, 380, 400, -*/ + */ diff --git a/Src/main.cpp b/Src/main.cpp index e5585481..e4790693 100644 --- a/Src/main.cpp +++ b/Src/main.cpp @@ -51,7 +51,7 @@ extern "C" { #include "pmic/pmic.hpp" #include "usbd_cdc_if.h" #include "webserial/commands.hpp" - +#include "ignition/include/ignition.hpp" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -140,7 +140,7 @@ int main(void) { W25qxx_Init(); srand(HAL_GetTick()); web_serial::setup(); - + ignition::setup(); /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { diff --git a/Src/memory/include/tables.hpp b/Src/memory/include/tables.hpp index dddbf947..ef244801 100644 --- a/Src/memory/include/tables.hpp +++ b/Src/memory/include/tables.hpp @@ -61,8 +61,8 @@ TABLEDATA read_all(table_ref); // TABLEDATA alter_table(TABLEDATA, uint16_t, uint16_t, uint16_t); // utils para manejar data de las tablas: -int32_t find_nearest_neighbor(std::vector, int32_t); - +int16_t find_nearest_neighbor(std::vector, int32_t); +std::vector col_to_row(TABLEDATA, uint16_t); /** * @brief erases page on memory and record new data of table */ diff --git a/Src/memory/src/tables.cpp b/Src/memory/src/tables.cpp index bff87995..65c51d4e 100644 --- a/Src/memory/src/tables.cpp +++ b/Src/memory/src/tables.cpp @@ -109,14 +109,16 @@ void tables::update_table(TABLEDATA data, table_ref table) { free(buffer); } -int32_t tables::find_nearest_neighbor(std::vector vec, +int16_t tables::find_nearest_neighbor(std::vector vec, int32_t search) { - auto const search_result = std::upper_bound(vec.begin(), vec.end(), search); - if (search_result == vec.end()) { + int16_t search_result = std::abs(std::distance( + vec.begin(), std::upper_bound(vec.begin(), vec.end(), search))); + + if (search_result == *vec.end()) { return -1; } - return *search_result; + return search_result; } void tables::plot_table(TABLEDATA table) { @@ -129,4 +131,13 @@ void tables::plot_table(TABLEDATA table) { trace_printf("%s\n", row); row[0] = 0; } +} + +std::vector tables::col_to_row(TABLEDATA table, uint16_t table_index) { + std::vector out_vec; + + for (auto table_y : table) { + out_vec.push_back(table_y[table_index]); + } + return out_vec; } \ No newline at end of file