Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jsphuebner/libopeninv
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphuebner committed Oct 22, 2024
2 parents 767dc9d + 93e9bcd commit a789ea7
Show file tree
Hide file tree
Showing 24 changed files with 1,695 additions and 41 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/CI-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI
on:
push:
pull_request:

jobs:
build:
name: build-linux
runs-on: ubuntu-latest

steps:
- name: Checkout libopeninv
uses: actions/checkout@v4
with:
path: libopeninv

- name: Checkout libopencm3
uses: actions/checkout@v4
with:
repository: jsphuebner/libopencm3
path: libopencm3

- name: Build unit tests on host
run: |
make -C libopeninv/test clean all CAN_SIGNED=0
- name: Run unit tests on host
run: |
libopeninv/test/test_libopeninv
- name: Build unit tests on host (Signed CAN receive)
run: |
make -C libopeninv/test clean all CAN_SIGNED=1
- name: Run unit tests on host (Signed CAN receive)
run: |
libopeninv/test/test_libopeninv
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
test/test_libopeninv
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# libopeninv

[![Build status](../../actions/workflows/CI-build.yml/badge.svg)](../../actions/workflows/CI-build.yml)

Generic modules that can be used in many projects
6 changes: 3 additions & 3 deletions include/canhardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
class CanCallback
{
public:
virtual bool HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) = 0;
virtual void HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) = 0;
virtual void HandleClear() = 0;
};

class FunctionPointerCallback: public CanCallback
{
public:
FunctionPointerCallback(bool (*r)(uint32_t, uint32_t*, uint8_t), void (*c)()) : recv(r), clear(c) { };
bool HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) { return recv(canId, data, dlc); }
void HandleClear() { clear(); }
void HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) override { recv(canId, data, dlc); }
void HandleClear() override { clear(); }

private:
bool (*recv)(uint32_t, uint32_t*, uint8_t);
Expand Down
4 changes: 2 additions & 2 deletions include/canmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class CanMap: CanCallback

CanMap(CanHardware* hw, bool loadFromFlash = true);
CanHardware* GetHardware() { return canHardware; }
void HandleClear();
bool HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc);
void HandleClear() override;
void HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) override;
void Clear();
void SendAll();
int AddSend(Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain);
Expand Down
4 changes: 2 additions & 2 deletions include/canobd2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class CanObd2: CanCallback
public:
/** Default constructor */
CanObd2(CanHardware* hw);
void HandleClear();
bool HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc);
void HandleClear() override;
void HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) override;
void SetNodeId(uint8_t id);

private:
Expand Down
2 changes: 1 addition & 1 deletion include/cansdo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CanSdo: CanCallback, public IPutChar
/** Default constructor */
CanSdo(CanHardware* hw, CanMap* cm = 0);
void HandleClear();
bool HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc);
void HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc) override;
void SDOWrite(uint8_t nodeId, uint16_t index, uint8_t subIndex, uint32_t data);
void SDORead(uint8_t nodeId, uint16_t index, uint8_t subIndex);
bool SDOReadReply(uint32_t& data);
Expand Down
2 changes: 1 addition & 1 deletion include/picontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PiController
* \param curVal currently measured value
* \return new actuator value
*/
int32_t Run(s32fp curVal);
int32_t Run(s32fp curVal, int32_t feedForward = 0);

/** Run controller to obtain a new actuator value, run only proportional part
* \param curVal currently measured value
Expand Down
7 changes: 3 additions & 4 deletions src/canhardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
class NullCallback: public CanCallback
{
public:
bool HandleRx(uint32_t, uint32_t*, uint8_t) { return false; }
void HandleClear() { }
void HandleRx(uint32_t, uint32_t*, uint8_t) override {}
void HandleClear() override { }
};


Expand Down Expand Up @@ -96,7 +96,6 @@ void CanHardware::HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc)
{
for (int i = 0; i < nextCallbackIndex; i++)
{
if (recvCallback[i]->HandleRx(canId, data, dlc))
break;
recvCallback[i]->HandleRx(canId, data, dlc);
}
}
36 changes: 17 additions & 19 deletions src/canmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,16 @@ void CanMap::HandleClear()
}
}

bool CanMap::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
void CanMap::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
{
if (isSaving) return false; //Only handle mapped messages when not currently saving to flash
if (isSaving) return; //Only handle mapped messages when not currently saving to flash

CANIDMAP *recvMap = FindById(canRecvMap, canId);

if (0 != recvMap)
{
forEachPosMap(curPos, recvMap)
{
float val;
uint32_t word;
uint8_t pos = curPos->offsetBits;
uint8_t numBits = ABS(curPos->numBits);
Expand Down Expand Up @@ -138,22 +137,24 @@ bool CanMap::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
uint32_t mask = (1L << numBits) - 1;
word = (word >> pos) & mask;

// sign-extend our arbitrary sized integer out to 32-bits but only if
// it is bigger than a single bit
int32_t ival;
#if CAN_SIGNED
if (numBits > 1)
{
uint32_t sign_bit = 1L << (numBits - 1);
ival = static_cast<int32_t>(((word + sign_bit) & mask)) - sign_bit;
}
else
// sign-extend our arbitrary sized integer out to 32-bits but only if
// it is bigger than a single bit
int32_t ival;
if (numBits > 1)
{
uint32_t sign_bit = 1L << (numBits - 1);
ival = static_cast<int32_t>(((word + sign_bit) & mask)) - sign_bit;
}
else
{
ival = word;
}
float val = ival;
#else
float val = word;
#endif
{
ival = word;
}

val = ival;
val += curPos->offset;
val *= curPos->gain;

Expand All @@ -162,10 +163,7 @@ bool CanMap::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
else
Param::SetFloat((Param::PARAM_NUM)curPos->mapParam, val);
}
return true;
}

return false;
}

/** \brief Clear all defined messages
Expand Down
4 changes: 1 addition & 3 deletions src/canobd2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ void CanObd2::HandleClear()
canHardware->RegisterUserMessage(OBD2_PID_REQUEST + nodeId); // ECU specific address
}

bool CanObd2::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
void CanObd2::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
{
if ((canId == OBD2_PID_REQUEST) || (canId == (OBD2_PID_REQUEST + nodeId))) //OBD2 request
{
ProcessOBD2(data);
return true;
}
return false;
}

void CanObd2::SetNodeId(uint8_t id)
Expand Down
5 changes: 1 addition & 4 deletions src/cansdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,17 @@ void CanSdo::HandleClear()
canHardware->RegisterUserMessage(SDO_REP_ID_BASE + remoteNodeId);
}

bool CanSdo::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
void CanSdo::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
{
if (canId == (SDO_REQ_ID_BASE + nodeId)) //SDO request
{
ProcessSDO(data);
return true;
}
else if (canId == (SDO_REP_ID_BASE + remoteNodeId))
{
sdoReplyValid = (data[0] & 0xFF) != SDO_ABORT;
sdoReplyData = data[1];
return true;
}
return false;
}

void CanSdo::SDOWrite(uint8_t nodeId, uint16_t index, uint8_t subIndex, uint32_t data)
Expand Down
4 changes: 2 additions & 2 deletions src/picontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PiController::PiController()
{
}

int32_t PiController::Run(s32fp curVal)
int32_t PiController::Run(s32fp curVal, int32_t feedForward)
{
s32fp err = refVal - curVal;
esum += err;
Expand All @@ -33,7 +33,7 @@ int32_t PiController::Run(s32fp curVal)
esum = MIN(esum, maxSum);
esum = MAX(esum, minSum);

int32_t y = FP_TOINT(err * kp + (esum / frequency) * ki);
int32_t y = feedForward + FP_TOINT(err * kp + (esum / frequency) * ki);
int32_t ylim = MAX(y, minY);
ylim = MIN(ylim, maxY);

Expand Down
34 changes: 34 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Option to allow signed reception of CAN variables
CAN_SIGNED ?= 0

CC = gcc
CPP = g++
LD = g++
CFLAGS = -std=c99 -ggdb -DSTM32F1 -DCAN_SIGNED=$(CAN_SIGNED) -Itest-include -I../include -I../../libopencm3/include
CPPFLAGS = -ggdb -DSTM32F1 -DCAN_SIGNED=$(CAN_SIGNED) -Itest-include -I../include -I../../libopencm3/include
LDFLAGS = -g
BINARY = test_libopeninv
OBJS = test_main.o fu.o test_fu.o test_fp.o my_fp.o my_string.o params.o \
stub_canhardware.o test_canmap.o canmap.o \
stub_libopencm3.o
VPATH = ../src ../libopeninv/src

# Check if the variable GITHUB_RUN_NUMBER exists. When running on the github actions running, this
# variable is automatically available.
# Create a compiler define with the content of the variable. Or, if it does not exist, use replacement value 99999.
CPPFLAGS += $(shell \
if [ -z "$$GITHUB_RUN_NUMBER" ]; then echo "-DGITHUB_RUN_NUMBER=0"; else echo "-DGITHUB_RUN_NUMBER=$$GITHUB_RUN_NUMBER"; fi )

all: $(BINARY)

$(BINARY): $(OBJS)
$(LD) $(LDFLAGS) -o $(BINARY) $(OBJS)

%.o: ../%.cpp
$(CPP) $(CPPFLAGS) -o $@ -c $<

%.o: ../%.c
$(CC) $(CFLAGS) -o $@ -c $<

clean:
rm -f $(OBJS) $(BINARY)
45 changes: 45 additions & 0 deletions test/stub_canhardware.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of the stm32-sine project.
*
* Copyright (C) 2021 Johannes Huebner <dev@johanneshuebner.com>
* Copyright (C) 2024 David J. Fiddes <D.J@fiddes.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stub_canhardware.h"

CanCallback* vcuCan = nullptr;
uint32_t vcuCanId;

CanHardware::CanHardware()
{}

bool CanHardware::AddCallback(CanCallback* cb)
{
vcuCan = cb;
return true;
}

bool CanHardware::RegisterUserMessage(uint32_t canId, uint32_t mask)
{
vcuCanId = canId;
return true;
}

void CanHardware::ClearUserMessages() {}

void CanHardware::HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc)
{
vcuCan->HandleRx(canId, data, dlc);
}
48 changes: 48 additions & 0 deletions test/stub_canhardware.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of the stm32-sine project.
*
* Copyright (C) 2021 Johannes Huebner <dev@johanneshuebner.com>
* Copyright (C) 2024 David J. Fiddes <D.J@fiddes.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TEST_CANHARDWARE_H
#define TEST_CANHARDWARE_H

#include "canhardware.h"
#include <stdint.h>
#include <string.h>
#include <array>

class CanStub: public CanHardware
{
void SetBaudrate(enum baudrates baudrate) {}
void Send(uint32_t canId, uint32_t data[2], uint8_t len)
{
m_canId = canId;
memcpy(&m_data[0], &data[0], sizeof(m_data));
m_len = len;
}
virtual void ConfigureFilters() {}

public:
std::array<uint8_t, 8> m_data;
uint8_t m_len;
uint32_t m_canId;
};

extern CanCallback* vcuCan;
extern uint32_t vcuCanId;

#endif // TEST_CANHARDWARE_H
Loading

0 comments on commit a789ea7

Please sign in to comment.