Skip to content

Commit

Permalink
Merge branch 'testing' into feature/launchcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
mathbrook committed May 12, 2024
2 parents 00f44de + feedb13 commit dc83ff4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
25 changes: 0 additions & 25 deletions include/device_status.h

This file was deleted.

27 changes: 26 additions & 1 deletion include/distance_tracker.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DISTANCE_TRACKER_H
#define DISTANCE_TRACKER_H
#include <stdint.h>
#include <deque>
#ifdef ARDUINO
#include <Arduino.h>
#endif
Expand All @@ -19,6 +20,28 @@ struct energy_data_t
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) {}
};

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

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

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

float getAverage() const {
return buffer.empty() ? 0.0 : sum / buffer.size();
}
};


class distance_tracker_s
{
public:
Expand All @@ -45,6 +68,7 @@ class distance_tracker_s
// update efficiencies
efficiency_kmkwh = distance_km/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
power_kw.oldval = power_kw.newval;
current_amps.oldval = current_amps.newval;
Expand All @@ -59,7 +83,7 @@ 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>(efficiency_instantaneous*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<int16_t>(avgEff.getAverage()*1000), static_cast<int16_t>(distance_km), static_cast<int16_t>(efficiency_kmkwh*1000));
return data;
}
float capacity_ah=0;
Expand All @@ -72,6 +96,7 @@ class distance_tracker_s
old_new_t current_amps;
old_new_t velocity_ms;
unsigned long time;
RollingAverage avgEff = RollingAverage(200);
};

#endif
3 changes: 3 additions & 0 deletions include/parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
const unsigned long LAUNCHCONTROL_RELEASE_DELAY = 1500;
#define WHEELSPEED_TOOTH_COUNT 18 // Wheel Speed sensor tooth coutn
const float WHEEL_CIRCUMFERENCE = 0.229*PI*2;
const float FRONT_SPROCKET_TEETH = 10;
const float REAR_SPROCKET_TEETH = 30;
const float FINAL_DRIVE = FRONT_SPROCKET_TEETH/REAR_SPROCKET_TEETH;
#define RPM_TIMEOUT 1000 // Timeout for wheel speed RPM to reset to 0
#define MIN_BRAKE_PEDAL 400 // ~0.5v, set on 2-29-2024
#define START_BRAKE_PEDAL 970 // 1.58V, set on 2-29-2024
Expand Down
4 changes: 4 additions & 0 deletions src/FlexCAN_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ FlexCAN_T4<CAN3, RX_SIZE_1024, TX_SIZE_1024> DaqCAN_;
FlexCAN_T4<CAN2, RX_SIZE_1024, TX_SIZE_1024> Inverter_CAN_;
FlexCAN_T4<CAN1, RX_SIZE_1024, TX_SIZE_1024> AccumulatorCAN_;

// number of rx and tx mailboxes
#define NUM_TX_MAILBOXES 32
#define NUM_RX_MAILBOXES 32

void InitCAN()
{
// Daqcan only needs to send, but fast
Expand Down
3 changes: 3 additions & 0 deletions src/inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ bool Inverter::command_torque(int16_t torque)
{
return false;
}
else{
return false;
}
}

//
Expand Down
2 changes: 1 addition & 1 deletion src/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void StateMachine::handle_state_machine(MCU_status &mcu_status)
// if (mcu_status.get_state() == MCU_STATE::READY_TO_DRIVE)
if (true)
{
distance_tracker.update(accumulator->get_acc_current(), accumulator->get_acc_voltage(), pedals->get_wsfl(), WHEEL_CIRCUMFERENCE, millis());
distance_tracker.update(accumulator->get_acc_current(), accumulator->get_acc_voltage(), pm100->getmcMotorRPM() * FINAL_DRIVE, WHEEL_CIRCUMFERENCE, micros());
mcu_status.set_distance_travelled(distance_tracker.get_data().distance_m);
}
bool _20hz_send = can_20hz_timer.check();
Expand Down

0 comments on commit dc83ff4

Please sign in to comment.