trigger_adc unit tests

This commit is contained in:
rusefillc 2021-12-01 13:35:44 -05:00
parent 99c4dd4cb4
commit a13bc09fe4
8 changed files with 1132 additions and 1 deletions

View File

@ -10,6 +10,7 @@
#include "trigger_structure.h"
#include "trigger_central.h"
#include "trigger_input.h"
#define TRIGGER_BAIL_IF_DISABLED \
if (!engine->hwTriggerInputEnabled) { \

View File

@ -24,4 +24,8 @@ static inline bool isAdcChannelValid(adc_channel_e hwChannel) {
}
}
#define adcToVoltsDivided(adc) (adcToVolts(adc) * engineConfiguration->analogInputDividerCoefficient)
#define GPT_FREQ_FAST 100000
#define GPT_PERIOD_FAST 10
#include "boards.h"

View File

@ -18,6 +18,8 @@ typedef uint32_t iomode_t;
typedef uint32_t ioportid_t;
typedef uint32_t ioportmask_t;
typedef uint16_t triggerAdcSample_t;
#define DL_OUTPUT_BUFFER 200
// just a stub implementation for unit tests

View File

@ -15,6 +15,12 @@ static char* trim(char *str) {
return str;
}
CsvReader::~CsvReader() {
if (fp) {
fclose(fp);
}
}
void CsvReader::open(const char *fileName, const int* columnIndeces) {
printf("Reading from %s\r\n", fileName);
fp = fopen(fileName, "r");
@ -33,6 +39,21 @@ bool CsvReader::haveMore() {
return result;
}
double CsvReader::readTimestampAndValues(double *v) {
const char s[2] = ",";
char *line = buffer;
char *timeStampstr = trim(strtok(line, s));
double timeStamp = std::stod(timeStampstr);
for (size_t i = 0; i < m_triggerCount; i++) {
char *triggerToken = trim(strtok(NULL, s));
v[i] = std::stod(triggerToken);
}
return timeStamp;
}
void CsvReader::processLine(EngineTestHelper *eth) {
Engine *engine = &eth->engine;

View File

@ -13,11 +13,13 @@ public:
, m_timestampOffset(timestampOffset)
{
}
~CsvReader();
void open(const char *fileName, const int* columnIndeces);
bool haveMore();
void processLine(EngineTestHelper *eth);
void readLine(EngineTestHelper *eth);
double readTimestampAndValues(double *v);
int lineIndex() const {
return m_lineIndex;
@ -28,7 +30,7 @@ private:
const size_t m_vvtCount;
const double m_timestampOffset;
FILE *fp;
FILE *fp = nullptr;
char buffer[255];
bool currentState[2] = {0, 0};

View File

@ -3,6 +3,7 @@ TESTS_SRC_CPP = \
tests/trigger/test_trigger_decoder.cpp \
tests/trigger/test_trigger_noiseless.cpp \
tests/trigger/test_trigger_multi_sync.cpp \
tests/trigger/test_trigger_input_adc.cpp \
tests/trigger/test_miata_na_tdc.cpp \
tests/trigger/test_cam_vvt_input.cpp \
tests/trigger/test_2jz_vvt.cpp \

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,100 @@
/**
* @file test_trigger_input_adc.cpp
*
* @date Jul 24, 2021
*/
#include "pch.h"
#include "engine_test_helper.h"
#include "trigger_decoder.h"
#include "engine_math.h"
#include "allsensors.h"
#include "rpm_calculator.h"
#include "event_queue.h"
#include "trigger_central.h"
#include "main_trigger_callback.h"
#include "engine.h"
#include "advance_map.h"
#include "speed_density.h"
#include "fuel_math.h"
#include "spark_logic.h"
#include "trigger_universal.h"
#include "trigger_input_adc.h"
#include "logicdata_csv_reader.h"
extern TriggerAdcDetector trigAdcState;
void setTriggerAdcMode(triggerAdcMode_t adcMode) {
trigAdcState.curAdcMode = adcMode;
}
void onTriggerChanged(efitick_t stamp, bool isPrimary, bool isRising) {
printf("*\r\n");
}
static void simulateTrigger(TriggerAdcDetector &trigAdcState, CsvReader &reader, float voltageDiv, float adcMaxVoltage) {
static const float Vil = 0.3f * adcMaxVoltage;
static const float Vih = 0.7f * adcMaxVoltage;
int prevLogicValue = -1;
while (reader.haveMore()) {
double value = 0;
double stamp = reader.readTimestampAndValues(&value);
efitick_t stampUs = (efitick_t)(stamp * 1'000'000);
printf("-- %lld %f\r\n", stamp, (float)value);
// convert into mcu-adc voltage
value = minF(maxF(value / voltageDiv, 0), adcMaxVoltage);
if (trigAdcState.curAdcMode == TRIGGER_ADC_EXTI) {
int logicValue = 0;
// imitate Schmitt trigger input
if (value < Vil || value > Vih) {
logicValue = value > Vih;
// we need at least two values to detect an edge
if (prevLogicValue != -1) {
printf("--> DIGITAL %d %d\r\n", logicValue, prevLogicValue);
trigAdcState.digitalCallback(stampUs, true, logicValue > prevLogicValue ? true : false);
}
prevLogicValue = logicValue;
}
} else if (trigAdcState.curAdcMode == TRIGGER_ADC_ADC) {
triggerAdcSample_t sampleValue = value * ADC_MAX_VALUE / adcMaxVoltage;
printf("--> ANALOG %d\r\n", sampleValue);
trigAdcState.analogCallback(stampUs, sampleValue);
}
}
}
TEST(big, testTriggerInputAdc) {
printf("====================================================================================== testTriggerInputAdc\r\n");
EngineTestHelper eth(TEST_ENGINE);
engineConfiguration->ignitionMode = IM_WASTED_SPARK;
engineConfiguration->useOnlyRisingEdgeForTrigger = true;
engineConfiguration->adcVcc = 3.3f;
engineConfiguration->analogInputDividerCoefficient = 2.0f;
// we'll test on 60-2 wheel
eth.setTriggerType(TT_TOOTHED_WHEEL_60_2);
ASSERT_EQ(0, engine->triggerCentral.triggerState.totalTriggerErrorCounter);
ASSERT_EQ(0, GET_RPM()) << "testTriggerInputAdc RPM #1";
trigAdcState.init();
setTriggerAdcMode(TRIGGER_ADC_ADC);
CsvReader reader(1, 0);
int indices[2] = {0, 1};
reader.open("tests/trigger/resources/trigger_adc_1.csv", indices);
simulateTrigger(trigAdcState, reader, 2.0f, 3.3f);
ASSERT_EQ(0, engine->triggerCentral.triggerState.totalTriggerErrorCounter);
ASSERT_EQ(0, GET_RPM()) << "testTriggerInputAdc RPM #2";
}