Index: firmware/App/Controllers/DialOutFlow.c =================================================================== diff -u -rb3712df3e9e729dc9d4e4b35a0c4567f07882d2f -r4410663759b990e232cd02b24c8c21fed87c01c4 --- firmware/App/Controllers/DialOutFlow.c (.../DialOutFlow.c) (revision b3712df3e9e729dc9d4e4b35a0c4567f07882d2f) +++ firmware/App/Controllers/DialOutFlow.c (.../DialOutFlow.c) (revision 4410663759b990e232cd02b24c8c21fed87c01c4) @@ -113,6 +113,8 @@ #define DOP_MOTOR_SPEED_RPM_TO_PWM(rpm) ( ( (F32)(rpm) / DOP_100_PCT_PWM_RPM_RANGE ) + DOP_PWM_ZERO_OFFSET ) /// Macro converts a PWM to an estimated flow rate. #define DOP_ML_PER_MIN_FROM_PWM(pwm) ( ( ( pwm - DOP_PWM_ZERO_OFFSET ) - 0.0972F ) / 0.0009F ) +/// Macro converts a PWM to an estimated flow rate (basic version). +#define DOP_ML_PER_MIN_FROM_PWM_BASIC(pwm) ( ( ( ( pwm ) - DOP_PWM_ZERO_OFFSET ) * DOP_100_PCT_PWM_RPM_RANGE ) * 0.2 ) #define PUMP_DIR_ERROR_COUNT_MASK 0x3F ///< Bit mask for pump direction error counter. #define DOP_MIN_DIR_CHECK_SPEED_RPM 10.0F ///< Minimum motor speed before we check pump direction. @@ -221,6 +223,7 @@ static void resetDialOutFlowMovingAverage( void ); static void filterDialOutFlowReadings( F64 flow ); +static BOOL setDialOutPumpToFixedPWM( F32 pwm ); /*********************************************************************//** * @brief @@ -1193,6 +1196,73 @@ /*********************************************************************//** * @brief + * The setDialOutPumpToFixedPWM function sets a new pwm value and pump direction. + * @details Inputs: isDialOutPumpOn, dialOutPumpPWMDutyCyclePct, dialOutPumpDirectionSet, + * dialOutPumpState + * @details Outputs: dialOutPumpControlMode, dialOutPumpDirection, dialOutPumpPWMDutyCyclePct + * @param pwm the new pwm value + * @return TRUE if new flow rate & dir are set, FALSE if not + **************************************************************************/ +static BOOL setDialOutPumpToFixedPWM( F32 pwm ) +{ + MOTOR_DIR_T dir = ( pwm < 0.0F ? MOTOR_DIR_REVERSE : MOTOR_DIR_FORWARD ); + BOOL result = FALSE; + F32 pwmFabs = fabs(pwm); + + // Direction change while pump is running is not allowed unless we are turning off + if ( ( FALSE == isDialOutPumpOn ) && ( dir != dialOutPumpDirectionSet ) ) + { + dialOutPumpDirection = dir; + } + + // Direction change while pump is running is not allowed + if ( ( ( FALSE == isDialOutPumpOn ) || ( dir == dialOutPumpDirectionSet ) ) || ( pwmFabs <= MIN_DIAL_OUT_PUMP_PWM_DUTY_CYCLE ) ) + { + // Don't interrupt pump control unless rate is changing + if ( ( pwmFabs != dialOutPumpPWMDutyCyclePct ) ) + { + resetDialOutFlowMovingAverage(); + dialOutPumpControlMode = PUMP_CONTROL_MODE_OPEN_LOOP; + dialOutPumpPWMDutyCyclePct = RANGE( pwmFabs, MIN_DIAL_OUT_PUMP_PWM_DUTY_CYCLE, MAX_DIAL_OUT_PUMP_PWM_DUTY_CYCLE ); + lastGivenRate = DOP_ML_PER_MIN_FROM_PWM_BASIC( pwmFabs ); + + switch ( dialOutPumpState ) + { + case DIAL_OUT_PUMP_RAMPING_UP_STATE: // See if we need to reverse direction of ramp + if ( dialOutPumpPWMDutyCyclePct < dialOutPumpPWMDutyCyclePctSet ) + { + dialOutPumpState = DIAL_OUT_PUMP_RAMPING_DOWN_STATE; + } + break; + case DIAL_OUT_PUMP_RAMPING_DOWN_STATE: // See if we need to reverse direction of ramp + if ( dialOutPumpPWMDutyCyclePct > dialOutPumpPWMDutyCyclePctSet ) + { + dialOutPumpState = DIAL_OUT_PUMP_RAMPING_UP_STATE; + } + break; + case DIAL_OUT_PUMP_CONTROL_TO_TARGET_STATE: // Start ramp to new target in appropriate direction + if ( dialOutPumpPWMDutyCyclePctSet > dialOutPumpPWMDutyCyclePct ) + { + dialOutPumpState = DIAL_OUT_PUMP_RAMPING_DOWN_STATE; + } + else + { + dialOutPumpState = DIAL_OUT_PUMP_RAMPING_UP_STATE; + } + break; + default: + // Ok - not all states need to be handled here + break; + } + result = TRUE; + } + } + + return result; +} + +/*********************************************************************//** + * @brief * The getTotalTargetDialOutUFVolumeInMl function gets the target UF volume. * @details Inputs: referenceUFVolumeInMl * @details Outputs: none @@ -1687,15 +1757,12 @@ BOOL testSetDialOutPumpTargetDutyCycle( F32 value ) { BOOL result = FALSE; - MOTOR_DIR_T dir = ( value < 0.0F ? MOTOR_DIR_REVERSE : MOTOR_DIR_FORWARD ); F32 absolutePWM = fabs( value ); - F32 targetRate = DOP_ML_PER_MIN_FROM_PWM( absolutePWM ); // check for max of pump pwm for acceptance. *** Function used in dialyzer re-prime, so no Dialin login required *** if ( absolutePWM < MAX_DIAL_OUT_PUMP_PWM_DUTY_CYCLE ) { - setDialOutPumpTargetRate( (U32)targetRate, dir, PUMP_CONTROL_MODE_OPEN_LOOP ); - result = TRUE; + result = setDialOutPumpToFixedPWM( value ); } return result;