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

Comp #49

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
11 changes: 5 additions & 6 deletions MPU/include/cascadiamc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class CascadiaMC
int16_t motorSpeed = 0;
uint16_t radiatorTemp = 0;
int16_t feedback_torque = 0;
uint16_t prevVsmState = 0;
uint16_t vsmState = 0;

Timer motorCommand_wait;

Expand Down Expand Up @@ -110,12 +112,6 @@ class CascadiaMC
*/
uint16_t getTorque();

/**
* @brief Raises the Fault for Failing to Generate High Voltage
*
*/
void raiseFault();

/**
* @brief Checks for the Fault for Failing to Generate High Voltage
*
Expand Down Expand Up @@ -175,6 +171,9 @@ class CascadiaMC

int16_t getFeedbackTorque();

void setVSMState(uint16_t p_vsmState);

uint16_t getVSMState();
};

#endif
2 changes: 2 additions & 0 deletions MPU/include/driverio.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class DriverIO
Button regenButton;
Button torqueIncreasePaddle;
Button torqueDecreasePaddle;
Button bmsFaultButton;
uint8_t prev_state = OFF;

bool isCharging = false;
Expand All @@ -59,6 +60,7 @@ class DriverIO
Timer changeStateTimer;
Timer changeTorqueLimitTimer;
Timer changeRegenTimer;
Timer changebms;

bool canTransitionToOff();

Expand Down
6 changes: 6 additions & 0 deletions MPU/include/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class GPIO
PDU *pdu;
float radiatorFanSpeedPercentage = 1.0;

bool bms_prefault = true;

public:
GPIO();

Expand Down Expand Up @@ -61,6 +63,10 @@ class GPIO
* @return int representing the percentage of the motor fan dial
*/
uint8_t getMotorFanDialPercentage();

void toggleBMSPreFault();

uint8_t getBMSPreFault();
};

#endif
1 change: 1 addition & 0 deletions MPU/include/mpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern bool tsms_status;

extern Timer canTest_wait;
extern Timer spinningCheck_wait;
extern Timer bms_fault_timer;

/**
* @brief Writes the fault latch whichever condition you pass (see above enumerator)
Expand Down
11 changes: 10 additions & 1 deletion MPU/include/mpuConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define CAN_CMD_DELAY 5 // ms

// motor torque constants
#define MAXIMUM_TORQUE 2220 // in Nm x 10 (ex: 123 = 12.3Nm)
#define MAXIMUM_TORQUE 2275 // in Nm x 10 (ex: 123 = 12.3Nm)
#define CONT_TORQUE 1020 // ' '
#define POT_LOWER_BOUND 35 // a pot value from 0 to 1023
#define POT_UPPER_BOUND MAX_ADC_VALUE // a pot value from 0 to 1023
Expand Down Expand Up @@ -79,6 +79,15 @@ enum mpu_states
};
extern enum mpu_states mpu_state;

enum precharge_states
{
GLV_ON,
TSMS_ON,
PRECHARGING,
READY,
MC_FAULTED
};

enum Regen_Level_t
{
ZILCH,
Expand Down
23 changes: 13 additions & 10 deletions MPU/src/cascadiamc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ uint16_t CascadiaMC::getTorque()
}


void CascadiaMC::raiseFault()
{
if (isFaulted == false) {
Serial.println("CUCK");
delay(1000);
}
isFaulted = true;
}


bool CascadiaMC::checkFault()
{
return isFaulted;
Expand Down Expand Up @@ -156,4 +146,17 @@ void CascadiaMC::setFeedbackTorque(int16_t fb_torque)
int16_t CascadiaMC::getFeedbackTorque()
{
return feedback_torque;
}

void CascadiaMC::setVSMState(uint16_t p_vsmState) {
if (vsmState > 20) {
vsmState = prevVsmState;
return;
}
prevVsmState = vsmState;
vsmState = p_vsmState;
}

uint16_t CascadiaMC::getVSMState() {
return vsmState;
}
7 changes: 7 additions & 0 deletions MPU/src/driverio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void DriverIO::handleButtonState(bool tsms_status)
torqueDecreasePaddle.checkButtonPin();
torqueIncreasePaddle.checkButtonPin();
regenButton.checkButtonPin();
bmsFaultButton.checkButtonPin();

if (tsms_status == false)
{
Expand Down Expand Up @@ -125,6 +126,11 @@ void DriverIO::handleButtonState(bool tsms_status)
changeRegenTimer.startTimer(CHANGE_REGEN_TIME);
pedals->incrRegenLevel();
}

if(bmsFaultButton.isButtonPressed() && changebms.isTimerExpired())
{
gpio->toggleBMSPreFault();
}
}

if(drive_state == SPEED) {
Expand Down Expand Up @@ -187,6 +193,7 @@ void DriverIO::wheelIO_cb(const CAN_message_t &msg)
decrButton.setButtonState(wheelio.io.button2);
incrButton.setButtonState(wheelio.io.button4);
regenButton.setButtonState(wheelio.io.button5);
bmsFaultButton.setButtonState(wheelio.io.button6);
torqueIncreasePaddle.setButtonState(wheelio.io.paddle_r);
torqueDecreasePaddle.setButtonState(wheelio.io.paddle_l);
accumulatorFanDial.setDialValue(wheelio.io.pot_l);
Expand Down
10 changes: 10 additions & 0 deletions MPU/src/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ void GPIO::setRadiatorFanPercentage(float speed)
uint8_t GPIO::getMotorFanDialPercentage()
{
return radiatorFanSpeedPercentage * 100;
}

void GPIO::toggleBMSPreFault()
{
bms_prefault = !bms_prefault;
}

uint8_t GPIO::getBMSPreFault()
{
return bms_prefault;
}
36 changes: 32 additions & 4 deletions MPU/src/mpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bool ssReady = false;
bool tsms_status = false;
Timer canTest_wait;
Timer spinningCheck_wait;
Timer bms_fault_timer;

void driverioProcess()
{
Expand All @@ -32,13 +33,11 @@ void pedalsProcess()
FaultStatus_t pedalFault = NOT_FAULTED;
pedalFault = pedals.readBrake();
pedalFault = pedals.readAccel();
/*
if(pedalFault == FAULTED)
{
Serial.println("ACCEL FAULT");
isShutdown = true;
}
*/
}


Expand All @@ -50,6 +49,15 @@ void gpioProcess()

isShutdown = isShutdown ? true : !isCANLineOK();
if(!isCANLineOK()){Serial.println("CAN FAULT");}

if(!bms_fault_timer.isTimerExpired() && gpio.getBMSPreFault())
{
writeFaultLatch(FAULTED);
}
else
{
writeFaultLatch(NOT_FAULTED);
}
}

bool isCANLineOK()
Expand Down Expand Up @@ -119,7 +127,7 @@ void sendMPUStatus()
{
union
{
uint8_t msg[6] = {0};
uint8_t msg[8] = {0};

struct
{
Expand All @@ -129,6 +137,8 @@ void sendMPUStatus()
uint8_t torquePercentage;
uint8_t regenStrength;
uint8_t tractionControl;
uint8_t prechargeState;
uint8_t prefault;
} info;
} mpu_msg;

Expand Down Expand Up @@ -167,11 +177,29 @@ void sendMPUStatus()
break;
};

uint16_t vsm_state = motorController.getVSMState();
uint8_t precharge_state = 0;
//Serial.print("VSM State: ");
//Serial.println(vsm_state);
if (vsm_state >= 1 && vsm_state <= 3) {
precharge_state = PRECHARGING;
} else if (vsm_state == 5) {
precharge_state = READY;
} else if (vsm_state == 7) {
precharge_state = MC_FAULTED;
} else if (gpio.getTSMS()){
precharge_state = TSMS_ON;
} else {
precharge_state = GLV_ON;
}

mpu_msg.info.accumulatorFanPercentage = driverio.getAccumulatorFanDialPercentage();
mpu_msg.info.radiatorFanPercentage = gpio.getMotorFanDialPercentage();
mpu_msg.info.torquePercentage = pedals.getTorqueLimitPercentage();
mpu_msg.info.regenStrength = pedals.getRegenLevel();
mpu_msg.info.tractionControl = pedals.getControlLaunch();
mpu_msg.info.prechargeState = precharge_state;
mpu_msg.info.prefault = gpio.getBMSPreFault();

sendMessageCAN1(MPU_STATUS_ID, 6, mpu_msg.msg);
sendMessageCAN1(MPU_STATUS_ID, 8, mpu_msg.msg);
}
19 changes: 19 additions & 0 deletions MPU/src/mpuCanHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define CANMSG_WHEELIO 0x400
#define CANMSG_BMSSTATUS 0x002
#define CANMSG_MC_TORQUE_N_TIMER 0x0AC
#define CANMSG_MC_VSM_STATES 0xAA
#define CANMSG_BMS_PREFAULT 0x500

/***************************************************************************/
/**
Expand Down Expand Up @@ -55,6 +57,10 @@ void bmsCurrentLimits_cb(const CAN_message_t &msg)
bms.setChargeCurrentLimit(chargeCurrentLimit);
}

void vsmState_cb(const CAN_message_t &msg) {
uint16_t vsm_state = (int16_t)(msg.buf[1] << 8) | msg.buf[0];
motorController.setVSMState(vsm_state);
}

void motorMotion_cb(const CAN_message_t &msg)
{
Expand Down Expand Up @@ -98,6 +104,11 @@ void motorFeedbackTorque_cb(const CAN_message_t &msg)
//Serial.println(torque);
}

void bsmPrefault_cb(const CAN_message_t &msg)
{
bms_fault_timer.startTimer(2000);
}

void mpuCanCallback(const CAN_message_t &msg)
{
switch(msg.id)
Expand Down Expand Up @@ -125,8 +136,16 @@ void mpuCanCallback(const CAN_message_t &msg)
break;
case CANMSG_WHEELIO:
driverio.wheelIO_cb(msg);
break;
case CANMSG_MC_TORQUE_N_TIMER:
motorFeedbackTorque_cb(msg);
break;
case CANMSG_MC_VSM_STATES:
vsmState_cb(msg);
break;
case CANMSG_BMS_PREFAULT:
bsmPrefault_cb(msg);
break;
default:
break;
}
Expand Down
19 changes: 13 additions & 6 deletions MPU/src/pedals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ FaultStatus_t Pedals::readAccel()

FaultStatus_t Pedals::readBrake()
{
static uint16_t avg = 0;
static const uint8_t num_samples = 10;

// compare min and max
// signficant dip or absolute min to enter not pressed

//Begin or continue the pedal reading process
uint16_t pedalVal = brakes.readValue();

Expand All @@ -82,13 +88,14 @@ FaultStatus_t Pedals::readBrake()
sendMessageCAN1(0xB1, 2, canBuf);

//If the pedal reading process is NOT finished, return NOT_FAULTED because it hasn't finished collecting data
if(pedalVal == NOT_DONE_READING) return NOT_FAULTED;
if (pedalVal == NOT_DONE_READING) return NOT_FAULTED;

//If the pedal reading process has detected a FAULT, return FAULT
if(brakes.isFaulted() == FAULTED) return FAULTED;
if (brakes.isFaulted() == FAULTED) return FAULTED;

avg = (avg * (num_samples - 1) + pedalVal) / num_samples;

//Set brakePressed to whether or not the brake value is greater than the braking threshold
brakePressed = pedalVal > ANALOG_BRAKE_THRESH ? HIGH : LOW;
brakePressed = avg > ANALOG_BRAKE_THRESH;

pdu->enableBrakeLight(brakePressed);

Expand Down Expand Up @@ -313,7 +320,7 @@ void Pedals::controlLaunch(int16_t *torque, const float mph)
/* Avg calc */
avg_err = (avg_err + error) / num_err_samples;

Serial.println(avg_err);
//Serial.println(avg_err);

if (avg_err > REGAIN_TRACTION_ERR) return;

Expand All @@ -322,7 +329,7 @@ void Pedals::controlLaunch(int16_t *torque, const float mph)
/* Cleansing values */
if (control_torque < *torque) *torque = control_torque;
if (*torque <= 0) *torque = 0;
if (*torque > MAXIMUM_TORQUE) *torque = MAXIMUM_TORQUE; //change ceiling
if (*torque > (MAXIMUM_TORQUE)) *torque = MAXIMUM_TORQUE; //change ceiling

}

Expand Down