Skip to content

Commit

Permalink
Revised test framework and added VCU tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphuebner committed Jul 27, 2023
1 parent 604b428 commit b01abcc
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 126 deletions.
2 changes: 2 additions & 0 deletions include/vehiclecontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class VehicleControl
static uint16_t bmwAdcValues[4];
static FunctionPointerCallback callback;
static uint32_t lastCanRxTime;
static uint8_t canErrors;
static uint8_t seqCounter;

static void GetTemps(float& tmphs, float &tmpm);
static float GetUserThrottleCommand();
Expand Down
19 changes: 11 additions & 8 deletions src/vehiclecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ float VehicleControl::tempmFiltered = 0;
int VehicleControl::udcFiltered = 0;
uint16_t VehicleControl::bmwAdcNextChan = 0;
uint16_t VehicleControl::bmwAdcValues[4];
uint8_t VehicleControl::canErrors;
uint8_t VehicleControl::seqCounter;

void VehicleControl::SetCan(CanHardware* canHw)
{
seqCounter = 0; //Mainly useful for unit tests
canErrors = 0;
can = canHw;
can->AddReceiveCallback(&callback);
CanClear();
Expand All @@ -66,7 +70,6 @@ void VehicleControl::CanClear()
bool VehicleControl::CanReceive(uint32_t canId, uint32_t data[2])
{
const int maxErrors = 5;
static uint8_t errors = 0, lastCounter = 0;

if (canId != (uint32_t)Param::GetInt(Param::controlid)) return false;

Expand All @@ -90,24 +93,24 @@ bool VehicleControl::CanReceive(uint32_t canId, uint32_t data[2])
if (calcCrc != crc)
{
ErrorMessage::Post(ERR_CANCRC);
if (errors < maxErrors) errors++;
if (canErrors < maxErrors) canErrors++;
}
else if (ctr1 != ctr2 || //The two counters within the message don't match up
ctr1 == lastCounter) //The counters match but haven't moved since the last message
ctr1 == seqCounter) //The counters match but haven't moved since the last message
{
ErrorMessage::Post(ERR_CANCOUNTER);
if (errors < maxErrors) errors++;
if (canErrors < maxErrors) canErrors++;
}
else if (errors > 0 && errors < maxErrors)
else if (canErrors > 0 && canErrors < maxErrors)
{
//As long as we haven't reached maxErrors, good frames decrease the error counter
errors--;
canErrors--;
}

lastCounter = ctr1;
seqCounter = ctr1;

//once we've reached maxerrors we cannot recover, inverter needs restarting.
if (errors < maxErrors)
if (canErrors < maxErrors)
{
//This lets consuming functions check the age of the last valid frame
lastCanRxTime = rtc_get_counter_val();
Expand Down
7 changes: 4 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ CC = gcc
CPP = g++
LD = g++
CP = cp
CFLAGS = -std=c99 -g -I../include -I../libopeninv/include
CPPFLAGS = -g -I../include -I../libopeninv/include
CFLAGS = -std=c99 -ggdb -DSTM32F1 -I../include -I../libopeninv/include -I../libopencm3/include
CPPFLAGS = -ggdb -DSTM32F1 -DCONTROL_FOC=1 -DCONTROL_SINE=0 -DCONTROL=DCONTROL_FOC -I../include -I../libopeninv/include -I../libopencm3/include
LDFLAGS = -g
BINARY = test_sine
OBJS = test_main.o fu.o test_fu.o test_fp.o my_fp.o my_string.o test_throttle.o throttle.o sine_core.o
OBJS = test_main.o fu.o test_fu.o test_fp.o test_vcu.o my_fp.o my_string.o params.o vehiclecontrol.o \
test_throttle.o throttle.o sine_core.o temp_meas.o
VPATH = ../src ../libopeninv/src

all: $(BINARY)
Expand Down
24 changes: 18 additions & 6 deletions test/test.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
#include <iostream>
#include <list>

class IUnitTest
typedef void (*VoidFunction)();

class UnitTest
{
public:
virtual ~IUnitTest() {}
virtual void RunTest() = 0;
public:
UnitTest(const std::list<VoidFunction>*);
virtual void TestSetup() {}
virtual void TestCaseSetup() {}
const std::list<VoidFunction> GetCases() { return *_cases; }
void SetTestCaseList(const std::list<VoidFunction>* cases) { _cases = cases; }

private:
const std::list<VoidFunction>* _cases;
};

extern int _failedAssertions;


#define REGISTER_TEST(t, ...) static UnitTest* test = new t (new std::list<VoidFunction> { __VA_ARGS__ });

#define STRING(s) #s
#define ASSERT(c) \
if (c) \
cout << "Test " << __FILE__ << "::" << __func__ << " passed." << endl; \
std::cout << "Test " << __FILE__ << "::" << __func__ << " passed." << std::endl; \
else \
{ \
cout << "Assertion failed: " << STRING(c) << " in " __FILE__ " : " << __LINE__ << endl; \
std::cout << "Assertion failed: " << STRING(c) << " in " __FILE__ " : " << __LINE__ << std::endl; \
_failedAssertions++; \
}

Expand Down
28 changes: 10 additions & 18 deletions test/test_fp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
#include "my_fp.h"
#include "my_math.h"
#include "sine_core.h"
#include "test_list.h"
#include "test.h"
#include "string.h"

using namespace std;
class FPTest: public UnitTest
{
public:
FPTest(const std::list<VoidFunction>* cases): UnitTest(cases) {}
};

static void TestMacros()
{
Expand All @@ -46,7 +50,7 @@ static void TestItoa()
static void TestAtoi()
{
ASSERT(fp_atoi("-2.5", 5) == FP_FROMFLT(-2.5));
ASSERT(fp_atoi("2.155", 5) == FP_FROMFLT(2.15));
ASSERT(fp_atoi("2.155", 5) == FP_FROMFLT(2.16));
}

static void TestMedian3()
Expand All @@ -68,19 +72,7 @@ static void TestAtan2()
ASSERT(SineCore::Atan2(2048, 3547) == 10922); //60°
}

static void TestLn()
{
//ASSERT(fp_ln(1) == 0);
ASSERT(fp_ln(5389) == FP_FROMFLT(8.5777));
ASSERT(fp_ln(8290) == FP_FROMFLT(9.0));
}
//This line registers the test
REGISTER_TEST(FPTest, TestMacros, TestItoa, TestAtoi, TestMedian3, TestAtan2);


void FPTest::RunTest()
{
TestMacros();
TestItoa();
TestAtoi();
TestMedian3();
TestAtan2();
TestLn();
}
38 changes: 18 additions & 20 deletions test/test_fu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,42 @@

#include "fu.h"
#include "my_fp.h"
#include "test_list.h"
#include "test.h"

using namespace std;
class FUTest: public UnitTest
{
public:
FUTest(const std::list<VoidFunction>* cases): UnitTest(cases) {}
};

static void Setup(u32fp fweak, int boost, u32fp fmin)
static void Setup(float fweak, int boost)
{
MotorVoltage::SetMaxAmp(10000);
MotorVoltage::SetBoost(boost);
MotorVoltage::SetWeakeningFrq(fweak);
//MotorVoltage::SetMinFrq(fmin);
MotorVoltage::SetMaxAmp(10000);
}

static void TestBoost1()
{
Setup(FP_FROMINT(10), 1000, FP_FROMFLT(0));
ASSERT(MotorVoltage::GetAmp(0)==1000);
Setup(10, 1000);
ASSERT(MotorVoltage::GetAmp(FP_FROMINT(1))==1900);
}

static void TestBoost2()
{
Setup(FP_FROMINT(10), 1000, FP_FROMFLT(1));
ASSERT(MotorVoltage::GetAmp(0.5)==0);
Setup(10, 1000);
ASSERT(MotorVoltage::GetAmp(FP_FROMFLT(0.1))==0);
}

static void TestFU1()
{
Setup(FP_FROMINT(10), 0, FP_FROMFLT(1));
Setup(10, 0);
ASSERT(MotorVoltage::GetAmp(FP_FROMFLT(5))==(5.0/10.0f * 10000));
}

static void TestFU2()
{
Setup(FP_FROMINT(10), 1000, FP_FROMFLT(1));
Setup(10, 1000);
ASSERT(MotorVoltage::GetAmp(FP_FROMFLT(5))==(1000 + 5.0/10.0f * (10000-1000)));
ASSERT(MotorVoltage::GetAmp(FP_FROMFLT(9.5))==(1000 + 9.5/10.0f * (10000-1000)));
ASSERT(MotorVoltage::GetAmp(FP_FROMFLT(10))==10000);
Expand All @@ -62,16 +65,11 @@ static void TestFU2()

static void TestFUPerc()
{
Setup(FP_FROMINT(10), 1000, FP_FROMFLT(1));
Setup(10, 1000);
ASSERT(MotorVoltage::GetAmpPerc(FP_FROMFLT(5), FP_FROMFLT(50))==((1000 + 5.0/10.0f * (10000-1000))/2));
ASSERT(MotorVoltage::GetAmpPerc(FP_FROMFLT(22), FP_FROMFLT(50))==10000);
}

void FUTest::RunTest()
{
TestBoost1();
TestBoost2();
TestFU1();
TestFU2();
TestFUPerc();
}
//This line registers the test
REGISTER_TEST(FUTest, TestBoost1, TestBoost2, TestFU1, TestFU2, TestFUPerc);

54 changes: 0 additions & 54 deletions test/test_list.h

This file was deleted.

25 changes: 17 additions & 8 deletions test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,28 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <list>
#include "test.h"
#define EXPORT_TESTLIST
#include "test_list.h"

using namespace std;

int _failedAssertions = 0;
int testIdx = 0;
list<UnitTest*> testList;

int main()
{
int dummy;
IUnitTest** currentTest = testList;

cout << "Starting unit Tests" << endl;

while (*currentTest)
for (UnitTest* currentTest: testList)
{
(*currentTest)->RunTest();
currentTest++;
bool allTestsRun = false;

for (VoidFunction testCase: currentTest->GetCases())
{
currentTest->TestCaseSetup();
testCase();
}
}

if (_failedAssertions > 0)
Expand All @@ -50,3 +53,9 @@ int main()

return 0;
}

UnitTest::UnitTest(const std::list<VoidFunction>* cases)
: _cases(cases)
{
testList.push_back(this);
}
18 changes: 9 additions & 9 deletions test/test_throttle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@

#include "my_fp.h"
#include "my_math.h"
#include "test_list.h"
#include "test.h"
#include "throttle.h"

using namespace std;
class ThrottleTest: public UnitTest
{
public:
ThrottleTest(const std::list<VoidFunction>* cases): UnitTest(cases) {}
};

static void TestSetup()
{
Expand Down Expand Up @@ -128,10 +132,6 @@ static void TestDualThrottle()
Throttle::potmax[1] = 4000;
}
#endif
void ThrottleTest::RunTest()
{
TestSetup();
TestBrkPedal();
TestRegen();
//TestDualThrottle();
}

//This line registers the test
REGISTER_TEST(ThrottleTest, TestSetup);
Loading

0 comments on commit b01abcc

Please sign in to comment.