Skip to content

Commit

Permalink
Merged main into coulomb_counting
Browse files Browse the repository at this point in the history
  • Loading branch information
jhwang04 committed Jul 9, 2024
2 parents f25220e + db987fa commit daa6799
Show file tree
Hide file tree
Showing 40 changed files with 1,988 additions and 418 deletions.
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ EXTRACT_ALL = YES
# be included in the documentation.
# The default value is: NO.

EXTRACT_PRIVATE = NO
EXTRACT_PRIVATE = YES

# If the EXTRACT_PRIV_VIRTUAL tag is set to YES, documented private virtual
# methods of a class will be included in the documentation.
Expand Down
17 changes: 17 additions & 0 deletions ETHERNET.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```mermaid
flowchart TD
ethernet[ethernet port] --> qnethernet[encoded ethernet data packets]
qnethernet --> port[protobuf union msgs port]
port --> ht_eth[HT ethernet interface]
ht_eth --> union_dec[union msgs splitter]
union_dec --> int1[interface 1 message buffer]
union_dec --> int2[interface 2 message buffer]
union_dec --> intN[interface N message buffer]
```

## explanation

### receiving
Packets stream over ethernet and hit the ethernet port itself. The ethernet library has an internal queue for the UDP packets received. We know that a specific port id (say 4521) all messages will be a protobuf union msg that can contain only one of the types of messages that we will be sending (config control, TCU status, CASE msgs, etc.) and the union decoder method in the ethernet interface itself will handle this. The ethernet shall be able to receive multiple messages in one loop (of a limited number) and in between iterations of the loop the ethernet driver will hold the un-parsed messages in it's queue.

The union message decoder will handle parsing of all of the messages in the queue and will be able to determine what message the union pb packet holds. If all messages of a specific type are needed, The message decoder will then add the decoded particular protobuf message struct to the message queue in the respective interface that way if multiple messages of the same type appear in one loop iteration they can all be processed by the underlying system / interface. If the interface just needs the latest version of a message each loop, the decoder will just update the message instance in its respective interface.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,63 @@ new MCU code:
- interface level:
- hytech_can interface
- spi interfaces: SPI adcs for load cells, steering input, glv, etc.


```mermaid
flowchart LR
subgraph user_inputs[user defined inputs]
simulink
o_params
CAN_data
other_protos
end
subgraph user_interact[user interfaces]
fxglv_live
mcap
database
param_server
end
subgraph user_code_interact[user embedded code interfaces]
MCU
end
subgraph CASE_lib_repo[CASE lib repo gen]
CASE_lib
params
outputs
end
subgraph HT_params[HT_params repo gen]
param_defaults
nanopb
end
simulink[CASE simulink model] --> CASE_lib
simulink --> params[CASE defined params.json]
simulink --> outputs[CASE_outputs.proto]
params --> param_protos[params.proto]
params --> param_defaults[config defaults header]
param_defaults --> MCU
o_params[other params.json] --> param_protos
outputs --> h_proto
param_protos --> param_server[parameter server]
h_proto --> data_acq
data_acq --> fxglv_live[live foxglove]
data_acq --> mcap[output mcap files]
CAN_data[CAN message definitions] --> can_protos[CAN messages in hytech.proto]
can_protos --> h_proto
mcap --> database[mcap metadata defined database]
CASE_lib --> MCU
CAN_data --> dbc_h[dbc to C code gen hytech.h for CAN]
param_protos --> h_proto[hytech.proto]
dbc_h --> MCU
h_proto --> nanopb[protobuf msg defs ht_eth.pb.h]
nanopb --> MCU
other_protos[other msgs.proto]
other_protos --> h_proto
```
21 changes: 21 additions & 0 deletions include/InterfaceParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef INTERFACEPARAMS
#define INTERFACEPARAMS
#include "NativeEthernet.h"

namespace EthParams
{
uint8_t default_MCU_MAC_address[6] =
{0x04, 0xe9, 0xe5, 0x10, 0x1f, 0x22};

const IPAddress default_MCU_ip(192, 168, 1, 30);
const IPAddress default_TCU_ip(192, 168, 1, 68);

const uint16_t default_protobuf_send_port = 2001;
const uint16_t default_protobuf_recv_port = 2000;

const IPAddress default_netmask(255, 255, 255, 0);
const IPAddress default_gateway(192, 168, 0, 1);

const uint16_t default_buffer_size = 512;
}
#endif
71 changes: 46 additions & 25 deletions include/MCU_rev15_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
#define __MCU15_H__

#include "PedalsSystem.h"
#ifndef TESTING_SYSTEMS
#include "InterfaceParams.h"
#endif


// pindefs
const int ADC1_CS = 34;
const int ADC2_CS = 33;
const int ADC3_CS = 29;
const int ADC1_CS = 34; // Main ECU
const int ADC2_CS = 33; // FL corner board
const int ADC3_CS = 29; // FR corner board
const int SOFTWARE_OK = 8;
const int WATCHDOG_INPUT = 32;

Expand All @@ -26,11 +30,22 @@ const int MCU15_FR_POTS_CHANNEL = 1;
const int MCU15_FL_LOADCELL_CHANNEL = 2;
const int MCU15_FR_LOADCELL_CHANNEL = 2;

//MCU teensy analog channels
const int MCU15_TEENSY_ADC_CHANNELS = 2;
// const int MCU15_NUM_THERMISTORS = 2;
const int MCU15_THERM_FL = 38;
const int MCU15_THERM_FR = 41;
// const int MCU15_THERM_FL_CHANNEL = 0;
// const int MCU15_THERM_FR_CHANNEL = 1;

const int DEFAULT_ANALOG_PINS[MCU15_TEENSY_ADC_CHANNELS] = {MCU15_THERM_FL, MCU15_THERM_FR};
// Time intervals
const unsigned long SETUP_PRESENT_ACTION_INTERVAL = 1000;
const unsigned long BUZZER_ON_INTERVAL = 2000;
const unsigned long INVERTER_ENABLING_TIMEOUT_INTERVAL = 5000;

//glv sense conversion
const float GLV_SENSE_SCALE = .0073 * 4.0865;
// Communication speeds
const unsigned long INV_CAN_BAUDRATE = 500000;
const unsigned long TELEM_CAN_BAUDRATE = 500000;
Expand All @@ -39,17 +54,17 @@ const unsigned long TELEM_CAN_BAUDRATE = 500000;
// All of these values are the PEDAL min/max
// the sensor min/max that trip implaus are calculated
// in the PedalsSystem constructor
const int ACCEL1_PEDAL_MAX = 3330;
const int ACCEL2_PEDAL_MAX = 388;
const int ACCEL1_PEDAL_MAX = 3409; // 3330
const int ACCEL2_PEDAL_MAX = 259; // 388

const int ACCEL1_PEDAL_MIN = 2124;
const int ACCEL1_PEDAL_MIN = 2129;
const int ACCEL2_PEDAL_MIN = 1517;

const int BRAKE1_PEDAL_MAX = 2200;
const int BRAKE2_PEDAL_MAX = 2200;
const int BRAKE1_PEDAL_MAX = 1945; // 2200;
const int BRAKE2_PEDAL_MAX = 1742; // 2200;

const int BRAKE1_PEDAL_MIN = 785;
const int BRAKE2_PEDAL_MIN = 785;
const int BRAKE1_PEDAL_MIN = 1230; // 785; // 1230 to 1750
const int BRAKE2_PEDAL_MIN = 2437; // 785; // 2450 to 1930

const int ACCEL1_PEDAL_OOR_MIN = 90;
const int ACCEL2_PEDAL_OOR_MIN = 90;
Expand All @@ -64,32 +79,38 @@ const int BRAKE1_PEDAL_OOR_MAX = 4000;
const int BRAKE2_PEDAL_OOR_MAX = 4000;

const float DEFAULT_PEDAL_DEADZONE = 0.05f;
const float DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN = 0.10f;
const float DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN = 0.12f;

const float APPS_ACTIVATION_PERCENTAGE = 0.1f;
const float BRAKE_ACTIVATION_PERCENTAGE = 0.05f;
const float BRAKE_MECH_THRESH = 0.40f;
const float BRAKE_MECH_THRESH = 0.46f;

// Load Cell Defs to convert raw to lbs
// lbs = (raw + offset) * scale

const float LOADCELL_FL_SCALE = 0.0554;
const float LOADCELL_FL_OFFSET = 19.976 / LOADCELL_FL_SCALE;
const float LOADCELL_FL_SCALE = 0.1053;
const float LOADCELL_FL_OFFSET = 19.674 / LOADCELL_FL_SCALE;

const float LOADCELL_FR_SCALE = 0.0514;
const float LOADCELL_FR_OFFSET = 19.892 / LOADCELL_FR_SCALE;
const float LOADCELL_FR_SCALE = 0.097;
const float LOADCELL_FR_OFFSET = 21.183 / LOADCELL_FR_SCALE;

const float LOADCELL_RL_SCALE = 0.0595;
const float LOADCELL_RL_OFFSET = 7.148 / LOADCELL_RL_SCALE;
const float LOADCELL_RL_SCALE = 0.1149;
const float LOADCELL_RL_OFFSET = 13.526 / LOADCELL_RL_SCALE;

const float LOADCELL_RR_SCALE = 0.06;
const float LOADCELL_RR_OFFSET = 23.761 / LOADCELL_RR_SCALE;
const float LOADCELL_RR_SCALE = 0.118;
const float LOADCELL_RR_OFFSET = 25.721 / LOADCELL_RR_SCALE;

// Steering parameters
const float PRIMARY_STEERING_SENSE_OFFSET = -21.18; // units are degrees
const int SECONDARY_STEERING_SENSE_LEFTMOST_BOUND = 812;
const int SECONDARY_STEERING_SENSE_RIGHTMOST_BOUND = 3179;
const int SECONDARY_STEERING_SENSE_CENTER = 1970;
const float STEERING_RANGE_DEGREES = 256.05f;
const float PRIMARY_STEERING_SENSE_OFFSET = 0.0; // units are degrees
const int SECONDARY_STEERING_SENSE_LEFTMOST_BOUND = 790; // 785; // 794 // 812 // 130 deg // 128.95
const int SECONDARY_STEERING_SENSE_RIGHTMOST_BOUND = 3133; // 3087; // 3075 // 3179 // 134 deg // 132.79
const int SECONDARY_STEERING_SENSE_CENTER = 1985; // 1945 // 1960 // 1970
const float STEERING_RANGE_DEGREES = 254.74; //257.0f // 253.0f // 256.05f // 134+130-7(slop)
const float STEERING_IIR_ALPHA = 0.7f; // shaves off around 1 deg of max discrepancy

// TC parameters
const float SIMPLE_TC_REAR_TORQUE_SCALE = 1.0;
// percent front wheels
const float SIMPLE_TC_REGEN_TORQUE_SCALE = 0.6;

#endif /* __MCU15_H__ */
72 changes: 69 additions & 3 deletions lib/interfaces/include/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const float DEFAULT_INIT_VOLTAGE = 3.5;
const float DEFAULT_TEMP_ALPHA = 0.8;
const float DEFAULT_VOLTAGE_ALPHA = 0.8;
const uint16_t MAX_PACK_CHARGE = 48600;
const unsigned long DEFAULT_INITIALIZATION_WAIT_INTERVAL = 5000;


/// @brief this class is for interfacing with the AMS (accumulator management system)
Expand All @@ -40,7 +41,8 @@ class AMSInterface
use_em_for_soc_(true),
charge_(0.0f),
SoC_(0.0f),
has_initialized_charge_(false) {};
has_initialized_charge_(false),
has_received_bms_voltage_(false) {};

/* Overloaded constructor that only takes in software OK pin and uses default voltages and temp*/
AMSInterface(CANBufferType *msg_output_queue, int sw_ok_pin):
Expand Down Expand Up @@ -69,6 +71,54 @@ class AMSInterface
float get_filtered_max_cell_temp();
/* IIR filter and return filtered min cell voltage */
float get_filtered_min_cell_voltage();
/*gets the derate factor for acc system*/
float get_acc_derate_factor();

/**
* Initializes the charge member variable from the voltage of the minimum cell using the VOLTAGE_LOOKUP_TABLE.
*
* @pre The bms_voltages_ member variable MUST be initialized before this function can be called.
* @return The charge, in coulombs, that the charge member variable is initialized to.
*/
float initialize_charge();

/**
* Calculates SoC based on the energy meter CAN message. Calling calculate_SoC_em()
* will update the SoC_ and charge_ member variables.
*
* @param The current tick that the calculate_SoC_em function should use for integration.
*
* @pre The last_tick_ member variable must be updated correctly.
* @post The charge_ field has the updated charge, and the SoC field contains an updated percentage.
*/
void calculate_SoC_em(const SysTick_s &tick);

/**
* Calculates SoC based on the ACU_SHUNT_MEASUREMENTS CAN message. Calling calculate_SoC_acu()
* will update the SoC_ and charge_ member variables.
*
* @param The current tick that the calculate_SoC_em function should use for integration.
*
* @pre The last_tick_ member variable must be updated correctly.
* @post The charge_ field has the updated charge, and the SoC field contains an updated percentage.
*/
void calculate_SoC_acu(const SysTick_s &tick);

/**
* Retrieves the value of the SoC member variable. This function does NOT recalculate
* the SoC_ variable, it only returns the value that is stored.
*
* @return the current value stored in the SoC_ member variable.
*/
float get_SoC() {return SoC_;}

/**
* This is AMSInterface's tick() function. It behaves correctly regardless of the
* since the functions calculate the elapsed time between the given tick and the stored last_tick_.
*
* @param tick The current system tick.
*/
void tick(const SysTick_s &tick);

/**
* Initializes the charge member variable from the voltage of the minimum cell using the VOLTAGE_LOOKUP_TABLE.
Expand Down Expand Up @@ -134,6 +184,9 @@ class AMSInterface
*/
void retrieve_voltage_CAN(CAN_message_t &recvd_msg);

/*Updates Acc_derate_factor*/
void calculate_acc_derate_factor();

/**
* Reads this CAN message into the em_measurements_ member variable. This function
* does NOT apply the fromS() functions on the data. This function uses the NEW CAN library.
Expand Down Expand Up @@ -209,6 +262,8 @@ class AMSInterface
float filtered_min_cell_voltage;
float cell_temp_alpha;
float cell_voltage_alpha;

float acc_derate_factor;

/**
* If set to TRUE, then SoC will use EM.
Expand Down Expand Up @@ -238,7 +293,18 @@ class AMSInterface
/**
* Stores whether or not this AMSInterface has initialized SoC_ or not.
*/
bool has_initialized_charge_;
bool has_initialized_charge_;

/**
* Stores whether or not this AMSInterface has properly received a bms_voltage CAN
* message.
*/
bool has_received_bms_voltage_;

/**
* Stores the time, in milliseconds, when this AMSInterface first received a bms voltages CAN message.
*/
unsigned long timestamp_start_;


// Check if lowest cell temperature is below threshold
Expand All @@ -248,4 +314,4 @@ class AMSInterface

};

#endif /* __AMSINTERFACE_H__ */
#endif /* __AMSINTERFACE_H__ */
2 changes: 1 addition & 1 deletion lib/interfaces/include/HytechCANInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,4 @@ void enqueue_matlab_msg(bufferType *msg_queue, const CAN_MESSAGE_BUS & structure
}


#endif /* HYTECHCANINTERFACE */
#endif /* HYTECHCANINTERFACE */
Loading

0 comments on commit daa6799

Please sign in to comment.