Skip to content

Commit

Permalink
Merge pull request #4 from DallasFormulaRacing/feature/CAN_Relay
Browse files Browse the repository at this point in the history
Feature/can relay
  • Loading branch information
NicoOhR authored Oct 12, 2024
2 parents 2c4a025 + bd437fb commit 148a3c8
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Application/DataLogger/DataLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ void DataLogger::Standby::Compute(DataLogger& context) {
if (!context.storage_connected_observer_) {
context.SetState(&context.idle_state_);
}

if (context.user_input_->ToggleDetected()) {
context.logging_enabled_ = context.user_input_->Read();

if (context.logging_enabled_) {
context.SetState(&context.logging_state_);
}
}
context.SetState(&context.logging_state_);
}

void DataLogger::Standby::Exit(DataLogger& context) {
Expand Down Expand Up @@ -144,16 +144,16 @@ void DataLogger::Logging::Compute(DataLogger& context) {
}
}


context.queue_.Lock();

DataPayload received_data;
if(!context.queue_.IsEmpty()) {
received_data = context.queue_.Dequeue();
context.RecordDataSample(received_data);
}
}

context.queue_.Unlock();

}

void DataLogger::Logging::Exit(DataLogger& context) {
Expand Down
1 change: 1 addition & 0 deletions Application/Mutex/imutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class IMutex {
virtual void Lock() = 0;

virtual void Unlock() = 0;

};

} // namespace application
Expand Down
2 changes: 0 additions & 2 deletions Application/Mutex/mutex_cmsisv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ void MutexCmsisV2::Unlock() {
osMutexRelease(mutex_id_);
}



} // namespace application
50 changes: 50 additions & 0 deletions Application/Relay/can_relay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* can_relay.cpp
*
* Created on: May 16, 2024
* Author: Nico
*/
#include "./can_relay.hpp"
namespace application{

Can_Relay::Can_Relay(std::shared_ptr<platform::ICan> can_bus,
CircularQueue<DataPayload> queue):
can_bus_(can_bus),
queue_(queue){}

void Can_Relay::bitSet(float value, uint8_t* byteArray) {
std::memcpy(byteArray, &value, sizeof(float));
std::reverse(byteArray, byteArray + 4);
}

void Can_Relay::Generate_Messages(application::DataPayload data){

transmission_ended_ = false;
uint8_t row;
uint8_t column;
data.RawRow(message_row);
for(int i = 0; i < 5; i++){//partial send for autocross
row = i/2; //integer division by default floors
column = (i%2) * 4; //the column which the row goes into is essentially sinusoidal
bitSet(message_row[i], &message[row][column]);
}

}


void Can_Relay::Send_Messages(){
can_bus_->ChangeArbId(0x417);
for(int i = 0; i < 3; i++){
can_bus_->ChangeArbId(0x417 + i); //each message has a unique ID
can_bus_->Transmit(message[i]);
printf("Sending Message! \n");
}
}

void Can_Relay::End_Transmission(bool logging_flag){
if(!logging_flag && !transmission_ended_){
can_bus_->Transmit(kEnd_transmission_);
transmission_ended_ = true;
}
}
}
56 changes: 56 additions & 0 deletions Application/Relay/can_relay.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* can_relay.hpp
*
* Created on: May 3, 2024
* Author: Nico
*/
#ifndef DFR_LIBRARIES_APPLICATION_RELAY_CAN_RELAY_HPP_
#define DFR_LIBRARIES_APPLICATION_RELAY_CAN_RELAY_HPP_
#include <memory>
#include <algorithm>
#include <cstring>

#include "../../Platform/STM/F4/CAN/bxcan_stmf4.hpp"
#include "../../Platform/Interfaces/igpio.hpp"
#include "../circular_queue.hpp"
#include "../data_payload.hpp"

#include <bitset>

namespace application{
class Can_Relay{
public:
Can_Relay(std::shared_ptr<platform::ICan> can_bus,
CircularQueue<DataPayload> queue);
//shared pointer to the can bus to relay and reference to the data queue to get the message
//allocate memory

~Can_Relay();

void Generate_Messages(DataPayload data);

void Send_Messages();

void End_Transmission(bool logging_flag);

void bitSet(float value, uint8_t*);
private:

std::shared_ptr<platform::ICan> can_bus_;

CircularQueue<DataPayload>& queue_;

uint8_t kEnd_transmission_[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

static constexpr uint8_t messageSize = 29;

static constexpr uint8_t kRows = 15;

bool transmission_ended_ = false;

uint8_t message[kRows][8] = {0};

float message_row[messageSize] = {0};
};
}
#endif /* DFR_LIBRARIES_APPLICATION_RELAY_CAN_RELAY_HPP_ */
5 changes: 4 additions & 1 deletion Application/circular_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class CircularQueue {
return full_;
}

uint8_t GetSize(){
return kMaxSize;
}

void Clear() {
while (!IsEmpty()) {
Dequeue();
Expand All @@ -76,7 +80,6 @@ class CircularQueue {

void Unlock() { mutex_->Unlock(); }


private:
void IncrementIndex(uint8_t &index) {
index = (index + 1) % kMaxSize;
Expand Down
49 changes: 49 additions & 0 deletions Application/data_payload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ struct DataPayload {
CsvString(buffer, length);
}

void RawRow(float* buffer){
RawArray(buffer);
}

void Lock() { mutex_->Lock(); }

void Unlock() { mutex_->Unlock(); }
Expand Down Expand Up @@ -144,6 +148,51 @@ struct DataPayload {
air_temp_,
coolant_temp_);
}
void RawArray(float* raw_buffer) {
float raw_array[] = {
timestamp_,

linpot_displacement_mm_[0],
linpot_displacement_mm_[1],
linpot_displacement_mm_[2],
linpot_displacement_mm_[3],

acceleration_[0],
acceleration_[1],
acceleration_[2],

angular_velocity_[0],
angular_velocity_[1],
angular_velocity_[2],

rpm_,
tps_,
fuel_open_time_,
ignition_angle_,

barometer_,
map_,
lambda_,

analog_inputs_[0],
analog_inputs_[1],
analog_inputs_[2],
analog_inputs_[3],
analog_inputs_[4],
analog_inputs_[5],
analog_inputs_[6],
analog_inputs_[7],

battery_voltage_,
air_temp_,
coolant_temp_
};

for (size_t i = 0; i < 29; ++i) {
raw_buffer[i] = raw_array[i];
}
}


std::shared_ptr<application::IMutex> mutex_;
const char* kCsvFormatSpecifiers = "%f," // Timestamp
Expand Down
2 changes: 2 additions & 0 deletions Platform/Interfaces/ican.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ICan {

virtual bool MessageArrivedFlag() = 0;

virtual void ChangeArbId(uint32_t newId) = 0;

virtual void ClearMessageArrivedFlag() = 0;

virtual uint32_t LatestCanId() = 0;
Expand Down
19 changes: 17 additions & 2 deletions Platform/STM/F4/CAN/bxcan_stmf4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Formula SAE International Collegiate Chapter
* GPL-3.0 License
*/

#include <cstdio>
#include "bxcan_stmf4.hpp"

namespace platform {
Expand Down Expand Up @@ -39,9 +39,18 @@ void BxCanStmF4::ConfigureFilter(uint32_t filder_id_high, uint32_t filter_id_low
}

void BxCanStmF4::Start() {
tx_message_header_.IDE = CAN_ID_STD; // setting ID to Standard ID
tx_message_header_.StdId = 0x417; // Identifier (D = 4/ A = 1/ Q = (1)7)
tx_message_header_.RTR = CAN_RTR_DATA;// Setting Remote Transmission Request
tx_message_header_.DLC = 8;//Data Length of Data bytes

HAL_CAN_Start(&bx_can_);
}

void BxCanStmF4::ChangeArbId(uint32_t newId){
tx_message_header_.StdId = newId;
}

void BxCanStmF4::EnableInterruptMode() {
HAL_CAN_ActivateNotification(&bx_can_, rx_interrupt_mode_);
}
Expand All @@ -57,9 +66,15 @@ void BxCanStmF4::Receive(uint8_t rx_buffer[kMaxBytes]) {
}

void BxCanStmF4::Transmit(uint8_t tx_buffer[kMaxBytes]) {
// TODO
if(HAL_CAN_AddTxMessage(&bx_can_, &tx_message_header_, tx_buffer,&tx_mailbox_) != HAL_OK){
printf("Error Transmit");
Error_Handler();
}

while(HAL_CAN_IsTxMessagePending(&bx_can_, tx_mailbox_)){}
}


void BxCanStmF4::ClearMessageArrivedFlag() { message_arrived_ = false; }

bool BxCanStmF4::MessageArrivedFlag() { return message_arrived_; }
Expand Down
9 changes: 9 additions & 0 deletions Platform/STM/F4/CAN/bxcan_stmf4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class BxCanStmF4 : public ICan {

/// Publishes data to the CAN bus.
/// @param tx_buffer The byte array whose data is to be sent.
/// This function will hang if the transmission never completes
virtual void Transmit(uint8_t tx_buffer[kMaxBytes]) override;

/// @return Indicates whether a new message has arrived.
Expand All @@ -57,9 +58,14 @@ class BxCanStmF4 : public ICan {
/// Clears the `MessageArrivedFlag()`.
virtual void ClearMessageArrivedFlag() override;

/// Changes the transmission IDs
virtual void ChangeArbId(uint32_t newId) override;


/// @return The CAN ID of the latest message received.
virtual uint32_t LatestCanId() override;


/// A user-configurable interrupt mode for generating notifications
/// when CAN messages are received. To be used with `ConfigureReceiveCallback()`.
enum class ReceiveInterruptMode : uint8_t {
Expand Down Expand Up @@ -102,9 +108,12 @@ class BxCanStmF4 : public ICan {
// ST's HAL library.
CAN_HandleTypeDef& bx_can_; // BxCAN (Basic Extended) peripheral
CAN_RxHeaderTypeDef rx_message_header_; // Receiving message info
CAN_TxHeaderTypeDef tx_message_header_; // Sending message info
CAN_FilterTypeDef filter_; // Filter bank configuration
uint8_t filter_bank_num_;

uint32_t tx_mailbox_;

// Configurable options
uint32_t rx_interrupt_mode_ = 0;
uint8_t rx_fifo_ = 0;
Expand Down

0 comments on commit 148a3c8

Please sign in to comment.