Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue #201] CAN errors C++ implementation #314

Merged
merged 15 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions firmware/projects/Demo/CANErrors/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Luai Bashar
# November 5, 2024

# The target executable 'main' is created in the master CMakeLists. Do not change its name.
# We only need to add the source code files and include directories.

include("${CMAKE_SOURCE_DIR}/cmake/cangen.cmake")

target_sources(main
PRIVATE
main.cc
)

target_include_directories(main
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)

# Notice that we don't include any mcal/ subdirectory in this CMake file.
# The master CMakeLists handles platform selection and library linking.
13 changes: 13 additions & 0 deletions firmware/projects/Demo/CANErrors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# CANErrors

This is the CAN Errors demo project. This defines the mechanism for reporting errors from the vehicle microcontrollers. This project will send errors over the CAN line and process them on a host machine.

## Generating The DBC Code
luaibash marked this conversation as resolved.
Show resolved Hide resolved
- Code must be generated using CanGen. Install CanGen in the racecar directory with "pip install -e scripts/cangen"
- A venv isn't needed for this!
- Generate the code in the firmware directory with "cangen projects/Demo/CANErrors"

## Building the Project
- To build the project, run the following in the firmware directory "make PROJECT=Demo/CANErrors PLATFORM=cli"
luaibash marked this conversation as resolved.
Show resolved Hide resolved
- This should create a build file with no errors!
- To execute the new build, run "./build/Demo/CANErrors/cli/main.exe"
9 changes: 9 additions & 0 deletions firmware/projects/Demo/CANErrors/bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "shared/comms/can/can_bus.h"

namespace bindings {
extern shared::periph::CanBase& error_can_base;
extern void Initialize();
extern void TickBlocking(uint32_t);
} // namespace bindings
5 changes: 5 additions & 0 deletions firmware/projects/Demo/CANErrors/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
canGen:
busses:
- name: error
node: tms
dbcFile: errors.dbc
39 changes: 39 additions & 0 deletions firmware/projects/Demo/CANErrors/errors.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
VERSION ""

NS_ :
luaibash marked this conversation as resolved.
Show resolved Hide resolved
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_: TMS

// Defines one 64 bit signal where each bit represents a different error
BO_ 390 TMS_ERROR: 8 TMS
SG_ Errors : 0|64@1- (1,0) [0|0] "bitfield" Vector__XXX
luaibash marked this conversation as resolved.
Show resolved Hide resolved
110 changes: 110 additions & 0 deletions firmware/projects/Demo/CANErrors/inc/app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/// @author Luai Bashar
/// @date 2024-11-10
/// @brief Functions and types that will be used in CANErrors main

#pragma once

#include "../generated/can/error_can_messages.h"
#include "shared/comms/can/can_bus.h"

/***************************************************************
App-level objects
***************************************************************/

// Interface to set/send all possible errors for the system
class ErrorHandler {
private:
// Object that holds a 64 bit int, each bit representing an error
generated::can::TMS_ERROR error_msg{};
public:
// Sets the error based on the error index given
void setError(uint32_t error) {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
if (error < 0 || error > 63) {
std::cerr << "Error index must be between 0 and 63!" << std::endl;
luaibash marked this conversation as resolved.
Show resolved Hide resolved
return;
}

error_msg.errors |= (1ULL << error);
}

// Sends the error message through the provided CAN bus
void sendError(shared::can::CanBus error_can_bus) {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
error_can_bus.Send(error_msg);

// Reset after a send to not send duplicate errors
resetError();
}
luaibash marked this conversation as resolved.
Show resolved Hide resolved

// Resets all errors back to untriggered
void resetError() {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
error_msg.errors = 0;
}
};

// Defines all possible errors to set
enum Error {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
Error0 = 0,
Error1 = 1,
Error2 = 2,
Error3 = 3,
Error4 = 4,
Error5 = 5,
Error6 = 6,
Error7 = 7,
Error8 = 8,
Error9 = 9,
Error10 = 10,
Error11 = 11,
Error12 = 12,
Error13 = 13,
Error14 = 14,
Error15 = 15,
Error16 = 16,
Error17 = 17,
Error18 = 18,
Error19 = 19,
Error20 = 20,
Error21 = 21,
Error22 = 22,
Error23 = 23,
Error24 = 24,
Error25 = 25,
Error26 = 26,
Error27 = 27,
Error28 = 28,
Error29 = 29,
Error30 = 30,
Error31 = 31,
Error32 = 32,
Error33 = 33,
Error34 = 34,
Error35 = 35,
Error36 = 36,
Error37 = 37,
Error38 = 38,
Error39 = 39,
Error40 = 40,
Error41 = 41,
Error42 = 42,
Error43 = 43,
Error44 = 44,
Error45 = 45,
Error46 = 46,
Error47 = 47,
Error48 = 48,
Error49 = 49,
Error50 = 50,
Error51 = 51,
Error52 = 52,
Error53 = 53,
Error54 = 54,
Error55 = 55,
Error56 = 56,
Error57 = 57,
Error58 = 58,
Error59 = 59,
Error60 = 60,
Error61 = 61,
Error62 = 62,
Error63 = 63
};
48 changes: 48 additions & 0 deletions firmware/projects/Demo/CANErrors/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// @author Luai Bashar
/// @date 2024-11-05

#include <cstdint>

#include "app.h"
#include "bindings.h"
#include "shared/comms/can/can_bus.h"
#include "generated/can/error_msg_registry.h"

// Initializing can bus that sends the error message
generated::can::ErrorMsgRegistry error_can_registry{};
shared::can::CanBus error_can_bus{
bindings::error_can_base,
error_can_registry,
};

int main(void) {
// Initialize the CLI and milliseconds to repeat by
bindings::Initialize();
uint32_t tick_duration = 100;

// Initialize the error object that sets/sends each error
ErrorHandler error_msg{};

while (true) {
error_can_bus.Update();

// Test #1: Iterate through each error, setting it and sending it
for (int i = 0; i < 63; i++) {
error_msg.setError(i);
error_msg.sendError(error_can_bus);
}

// Test #2: Set random errors using the enum
error_msg.setError(Error0);
error_msg.setError(Error5);
error_msg.setError(Error10);
error_msg.setError(Error15);
error_msg.setError(Error20);
error_msg.sendError(error_can_bus);

// Wait for 100ms before repeating the process
bindings::TickBlocking(tick_duration);
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target_sources(bindings PRIVATE bindings.cc)

target_link_libraries(bindings PUBLIC mcal-cli)
34 changes: 34 additions & 0 deletions firmware/projects/Demo/CANErrors/platforms/cli/bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// @author Luai Bashar
/// @date 2024-11-05

#include <chrono>
#include <thread>

#include "mcal/cli/periph/can.h"
#include "shared/periph/can.h"

#include "../../generated/can/error_can_messages.h"
#include "shared/comms/can/can_bus.h"

namespace mcal {
using namespace cli::periph;

CanBase error_can_base{"vcan0"};
}

namespace bindings {
shared::periph::CanBase& error_can_base = mcal::error_can_base;

// Simulates a sleep, waiting for inputted ticks ms
void TickBlocking(uint32_t ticks) {
std::chrono::milliseconds duration(ticks);

std::this_thread::sleep_for(duration);
}

// Initializes the can/CLI outputs
void Initialize() {
mcal::error_can_base.Setup();
std::cout << "Initializing CLI..." << std::endl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(MCAL cli)
Loading