Skip to content

Commit

Permalink
angleToTime tests (speeduino#790)
Browse files Browse the repository at this point in the history
* angleToTime tests

* Change calculation to work around integer rounding
  • Loading branch information
DeionSi authored Feb 13, 2022
1 parent c1db691 commit 323c938
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 9 deletions.
11 changes: 3 additions & 8 deletions speeduino/crankMaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ unsigned long angleToTime(int16_t, byte);
uint16_t timeToAngle(unsigned long, byte);
void doCrankSpeedCalcs();

volatile uint16_t timePerDegree;
volatile uint16_t timePerDegreex16;
volatile uint16_t degreesPeruSx2048;
volatile unsigned long degreesPeruSx32768;

//These are only part of the experimental 2nd deriv calcs
byte deltaToothCount = 0; //The last tooth that was used with the deltaV calc
int rpmDelta;
extern volatile uint16_t timePerDegree;
extern volatile uint16_t timePerDegreex16;
extern volatile unsigned long degreesPeruSx32768;

#endif
11 changes: 10 additions & 1 deletion speeduino/crankMaths.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#include "decoders.h"
#include "timers.h"

volatile uint16_t timePerDegree;
volatile uint16_t timePerDegreex16;
volatile uint16_t degreesPeruSx2048;
volatile unsigned long degreesPeruSx32768;

//These are only part of the experimental 2nd deriv calcs
byte deltaToothCount = 0; //The last tooth that was used with the deltaV calc
int rpmDelta;

/*
* Converts a crank angle into a time from or since that angle occurred.
* Positive angles are assumed to be in the future, negative angles in the past:
Expand Down Expand Up @@ -33,7 +42,7 @@ unsigned long angleToTime(int16_t angle, byte method)
unsigned long toothTime = (toothLastToothTime - toothLastMinusOneToothTime);
interrupts();

returnTime = ( (toothTime / triggerToothAngle) * angle );
returnTime = ( (toothTime * angle) / triggerToothAngle );
}
else { returnTime = angleToTime(angle, CRANKMATH_METHOD_INTERVAL_REV); } //Safety check. This can occur if the last tooth seen was outside the normal pattern etc
}
Expand Down
30 changes: 30 additions & 0 deletions test/test_misc2/test_misc2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <Arduino.h>
#include <unity.h>

#include "tests_crankmaths.h"

#define UNITY_EXCLUDE_DETAILS

void setup()
{
pinMode(LED_BUILTIN, OUTPUT);

// NOTE!!! Wait for >2 secs
// if board doesn't support software reset via Serial.DTR/RTS
delay(2000);

UNITY_BEGIN(); // IMPORTANT LINE!

testCrankMaths();

UNITY_END(); // stop unit testing
}

void loop()
{
// Blink to indicate end of test
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
87 changes: 87 additions & 0 deletions test/test_misc2/tests_crankmaths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "globals.h"
#include "crankMaths.h"
#include "unity.h"
#include "decoders.h"

struct crankmaths_rev_testdata {
uint16_t rpm;
unsigned long revolutionTime;
uint16_t angle;
unsigned long expected;
} *crankmaths_rev_testdata_current;

struct crankmaths_tooth_testdata {
uint16_t rpm;
uint16_t triggerToothAngle;
unsigned long toothTime;
uint16_t angle;
unsigned long expected;
} *crankmaths_tooth_testdata_current;

void test_crankmaths_angletotime_revolution_execute() {
crankmaths_rev_testdata *testdata = crankmaths_rev_testdata_current;
revolutionTime = testdata->revolutionTime;
TEST_ASSERT_EQUAL(testdata->expected, angleToTime(testdata->angle, CRANKMATH_METHOD_INTERVAL_REV));
}

void test_crankmaths_angletotime_tooth_execute() {
crankmaths_tooth_testdata *testdata = crankmaths_tooth_testdata_current;
triggerToothAngle = testdata->triggerToothAngle;
toothLastToothTime = toothLastMinusOneToothTime + testdata->toothTime;
TEST_ASSERT_EQUAL(testdata->expected, angleToTime(testdata->angle, CRANKMATH_METHOD_INTERVAL_TOOTH));
}

void testCrankMaths()
{
const byte testNameLength = 200;
char testName[testNameLength];

const crankmaths_rev_testdata crankmaths_rev_testdatas[] = {
{ .rpm = 50, .revolutionTime = 1200000, .angle = 0, .expected = 0 },
{ .rpm = 50, .revolutionTime = 1200000, .angle = 25, .expected = 83333 }, // 83333,3333
{ .rpm = 50, .revolutionTime = 1200000, .angle = 720, .expected = 2400000 },
{ .rpm = 2500, .revolutionTime = 24000, .angle = 0, .expected = 0 },
{ .rpm = 2500, .revolutionTime = 24000, .angle = 25, .expected = 1666 }, // 1666,6666
{ .rpm = 2500, .revolutionTime = 24000, .angle = 720, .expected = 48000 },
{ .rpm = 20000, .revolutionTime = 3000, .angle = 0, .expected = 0 },
{ .rpm = 20000, .revolutionTime = 3000, .angle = 25, .expected = 208 }, // 208,3333
{ .rpm = 20000, .revolutionTime = 3000, .angle = 720, .expected = 6000 }
};

for (auto testdata : crankmaths_rev_testdatas) {
crankmaths_rev_testdata_current = &testdata;
snprintf(testName, testNameLength, "crankmaths/angletotime/revolution/%urpm/%uangle", testdata.rpm, testdata.angle);
UnityDefaultTestRun(test_crankmaths_angletotime_revolution_execute, testName, __LINE__);
}

const crankmaths_tooth_testdata crankmaths_tooth_testdatas[] = {
{ .rpm = 50, .triggerToothAngle = 3, .toothTime = 10000, .angle = 0, .expected = 0 },
{ .rpm = 50, .triggerToothAngle = 3, .toothTime = 10000, .angle = 25, .expected = 83333 }, // 83333,3333
{ .rpm = 50, .triggerToothAngle = 3, .toothTime = 10000, .angle = 720, .expected = 2400000 },
{ .rpm = 2500, .triggerToothAngle = 3, .toothTime = 200, .angle = 0, .expected = 0 },
{ .rpm = 2500, .triggerToothAngle = 3, .toothTime = 200, .angle = 25, .expected = 1666 }, // 1666,6666
{ .rpm = 2500, .triggerToothAngle = 3, .toothTime = 200, .angle = 720, .expected = 48000 },
{ .rpm = 20000, .triggerToothAngle = 3, .toothTime = 25, .angle = 0, .expected = 0 },
{ .rpm = 20000, .triggerToothAngle = 3, .toothTime = 25, .angle = 25, .expected = 208 }, // 208,3333
{ .rpm = 20000, .triggerToothAngle = 3, .toothTime = 25, .angle = 720, .expected = 6000 },
{ .rpm = 50, .triggerToothAngle = 180, .toothTime = 600000, .angle = 0, .expected = 0 },
{ .rpm = 50, .triggerToothAngle = 180, .toothTime = 600000, .angle = 25, .expected = 83333 }, // 83333,3333
{ .rpm = 50, .triggerToothAngle = 180, .toothTime = 600000, .angle = 720, .expected = 2400000 },
{ .rpm = 2500, .triggerToothAngle = 180, .toothTime = 12000, .angle = 0, .expected = 0 },
{ .rpm = 2500, .triggerToothAngle = 180, .toothTime = 12000, .angle = 25, .expected = 1666 }, // 1666,6666
{ .rpm = 2500, .triggerToothAngle = 180, .toothTime = 12000, .angle = 720, .expected = 48000 },
{ .rpm = 20000, .triggerToothAngle = 180, .toothTime = 1500, .angle = 0, .expected = 0 },
{ .rpm = 20000, .triggerToothAngle = 180, .toothTime = 1500, .angle = 25, .expected = 208 }, // 208,3333
{ .rpm = 20000, .triggerToothAngle = 180, .toothTime = 1500, .angle = 720, .expected = 6000 },
};
// The same for all tests
triggerToothAngleIsCorrect = true;
toothLastMinusOneToothTime = 200000;

for (auto testdata : crankmaths_tooth_testdatas) {
crankmaths_tooth_testdata_current = &testdata;
snprintf(testName, testNameLength, "crankmaths/angletotime/tooth/%urpm/%uangle/%utoothangle", testdata.rpm, testdata.angle, testdata.triggerToothAngle);
UnityDefaultTestRun(test_crankmaths_angletotime_tooth_execute, testName, __LINE__);
}

}
1 change: 1 addition & 0 deletions test/test_misc2/tests_crankmaths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void testCrankMaths();

0 comments on commit 323c938

Please sign in to comment.