Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code review main charge system #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"random": "cpp",
"limits": "cpp"
}
}
19 changes: 19 additions & 0 deletions .vscode/test/test_maincharge.cpp
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait wat, why is this here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized it was in the wrong spot right before you posted this and forgot to push the changes. Will be pushed out with some unit tests soon

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Arduino.h>
#include "MainChargeSystem.h"

// Function to test


// Test functions
void test_mainCharge(){

}


void setup() {

}

void loop() {
// Leave empty for tests
}
2 changes: 1 addition & 1 deletion include/CCUTasks.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include <ht_sched.hpp>
#include <ht_sched.hpp>
7 changes: 7 additions & 0 deletions lib/constants/CCUParams.h
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typically we do not have a library dedicated to constants within firmware projects but its not the worst pattern. typically the systems are themselves configurable via their constructors and the args of the constructors are passed in on construction from a platformio project specific include (such as MCU_rev15_defs.h)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <Arduino.h>

struct CCUParams{
double target_voltage = 3.1; //per cell
int max_AC_current = 15; //need to add the DC current setpoint probably 30 amps
float ideal_cell_temp = 40;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would recommend using a namespace instead for this and make these const instead of a struct.

what this means essentially is you can create an instance of CCUParams and these are the default values for CCUParams instance structs. we dont really ever need multiple instances of CCUParams floating around so we dont need to make a struct for them.

8 changes: 8 additions & 0 deletions lib/constants/pidParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <Arduino.h>

struct PIDParams {
uint32_t PIDrunInterval = 12;
float Kp = 1;
float Ki = 1;
float Kd = 1;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment about CCUParams struct

22 changes: 22 additions & 0 deletions lib/interfaces/EthernetInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Template message
{
std::array<std::array<std::optional<volt>, 12>, num_chips> voltages;
std::array<celcius, 4 * num_chips> cell_temperatures;
std::array<float, num_humidity_sensors> humidity;
std::array<float, num_board_thermistors> board_temperatures;
float min_voltage;
float max_voltage;
size_t min_voltage_cell_id; // 0 - 125
size_t max_voltage_cell_id; // 0 - 125
size_t max_board_temperature_segment_id; // 0 - 5
size_t max_humidity_segment_id; // 0 - 5
size_t max_cell_temperature_cell_id; // 0 - 47
float total_voltage;
float average_cell_temperature;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls dont push commented code. looks like this is just a copy-paste of what exists in the shared firmware types repo. you can just include a url link to the type instead

*/
struct EthernetInterface
{
float maxCellTemp = 70; //in degress C
float maxCellV = 2.3; //in Volts
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for? is this just the beginnings of the ethernet interface im assuming?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just placeholder values for what the ethernet interface will return, I was using it as input into my unit tests but I think now I will be setting those values within the actual tests.

17 changes: 0 additions & 17 deletions lib/systems/DerateFactorSystem.h

This file was deleted.

56 changes: 28 additions & 28 deletions lib/systems/MainChargeSystem.h
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please turn this into a proper class

Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/* Imports */
#include <Arduino.h>
//#include "EthernetInterface.h" // this is not created yet but will take info from acu and give out usable values
#include "DerateFactorSystem.h" // not made yet need to decide the scaling based on temp of cells
//#include "Charger_data.h"

#include <Arduino.h>
#include <PID_RT.h>
#include "CCUParams.h"
#include "PIDParams.h"
#include "EthernetInterface.h" // this is not created yet but will take info from acu and give out usable values

/* TO-DO
Taking in the message from ACU protobuf recieving
_sys becomes System
*/
/* Constants */

double target_voltage = 12.4; //currently arbitrary
bool current_flow = false;
int max_AC_current = 5;

/* Class Instance Creation */

bool current_flow = false;
float current_setpoint;

/* Function Init */

int set_charge_current();
/* Object Instance Creation */
EthernetInterface ethernetInterface;
CCUParams ccuParams;
PID_RT pid;
PIDParams pidParams;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont just put variables in a header generally, this is a good way to get duplicate symbol errors and it is bad practice (generally speaking)


/* Logic */
void charge_setup() {
pid.setInterval(pidParams.PIDrunInterval);
pid.setKp(pidParams.Kp);
pid.setKi(pidParams.Ki);
pid.setKd(pidParams.Kd);
}

void charge_cycle() {
double max_cell_V = 1; // ethernet_message.get_max_cell_V(); returns the max cell Voltage to allow for decision whether or not to continue charging or not (not yet created) the code for individual cell temps is in the AMS code that already exists

if (max_cell_V >= target_voltage) { //checks whether or not any of the cells are charged yet
void charge_cycle() { //add safe start that ramps up to max over 10 seconds
if (ethernetInterface.maxCellV >= ccuParams.target_voltage || ethernetInterface.maxCellTemp > ccuParams.ideal_cell_temp) { //checks whether or not any of the cells are charged yet
current_flow = false; //this should be a function that actually turns on/off current flow
}
else {
set_charge_current(); //sets the charge current to the PID adjusted value
current_flow = true; //same as line 32
pid.compute(ethernetInterface.maxCellTemp-ccuParams.ideal_cell_temp); //also probably account to voltage
float pidTemp = pid.getOutput();
pid.compute(ethernetInterface.maxCellV-ccuParams.target_voltage);
float pidVolts = pid.getOutput();
float scalar = min(pidTemp, pidVolts);
current_setpoint = ccuParams.max_AC_current * scalar; //replace with just a Pd
current_flow = true; //same as line 32
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put this functionality into a class and not just an anonymous function that affects random global variables. also please prefer functions that take args (if input is needed) and return the results of some computation to be more functional instead of stateful (stateful is what you are doing here)

}


int set_charge_current() { //returns an int value for current
int current_setpoint = max_AC_current * 1; //derateCalculation(); derate calculation actually needs to be thought of
return current_setpoint;
}
}
5 changes: 2 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
lib_deps_shared =
https://github.com/ssilverman/QNEthernet#v0.26.0
https://github.com/hytech-racing/HT_SCHED
https://github.com/RobTillaart/PID_RT.git

https://github.com/RobTillaart/PID_RT
; Teensy41 Environment. This environment is the primary environment for uploading code to the car.
; * Build to verify the on-charger software
; * UPLOAD to compile and upload the on-charger software
Expand Down Expand Up @@ -41,7 +40,7 @@ test_ignore =
test_systems
lib_deps =
${common.lib_deps_shared}
https://github.com/hytech-racing/shared_firmware_interfaces.git
;https://github.com/hytech-racing/shared_firmware_interfaces.git

; Test Systems Environment. This is only for compiling and uploading the hardware-abstracted code.
; * BUILD to verify the SYSTEMS compile.
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Libraries */
#include <Arduino.h>
#include "MainCharge_sys.h"
#include "MainChargeSystem.h"
#include <QNEthernet.h>

/* Parameters */
Expand Down