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 2 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
33 changes: 28 additions & 5 deletions firmware/projects/Demo/CANErrors/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +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

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.
<br>
luaibash marked this conversation as resolved.
Show resolved Hide resolved

## Generating The DBC Code
- 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"
## 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"
30 changes: 0 additions & 30 deletions firmware/projects/Demo/CANErrors/errors.dbc
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
VERSION ""

NS_ :
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
Expand Down
60 changes: 30 additions & 30 deletions firmware/projects/Demo/CANErrors/inc/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,6 @@
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) {
if (error < 0 || error > 63) {
std::cerr << "Error index must be between 0 and 63!" << std::endl;
return;
}

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

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

// Reset after a send to not send duplicate errors
resetError();
}

// Resets all errors back to untriggered
void resetError() {
error_msg.errors = 0;
}
};

// Defines all possible errors to set
enum Error {
luaibash marked this conversation as resolved.
Show resolved Hide resolved
Error0 = 0,
Expand Down Expand Up @@ -107,4 +77,34 @@ enum Error {
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();
}
};
21 changes: 11 additions & 10 deletions firmware/projects/Demo/CANErrors/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ int main(void) {
uint32_t tick_duration = 100;

// Initialize the error object that sets/sends each error
ErrorHandler error_msg{};
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 < 63; i++) {
error_msg.setError(i);
error_msg.sendError(error_can_bus);
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_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);
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);
Expand Down
Loading