Skip to content

PD Board Current reading correction #27

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

Merged
merged 2 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 18 additions & 12 deletions CANbus.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ NS_ :
SG_MUL_VAL_

BS_:

BU_: SME ACC ETC PDB TMAIN VDM TPERIPH_FL TPERIPH_FR TPERIPH_BL TPERIPH_BR SMPC CHARGER_BOARD


BO_ 2147483648 VECTOR__INDEPENDENT_SIG_MSG: 0 Vector__XXX
SG_ ACC_STATUS_PRECARGE_DONE : 5|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ ACC_SEG5_VOLTS_CELL0 : 0|8@1+ (0.01,2) [2|4.55] "V" Vector__XXX
Expand Down Expand Up @@ -237,20 +240,20 @@ BO_ 393 ETC_TPDO_STATUS: 8 ETC

BO_ 410 PDB_TPDO_POWER_A: 8 PDB
SG_ PDB_POWER_A_GLV_VOLTAGE : 0|16@1+ (1,0) [0|65535] "mV" Vector__XXX
SG_ PDB_POWER_A_CURRENT_SHUTDOWN : 16|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_A_CURRENT_ACC : 24|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_A_CURRENT_ETC : 32|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_A_CURRENT_BPS : 40|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_A_CURRENT_TRACTIVE : 48|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_A_CURRENT_BSPD : 56|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_A_CURRENT_SHUTDOWN : 16|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_A_CURRENT_ACC : 24|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_A_CURRENT_ETC : 32|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_A_CURRENT_BPS : 40|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_A_CURRENT_TRACTIVE : 48|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_A_CURRENT_BSPD : 56|8@1+ (1,0) [0|255] "lsb" Vector__XXX

BO_ 666 PDB_TPDO_POWER_B: 8 PDB
SG_ PDB_POWER_B_CURRENT_TELEMETRY : 0|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_B_CURRENT_PDB : 8|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_B_CURRENT_DASH : 16|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_B_CURRENT_RTML : 24|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_B_CURRENT_EXTRA_1 : 32|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_B_CURRENT_EXTRA_2 : 40|8@1+ (0.1,0) [0|25.5] "A" Vector__XXX
SG_ PDB_POWER_B_CURRENT_TELEMETRY : 0|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_B_CURRENT_PDB : 8|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_B_CURRENT_DASH : 16|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_B_CURRENT_RTML : 24|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_B_CURRENT_EXTRA_1 : 32|8@1+ (1,0) [0|255] "lsb" Vector__XXX
SG_ PDB_POWER_B_CURRENT_EXTRA_2 : 40|8@1+ (1,0) [0|255] "lsb" Vector__XXX

BO_ 416 TMAIN_TPDO_DATA: 8 TMAIN
SG_ TMAIN_DATA_BRAKES_F : 0|16@1+ (0.1,0) [0|0] "psi" Vector__XXX
Expand Down Expand Up @@ -375,9 +378,12 @@ BO_ 1168 SMPC_TPDO_SERIAL_PART_NUM: 8 SMPC
SG_ SMPC_SER_SERIAL_NUMBER : 16|32@1+ (1,0) [0|2704] "" Vector__XXX
SG_ SMPC_SER_PART_NUMBER : 0|16@1+ (1,0) [0|65535] "" Vector__XXX



BA_DEF_ "BusType" STRING ;
BA_DEF_ BO_ "GenMsgBackgroundColor" STRING ;
BA_DEF_ BO_ "GenMsgForegroundColor" STRING ;
BA_DEF_DEF_ "BusType" "CAN";
BA_DEF_DEF_ "GenMsgBackgroundColor" "#c8c8c8";
BA_DEF_DEF_ "GenMsgForegroundColor" "#000000";

30 changes: 14 additions & 16 deletions PDB/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "mbed.h"
#include <vector>
#include <numeric>
#include <map>
#include <numeric>
#include <vector>

DigitalOut dcdc_ctrl(PB_5);
DigitalOut reset_ctrl(PA_1);
Expand All @@ -14,7 +14,7 @@ AnalogIn viout(PB_0);

CAN* can;

EventQueue queue(32* EVENTS_EVENT_SIZE);
EventQueue queue(32 * EVENTS_EVENT_SIZE);

uint16_t glv;

Expand All @@ -31,7 +31,6 @@ vector<uint8_t> dash_curr_vec;
vector<uint8_t> ex1_curr_vec;
vector<uint8_t> etc_curr_vec;


uint8_t rtm_curr;
uint8_t bps_curr;
uint8_t bspd_curr;
Expand All @@ -45,7 +44,6 @@ uint8_t dash_curr;
uint8_t ex1_curr;
uint8_t etc_curr;


enum DEVICES {
RTM = 1,
BPS = 2,
Expand All @@ -61,14 +59,12 @@ enum DEVICES {
ETC = 14
};


void send_PDB_TPDO_POWER_A();
void send_PDB_TPDO_POWER_B();

uint8_t read_current();

uint8_t process_current(std::vector<uint8_t> vec);

uint8_t process_current(std::vector<uint8_t> &vec);

void send_PDB_TPDO_POWER_A() {
CANMessage msg;
Expand Down Expand Up @@ -100,13 +96,11 @@ void send_PDB_TPDO_POWER_B() {
// printf("CAN B sent\n");
}



int main(){
int main() {
printf("entering main\n");

can = new CAN(PA_11, PA_12, 500000);
can->filter(0x188, 0xFF, CANAny); //ACC_TPDO_STATUS TODO: does this even work?
can->filter(0x188, 0xFF, CANAny); // ACC_TPDO_STATUS TODO: does this even work?

queue.call_every(200ms, send_PDB_TPDO_POWER_A);
queue.call_every(200ms, send_PDB_TPDO_POWER_B);
Expand Down Expand Up @@ -174,16 +168,20 @@ int main(){
}
}

uint8_t process_current(std::vector<uint8_t> vec) {
uint8_t process_current(std::vector<uint8_t> &vec) {
vec.push_back(read_current());
if (vec.size() > 5)
{
vec.erase(vec.begin());
}
return std::accumulate(vec.begin(), vec.end(), 0)/vec.size();
return std::accumulate(vec.begin(), vec.end(), 0) / vec.size();
}

uint8_t read_current() {
double current_A = ((viout.read() * 3.3) - 1.65) / 0.055;
return clamp(static_cast<int>(current_A / 10), 0, 255);
// Read the current sensor and store the value as a uint8 [0,255]
// Could do this with .read_uint16() but the bitshift would be different
// depending on the ADC bitdepth and this is guaranteed to work.

// TODO: Update DBC with Voltage-Current mappings for each sensor
return static_cast<uint8_t>(viout.read() * 255);
}