-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added in stats system and eeprom interface
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#ifndef __EEPROMINTERFACE_H__ | ||
#define __EEPROMINTERFACE_H__ | ||
#include <cstdlib> | ||
#include <cstddef> | ||
#include <SharedDataTypes.h> | ||
|
||
// what this is: | ||
// this interface will encompass any and all interfacing / writing of the eeprom on a micro platform (for now: teensy) | ||
|
||
// first goal of this interface: be able to keep a log of our mileage with intermittent updates of the current km driven. | ||
|
||
// we will be writing a struct to the memory that will contain the mileage data / any of the other | ||
// data we will write and be able to read / recall from the eeprom | ||
|
||
|
||
// this class will also manage the time in which we update the EEPROM values so that we can safely call the update function | ||
// repeatedly without concern for the life of the underlying EEPROM. | ||
class EEPROMInterface | ||
{ | ||
public: | ||
struct params | ||
{ | ||
unsigned int write_interval_ms; | ||
}; | ||
|
||
EEPROMInterface(params p = {120000000}) : | ||
_params(p) { | ||
_last_write_time_millis = 0; | ||
_last_written_distance_m = 0; | ||
_eeprom_written_data = {}; | ||
} | ||
|
||
/// @brief initialize the EEPROMInterface by reading the last written data from the EEPROM. | ||
// reads first the size of the low level data in bytes (a uint16_t) and then | ||
// proceeds to read that amount of data. | ||
|
||
/// @brief | ||
/// @param reset_eeprom use this flag to write | ||
void init(bool reset_eeprom); | ||
|
||
void update_eeprom(const SharedCarState_s & car_state); | ||
EEPROMInterfaceData_s get_current_data(); | ||
|
||
private: | ||
unsigned long _last_write_time_millis; | ||
int _last_written_distance_m; | ||
params _params; | ||
std::byte _data_buffer[4284]; // 4284 bytes is the size of the EEPROM on the teensy41 | ||
|
||
EEPROMInterfaceData_s _eeprom_written_data; | ||
}; | ||
#endif // __EEPROMINTERFACE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <EEPROMInterface.h> | ||
#include <EEPROM.h> | ||
#include <cmath> | ||
#include <cstring> | ||
#include <bit> | ||
|
||
void EEPROMInterface::init(bool reset_eeprom) | ||
{ | ||
// we know that the first 2 bytes tell us the size of the previously written data | ||
std::byte size_data[2]; | ||
|
||
// if the reset eeprom flag is set, the eeprom previously written data will stay initialized to all zeros | ||
// so all previous data will be lost if there was any written as the new sums will not include this previous data. | ||
if(!reset_eeprom) | ||
{ | ||
uint16_t written_size; | ||
|
||
size_data[0] = static_cast<std::byte>(EEPROM.read(0)); | ||
size_data[1] = static_cast<std::byte>(EEPROM.read(1)); | ||
memcpy(&written_size, size_data, 2); | ||
|
||
// proceed to read out all of the existing data | ||
|
||
for(int i = 2; i < written_size; i++) | ||
{ | ||
_data_buffer[i] = static_cast<std::byte>(EEPROM.read(i)); | ||
} | ||
|
||
memcpy(&_eeprom_written_data, _data_buffer, written_size); | ||
} | ||
// go ahead and updating the written size to the current struct size as we are now finished using the data | ||
uint16_t new_write_size = sizeof(LowLevelStatData_s); | ||
memcpy(size_data, &new_write_size, 2); | ||
EEPROM.write(0, static_cast<uint8_t>(size_data[0])); | ||
EEPROM.write(1, static_cast<uint8_t>(size_data[1])); | ||
} | ||
|
||
void EEPROMInterface::update_eeprom(const SharedCarState_s &car_state) | ||
{ | ||
|
||
if(car_state.systick.millis - _last_write_time_millis > _params.write_interval_ms) | ||
{ | ||
// distanc_m current_session_distance = car_state.low_level_stats.session_distance; | ||
|
||
// get the difference between the last written distance to the previous driven meters; | ||
|
||
auto diff = static_cast<uint32_t>(abs(std::round(car_state.low_level_stats.session_distance)) - _last_written_distance_m); | ||
_eeprom_written_data.current_driven_m += diff; | ||
memcpy(_data_buffer, &_eeprom_written_data, sizeof(EEPROMInterfaceData_s)); | ||
size_t byte_size_to_write = sizeof(EEPROMInterfaceData_s); | ||
for(size_t i = 0; i < byte_size_to_write; i++) | ||
{ | ||
EEPROM.write(i+2, static_cast<uint8_t>(_data_buffer[i])); | ||
} | ||
|
||
_last_written_distance_m = car_state.low_level_stats.session_distance; | ||
_last_write_time_millis = car_state.systick.millis; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef __STATSSYSTEM_H__ | ||
#define __STATSSYSTEM_H__ | ||
|
||
#include <SharedDataTypes.h> | ||
#include <Utility.h> | ||
|
||
class StatsSystem | ||
{ | ||
public: | ||
StatsSystem() { | ||
_prev_dt_s = 0.0f; | ||
_last_stat_update_timestamp = 0; | ||
_current_data = {}; | ||
} | ||
LowLevelStatData_s get_latest_vehicle_stats(const SharedCarState_s& veh_state); | ||
private: | ||
float _prev_dt_s; | ||
unsigned long _last_stat_update_timestamp; | ||
LowLevelStatData_s _current_data; | ||
|
||
}; | ||
#endif // __STATSSYSTEM_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <StatsSystem.h> | ||
|
||
LowLevelStatData_s StatsSystem::get_latest_vehicle_stats(const SharedCarState_s &veh_state) | ||
{ | ||
if (_last_stat_update_timestamp == 0) | ||
{ | ||
_prev_dt_s = 0.0f; | ||
_last_stat_update_timestamp = veh_state.systick.millis; | ||
} | ||
else | ||
{ | ||
_prev_dt_s = (veh_state.systick.millis - _last_stat_update_timestamp) / 1000.0f; | ||
} | ||
|
||
float avg_rpm = 0; | ||
for (int i = 0; i < NUM_MOTORS; i++) | ||
{ | ||
avg_rpm += veh_state.drivetrain_data.measuredSpeeds[i]; | ||
} | ||
|
||
avg_rpm = avg_rpm / static_cast<float>(NUM_MOTORS); | ||
_current_data.session_distance += _prev_dt_s * (avg_rpm * RPM_TO_METERS_PER_SECOND); | ||
return _current_data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters