Index: firmware/App/Controllers/BloodFlow.c =================================================================== diff -u -rcb5c7321fae3036d7a3641ae49097b4b361270f5 -r6b6b337c1c0e7dd7c1b7311a39596473d7214ee4 --- firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision cb5c7321fae3036d7a3641ae49097b4b361270f5) +++ firmware/App/Controllers/BloodFlow.c (.../BloodFlow.c) (revision 6b6b337c1c0e7dd7c1b7311a39596473d7214ee4) @@ -29,8 +29,8 @@ #define MAX_BLOOD_FLOW_RATE 600 // mL/min #define MAX_BLOOD_PUMP_PWM_STEP_CHANGE 0.005 // duty cycle TODO - fixed or parameterized or set in motor controller? -#define BP_SPEED_ADC_2_RPM_FACTOR 0.05 // conversion factor from ADC counts to RPM for blood pump motor TODO - set appropriate value -#define BP_CURRENT_ADC_2_MA_FACTOR 0.1 // conversion factor from ADC counts to mA for blood pump motor TODO - set appropriate value +#define BP_SPEED_ADC_2_RPM_FACTOR 1.0 // conversion factor from ADC counts to RPM for blood pump motor TODO - set appropriate value +#define BP_CURRENT_ADC_2_MA_FACTOR 1.0 // conversion factor from ADC counts to mA for blood pump motor TODO - set appropriate value typedef enum BloodPump_States { @@ -50,8 +50,8 @@ } BLOOD_FLOW_SELF_TEST_STATE_T; // CAN3 port pin assignments for pump stop and direction outputs -#define STOP_CAN3_PORT_MASK 0x00000002 // (Tx - re-purposed as output GPIO) -#define DIR_CAN3_PORT_MASK 0x00000002 // (Rx - re-purposed as output GPIO) +#define STOP_CAN3_PORT_MASK 0x00000002 // (Tx - re-purposed as output GPIO for blood pump stop signal) +#define DIR_CAN3_PORT_MASK 0x00000002 // (Rx - re-purposed as output GPIO for blood pump direction signal) // blood pump stop and direction macros #define SET_BP_DIR() {canREG3->RIOC |= DIR_CAN3_PORT_MASK;} #define SET_BP_STOP() {canREG3->TIOC |= STOP_CAN3_PORT_MASK;} @@ -62,15 +62,16 @@ static BLOOD_PUMP_STATE_T bloodPumpState = BLOOD_PUMP_OFF_STATE; // current state of blood flow controller state machine static BOOL isBloodPumpOn = FALSE; // blood pump is currently running -static U32 bloodPumpTargetFlowRate = 0; // requested blood flow rate -static U32 bloodPumpTargetFlowRateSet = 0; // currently set blood flow rate +DATA_DECL( U32, TargetBloodFlowRate, targetBloodFlowRate, 0, 0 ); // requested blood flow rate +static U32 targetBloodFlowRateSet = 0; // currently set blood flow rate static F32 bloodPumpPWMDutyCyclePct = 0.0; // initial blood pump PWM duty cycle static F32 bloodPumpPWMDutyCyclePctSet = 0.0; // currently set blood pump PWM duty cycle static MOTOR_DIR_T bloodPumpDirection = MOTOR_DIR_FORWARD; // requested blood flow direction static MOTOR_DIR_T bloodPumpDirectionSet = MOTOR_DIR_FORWARD; // currently set blood flow direction -static F32 adcBloodPumpSpeedRPM = 0; -static F32 adcBloodPumpCurrentmA = 0; +DATA_DECL( F32, MeasuredBloodFlowRate, measuredBloodFlowRate, 0.0, 0.0 ); // requested blood flow rate +DATA_DECL( F32, MeasuredBloodPumpSpeed, adcBloodPumpSpeedRPM, 0.0, 0.0 ); // requested blood flow rate +DATA_DECL( F32, MeasuredBloodPumpCurrent, adcBloodPumpCurrentmA, 0.0, 0.0 ); // requested blood flow rate static BLOOD_FLOW_SELF_TEST_STATE_T bloodPumpSelfTestState = BLOOD_FLOW_SELF_TEST_STATE_START; static U32 bloodPumpSelfTestTimerCount = 0; @@ -106,7 +107,7 @@ * pump direction. * @details * Inputs : isBloodPumpOn, bloodPumpDirectionSet - * Outputs : bloodPumpTargetFlowRate, bloodPumpdirection, bloodPumpPWMDutyCyclePct + * Outputs : targetBloodFlowRate, bloodPumpdirection, bloodPumpPWMDutyCyclePct * @param flowRate : new target blood flow rate * @param dir : new blood flow direction * @return TRUE if new flow rate & dir are set, FALSE if not @@ -121,7 +122,7 @@ // verify flow rate if ( flowRate <= MAX_BLOOD_FLOW_RATE ) { - bloodPumpTargetFlowRate = flowRate; + targetBloodFlowRate.data = flowRate; bloodPumpDirection = dir; // TODO - this is temporary conversion to initial duty cycle bloodPumpPWMDutyCyclePct = ( (F32)flowRate / 800.0 ); @@ -140,7 +141,7 @@ } break; case BLOOD_PUMP_CONTROL_TO_TARGET_STATE: // start ramp in appropriate direction - if ( bloodPumpTargetFlowRate < bloodPumpTargetFlowRateSet ) + if ( targetBloodFlowRateSet > getTargetBloodFlowRate() ) { bloodPumpState = BLOOD_PUMP_RAMPING_DOWN_STATE; } @@ -175,8 +176,11 @@ *************************************************************************/ void execBloodFlowMonitor( void ) { - adcBloodPumpSpeedRPM = (F32)getIntADCReading( INT_ADC_BLOOD_PUMP_SPEED ) * BP_SPEED_ADC_2_RPM_FACTOR; - adcBloodPumpCurrentmA = (F32)getIntADCReading( INT_ADC_BLOOD_PUMP_MOTOR_CURRENT ) * BP_CURRENT_ADC_2_MA_FACTOR; + U16 bpRPM = getIntADCReading( INT_ADC_BLOOD_PUMP_SPEED ); + U16 bpmA = getIntADCReading( INT_ADC_BLOOD_PUMP_MOTOR_CURRENT ); + + adcBloodPumpSpeedRPM.data = (F32)(SIGN_FROM_12_BIT_VALUE(bpRPM)) * BP_SPEED_ADC_2_RPM_FACTOR; + adcBloodPumpCurrentmA.data = (F32)(SIGN_FROM_12_BIT_VALUE(bpmA)) * BP_CURRENT_ADC_2_MA_FACTOR; } /************************************************************************* @@ -219,7 +223,7 @@ * The handleBloodPumpOffState function handles the blood pump off state \n * of the blood pump controller state machine. * @details - * Inputs : bloodPumpTargetFlowRate, bloodPumpDirection + * Inputs : targetBloodFlowRate, bloodPumpDirection * Outputs : bloodPumpPWMDutyCyclePctSet, bloodPumpDirectionSet, isBloodPumpOn * @param none * @return next state @@ -229,7 +233,7 @@ BLOOD_PUMP_STATE_T result = BLOOD_PUMP_OFF_STATE; // if we've been given a flow rate, setup ramp up and transition to ramp up state - if ( bloodPumpTargetFlowRate > 0 ) + if ( getTargetBloodFlowRate() > 0 ) { // set initial PWM duty cycle bloodPumpPWMDutyCyclePctSet = MAX_BLOOD_PUMP_PWM_STEP_CHANGE; @@ -261,7 +265,7 @@ BLOOD_PUMP_STATE_T result = BLOOD_PUMP_RAMPING_UP_STATE; // have we been asked to stop the blood pump? - if ( 0 == bloodPumpTargetFlowRate ) + if ( 0 == getTargetBloodFlowRate() ) { // start ramp down to stop bloodPumpPWMDutyCyclePctSet -= MAX_BLOOD_PUMP_PWM_STEP_CHANGE; @@ -271,7 +275,7 @@ // have we reached end of ramp up? else if ( bloodPumpPWMDutyCyclePctSet >= bloodPumpPWMDutyCyclePct ) { - bloodPumpTargetFlowRateSet = bloodPumpTargetFlowRate; + targetBloodFlowRateSet = getTargetBloodFlowRate(); result = BLOOD_PUMP_CONTROL_TO_TARGET_STATE; } // continue ramp up @@ -302,7 +306,7 @@ if ( bloodPumpPWMDutyCyclePctSet < MAX_BLOOD_PUMP_PWM_STEP_CHANGE ) { isBloodPumpOn = FALSE; - bloodPumpTargetFlowRateSet = 0; + targetBloodFlowRateSet = 0; bloodPumpPWMDutyCyclePctSet = 0.0; etpwmSetCmpA( etpwmREG1, 0 ); etpwmStopTBCLK(); @@ -312,7 +316,7 @@ // have we reached end of ramp down? else if ( bloodPumpPWMDutyCyclePctSet <= bloodPumpPWMDutyCyclePct ) { - bloodPumpTargetFlowRateSet = bloodPumpTargetFlowRate; + targetBloodFlowRateSet = getTargetBloodFlowRate(); result = BLOOD_PUMP_CONTROL_TO_TARGET_STATE; } // continue ramp down @@ -356,6 +360,7 @@ static void stopBloodPump( void ) { SET_BP_STOP(); +// CLR_BP_STOP(); } /************************************************************************* @@ -370,6 +375,7 @@ static void releaseBloodPumpStop( void ) { CLR_BP_STOP(); +// SET_BP_STOP(); } /************************************************************************* @@ -388,12 +394,12 @@ { case MOTOR_DIR_FORWARD: bloodPumpDirectionSet = dir; - CLR_BP_DIR(); + SET_BP_DIR(); break; case MOTOR_DIR_REVERSE: bloodPumpDirectionSet = dir; - SET_BP_DIR(); + CLR_BP_DIR(); break; default: @@ -402,4 +408,114 @@ } } +/************************************************************************* + * @brief getTargetBloodFlowRate + * The getTargetBloodFlowRate function gets the current target blood flow \n + * rate. + * @details + * Inputs : targetBloodFlowRate + * Outputs : none + * @param none + * @return the current target blood flow rate (in mL/min). + *************************************************************************/ +DATA_GET( U32, getTargetBloodFlowRate, targetBloodFlowRate ) +/************************************************************************* + * @brief getMeasuredBloodFlowRate + * The getMeasuredBloodFlowRate function gets the measured blood flow \n + * rate. + * @details + * Inputs : measuredBloodFlowRate + * Outputs : none + * @param none + * @return the current blood flow rate (in mL/min). + *************************************************************************/ +DATA_GET( F32, getMeasuredBloodFlowRate, measuredBloodFlowRate ) + +/************************************************************************* + * @brief getMeasuredBloodPumpSpeed + * The getMeasuredBloodPumpSpeed function gets the measured blood pump \n + * speed. + * @details + * Inputs : adcBloodPumpSpeedRPM + * Outputs : none + * @param none + * @return the current blood pump speed (in RPM). + *************************************************************************/ +DATA_GET( F32, getMeasuredBloodPumpSpeed, adcBloodPumpSpeedRPM ) + +/************************************************************************* + * @brief getMeasuredBloodPumpCurrent + * The getMeasuredBloodPumpCurrent function gets the measured blood pump \n + * current. + * @details + * Inputs : adcBloodPumpCurrentmA + * Outputs : none + * @param none + * @return the current blood pump current (in mA). + *************************************************************************/ +DATA_GET( F32, getMeasuredBloodPumpCurrent, adcBloodPumpCurrentmA ) + + +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/ + + +/************************************************************************* + * @brief testSetTargetBloodFlowRateOverride and testResetTargetBloodFlowRateOverride + * The testSetTargetBloodFlowRateOverride function overrides the target \n + * blood flow rate. \n + * The testResetTargetBloodFlowRateOverride function resets the override of the \n + * target blood flow rate. + * @details + * Inputs : none + * Outputs : targetBloodFlowRate + * @param value : override target blood flow rate (in mL/min) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +DATA_OVERRIDE_FUNC( U32, testSetTargetBloodFlowRateOverride, testResetTargetBloodFlowRateOverride, targetBloodFlowRate ) + +/************************************************************************* + * @brief testSetMeasuredBloodFlowRateOverride and testResetMeasuredBloodFlowRateOverride + * The testResetMeasuredBloodFlowRateOverride function overrides the measured \n + * blood flow rate. \n + * The testResetOffButtonStateOverride function resets the override of the \n + * measured blood flow rate. + * @details + * Inputs : none + * Outputs : measuredBloodFlowRate + * @param value : override measured blood flow rate (in mL/min) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodFlowRateOverride, testResetMeasuredBloodFlowRateOverride, measuredBloodFlowRate ) + +/************************************************************************* + * @brief testSetMeasuredBloodPumpSpeedOverride and testResetMeasuredBloodPumpSpeedOverride + * The testSetMeasuredBloodPumpSpeedOverride function overrides the measured \n + * blood pump motor speed. \n + * The testResetMeasuredBloodPumpSpeedOverride function resets the override of the \n + * measured blood pump motor speed. + * @details + * Inputs : none + * Outputs : adcBloodPumpSpeedRPM + * @param value : override measured blood pump speed (in RPM) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodPumpSpeedOverride, testResetMeasuredBloodPumpSpeedOverride, adcBloodPumpSpeedRPM ) + +/************************************************************************* + * @brief testSetMeasuredBloodPumpCurrentOverride and testResetMeasuredBloodPumpCurrentOverride + * The testSetMeasuredBloodPumpCurrentOverride function overrides the measured \n + * blood pump motor current. \n + * The testResetMeasuredBloodPumpCurrentOverride function resets the override of the \n + * measured blood pump motor current. + * @details + * Inputs : none + * Outputs : adcBloodPumpCurrentmA + * @param value : override measured blood pump current (in mA) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +DATA_OVERRIDE_FUNC( F32, testSetMeasuredBloodPumpCurrentOverride, testResetMeasuredBloodPumpCurrentOverride, adcBloodPumpCurrentmA ) + +