Skip to content

Commit

Permalink
added ignition interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
FDSoftware committed Jan 20, 2022
1 parent b341291 commit 43b6b9a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 49 deletions.
1 change: 1 addition & 0 deletions Inc/templates/CHT_1_6_.h
Expand Up @@ -15,6 +15,7 @@
#undef PMSI
#undef AVCI
#undef ECNT
#undef MAX_RPM

/*-----( Globales )-----*/

Expand Down
15 changes: 7 additions & 8 deletions Inc/variables.h
@@ -1,21 +1,20 @@
/*-----( Variables Globales )-----*/
// estas variables las puede usar cualquieeer funcion
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

#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 )-----*/

Expand Down
22 changes: 12 additions & 10 deletions Src/ignition/include/ignition.hpp
Expand Up @@ -6,20 +6,22 @@
#define IGNITION_HPP

#include <limits.h>
#include <vector>
#include <stdint.h>
#include <vector>

#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

Expand Down
60 changes: 37 additions & 23 deletions Src/ignition/src/ignition.cpp
Expand Up @@ -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,
*/
*/
4 changes: 2 additions & 2 deletions Src/main.cpp
Expand Up @@ -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 -----------------------------------------------------------*/
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Src/memory/include/tables.hpp
Expand Up @@ -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>, int32_t);

int16_t find_nearest_neighbor(std::vector<int32_t>, int32_t);
std::vector<int32_t> col_to_row(TABLEDATA, uint16_t);
/**
* @brief erases page on memory and record new data of table
*/
Expand Down
19 changes: 15 additions & 4 deletions Src/memory/src/tables.cpp
Expand Up @@ -109,14 +109,16 @@ void tables::update_table(TABLEDATA data, table_ref table) {
free(buffer);
}

int32_t tables::find_nearest_neighbor(std::vector<int32_t> vec,
int16_t tables::find_nearest_neighbor(std::vector<int32_t> 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) {
Expand All @@ -129,4 +131,13 @@ void tables::plot_table(TABLEDATA table) {
trace_printf("%s\n", row);
row[0] = 0;
}
}

std::vector<int32_t> tables::col_to_row(TABLEDATA table, uint16_t table_index) {
std::vector<int32_t> out_vec;

for (auto table_y : table) {
out_vec.push_back(table_y[table_index]);
}
return out_vec;
}

0 comments on commit 43b6b9a

Please sign in to comment.