Skip to content

Commit

Permalink
schizo
Browse files Browse the repository at this point in the history
  • Loading branch information
mathbrook committed May 13, 2024
1 parent dd04952 commit 72826e6
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 71 deletions.
4 changes: 3 additions & 1 deletion include/KS2eCAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
#define ID_DASH_FW_VERSION 0xEC
#define ID_MC_CURRENT_LIMIT_COMMAND 0x202
#define ID_BMS_SOC 0x6B3 //made this real!

#define ID_VCU_DISTANCE_TRACKER_MOTOR 0xCE
#define ID_VCU_DISTANCE_TRACKER_WHEELSPEED 0xCF
#define ID_VCU_LIFETIME_DATA 0xD0

#define ID_BMS_CURRENT_LIMIT_INFO 0x6B1
#define ID_BMS_PACK_VOLTAGE_INFO 0x6B2
Expand Down
6 changes: 6 additions & 0 deletions include/common_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ typedef struct lc_countdown_t
unsigned long release_countdown;
const unsigned long release_delay = LAUNCHCONTROL_RELEASE_DELAY;
} lc_countdown_t;

typedef struct time_and_distance_tracker_t
{
unsigned long vcu_lifetime_ontime;
unsigned long vcu_lifetime_distance;
} time_and_distance_tracker_t;
#endif
22 changes: 12 additions & 10 deletions include/distance_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,31 @@ struct old_new_t
struct energy_data_t
{
uint16_t energy_wh;
int16_t eff_inst;
uint16_t eff_inst;
uint16_t distance_m;
int16_t efficiency_kmkwh;
energy_data_t(uint16_t wh, int16_t eff, uint16_t m, int16_t kmkwh) : energy_wh(wh), eff_inst(eff), distance_m(m), efficiency_kmkwh(kmkwh) {}
uint16_t efficiency_kmkwh;
energy_data_t(uint16_t wh, uint16_t eff, uint16_t m, uint16_t kmkwh) : energy_wh(wh), eff_inst(eff), distance_m(m), efficiency_kmkwh(kmkwh) {}
};

struct RollingAverage {
std::deque<float> buffer;
float sum = 0.0;
float sum;
int maxSize;

RollingAverage(int maxBufferSize) : maxSize(maxBufferSize) {}

void addValue(float value) {
buffer.push_back(value);
sum += value;
sum = sum + value;
printf("%f",sum);
if (buffer.size() > maxSize) {
sum -= buffer.front();
buffer.pop_front();
}
}

float getAverage() const {
printf("%d, %f",buffer.empty(),buffer.front());
return buffer.empty() ? 0.0 : sum / buffer.size();
}
};
Expand All @@ -57,7 +59,7 @@ class distance_tracker_s
// update time
this->time = newtime;
// calculate distance travelled during the last update time
distance_km += elapsed_time_seconds * velocity_ms.oldval;
distance_m += elapsed_time_seconds * velocity_ms.oldval;
// calculate power used during the last update time
float kwh = (elapsed_time_seconds / 3600) * power_kw.oldval;
energy_wh += kwh;
Expand All @@ -66,7 +68,7 @@ class distance_tracker_s
capacity_ah += (elapsed_time_seconds / 3600) * current_amps.oldval;

// update efficiencies
efficiency_kmkwh = distance_km/energy_wh;
efficiency_kmkwh = distance_m/energy_wh;
efficiency_instantaneous = (elapsed_time_seconds * velocity_ms.oldval) / (elapsed_time_seconds / 3600 * power_kw.oldval);
avgEff.addValue(efficiency_instantaneous);
// set old vals to the previous new one
Expand All @@ -83,20 +85,20 @@ class distance_tracker_s
}
energy_data_t get_data()
{
energy_data_t data = energy_data_t(static_cast<uint16_t>(energy_wh*10), static_cast<int16_t>(avgEff.getAverage()*1000), static_cast<int16_t>(distance_km), static_cast<int16_t>(efficiency_kmkwh*1000));
energy_data_t data = energy_data_t(static_cast<uint16_t>(energy_wh*10), static_cast<uint16_t>(avgEff.getAverage()*1000), static_cast<uint16_t>(distance_m), static_cast<uint16_t>(efficiency_kmkwh*1000));
return data;
}
float capacity_ah=0;
float energy_wh = 0;
float distance_km = 0;
float distance_m = 0;
float efficiency_kmkwh = 0;
float efficiency_instantaneous = 0;
private:
old_new_t power_kw;
old_new_t current_amps;
old_new_t velocity_ms;
unsigned long time;
RollingAverage avgEff = RollingAverage(200);
RollingAverage avgEff = RollingAverage(100);
};

#endif
19 changes: 17 additions & 2 deletions include/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define STATE_MACHINE_HPP
#include <Arduino.h>
#include <Metro.h>
#include <EEPROM.h>
#include "MCU_status.hpp"
#include "inverter.hpp"
#include "accumulator.hpp"
Expand All @@ -11,6 +12,7 @@
#include "launch_system.h"
#include <distance_tracker.h>

#define ODOMETER_EEPROM_ADDR 0
class StateMachine
{
private:
Expand All @@ -22,16 +24,29 @@ class StateMachine
PedalHandler *pedals;
launchControlSystem *lcSystem;
Metro *pedal_check_;
distance_tracker_s distance_tracker;
distance_tracker_s distance_tracker_motor;
distance_tracker_s distance_tracker_fl;
// To hold initial values from eeprom:
unsigned long _lifetime_distance = 0;
unsigned long _lifetime_on_time = 0;
time_and_distance_tracker_t time_and_distance_t;
void set_state(MCU_status &mcu_status, MCU_STATE new_state);
void send_state_msg(MCU_status &mcu_status);
Metro can_20hz_timer = Metro(10,1);
Metro can_100hz_timer = Metro(10,1);
Metro can_20hz_timer = Metro(50,1);
Metro can_10hz_timer = Metro(100,1);
Metro _log_distance_timer_10s = Metro(10000,1);
public:
StateMachine(Inverter *inv, Accumulator *acc, Metro *rs_tim, Dashboard *dash, Metro *debug, PedalHandler *pedals, launchControlSystem *lcSys,Metro *ped_t)
: pm100(inv), accumulator(acc), timer_ready_sound(rs_tim), dash_(dash), debug_(debug), pedals(pedals), lcSystem(lcSys),pedal_check_(ped_t){};

void init_state_machine(MCU_status &mcu_status);
void handle_state_machine(MCU_status &mcu_status);
void handle_distance_trackers(MCU_status &mcu_status);
time_and_distance_tracker_t get_lifetime_data()
{
return time_and_distance_t;
}
// void joe_mock_lc(MCU_status* mcu_status, int torq, bool implaus);

};
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ void loop()
memcpy(fw_hash_msg.buf, &vcu_status_t, sizeof(vcu_status_t));
WriteCANToInverter(fw_hash_msg);

sendStructOnCan(state_machine.get_lifetime_data(),ID_VCU_LIFETIME_DATA);

// broadcast pedal thresholds information
pedal_thresholds_msg.id = ID_VCU_PEDAL_THRESHOLD_SETTINGS;
pedal_thresholds_msg.len = 7;
Expand Down
Loading

0 comments on commit 72826e6

Please sign in to comment.