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 13 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.
36 changes: 36 additions & 0 deletions firmware/projects/Demo/CANErrors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# CANErrors
This is the CAN Errors demo project. It defines a global system for the vehicle to send/receive errors through different subsystems. It defines:
- A 64 bit CAN error signal. Each bit represents a different error, and will be sent periodically for other systems to know if an error has occurred
- The `ErrorHandler` class, an interface to set/send different errors on the CAN signal
- Enum `Error` that defines all 64 possible errors to send through the CAN signal

<br>
luaibash marked this conversation as resolved.
Show resolved Hide resolved

## How to use `ErrorHandler`

### Defining an instance of ErrorHandler:
```
ErrorHandler error_handler{};
```

### Setting errors with ErrorHandler:
- `setError` takes an error from the `Error` enum and sets it
```
error_handler.setError(Error0);
error_handler.setError(Error15);
error_handler.setError(Error48);
```

### Sending errors with ErrorHandler:
- `sendError` takes a CAN bus and sends the errors through that bus. It resets the errors to send the next set of errors
```
error_handler.sendError(error_can_bus);
```

<br>

## 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!
- This will also generate the dbc files automatically, but if you want to generate them seperately, run "cangen projects/Demo/CANErrors"
- 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
9 changes: 9 additions & 0 deletions firmware/projects/Demo/CANErrors/errors.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
VERSION ""

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
***************************************************************/

// 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
};

// Interface to set/send all possible errors for the system
class ErrorHandler {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
private:
luaibash marked this conversation as resolved.
Show resolved Hide resolved
// Object that holds a 64 bit int, each bit representing an error
generated::can::TMS_ERROR error_message{};

// Resets all errors back to untriggered
void resetError() {
error_message.errors = 0;
}
public:
// Sets the error based on the error index given
void setError(Error error) {
if (error < 0 || error > 63) {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
std::cerr << "Error index must be between 0 and 63!" << std::endl;
return;
}

error_message.errors |= (1ULL << error);
luaibash marked this conversation as resolved.
Show resolved Hide resolved
}

// Sends the error message through the provided CAN bus
void sendError(shared::can::CanBus error_can_bus) {
error_can_bus.Send(error_message);

// Reset after a send to not send duplicate errors
resetError();
}
};
49 changes: 49 additions & 0 deletions firmware/projects/Demo/CANErrors/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// @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_message{};

while (true) {
error_can_bus.Update();

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

// Test #2: Set random errors using the enum
error_message.setError(Error0);
error_message.setError(Error5);
error_message.setError(Error10);
error_message.setError(Error15);
error_message.setError(Error20);
error_message.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