Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions lib/can/receiver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <functional>
#include <stdint.h>

#include "types.h"

namespace CAN {

/**
* @brief A generic enum for the status of CAN devices
* @note Device specific errors should be converted into a status error
*
* @note Less than 0 is Fatal Errors
* @note Greater than 0 is non-fatal errors/warnings
*/
enum class Status {
OK = 0,
// Warnings/Non-Fatal Errors
WARN = 1,
COMM_WARN = 2,
// Fatal Errors
FATAL = -1,
COMM_ERR = -2, // TODO: Make a list of errors that should kill the car and not

};
typedef std::function<void(const Status&)> ErrorCallback;

class Receiver {
private:
ErrorCallback onErrorCallback;
public:
virtual Status digestFrame(const Frame&) const = 0;
virtual void onError(const Status&) = 0;
virtual uint32_t* getIDs() = 0;
virtual uint32_t idCount() = 0;
};

};
3 changes: 2 additions & 1 deletion lib/can/types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <functional>
#include <stdint.h>

namespace CAN {
Expand Down Expand Up @@ -144,7 +145,7 @@ struct Frame {
}

template<typename T>
T* decode() {
T* decode() const {
return (T*) data;
}
};
Expand Down
22 changes: 19 additions & 3 deletions lib/inverter/DTIX50/heartbeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ using namespace CAN;
namespace Inverter {
namespace DTIX50 {

Heartbeat::Heartbeat(std::shared_ptr<Provider> canProvider, std::unique_ptr<Core::iLockStrategy> lock_strategy, std::unique_ptr<Core::iThreadStrategy> thread_strategy) {
uint8_t MAX_FAILED_TRANSMITS = 3;

Heartbeat::Heartbeat(std::shared_ptr<Provider> canProvider, std::unique_ptr<Core::iLockStrategy> lock_strategy, std::unique_ptr<Core::iThreadStrategy> thread_strategy, ErrorCallback callback) : onErrorCallback(callback) {
m_canProvider = canProvider;
m_shouldStop_mut = std::move(lock_strategy);
m_thread = std::move(thread_strategy);
Expand Down Expand Up @@ -57,15 +59,29 @@ void Heartbeat::heartbeat(void* s) {
self->m_shouldStop_mut->unlock();
// Send drive disable
Frame frame(0x0C52, &self->disable);
self->m_canProvider->transmit(frame, 1000);
bool success = self->m_canProvider->transmit(frame, 1000);

if(!success) {
self->onErrorCallback(Status::COMM_ERR);
}

return;
}
self->m_shouldStop_mut->unlock();

// Send drive enable
Frame frame(0x0C52, &self->enable);

self->m_canProvider->transmit(frame, 1000);
if(!self->m_canProvider->transmit(frame, 100)) {
if(self->failed_transmits > MAX_FAILED_TRANSMITS) {
self->onErrorCallback(Status::COMM_ERR);
} else {
self->onErrorCallback(Status::COMM_WARN);
self->failed_transmits++;
}
} else {
self->failed_transmits = 0;
}

self->m_thread->sleep(250U);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/inverter/DTIX50/heartbeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../core/lock/i_lock_strategy.h"
#include "../core/thread/i_thread_strategy.h"
#include "../can/provider.h"
#include "../can/types.h"
#include "../can/receiver.h"

using namespace CAN;

Expand All @@ -24,8 +24,11 @@ class Heartbeat {

Command::SetDriveEnable enable;
Command::SetDriveEnable disable;

uint8_t failed_transmits; // Counts consecutive failed transmits
ErrorCallback onErrorCallback;
public:
Heartbeat(std::shared_ptr<Provider> canProvider, std::unique_ptr<Core::iLockStrategy> lock_strategy, std::unique_ptr<Core::iThreadStrategy> thread_strategy);
Heartbeat(std::shared_ptr<Provider> canProvider, std::unique_ptr<Core::iLockStrategy> lock_strategy, std::unique_ptr<Core::iThreadStrategy> thread_strategy, ErrorCallback callback);

void start();
void stop();
Expand Down
47 changes: 47 additions & 0 deletions lib/inverter/DTIX50/watchdog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "watchdog.h"

using namespace CAN;

namespace Inverter {
namespace DTIX50 {

Watchdog::Watchdog(ErrorCallback callback) : onErrorCallback(callback) {}

Status Watchdog::digestFrame(const Frame& frame) const {
uint32_t id = frame.identifier >> 5;

switch(id) {
case 0x22:
return handleMessage22(frame);
default:
break;
}
return Status::OK;
}

Status Watchdog::handleMessage22(const Frame& frame) const {
auto message = frame.decode<Message22>();

// We can expand this to cover all errors, status just doesn't have much support yet
if(message->fault_code != FaultCodes::NONE) {
return Status::FATAL;
}

return Status::OK;
}

void Watchdog::onError(const Status& status) {
onErrorCallback(status);
}

uint32_t* Watchdog::getIDs() {
uint32_t ids[] = { 0x52 } ;
return ids;
}

uint32_t Watchdog::idCount() {
return 1;
}

};
};
30 changes: 30 additions & 0 deletions lib/inverter/DTIX50/watchdog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "../../can/receiver.h"

#include "messages.h"

using namespace CAN;

namespace Inverter {
namespace DTIX50 {

class Watchdog final : public Receiver {
public:
ErrorCallback onErrorCallback;
public:
Watchdog(ErrorCallback callback);

Status digestFrame(const Frame& frame) const override;

void onError(const Status& error);

uint32_t* getIDs() override;
uint32_t idCount() override;
private:

Status handleMessage22(const Frame& frame) const;
};

};
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void test_Heartbeat() {
std::unique_ptr<Core::iLockStrategy> lockStrategy(new NativeLockStrategy()); // We'll want the class to recieve ownership
std::unique_ptr<Core::iThreadStrategy> threadStrategy(new NativeThreadStrategy()); // We'll want the class to recieve ownership

DTIX50::Heartbeat heartbeat(canProvider, std::move(lockStrategy), std::move(threadStrategy));
DTIX50::Heartbeat heartbeat(canProvider, std::move(lockStrategy), std::move(threadStrategy), nullptr);

TEST_ASSERT(!heartbeat.started());
heartbeat.start();
Expand Down