Index: firmware/App/Controllers/DialInFlow.c =================================================================== diff -u -r206e45dff167966b800a628d1708f74e597f1772 -r3aed94a6a0af49bed46f25c900f74e98997ac2e7 --- firmware/App/Controllers/DialInFlow.c (.../DialInFlow.c) (revision 206e45dff167966b800a628d1708f74e597f1772) +++ firmware/App/Controllers/DialInFlow.c (.../DialInFlow.c) (revision 3aed94a6a0af49bed46f25c900f74e98997ac2e7) @@ -14,260 +14,260 @@ * @date (original) 16-Dec-2019 * ***************************************************************************/ - -#include - -#include "etpwm.h" -#include "gio.h" -#include "mibspi.h" - + +#include + +#include "etpwm.h" +#include "gio.h" +#include "mibspi.h" + #include "DialInFlow.h" -#include "FPGA.h" -#include "InternalADC.h" +#include "FPGA.h" +#include "InternalADC.h" #include "NVDataMgmt.h" -#include "OperationModes.h" +#include "OperationModes.h" #include "PersistentAlarm.h" -#include "PIControllers.h" +#include "PIControllers.h" #include "SafetyShutdown.h" #include "SystemCommMessages.h" -#include "SystemComm.h" -#include "TaskGeneral.h" -#include "TaskPriority.h" -#include "Timers.h" - -/** - * @addtogroup DialysateInletFlow - * @{ - */ - -// ********** private definitions ********** - +#include "SystemComm.h" +#include "TaskGeneral.h" +#include "TaskPriority.h" +#include "Timers.h" + +/** + * @addtogroup DialysateInletFlow + * @{ + */ + +// ********** private definitions ********** + /// Interval (ms/task time) at which the dialIn flow data is published on the CAN bus. -#define DIAL_IN_FLOW_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) - -#define MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE 0.0133F ///< Max duty cycle change when ramping up ~ 200 mL/min/s. -#define MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE 0.02F ///< Max duty cycle change when ramping down ~ 300 mL/min/s. -#define MAX_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.89F ///< Controller will error if PWM duty cycle > 90%, so set max to 89%. -#define MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.10F ///< Controller will error if PWM duty cycle < 10%, so set min to 10%. +#define DIAL_IN_FLOW_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) +#define MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE 0.0133F ///< Max duty cycle change when ramping up ~ 200 mL/min/s. +#define MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE 0.02F ///< Max duty cycle change when ramping down ~ 300 mL/min/s. +#define MAX_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.89F ///< Controller will error if PWM duty cycle > 90%, so set max to 89%. +#define MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.10F ///< Controller will error if PWM duty cycle < 10%, so set min to 10%. + #define DIP_CONTROL_INTERVAL_SEC 10 ///< Dialysate inlet pump control interval (in seconds). -/// Interval (ms/task time) at which the dialIn pump is controlled. -static const U32 DIP_CONTROL_INTERVAL = ( DIP_CONTROL_INTERVAL_SEC * MS_PER_SECOND / TASK_GENERAL_INTERVAL ); -#define DIP_P_COEFFICIENT 0.0001F ///< P term for dialIn pump control. -#define DIP_I_COEFFICIENT 0.00075F ///< I term for dialIn pump control. - -#define DIP_HOME_RATE 100 ///< Target pump speed (in estimate mL/min) for homing. -#define DIP_HOME_TIMEOUT_MS 10000 ///< Maximum time allowed for homing to complete (in ms). +/// Interval (ms/task time) at which the dialIn pump is controlled. +static const U32 DIP_CONTROL_INTERVAL = ( DIP_CONTROL_INTERVAL_SEC * MS_PER_SECOND / TASK_GENERAL_INTERVAL ); +#define DIP_P_COEFFICIENT 0.0001F ///< P term for dialIn pump control. +#define DIP_I_COEFFICIENT 0.00075F ///< I term for dialIn pump control. + +#define DIP_HOME_RATE 100 ///< Target pump speed (in estimate mL/min) for homing. +#define DIP_HOME_TIMEOUT_MS 10000 ///< Maximum time allowed for homing to complete (in ms). /// Interval (ms/task time) at which the blood pump speed is calculated (every 40 ms). #define DIP_SPEED_CALC_INTERVAL ( 40 / TASK_PRIORITY_INTERVAL ) /// Number of hall sensor counts kept in buffer to hold last 1 second of count data. #define DIP_SPEED_CALC_BUFFER_LEN ( 1000 / DIP_SPEED_CALC_INTERVAL / TASK_PRIORITY_INTERVAL ) -#define DIP_HALL_EDGE_COUNTS_PER_REV 48 ///< Number of hall sensor edge counts per motor revolution. - +#define DIP_HALL_EDGE_COUNTS_PER_REV 48 ///< Number of hall sensor edge counts per motor revolution. + #define DIP_MAX_FLOW_RATE 1320.0F ///< Maximum measured BP flow rate allowed. #define DIP_MIN_FLOW_RATE -1320.0F ///< Minimum measured BP flow rate allowed. -#define DIP_MAX_FLOW_VS_SPEED_DIFF_RPM 200.0F ///< Maximum difference between measured motor speed and speed implied by measured flow. -#define DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0F ///< Maximum motor speed (RPM) while motor is commanded off. -#define DIP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0F ///< Maximum difference in speed between motor and rotor (in rotor RPM). +#define DIP_MAX_FLOW_VS_SPEED_DIFF_RPM 350.0F ///< Maximum difference between measured motor speed and speed implied by measured flow. +#define DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0F ///< Maximum motor speed (RPM) while motor is commanded off. +#define DIP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0F ///< Maximum difference in speed between motor and rotor (in rotor RPM). #define DIP_MAX_MOTOR_SPEED_ERROR_RPM 300.0F ///< Maximum difference in speed between measured and commanded RPM. -/// Persist time (task intervals) for flow vs. motor speed error condition. +/// Persist time (task intervals) for flow vs. motor speed error condition. static const U32 DIP_FLOW_VS_SPEED_PERSIST = ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL); -/// Persist time (task intervals) for motor off error condition. +/// Persist time (task intervals) for motor off error condition. static const U32 DIP_OFF_ERROR_PERSIST = ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL); -/// Persist time (task intervals) motor speed error condition. +/// Persist time (task intervals) motor speed error condition. static const U32 DIP_MOTOR_SPEED_ERROR_PERSIST = ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL); -/// Persist time (task intervals) rotor speed error condition. +/// Persist time (task intervals) rotor speed error condition. static const U32 DIP_ROTOR_SPEED_ERROR_PERSIST = ((12 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL); -/// Persist time (task intervals) pump direction error condition. +/// Persist time (task intervals) pump direction error condition. static const U32 DIP_DIRECTION_ERROR_PERSIST = (250 / TASK_PRIORITY_INTERVAL); /// Persist time (task intervals) dialysate flow rate out of range error condition. static const U32 DIP_MAX_FLOW_RATE_OUT_OF_RANGE_PERSIST = (1 * MS_PER_SECOND); /// Time threshold to trigger an alarm if Dialysate flow data has not arrived within 3 seconds static const U32 DIP_DIALYSATE_FLOW_DATA_ALARM_THRESHOLD = ((3 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL); - -#define DIP_MAX_CURR_WHEN_STOPPED_MA 150.0F ///< Motor controller current should not exceed this when pump should be stopped. -#define DIP_MAX_CURR_WHEN_RUNNING_MA 2000.0F ///< Motor controller current should not exceed this when pump should be running. -#define DIP_MAX_CURR_ERROR_DURATION_MS 2000 ///< Motor controller current errors persisting beyond this duration will trigger an alarm. - + +#define DIP_MAX_CURR_WHEN_STOPPED_MA 150.0F ///< Motor controller current should not exceed this when pump should be stopped. +#define DIP_MAX_CURR_WHEN_RUNNING_MA 2000.0F ///< Motor controller current should not exceed this when pump should be running. +#define DIP_MAX_CURR_ERROR_DURATION_MS 2000 ///< Motor controller current errors persisting beyond this duration will trigger an alarm. + #define DIAL_IN_PUMP_ADC_FULL_SCALE_V 3.0F ///< BP analog signals are 0-3V (while int. ADC ref V may be different). #define DIAL_IN_PUMP_ADC_ZERO 1998 ///< Mid-point (zero) for ADC readings. ///< Macro converts a 12-bit ADC reading to a signed 16-bit value. #define SIGN_FROM_12_BIT_VALUE(v) ( (S16)(v) - (S16)DIAL_IN_PUMP_ADC_ZERO ) -#define DIP_SPEED_ADC_TO_RPM_FACTOR 1.751752F ///< Conversion factor from ADC counts to RPM for dialIn pump motor (3500 RPM/1998 counts). +#define DIP_SPEED_ADC_TO_RPM_FACTOR 1.751752F ///< Conversion factor from ADC counts to RPM for dialIn pump motor (3500 RPM/1998 counts). #define DIP_MOTOR_RPM_TO_PWM_DC_FACTOR 0.000238F ///< ~42 BP motor RPM = 1% PWM duty cycle -#define DIP_CURRENT_ADC_TO_MA_FACTOR 3.002F ///< Conversion factor from ADC counts to mA for dialIn pump motor. - -#define DIP_REV_PER_LITER 146.84F ///< Rotor revolutions per liter. +#define DIP_CURRENT_ADC_TO_MA_FACTOR 3.002F ///< Conversion factor from ADC counts to mA for dialIn pump motor. + +#define DIP_REV_PER_LITER 146.84F ///< Rotor revolutions per liter. /// Macro converts flow rate to motor RPM. -#define DIP_ML_PER_MIN_TO_PUMP_RPM_FACTOR ( DIP_REV_PER_LITER / ML_PER_LITER ) -#define DIP_GEAR_RATIO 32.0F ///< DialIn pump motor to dialIn pump gear ratio. +#define DIP_ML_PER_MIN_TO_PUMP_RPM_FACTOR ( DIP_REV_PER_LITER / ML_PER_LITER ) +#define DIP_GEAR_RATIO 32.0F ///< DialIn pump motor to dialIn pump gear ratio. #define DIP_PWM_ZERO_OFFSET 0.1F ///< 10% PWM duty cycle = zero speed. -/// Macro converts flow rate to estimate PWM needed to achieve it. +/// Macro converts flow rate to estimate PWM needed to achieve it. #define DIP_PWM_FROM_ML_PER_MIN(rate) ( ( ((rate)*(rate)) * 0.000001 ) + ( (rate) * 0.0005 ) + 0.126 + DIP_PWM_ZERO_OFFSET ) -//#define DIP_PWM_FROM_ML_PER_MIN(rate) ( (rate) * DIP_ML_PER_MIN_TO_PUMP_RPM_FACTOR * DIP_GEAR_RATIO * DIP_MOTOR_RPM_TO_PWM_DC_FACTOR + DIP_PWM_ZERO_OFFSET ) -/// Conversion from PWM duty cycle % to commanded pump motor speed. +//#define DIP_PWM_FROM_ML_PER_MIN(rate) ( (rate) * DIP_ML_PER_MIN_TO_PUMP_RPM_FACTOR * DIP_GEAR_RATIO * DIP_MOTOR_RPM_TO_PWM_DC_FACTOR + DIP_PWM_ZERO_OFFSET ) +/// Conversion from PWM duty cycle % to commanded pump motor speed. PWM range is 10% to 90%. RPM range is 0 to 3200. 3200 / 0.8 = 4000. #define DIP_PWM_TO_MOTOR_SPEED_RPM(pwm) ( ((pwm) - DIP_PWM_ZERO_OFFSET) * 4000.0F ) - -/// Measured dialIn flow is filtered w/ moving average. + +/// Measured dialIn flow is filtered w/ moving average. #define SIZE_OF_ROLLING_AVG 10 #define PUMP_DIR_ERROR_COUNT_MASK 0x3F ///< Bit mask for pump direction error counter. #define DATA_PUBLISH_COUNTER_START_COUNT 30 ///< Data publish counter start count. -/// Enumeration of dialysate inlet pump states. -typedef enum DialInPump_States -{ - DIAL_IN_PUMP_OFF_STATE = 0, ///< Off state for the dialysate inlet pump. - DIAL_IN_PUMP_RAMPING_UP_STATE, ///< Ramping up state for the dialysate inlet pump. - DIAL_IN_PUMP_RAMPING_DOWN_STATE, ///< Ramping down state for the dialysate inlet pump. - DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE, ///< Control to target state for the dialysate inlet pump. - NUM_OF_DIAL_IN_PUMP_STATES ///< Number of dialysate inlet pump states. -} DIAL_IN_PUMP_STATE_T; - -/// Enumeration of dialysate inlet self-test states. -typedef enum DialInFlow_Self_Test_States -{ - DIAL_IN_FLOW_SELF_TEST_STATE_START = 0, ///< Start state for the dialysate inlet pump self-test. - DIAL_IN_FLOW_TEST_STATE_IN_PROGRESS, ///< Test in progress state for the dialysate inlet pump self-test. - DIAL_IN_FLOW_TEST_STATE_COMPLETE, ///< Test completed state for the dialysate inlet pump self-test. - NUM_OF_DIAL_IN_FLOW_SELF_TEST_STATES ///< Number of dialysate inlet pump self-test states. -} DIAL_IN_FLOW_SELF_TEST_STATE_T; - -// Pin assignments for pump stop and direction outputs -#define STOP_DI_PUMP_GIO_PORT_PIN 2U ///< Pin # on GIO A for stopping the dialysate inlet pump. -#define DIR_DI_PUMP_SPI5_PORT_MASK 0x00000100 ///< Pin on unused SPI5 peripheral (ENA) - re-purposed as output GPIO to set dialysate inlet pump direction. -// DialIn pump stop and direction macros -#define SET_DIP_DIR() {mibspiREG5->PC3 |= DIR_DI_PUMP_SPI5_PORT_MASK;} ///< Macro for setting the dialysate inlet pump direction pin high. -#define CLR_DIP_DIR() {mibspiREG5->PC3 &= ~DIR_DI_PUMP_SPI5_PORT_MASK;} ///< Macro for setting the dialysate inlet pump direction pin low. -#define SET_DIP_STOP() gioSetBit( gioPORTA, STOP_DI_PUMP_GIO_PORT_PIN, PIN_SIGNAL_LOW ) ///< Macro for setting the dialysate inlet pump stop pin low. -#define CLR_DIP_STOP() gioSetBit( gioPORTA, STOP_DI_PUMP_GIO_PORT_PIN, PIN_SIGNAL_HIGH ) ///< Macro for setting the dialysate inlet pump stop pin high. - -// ********** private data ********** - -static DIAL_IN_PUMP_STATE_T dialInPumpState = DIAL_IN_PUMP_OFF_STATE; ///< Current state of dialIn flow controller state machine -static U32 dialInFlowDataPublicationTimerCounter; ///< Used to schedule dialIn flow data publication to CAN bus -static BOOL isDialInPumpOn = FALSE; ///< DialIn pump is currently running -static F32 dialInPumpPWMDutyCyclePct = 0.0; ///< Initial dialIn pump PWM duty cycle -static F32 dialInPumpPWMDutyCyclePctSet = 0.0; ///< Currently set dialIn pump PWM duty cycle -static MOTOR_DIR_T dialInPumpDirection = MOTOR_DIR_FORWARD; ///< Requested dialysate flow direction -static MOTOR_DIR_T dialInPumpDirectionSet = MOTOR_DIR_FORWARD; ///< Currently set dialysate flow direction -static PUMP_CONTROL_MODE_T dialInPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< Requested dialIn pump control mode. -static PUMP_CONTROL_MODE_T dialInPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP;///< Currently set dialIn pump control mode. - +/// Enumeration of dialysate inlet pump states. +typedef enum DialInPump_States +{ + DIAL_IN_PUMP_OFF_STATE = 0, ///< Off state for the dialysate inlet pump. + DIAL_IN_PUMP_RAMPING_UP_STATE, ///< Ramping up state for the dialysate inlet pump. + DIAL_IN_PUMP_RAMPING_DOWN_STATE, ///< Ramping down state for the dialysate inlet pump. + DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE, ///< Control to target state for the dialysate inlet pump. + NUM_OF_DIAL_IN_PUMP_STATES ///< Number of dialysate inlet pump states. +} DIAL_IN_PUMP_STATE_T; + +/// Enumeration of dialysate inlet self-test states. +typedef enum DialInFlow_Self_Test_States +{ + DIAL_IN_FLOW_SELF_TEST_STATE_START = 0, ///< Start state for the dialysate inlet pump self-test. + DIAL_IN_FLOW_TEST_STATE_IN_PROGRESS, ///< Test in progress state for the dialysate inlet pump self-test. + DIAL_IN_FLOW_TEST_STATE_COMPLETE, ///< Test completed state for the dialysate inlet pump self-test. + NUM_OF_DIAL_IN_FLOW_SELF_TEST_STATES ///< Number of dialysate inlet pump self-test states. +} DIAL_IN_FLOW_SELF_TEST_STATE_T; + +// Pin assignments for pump stop and direction outputs +#define STOP_DI_PUMP_GIO_PORT_PIN 2U ///< Pin # on GIO A for stopping the dialysate inlet pump. +#define DIR_DI_PUMP_SPI5_PORT_MASK 0x00000100 ///< Pin on unused SPI5 peripheral (ENA) - re-purposed as output GPIO to set dialysate inlet pump direction. +// DialIn pump stop and direction macros +#define SET_DIP_DIR() {mibspiREG5->PC3 |= DIR_DI_PUMP_SPI5_PORT_MASK;} ///< Macro for setting the dialysate inlet pump direction pin high. +#define CLR_DIP_DIR() {mibspiREG5->PC3 &= ~DIR_DI_PUMP_SPI5_PORT_MASK;} ///< Macro for setting the dialysate inlet pump direction pin low. +#define SET_DIP_STOP() gioSetBit( gioPORTA, STOP_DI_PUMP_GIO_PORT_PIN, PIN_SIGNAL_LOW ) ///< Macro for setting the dialysate inlet pump stop pin low. +#define CLR_DIP_STOP() gioSetBit( gioPORTA, STOP_DI_PUMP_GIO_PORT_PIN, PIN_SIGNAL_HIGH ) ///< Macro for setting the dialysate inlet pump stop pin high. + +// ********** private data ********** + +static DIAL_IN_PUMP_STATE_T dialInPumpState = DIAL_IN_PUMP_OFF_STATE; ///< Current state of dialIn flow controller state machine +static U32 dialInFlowDataPublicationTimerCounter; ///< Used to schedule dialIn flow data publication to CAN bus +static BOOL isDialInPumpOn = FALSE; ///< DialIn pump is currently running +static F32 dialInPumpPWMDutyCyclePct = 0.0; ///< Initial dialIn pump PWM duty cycle +static F32 dialInPumpPWMDutyCyclePctSet = 0.0; ///< Currently set dialIn pump PWM duty cycle +static MOTOR_DIR_T dialInPumpDirection = MOTOR_DIR_FORWARD; ///< Requested dialysate flow direction +static MOTOR_DIR_T dialInPumpDirectionSet = MOTOR_DIR_FORWARD; ///< Currently set dialysate flow direction +static PUMP_CONTROL_MODE_T dialInPumpControlMode = PUMP_CONTROL_MODE_CLOSED_LOOP; ///< Requested dialIn pump control mode. +static PUMP_CONTROL_MODE_T dialInPumpControlModeSet = PUMP_CONTROL_MODE_CLOSED_LOOP;///< Currently set dialIn pump control mode. + /// Interval (in ms) at which to publish dialIn flow data to CAN bus static OVERRIDE_U32_T dialInFlowDataPublishInterval = { DIAL_IN_FLOW_DATA_PUB_INTERVAL, DIAL_IN_FLOW_DATA_PUB_INTERVAL, DIAL_IN_FLOW_DATA_PUB_INTERVAL, 0 }; -static U32 dialysateFlowDataFreshStatusCounter = 0; ///< Counter use to trigger alarm if no fresh dialysate flow data is received -static S32 targetDialInFlowRate = 0; ///< Requested dialIn flow rate -static OVERRIDE_F32_T measuredDialInFlowRate = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet flow rate -static OVERRIDE_F32_T dialInPumpRotorSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump rotor speed -static OVERRIDE_F32_T dialInPumpSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump motor speed -static OVERRIDE_F32_T adcDialInPumpMCSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump motor controller speed -static OVERRIDE_F32_T adcDialInPumpMCCurrentmA = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump motor controller current +static U32 dialysateFlowDataFreshStatusCounter = 0; ///< Counter use to trigger alarm if no fresh dialysate flow data is received +static S32 targetDialInFlowRate = 0; ///< Requested dialIn flow rate +static OVERRIDE_F32_T measuredDialInFlowRate = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet flow rate +static OVERRIDE_F32_T dialInPumpRotorSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump rotor speed +static OVERRIDE_F32_T dialInPumpSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump motor speed +static OVERRIDE_F32_T adcDialInPumpMCSpeedRPM = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump motor controller speed +static OVERRIDE_F32_T adcDialInPumpMCCurrentmA = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate inlet pump motor controller current static U08 lastDialInPumpDirectionCount = 0; ///< Previous pump direction error count reported by FPGA. - -static U32 dipControlTimerCounter = 0; ///< Determines when to perform control on dialIn flow - -static U32 dipRotorRevStartTime = 0; ///< Dialysate inlet pump rotor rotation start time (in ms) -static BOOL dipStopAtHomePosition = FALSE; ///< Stop dialysate inlet pump at next home position -static U32 dipHomeStartTime = 0; ///< When did dialysate inlet pump home command begin? (in ms) - + +static U32 dipControlTimerCounter = 0; ///< Determines when to perform control on dialIn flow + +static U32 dipRotorRevStartTime = 0; ///< Dialysate inlet pump rotor rotation start time (in ms) +static BOOL dipStopAtHomePosition = FALSE; ///< Stop dialysate inlet pump at next home position +static U32 dipHomeStartTime = 0; ///< When did dialysate inlet pump home command begin? (in ms) + static U16 dipLastMotorHallSensorCounts[ DIP_SPEED_CALC_BUFFER_LEN ]; ///< Last hall sensor count for the dialysate inlet pump motor static U32 dipMotorSpeedCalcIdx = 0; ///< Index into 1 second buffer of motor speed hall sensor counts -static U32 dipMotorSpeedCalcTimerCtr = 0; ///< Counter determines interval for calculating dialysate inlet pump motor speed from hall sensor count. - -static U32 errorDialInFlowVsMotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for flow vs. motor speed error condition. -static U32 errorDialInMotorOffPersistTimerCtr = 0; ///< Persistence timer counter for motor off check error condition. -static U32 errorDialInMotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for motor speed error condition. -static U32 errorDialInRotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for rotor speed error condition. +static U32 dipMotorSpeedCalcTimerCtr = 0; ///< Counter determines interval for calculating dialysate inlet pump motor speed from hall sensor count. + +static U32 errorDialInFlowVsMotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for flow vs. motor speed error condition. +static U32 errorDialInMotorOffPersistTimerCtr = 0; ///< Persistence timer counter for motor off check error condition. +static U32 errorDialInMotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for motor speed error condition. +static U32 errorDialInRotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for rotor speed error condition. static U32 errorDialInPumpDirectionPersistTimerCtr = 0; ///< Persistence timer counter for pump direction error condition. - -static F64 flowReadings[ SIZE_OF_ROLLING_AVG ]; ///< Holds flow samples for a rolling average. -static U32 flowReadingsIdx = 0; ///< Index for next sample in rolling average array. -static F64 flowReadingsTotal = 0.0; ///< Rolling total - used to calc average. -static U32 flowReadingsCount = 0; ///< Number of samples in flow rolling average buffer. - + +static F64 flowReadings[ SIZE_OF_ROLLING_AVG ]; ///< Holds flow samples for a rolling average. +static U32 flowReadingsIdx = 0; ///< Index for next sample in rolling average array. +static F64 flowReadingsTotal = 0.0; ///< Rolling total - used to calc average. +static U32 flowReadingsCount = 0; ///< Number of samples in flow rolling average buffer. + static U32 dipCurrErrorDurationCtr = 0; ///< Used for tracking persistence of dip current errors. -static HD_PUMPS_CAL_RECORD_T dialInPumpCalRecord; ///< Dialysate inlet calibration record. +static HD_PUMPS_CAL_RECORD_T dialInPumpCalRecord; ///< Dialysate inlet calibration record. -// ********** private function prototypes ********** - -static DIAL_IN_PUMP_STATE_T handleDialInPumpOffState( void ); -static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingUpState( void ); -static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingDownState( void ); -static DIAL_IN_PUMP_STATE_T handleDialInPumpControlToTargetState( void ); -static void setDialInPumpControlSignalPWM( F32 newPWM ); -static void stopDialInPump( void ); -static void releaseDialInPumpStop( void ); -static void setDialInPumpDirection( MOTOR_DIR_T dir ); -static void publishDialInFlowData( void ); +// ********** private function prototypes ********** + +static DIAL_IN_PUMP_STATE_T handleDialInPumpOffState( void ); +static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingUpState( void ); +static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingDownState( void ); +static DIAL_IN_PUMP_STATE_T handleDialInPumpControlToTargetState( void ); +static void setDialInPumpControlSignalPWM( F32 newPWM ); +static void stopDialInPump( void ); +static void releaseDialInPumpStop( void ); +static void setDialInPumpDirection( MOTOR_DIR_T dir ); +static void publishDialInFlowData( void ); static void resetDialInFlowMovingAverage( void ); -static void filterDialInFlowReadings( F64 flow ); -static void updateDialInPumpSpeedAndDirectionFromHallSensors( void ); -static void checkDialInPumpRotor( void ); -static void checkDialInPumpDirection( void ); -static void checkDialInPumpSpeeds( void ); -static void checkDialInPumpMCCurrent( void ); +static void filterDialInFlowReadings( F64 flow ); +static void updateDialInPumpSpeedAndDirectionFromHallSensors( void ); +static void checkDialInPumpRotor( void ); +static void checkDialInPumpDirection( void ); +static void checkDialInPumpSpeeds( void ); +static void checkDialInPumpMCCurrent( void ); static void checkDialInPumpFlowRate( void ); - -/*********************************************************************//** - * @brief - * The initDialInFlow function initializes the DialInFlow module. - * @details Inputs: none - * @details Outputs: DialInFlow module initialized. - * @return none - *************************************************************************/ -void initDialInFlow( void ) + +/*********************************************************************//** + * @brief + * The initDialInFlow function initializes the DialInFlow module. + * @details Inputs: none + * @details Outputs: DialInFlow module initialized. + * @return none + *************************************************************************/ +void initDialInFlow( void ) { U32 i; dialInFlowDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; - - signalDialInPumpHardStop(); - setDialInPumpDirection( MOTOR_DIR_FORWARD ); - - // Zero rolling flow average buffer - resetDialInFlowMovingAverage(); - + + signalDialInPumpHardStop(); + setDialInPumpDirection( MOTOR_DIR_FORWARD ); + + // Zero rolling flow average buffer + resetDialInFlowMovingAverage(); + // Zero motor hall sensors counts buffer dipMotorSpeedCalcIdx = 0; for ( i = 0; i < DIP_SPEED_CALC_BUFFER_LEN; i++ ) { dipLastMotorHallSensorCounts[ i ] = getFPGADialInPumpHallSensorCount(); } - // Initialize dialysate inlet flow PI controller - initializePIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE, - DIP_P_COEFFICIENT, DIP_I_COEFFICIENT, - MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE, MAX_DIAL_IN_PUMP_PWM_DUTY_CYCLE ); + // Initialize dialysate inlet flow PI controller + initializePIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE, + DIP_P_COEFFICIENT, DIP_I_COEFFICIENT, + MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE, MAX_DIAL_IN_PUMP_PWM_DUTY_CYCLE ); // Initialize persistent alarm for flow sensor signal strength too low initPersistentAlarm( ALARM_ID_HD_DIAL_IN_FLOW_OUT_OF_RANGE, 0, DIP_MAX_FLOW_RATE_OUT_OF_RANGE_PERSIST ); -} - -/*********************************************************************//** - * @brief - * The setDialInPumpTargetFlowRate function sets a new target flow rate and - * pump direction. - * @details Inputs: isDialInPumpOn, dialInPumpDirectionSet - * @details Outputs: targetDialInFlowRate, dialInPumpdirection, dialInPumpPWMDutyCyclePct - * @param flowRate new target dialIn flow rate - * @param dir new dialIn flow direction - * @param mode new control mode - * @return TRUE if new flow rate & dir are set, FALSE if not - *************************************************************************/ -BOOL setDialInPumpTargetFlowRate( U32 flowRate, MOTOR_DIR_T dir, PUMP_CONTROL_MODE_T mode ) -{ - BOOL result = FALSE; - - // Direction change while pump is running is not allowed - if ( ( FALSE == isDialInPumpOn ) || ( 0 == flowRate ) || ( dir == dialInPumpDirectionSet ) ) - { +} + +/*********************************************************************//** + * @brief + * The setDialInPumpTargetFlowRate function sets a new target flow rate and + * pump direction. + * @details Inputs: isDialInPumpOn, dialInPumpDirectionSet + * @details Outputs: targetDialInFlowRate, dialInPumpdirection, dialInPumpPWMDutyCyclePct + * @param flowRate new target dialIn flow rate + * @param dir new dialIn flow direction + * @param mode new control mode + * @return TRUE if new flow rate & dir are set, FALSE if not + *************************************************************************/ +BOOL setDialInPumpTargetFlowRate( U32 flowRate, MOTOR_DIR_T dir, PUMP_CONTROL_MODE_T mode ) +{ + BOOL result = FALSE; + + // Direction change while pump is running is not allowed + if ( ( FALSE == isDialInPumpOn ) || ( 0 == flowRate ) || ( dir == dialInPumpDirectionSet ) ) + { S32 dirFlowRate = ( dir == MOTOR_DIR_FORWARD ? (S32)flowRate : (S32)flowRate * -1 ); // Don't interrupt pump control unless rate or mode is changing @@ -282,132 +282,132 @@ } #endif - // Verify flow rate - if ( TRUE == isFlowRateInRange ) - { - resetDialInFlowMovingAverage(); - targetDialInFlowRate = dirFlowRate; - dialInPumpDirection = dir; - dialInPumpControlMode = mode; - // Set PWM duty cycle target to an estimated initial target to ramp to based on target flow rate - then we will control to flow when ramp completed - dialInPumpPWMDutyCyclePct = ( 0 == flowRate ? DIP_PWM_ZERO_OFFSET : DIP_PWM_FROM_ML_PER_MIN( (F32)flowRate ) ); - - switch ( dialInPumpState ) - { - case DIAL_IN_PUMP_RAMPING_UP_STATE: // See if we need to reverse direction of ramp - if ( dialInPumpPWMDutyCyclePct < dialInPumpPWMDutyCyclePctSet ) - { - dialInPumpState = DIAL_IN_PUMP_RAMPING_DOWN_STATE; - } + // Verify flow rate + if ( TRUE == isFlowRateInRange ) + { + resetDialInFlowMovingAverage(); + targetDialInFlowRate = dirFlowRate; + dialInPumpDirection = dir; + dialInPumpControlMode = mode; + // Set PWM duty cycle target to an estimated initial target to ramp to based on target flow rate - then we will control to flow when ramp completed + dialInPumpPWMDutyCyclePct = ( 0 == flowRate ? DIP_PWM_ZERO_OFFSET : DIP_PWM_FROM_ML_PER_MIN( (F32)flowRate ) ); + + switch ( dialInPumpState ) + { + case DIAL_IN_PUMP_RAMPING_UP_STATE: // See if we need to reverse direction of ramp + if ( dialInPumpPWMDutyCyclePct < dialInPumpPWMDutyCyclePctSet ) + { + dialInPumpState = DIAL_IN_PUMP_RAMPING_DOWN_STATE; + } break; - - case DIAL_IN_PUMP_RAMPING_DOWN_STATE: // See if we need to reverse direction of ramp - if ( dialInPumpPWMDutyCyclePct > dialInPumpPWMDutyCyclePctSet ) - { - dialInPumpState = DIAL_IN_PUMP_RAMPING_UP_STATE; - } + + case DIAL_IN_PUMP_RAMPING_DOWN_STATE: // See if we need to reverse direction of ramp + if ( dialInPumpPWMDutyCyclePct > dialInPumpPWMDutyCyclePctSet ) + { + dialInPumpState = DIAL_IN_PUMP_RAMPING_UP_STATE; + } break; - - case DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE: // Start ramp to new target in appropriate direction - if ( dialInPumpPWMDutyCyclePctSet > dialInPumpPWMDutyCyclePct ) - { - dialInPumpState = DIAL_IN_PUMP_RAMPING_DOWN_STATE; - } - else - { - dialInPumpState = DIAL_IN_PUMP_RAMPING_UP_STATE; - } + + case DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE: // Start ramp to new target in appropriate direction + if ( dialInPumpPWMDutyCyclePctSet > dialInPumpPWMDutyCyclePct ) + { + dialInPumpState = DIAL_IN_PUMP_RAMPING_DOWN_STATE; + } + else + { + dialInPumpState = DIAL_IN_PUMP_RAMPING_UP_STATE; + } break; - - default: - // Ok - not all states need to be handled here - break; - } - result = TRUE; - } - else // Requested flow rate too high + default: + // Ok - not all states need to be handled here + break; + } + result = TRUE; + } + + else // Requested flow rate too high { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMPS_FLOW_LIMITS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { + { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIAL_IN_FLOW_SET_TOO_HIGH, flowRate ) - } + } } - } - } - - return result; -} - -/*********************************************************************//** - * @brief - * The signalDialInPumpHardStop function stops the dialIn pump immediately. - * @details Inputs: none - * @details Outputs: DialIn pump stopped, set point reset, state changed to off - * @return none - *************************************************************************/ -void signalDialInPumpHardStop( void ) -{ - targetDialInFlowRate = 0; - stopDialInPump(); - dialInPumpState = DIAL_IN_PUMP_OFF_STATE; - dialInPumpPWMDutyCyclePct = DIP_PWM_ZERO_OFFSET; - dipControlTimerCounter = 0; - resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE ); -} - -/*********************************************************************//** - * @brief - * The signalDialInPumpRotorHallSensor function handles the dialysate inlet pump rotor - * hall sensor detection. Calculates rotor speed (in RPM). Stops pump if - * there is a pending request to home the pump. - * @details Inputs: dipRotorRevStartTime, dipStopAtHomePosition - * @details Outputs: dipRotorRevStartTime, dialInPumpRotorSpeedRPM - * @return none - *************************************************************************/ -void signalDialInPumpRotorHallSensor( void ) -{ - U32 rotTime = getMSTimerCount(); - U32 deltaTime = calcTimeBetween( dipRotorRevStartTime, rotTime ); - - // Calculate rotor speed (in RPM) - dialInPumpRotorSpeedRPM.data = ( 1.0 / (F32)deltaTime ) * (F32)MS_PER_SECOND * (F32)SEC_PER_MIN; - dipRotorRevStartTime = rotTime; - - // If we are supposed to stop pump at home position, stop pump now. - if ( TRUE == dipStopAtHomePosition ) - { - signalDialInPumpHardStop(); - dipStopAtHomePosition = FALSE; - } -} - -/*********************************************************************//** - * @brief - * The homeDialInPump function initiates a dialysate inlet pump home operation. - * @details Inputs: dialInPumpState - * @details Outputs: dipStopAtHomePosition, dipHomeStartTime, dialysate inlet pump started (slow) - * @return none - *************************************************************************/ -BOOL homeDialInPump( void ) -{ - BOOL result = FALSE; - - if ( DIAL_IN_PUMP_OFF_STATE == dialInPumpState ) - { - dipStopAtHomePosition = TRUE; - dipHomeStartTime = getMSTimerCount(); - result = setDialInPumpTargetFlowRate( DIP_HOME_RATE, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); - } - - return result; -} - + } + } + + return result; +} + /*********************************************************************//** * @brief + * The signalDialInPumpHardStop function stops the dialIn pump immediately. + * @details Inputs: none + * @details Outputs: DialIn pump stopped, set point reset, state changed to off + * @return none + *************************************************************************/ +void signalDialInPumpHardStop( void ) +{ + targetDialInFlowRate = 0; + stopDialInPump(); + dialInPumpState = DIAL_IN_PUMP_OFF_STATE; + dialInPumpPWMDutyCyclePct = DIP_PWM_ZERO_OFFSET; + dipControlTimerCounter = 0; + resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE ); +} + +/*********************************************************************//** + * @brief + * The signalDialInPumpRotorHallSensor function handles the dialysate inlet pump rotor + * hall sensor detection. Calculates rotor speed (in RPM). Stops pump if + * there is a pending request to home the pump. + * @details Inputs: dipRotorRevStartTime, dipStopAtHomePosition + * @details Outputs: dipRotorRevStartTime, dialInPumpRotorSpeedRPM + * @return none + *************************************************************************/ +void signalDialInPumpRotorHallSensor( void ) +{ + U32 rotTime = getMSTimerCount(); + U32 deltaTime = calcTimeBetween( dipRotorRevStartTime, rotTime ); + + // Calculate rotor speed (in RPM) + dialInPumpRotorSpeedRPM.data = ( 1.0 / (F32)deltaTime ) * (F32)MS_PER_SECOND * (F32)SEC_PER_MIN; + dipRotorRevStartTime = rotTime; + + // If we are supposed to stop pump at home position, stop pump now. + if ( TRUE == dipStopAtHomePosition ) + { + signalDialInPumpHardStop(); + dipStopAtHomePosition = FALSE; + } +} + +/*********************************************************************//** + * @brief + * The homeDialInPump function initiates a dialysate inlet pump home operation. + * @details Inputs: dialInPumpState + * @details Outputs: dipStopAtHomePosition, dipHomeStartTime, dialysate inlet pump started (slow) + * @return none + *************************************************************************/ +BOOL homeDialInPump( void ) +{ + BOOL result = FALSE; + + if ( DIAL_IN_PUMP_OFF_STATE == dialInPumpState ) + { + dipStopAtHomePosition = TRUE; + dipHomeStartTime = getMSTimerCount(); + result = setDialInPumpTargetFlowRate( DIP_HOME_RATE, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + } + + return result; +} + +/*********************************************************************//** + * @brief * The isDialInPumpRunning function returns whether the dialysate inlet pump * is currently running or not. * @details Inputs: isDialInPumpOn @@ -434,18 +434,18 @@ return result; } -/*********************************************************************//** - * @brief - * The execDialInFlowMonitor function executes the dialIn flow monitor. - * @details Inputs: none - * @details Outputs: measuredDialInFlowRate, adcDialInPumpMCSpeedRPM, adcDialInPumpMCCurrentmA - * @return none - *************************************************************************/ -void execDialInFlowMonitor( void ) +/*********************************************************************//** + * @brief + * The execDialInFlowMonitor function executes the dialIn flow monitor. + * @details Inputs: none + * @details Outputs: measuredDialInFlowRate, adcDialInPumpMCSpeedRPM, adcDialInPumpMCCurrentmA + * @return none + *************************************************************************/ +void execDialInFlowMonitor( void ) { HD_OP_MODE_T opMode = getCurrentOperationMode(); - U16 dipRPM = getIntADCReading( INT_ADC_DIAL_IN_PUMP_SPEED ); - U16 dipmA = getIntADCReading( INT_ADC_DIAL_IN_PUMP_MOTOR_CURRENT ); + U16 dipRPM = getIntADCReading( INT_ADC_DIAL_IN_PUMP_SPEED ); + U16 dipmA = getIntADCReading( INT_ADC_DIAL_IN_PUMP_MOTOR_CURRENT ); F64 dipFlow; // Process new dialysate flow readings @@ -471,299 +471,299 @@ } } - // Get latest pump speed and current from motor controller - adcDialInPumpMCSpeedRPM.data = (F32)(SIGN_FROM_12_BIT_VALUE(dipRPM)) * DIP_SPEED_ADC_TO_RPM_FACTOR; - adcDialInPumpMCCurrentmA.data = (F32)(SIGN_FROM_12_BIT_VALUE(dipmA)) * DIP_CURRENT_ADC_TO_MA_FACTOR; + // Get latest pump speed and current from motor controller + adcDialInPumpMCSpeedRPM.data = (F32)(SIGN_FROM_12_BIT_VALUE(dipRPM)) * DIP_SPEED_ADC_TO_RPM_FACTOR; + adcDialInPumpMCCurrentmA.data = (F32)(SIGN_FROM_12_BIT_VALUE(dipmA)) * DIP_CURRENT_ADC_TO_MA_FACTOR; - // Calculate dialysate inlet pump motor speed/direction from hall sensor count - updateDialInPumpSpeedAndDirectionFromHallSensors(); - - // Do not start enforcing checks until out of init/POST mode - if ( opMode != MODE_INIT ) - { - // Check pump direction - checkDialInPumpDirection(); - // Check pump controller current - checkDialInPumpMCCurrent(); - // Check pump speeds and flow - checkDialInPumpSpeeds(); + // Calculate dialysate inlet pump motor speed/direction from hall sensor count + updateDialInPumpSpeedAndDirectionFromHallSensors(); + + // Do not start enforcing checks until out of init/POST mode + if ( opMode != MODE_INIT ) + { + // Check pump direction + checkDialInPumpDirection(); + // Check pump controller current + checkDialInPumpMCCurrent(); + // Check pump speeds and flow + checkDialInPumpSpeeds(); checkDialInPumpFlowRate(); - // Check for home position, zero/low speed + // Check for home position, zero/low speed checkDialInPumpRotor(); } else { lastDialInPumpDirectionCount = getFPGADialInPumpHallSensorStatus() & PUMP_DIR_ERROR_COUNT_MASK; } - // Publish dialIn flow data on interval - publishDialInFlowData(); -} - -/*********************************************************************//** - * @brief - * The execDialInFlowController function executes the dialIn flow controller. - * @details Inputs: dialInPumpState - * @details Outputs: dialInPumpState - * @return none - *************************************************************************/ -void execDialInFlowController( void ) -{ - switch ( dialInPumpState ) - { - case DIAL_IN_PUMP_OFF_STATE: - dialInPumpState = handleDialInPumpOffState(); - break; - - case DIAL_IN_PUMP_RAMPING_UP_STATE: - dialInPumpState = handleDialInPumpRampingUpState(); - break; - - case DIAL_IN_PUMP_RAMPING_DOWN_STATE: - dialInPumpState = handleDialInPumpRampingDownState(); - break; - - case DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE: - dialInPumpState = handleDialInPumpControlToTargetState(); - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIAL_IN_FLOW_INVALID_DIAL_IN_PUMP_STATE, dialInPumpState ) - break; - } -} - -/*********************************************************************//** - * @brief - * The handleDialInPumpOffState function handles the dialIn pump off state - * of the dialIn pump controller state machine. - * @details Inputs: targetDialInFlowRate, dialInPumpDirection - * @details Outputs: dialInPumpPWMDutyCyclePctSet, dialInPumpDirectionSet, isDialInPumpOn - * @return next state - *************************************************************************/ -static DIAL_IN_PUMP_STATE_T handleDialInPumpOffState( void ) -{ - DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_OFF_STATE; - - // If we have been given a flow rate, setup ramp up and transition to ramp up state - if ( targetDialInFlowRate != 0 ) - { - // Set initial PWM duty cycle - dialInPumpPWMDutyCyclePctSet = DIP_PWM_ZERO_OFFSET + MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE; - setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); - // Allow dialIn pump to run in requested direction - setDialInPumpDirection( dialInPumpDirection ); - releaseDialInPumpStop(); - isDialInPumpOn = TRUE; - result = DIAL_IN_PUMP_RAMPING_UP_STATE; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The handleDialInPumpRampingUpState function handles the ramp up state - * of the dialIn pump controller state machine. - * @details Inputs: dialInPumpPWMDutyCyclePctSet - * @details Outputs: dialInPumpPWMDutyCyclePctSet - * @return next state - *************************************************************************/ -static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingUpState( void ) -{ - DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_RAMPING_UP_STATE; - - // Have we been asked to stop the dialIn pump? - if ( 0 == targetDialInFlowRate ) - { - // Start ramp down to stop - dialInPumpPWMDutyCyclePctSet -= MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE; - setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); - result = DIAL_IN_PUMP_RAMPING_DOWN_STATE; - } - // Have we reached end of ramp up? - else if ( dialInPumpPWMDutyCyclePctSet >= dialInPumpPWMDutyCyclePct ) - { - resetDialInFlowMovingAverage(); + // Publish dialIn flow data on interval + publishDialInFlowData(); +} + +/*********************************************************************//** + * @brief + * The execDialInFlowController function executes the dialIn flow controller. + * @details Inputs: dialInPumpState + * @details Outputs: dialInPumpState + * @return none + *************************************************************************/ +void execDialInFlowController( void ) +{ + switch ( dialInPumpState ) + { + case DIAL_IN_PUMP_OFF_STATE: + dialInPumpState = handleDialInPumpOffState(); + break; + + case DIAL_IN_PUMP_RAMPING_UP_STATE: + dialInPumpState = handleDialInPumpRampingUpState(); + break; + + case DIAL_IN_PUMP_RAMPING_DOWN_STATE: + dialInPumpState = handleDialInPumpRampingDownState(); + break; + + case DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE: + dialInPumpState = handleDialInPumpControlToTargetState(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIAL_IN_FLOW_INVALID_DIAL_IN_PUMP_STATE, dialInPumpState ) + break; + } +} + +/*********************************************************************//** + * @brief + * The handleDialInPumpOffState function handles the dialIn pump off state + * of the dialIn pump controller state machine. + * @details Inputs: targetDialInFlowRate, dialInPumpDirection + * @details Outputs: dialInPumpPWMDutyCyclePctSet, dialInPumpDirectionSet, isDialInPumpOn + * @return next state + *************************************************************************/ +static DIAL_IN_PUMP_STATE_T handleDialInPumpOffState( void ) +{ + DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_OFF_STATE; + + // If we have been given a flow rate, setup ramp up and transition to ramp up state + if ( targetDialInFlowRate != 0 ) + { + // Set initial PWM duty cycle + dialInPumpPWMDutyCyclePctSet = DIP_PWM_ZERO_OFFSET + MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE; + setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); + // Allow dialIn pump to run in requested direction + setDialInPumpDirection( dialInPumpDirection ); + releaseDialInPumpStop(); + isDialInPumpOn = TRUE; + result = DIAL_IN_PUMP_RAMPING_UP_STATE; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleDialInPumpRampingUpState function handles the ramp up state + * of the dialIn pump controller state machine. + * @details Inputs: dialInPumpPWMDutyCyclePctSet + * @details Outputs: dialInPumpPWMDutyCyclePctSet + * @return next state + *************************************************************************/ +static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingUpState( void ) +{ + DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_RAMPING_UP_STATE; + + // Have we been asked to stop the dialIn pump? + if ( 0 == targetDialInFlowRate ) + { + // Start ramp down to stop + dialInPumpPWMDutyCyclePctSet -= MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE; + setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); + result = DIAL_IN_PUMP_RAMPING_DOWN_STATE; + } + // Have we reached end of ramp up? + else if ( dialInPumpPWMDutyCyclePctSet >= dialInPumpPWMDutyCyclePct ) + { + resetDialInFlowMovingAverage(); dialInPumpPWMDutyCyclePctSet = dialInPumpPWMDutyCyclePct; - resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, dialInPumpPWMDutyCyclePctSet ); - dialInPumpControlModeSet = dialInPumpControlMode; + resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, dialInPumpPWMDutyCyclePctSet ); + dialInPumpControlModeSet = dialInPumpControlMode; setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); - dipControlTimerCounter = 0; - result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; - } - // Continue ramp up - else - { - dialInPumpPWMDutyCyclePctSet += MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE; - setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); - } - - return result; -} - -/*********************************************************************//** - * @brief - * The handleDialInPumpRampingDownState function handles the ramp down state - * of the dialIn pump controller state machine. - * @details Inputs: dialInPumpPWMDutyCyclePctSet - * @details Outputs: dialInPumpPWMDutyCyclePctSet - * @return next state - *************************************************************************/ -static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingDownState( void ) -{ - DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_RAMPING_DOWN_STATE; - - // Have we essentially reached zero speed - if ( dialInPumpPWMDutyCyclePctSet < (MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE + DIP_PWM_ZERO_OFFSET) ) - { - signalDialInPumpHardStop(); - result = DIAL_IN_PUMP_OFF_STATE; - } - // Have we reached end of ramp down? - else if ( dialInPumpPWMDutyCyclePctSet <= dialInPumpPWMDutyCyclePct ) - { - resetDialInFlowMovingAverage(); + dipControlTimerCounter = 0; + result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; + } + // Continue ramp up + else + { + dialInPumpPWMDutyCyclePctSet += MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE; + setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleDialInPumpRampingDownState function handles the ramp down state + * of the dialIn pump controller state machine. + * @details Inputs: dialInPumpPWMDutyCyclePctSet + * @details Outputs: dialInPumpPWMDutyCyclePctSet + * @return next state + *************************************************************************/ +static DIAL_IN_PUMP_STATE_T handleDialInPumpRampingDownState( void ) +{ + DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_RAMPING_DOWN_STATE; + + // Have we essentially reached zero speed + if ( dialInPumpPWMDutyCyclePctSet < (MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE + DIP_PWM_ZERO_OFFSET) ) + { + signalDialInPumpHardStop(); + result = DIAL_IN_PUMP_OFF_STATE; + } + // Have we reached end of ramp down? + else if ( dialInPumpPWMDutyCyclePctSet <= dialInPumpPWMDutyCyclePct ) + { + resetDialInFlowMovingAverage(); dialInPumpPWMDutyCyclePctSet = dialInPumpPWMDutyCyclePct; - resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, dialInPumpPWMDutyCyclePctSet ); - dialInPumpControlModeSet = dialInPumpControlMode; - setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); + resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, dialInPumpPWMDutyCyclePctSet ); + dialInPumpControlModeSet = dialInPumpControlMode; + setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); dipControlTimerCounter = 0; - result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; - } - // Continue ramp down - else - { - dialInPumpPWMDutyCyclePctSet -= MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE; - setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); - } - - return result; -} - -/*********************************************************************//** - * @brief - * The handleDialInPumpControlToTargetState function handles the "control to - * target" state of the dialIn pump controller state machine. - * @details Inputs: none - * @details Outputs: dialInPumpState - * @return next state - *************************************************************************/ -static DIAL_IN_PUMP_STATE_T handleDialInPumpControlToTargetState( void ) -{ - DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; - - // Control at set interval - if ( ++dipControlTimerCounter >= DIP_CONTROL_INTERVAL ) - { - if ( dialInPumpControlModeSet == PUMP_CONTROL_MODE_CLOSED_LOOP ) - { - F32 tgtFlow = (F32)targetDialInFlowRate; - F32 actFlow = getMeasuredDialInFlowRate(); - F32 newPWM; - - newPWM = runPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, tgtFlow, actFlow ); - dialInPumpPWMDutyCyclePctSet = newPWM; + result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; + } + // Continue ramp down + else + { + dialInPumpPWMDutyCyclePctSet -= MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE; + setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePctSet ); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleDialInPumpControlToTargetState function handles the "control to + * target" state of the dialIn pump controller state machine. + * @details Inputs: none + * @details Outputs: dialInPumpState + * @return next state + *************************************************************************/ +static DIAL_IN_PUMP_STATE_T handleDialInPumpControlToTargetState( void ) +{ + DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; + + // Control at set interval + if ( ++dipControlTimerCounter >= DIP_CONTROL_INTERVAL ) + { + if ( dialInPumpControlModeSet == PUMP_CONTROL_MODE_CLOSED_LOOP ) + { + F32 tgtFlow = (F32)targetDialInFlowRate; + F32 actFlow = getMeasuredDialInFlowRate(); + F32 newPWM; + + newPWM = runPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, tgtFlow, actFlow ); + dialInPumpPWMDutyCyclePctSet = newPWM; setDialInPumpControlSignalPWM( newPWM ); - } + } signalDialOutControl(); - dipControlTimerCounter = 0; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The setDialInPumpControlSignalPWM function sets the PWM duty cycle for - * the dialysate inlet pump to a given %. - * @details Inputs: none - * @details Outputs: dialIn pump stop signal activated, PWM duty cycle zeroed - * @param newPWM new duty cycle % to apply to PWM - * @return none - *************************************************************************/ -static void setDialInPumpControlSignalPWM( F32 newPWM ) -{ - etpwmSetCmpA( etpwmREG2, (U32)( (S32)( ( newPWM * (F32)(etpwmREG2->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); -} - -/*********************************************************************//** - * @brief - * The stopDialInPump function sets the dialIn pump stop signal. - * @details Inputs: none - * @details Outputs: dialIn pump stop signal activated, PWM duty cycle zeroed - * @return none - *************************************************************************/ -static void stopDialInPump( void ) -{ - isDialInPumpOn = FALSE; - dialInPumpPWMDutyCyclePctSet = DIP_PWM_ZERO_OFFSET; - etpwmSetCmpA( etpwmREG2, 0 ); - SET_DIP_STOP(); -} - -/*********************************************************************//** - * @brief - * The releaseDialInPumpStop function clears the dialIn pump stop signal. - * @details Inputs: none - * @details Outputs: dialIn pump stop signal - * @return none - *************************************************************************/ -static void releaseDialInPumpStop( void ) -{ - CLR_DIP_STOP(); -} - -/*********************************************************************//** - * @brief - * The setDialInPumpDirection function sets the set dialIn pump direction to - * the given direction. - * @details Inputs: dialInPumpState - * @details Outputs: dialInPumpState - * @param dir dialIn pump direction to set - * @return none - *************************************************************************/ -static void setDialInPumpDirection( MOTOR_DIR_T dir ) -{ - switch ( dir ) - { - case MOTOR_DIR_FORWARD: - dialInPumpDirectionSet = dir; - SET_DIP_DIR(); - break; - - case MOTOR_DIR_REVERSE: - dialInPumpDirectionSet = dir; - CLR_DIP_DIR(); - break; - - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIAL_IN_FLOW_INVALID_DIAL_IN_PUMP_DIRECTION, dir ) - break; - } -} - -/*********************************************************************//** - * @brief - * The getTargetDialInFlowRate function gets the current target dialIn flow - * rate. - * @details Inputs: targetDialInFlowRate - * @details Outputs: none - * @return the current target dialIn flow rate (in mL/min). - *************************************************************************/ + dipControlTimerCounter = 0; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The setDialInPumpControlSignalPWM function sets the PWM duty cycle for + * the dialysate inlet pump to a given %. + * @details Inputs: none + * @details Outputs: dialIn pump stop signal activated, PWM duty cycle zeroed + * @param newPWM new duty cycle % to apply to PWM + * @return none + *************************************************************************/ +static void setDialInPumpControlSignalPWM( F32 newPWM ) +{ + etpwmSetCmpA( etpwmREG2, (U32)( (S32)( ( newPWM * (F32)(etpwmREG2->TBPRD) ) + FLOAT_TO_INT_ROUNDUP_OFFSET ) ) ); +} + +/*********************************************************************//** + * @brief + * The stopDialInPump function sets the dialIn pump stop signal. + * @details Inputs: none + * @details Outputs: dialIn pump stop signal activated, PWM duty cycle zeroed + * @return none + *************************************************************************/ +static void stopDialInPump( void ) +{ + isDialInPumpOn = FALSE; + dialInPumpPWMDutyCyclePctSet = DIP_PWM_ZERO_OFFSET; + etpwmSetCmpA( etpwmREG2, 0 ); + SET_DIP_STOP(); +} + +/*********************************************************************//** + * @brief + * The releaseDialInPumpStop function clears the dialIn pump stop signal. + * @details Inputs: none + * @details Outputs: dialIn pump stop signal + * @return none + *************************************************************************/ +static void releaseDialInPumpStop( void ) +{ + CLR_DIP_STOP(); +} + +/*********************************************************************//** + * @brief + * The setDialInPumpDirection function sets the set dialIn pump direction to + * the given direction. + * @details Inputs: dialInPumpState + * @details Outputs: dialInPumpState + * @param dir dialIn pump direction to set + * @return none + *************************************************************************/ +static void setDialInPumpDirection( MOTOR_DIR_T dir ) +{ + switch ( dir ) + { + case MOTOR_DIR_FORWARD: + dialInPumpDirectionSet = dir; + SET_DIP_DIR(); + break; + + case MOTOR_DIR_REVERSE: + dialInPumpDirectionSet = dir; + CLR_DIP_DIR(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIAL_IN_FLOW_INVALID_DIAL_IN_PUMP_DIRECTION, dir ) + break; + } +} + +/*********************************************************************//** + * @brief + * The getTargetDialInFlowRate function gets the current target dialIn flow + * rate. + * @details Inputs: targetDialInFlowRate + * @details Outputs: none + * @return the current target dialIn flow rate (in mL/min). + *************************************************************************/ S32 getTargetDialInFlowRate( void ) { return targetDialInFlowRate; } - -/*********************************************************************//** - * @brief - * The getMeasuredDialInFlowRate function gets the measured dialIn flow - * rate. - * @details Inputs: measuredDialInFlowRate - * @details Outputs: none - * @return the current dialIn flow rate (in mL/min). - *************************************************************************/ + +/*********************************************************************//** + * @brief + * The getMeasuredDialInFlowRate function gets the measured dialIn flow + * rate. + * @details Inputs: measuredDialInFlowRate + * @details Outputs: none + * @return the current dialIn flow rate (in mL/min). + *************************************************************************/ F32 getMeasuredDialInFlowRate( void ) { F32 result = measuredDialInFlowRate.data; @@ -776,14 +776,14 @@ return result; } -/*********************************************************************//** - * @brief - * The getMeasuredDialInPumpRotorSpeed function gets the measured dialIn flow - * rate. - * @details Inputs: dialInPumpRotorSpeedRPM - * @details Outputs: none - * @return the current dialIn flow rate (in mL/min). - *************************************************************************/ +/*********************************************************************//** + * @brief + * The getMeasuredDialInPumpRotorSpeed function gets the measured dialIn flow + * rate. + * @details Inputs: dialInPumpRotorSpeedRPM + * @details Outputs: none + * @return the current dialIn flow rate (in mL/min). + *************************************************************************/ F32 getMeasuredDialInPumpRotorSpeed( void ) { F32 result = dialInPumpRotorSpeedRPM.data; @@ -794,16 +794,16 @@ } return result; -} - -/*********************************************************************//** - * @brief - * The getMeasuredDialInPumpSpeed function gets the measured dialIn flow - * rate. - * @details Inputs: dialInPumpSpeedRPM - * @details Outputs: none - * @return the current dialIn flow rate (in mL/min). - *************************************************************************/ +} + +/*********************************************************************//** + * @brief + * The getMeasuredDialInPumpSpeed function gets the measured dialIn flow + * rate. + * @details Inputs: dialInPumpSpeedRPM + * @details Outputs: none + * @return the current dialIn flow rate (in mL/min). + *************************************************************************/ F32 getMeasuredDialInPumpSpeed( void ) { F32 result = dialInPumpSpeedRPM.data; @@ -814,16 +814,16 @@ } return result; -} - -/*********************************************************************//** - * @brief - * The getMeasuredDialInPumpMCSpeed function gets the measured dialIn pump - * speed. - * @details Inputs: adcDialInPumpMCSpeedRPM - * @details Outputs: none - * @return the current dialIn pump speed (in RPM). - *************************************************************************/ +} + +/*********************************************************************//** + * @brief + * The getMeasuredDialInPumpMCSpeed function gets the measured dialIn pump + * speed. + * @details Inputs: adcDialInPumpMCSpeedRPM + * @details Outputs: none + * @return the current dialIn pump speed (in RPM). + *************************************************************************/ F32 getMeasuredDialInPumpMCSpeed( void ) { F32 result = adcDialInPumpMCSpeedRPM.data; @@ -834,16 +834,16 @@ } return result; -} - -/*********************************************************************//** - * @brief - * The getMeasuredDialInPumpMCCurrent function gets the measured dialIn pump - * current. - * @details Inputs: adcDialInPumpMCCurrentmA - * @details Outputs: none - * @return the current dialIn pump current (in mA). - *************************************************************************/ +} + +/*********************************************************************//** + * @brief + * The getMeasuredDialInPumpMCCurrent function gets the measured dialIn pump + * current. + * @details Inputs: adcDialInPumpMCCurrentmA + * @details Outputs: none + * @return the current dialIn pump current (in mA). + *************************************************************************/ F32 getMeasuredDialInPumpMCCurrent( void ) { F32 result = adcDialInPumpMCCurrentmA.data; @@ -875,22 +875,22 @@ } return result; -} - -/*********************************************************************//** - * @brief - * The publishDialInFlowData function publishes dialIn flow data at the set - * interval. - * @details Inputs: target flow rate, measured flow rate, measured MC speed, - * measured MC current - * @details Outputs: DialIn flow data is published to CAN bus. - * @return none - *************************************************************************/ -static void publishDialInFlowData( void ) -{ - // Publish dialIn flow data on interval - if ( ++dialInFlowDataPublicationTimerCounter >= getU32OverrideValue( &dialInFlowDataPublishInterval ) ) - { +} + +/*********************************************************************//** + * @brief + * The publishDialInFlowData function publishes dialIn flow data at the set + * interval. + * @details Inputs: target flow rate, measured flow rate, measured MC speed, + * measured MC current + * @details Outputs: DialIn flow data is published to CAN bus. + * @return none + *************************************************************************/ +static void publishDialInFlowData( void ) +{ + // Publish dialIn flow data on interval + if ( ++dialInFlowDataPublicationTimerCounter >= getU32OverrideValue( &dialInFlowDataPublishInterval ) ) + { DIALIN_PUMP_STATUS_PAYLOAD_T payload; payload.setPoint = targetDialInFlowRate; @@ -902,155 +902,156 @@ payload.pwmDC = dialInPumpPWMDutyCyclePctSet * FRACTION_TO_PERCENT_FACTOR; payload.flowSigStrength = 0.0; broadcastData( MSG_ID_DIALYSATE_FLOW_DATA, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&payload, sizeof( DIALIN_PUMP_STATUS_PAYLOAD_T ) ); - dialInFlowDataPublicationTimerCounter = 0; - } -} - -/*********************************************************************//** - * @brief - * The resetDialInFlowMovingAverage function resets the properties of the - * dialIn flow moving average sample buffer. - * @details Inputs: none - * @details Outputs: flowReadingsTotal, flowReadingsIdx, flowReadingsCount all set to zero. + dialInFlowDataPublicationTimerCounter = 0; + } +} + +/*********************************************************************//** + * @brief + * The resetDialInFlowMovingAverage function resets the properties of the + * dialIn flow moving average sample buffer. + * @details Inputs: none + * @details Outputs: flowReadingsTotal, flowReadingsIdx, flowReadingsCount all set to zero. * @return none - *************************************************************************/ -static void resetDialInFlowMovingAverage( void ) -{ - flowReadingsIdx = 0; - flowReadingsCount = 0; - flowReadingsTotal = 0.0; - dipControlTimerCounter = 0; -} - -/*********************************************************************//** - * @brief - * The filterDialInFlowReadings function adds a new flow sample to the filter. - * @details Inputs: none - * @details Outputs: flowReadings[], flowReadingsIdx, flowReadingsCount, flowReadingsTotal - * @return none - *************************************************************************/ -void filterDialInFlowReadings( F64 flow ) -{ - if ( flowReadingsCount >= SIZE_OF_ROLLING_AVG ) - { - flowReadingsTotal -= flowReadings[ flowReadingsIdx ]; - } - flowReadings[ flowReadingsIdx ] = flow; - flowReadingsTotal += flow; - flowReadingsIdx = INC_WRAP( flowReadingsIdx, 0, SIZE_OF_ROLLING_AVG - 1 ); - flowReadingsCount = INC_CAP( flowReadingsCount, SIZE_OF_ROLLING_AVG ); - measuredDialInFlowRate.data = (F32)( flowReadingsTotal / (F64)flowReadingsCount ); -} - -/*********************************************************************//** - * @brief - * The updateDialInPumpSpeedAndDirectionFromHallSensors function calculates - * the dialysate inlet pump motor speed and direction from hall sensor counter on - * a 1 second interval. - * @details Inputs: dipLastMotorHallSensorCount, dipMotorSpeedCalcTimerCtr, current count from FPGA - * @details Outputs: dipMotorDirectionFromHallSensors, dialInPumpSpeedRPM - * @return none - *************************************************************************/ -static void updateDialInPumpSpeedAndDirectionFromHallSensors( void ) -{ - if ( ++dipMotorSpeedCalcTimerCtr >= DIP_SPEED_CALC_INTERVAL ) - { - U16 dipMotorHallSensorCount = getFPGADialInPumpHallSensorCount(); + *************************************************************************/ +static void resetDialInFlowMovingAverage( void ) +{ + flowReadingsIdx = 0; + flowReadingsCount = 0; + flowReadingsTotal = 0.0; + dipControlTimerCounter = 0; +} + +/*********************************************************************//** + * @brief + * The filterDialInFlowReadings function adds a new flow sample to the filter. + * @details Inputs: none + * @details Outputs: flowReadings[], flowReadingsIdx, flowReadingsCount, flowReadingsTotal + * @return none + *************************************************************************/ +void filterDialInFlowReadings( F64 flow ) +{ + if ( flowReadingsCount >= SIZE_OF_ROLLING_AVG ) + { + flowReadingsTotal -= flowReadings[ flowReadingsIdx ]; + } + flowReadings[ flowReadingsIdx ] = flow; + flowReadingsTotal += flow; + flowReadingsIdx = INC_WRAP( flowReadingsIdx, 0, SIZE_OF_ROLLING_AVG - 1 ); + flowReadingsCount = INC_CAP( flowReadingsCount, SIZE_OF_ROLLING_AVG ); + measuredDialInFlowRate.data = (F32)( flowReadingsTotal / (F64)flowReadingsCount ); +} + +/*********************************************************************//** + * @brief + * The updateDialInPumpSpeedAndDirectionFromHallSensors function calculates + * the dialysate inlet pump motor speed and direction from hall sensor counter on + * a 1 second interval. + * @details Inputs: dipLastMotorHallSensorCount, dipMotorSpeedCalcTimerCtr, current count from FPGA + * @details Outputs: dipMotorDirectionFromHallSensors, dialInPumpSpeedRPM + * @return none + *************************************************************************/ +static void updateDialInPumpSpeedAndDirectionFromHallSensors( void ) +{ + if ( ++dipMotorSpeedCalcTimerCtr >= DIP_SPEED_CALC_INTERVAL ) + { + U16 dipMotorHallSensorCount = getFPGADialInPumpHallSensorCount(); U32 nextIdx = INC_WRAP( dipMotorSpeedCalcIdx, 0, DIP_SPEED_CALC_BUFFER_LEN - 1 ); U16 incDelta = ( dipMotorHallSensorCount >= dipLastMotorHallSensorCounts[ nextIdx ] ? \ dipMotorHallSensorCount - dipLastMotorHallSensorCounts[ nextIdx ] : \ ( HEX_64_K - dipLastMotorHallSensorCounts[ nextIdx ] ) + dipMotorHallSensorCount ); - U16 decDelta = ( 0 == incDelta ? 0xFFFF : HEX_64_K - incDelta ); - U16 delta; - - // Determine dialysate inlet pump speed/direction from delta hall sensor count since last interval - if ( incDelta < decDelta ) - { - delta = incDelta; - dialInPumpSpeedRPM.data = ( (F32)delta / (F32)DIP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN; - } - else - { - delta = decDelta; - dialInPumpSpeedRPM.data = ( (F32)delta / (F32)DIP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN * -1.0; - } - - // Update last count for next time + U16 decDelta = ( 0 == incDelta ? 0xFFFF : HEX_64_K - incDelta ); + U16 delta; + + // Determine dialysate inlet pump speed/direction from delta hall sensor count since last interval + if ( incDelta < decDelta ) + { + delta = incDelta; + dialInPumpSpeedRPM.data = ( (F32)delta / (F32)DIP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN; + } + else + { + delta = decDelta; + dialInPumpSpeedRPM.data = ( (F32)delta / (F32)DIP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN * -1.0; + } + + // Update last count for next time dipLastMotorHallSensorCounts[ nextIdx ] = dipMotorHallSensorCount; dipMotorSpeedCalcIdx = nextIdx; - dipMotorSpeedCalcTimerCtr = 0; - } -} - -/*********************************************************************//** - * @brief - * The checkDialInPumpRotor function checks the rotor for the dialysate inlet - * pump. If homing, this function will stop when hall sensor detected. If pump - * is off or running very slowly, rotor speed will be set to zero. - * @details Inputs: dipStopAtHomePosition, dipHomeStartTime, dipRotorRevStartTime - * @details Outputs: pump may be stopped if homing, dialInPumpRotorSpeedRPM may be set to zero. - * @return none - *************************************************************************/ -static void checkDialInPumpRotor( void ) -{ - // If homing, check timeout - if ( ( TRUE == dipStopAtHomePosition ) && ( TRUE == didTimeout( dipHomeStartTime, DIP_HOME_TIMEOUT_MS ) ) ) - { - signalDialInPumpHardStop(); - dipStopAtHomePosition = FALSE; - // TODO - alarm??? - } - - // If pump is stopped or running very slowly, set rotor speed to zero - if ( TRUE == didTimeout( dipRotorRevStartTime, DIP_HOME_TIMEOUT_MS ) ) - { - dialInPumpRotorSpeedRPM.data = 0.0; - } -} - -/*********************************************************************//** - * @brief - * The checkDialInPumpDirection function checks the set direction vs. - * the direction implied by the sign of the measured MC speed. - * @details Inputs: adcDialInPumpMCSpeedRPM, dialInPumpDirectionSet, dialInPumpState - * @details Outputs: none - * @return none - *************************************************************************/ -static void checkDialInPumpDirection( void ) -{ - if ( DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE == dialInPumpState ) - { + dipMotorSpeedCalcTimerCtr = 0; + } +} + +/*********************************************************************//** + * @brief + * The checkDialInPumpRotor function checks the rotor for the dialysate inlet + * pump. If homing, this function will stop when hall sensor detected. If pump + * is off or running very slowly, rotor speed will be set to zero. + * @details Inputs: dipStopAtHomePosition, dipHomeStartTime, dipRotorRevStartTime + * @details Outputs: pump may be stopped if homing, dialInPumpRotorSpeedRPM may be set to zero. + * @return none + *************************************************************************/ +static void checkDialInPumpRotor( void ) +{ + // If homing, check timeout + if ( ( TRUE == dipStopAtHomePosition ) && ( TRUE == didTimeout( dipHomeStartTime, DIP_HOME_TIMEOUT_MS ) ) ) + { + signalDialInPumpHardStop(); + dipStopAtHomePosition = FALSE; + // TODO - alarm??? + } + + // If pump is stopped or running very slowly, set rotor speed to zero + if ( TRUE == didTimeout( dipRotorRevStartTime, DIP_HOME_TIMEOUT_MS ) ) + { + dialInPumpRotorSpeedRPM.data = 0.0; + } +} + +/*********************************************************************//** + * @brief + * The checkDialInPumpDirection function checks the set direction vs. + * the direction implied by the sign of the measured MC speed. + * @details Inputs: adcDialInPumpMCSpeedRPM, dialInPumpDirectionSet, dialInPumpState + * @details Outputs: none + * @return none + *************************************************************************/ +static void checkDialInPumpDirection( void ) +{ + if ( DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE == dialInPumpState ) + { MOTOR_DIR_T dipMCDir, dipDir; U08 dirErrorCnt = getFPGADialInPumpHallSensorStatus() & PUMP_DIR_ERROR_COUNT_MASK; + BOOL isHallSensorFailed = ( lastDialInPumpDirectionCount != dirErrorCnt ? TRUE : FALSE ); // Check pump direction error count - if ( lastDialInPumpDirectionCount != dirErrorCnt ) + if ( TRUE == isPersistentAlarmTriggered( ALARM_ID_HD_PUMP_DIRECTION_STATUS_ERROR, isHallSensorFailed ) ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMP_DIRECTION_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif { - lastDialInPumpDirectionCount = dirErrorCnt; SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_PUMP_DIRECTION_STATUS_ERROR, (U32)HD_PUMP_DIALYSATE_INLET_PUMP ) } } + lastDialInPumpDirectionCount = dirErrorCnt; dipMCDir = ( getMeasuredDialInPumpMCSpeed() >= 0.0 ? MOTOR_DIR_FORWARD : MOTOR_DIR_REVERSE ); dipDir = ( getMeasuredDialInPumpSpeed() >= 0.0 ? MOTOR_DIR_FORWARD : MOTOR_DIR_REVERSE ); - + // Check set direction vs. direction from hall sensors - if ( dialInPumpDirectionSet != dipDir ) - { + if ( dialInPumpDirectionSet != dipDir ) + { if ( ++errorDialInPumpDirectionPersistTimerCtr >= DIP_DIRECTION_ERROR_PERSIST ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMP_DIRECTION_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DIAL_IN_PUMP_MC_DIRECTION_CHECK, (U32)dialInPumpDirectionSet, (U32)dipDir ) - } - } - } + } + } + } // Check set direction vs. direction from sign of motor controller speed else if ( dialInPumpDirectionSet != dipMCDir ) { @@ -1072,116 +1073,116 @@ else { errorDialInPumpDirectionPersistTimerCtr = 0; - } -} - -/*********************************************************************//** - * @brief - * The checkDialInPumpSpeeds function checks several aspects of the dialysate - * inlet pump speed. - * 1. while pump is commanded off, measured motor speed should be < limit. - * 2. while pump is controlling, measured motor speed should be within allowed range of commanded speed. - * 3. measured motor speed should be within allowed range of measured rotor speed. - * All 3 checks have a persistence time that must be met before an alarm is triggered. - * @details Inputs: targetDialInFlowRate, dialInPumpSpeedRPM, dialInPumpRotorSpeedRPM - * @details Outputs: alarm(s) may be triggered - * @return none - *************************************************************************/ -static void checkDialInPumpSpeeds( void ) -{ - F32 measMotorSpeed = getMeasuredDialInPumpSpeed(); + } +} + +/*********************************************************************//** + * @brief + * The checkDialInPumpSpeeds function checks several aspects of the dialysate + * inlet pump speed. + * 1. while pump is commanded off, measured motor speed should be < limit. + * 2. while pump is controlling, measured motor speed should be within allowed range of commanded speed. + * 3. measured motor speed should be within allowed range of measured rotor speed. + * All 3 checks have a persistence time that must be met before an alarm is triggered. + * @details Inputs: targetDialInFlowRate, dialInPumpSpeedRPM, dialInPumpRotorSpeedRPM + * @details Outputs: alarm(s) may be triggered + * @return none + *************************************************************************/ +static void checkDialInPumpSpeeds( void ) +{ + F32 measMotorSpeed = getMeasuredDialInPumpSpeed(); F32 measMCMotorSpeed = fabs( getMeasuredDialInPumpMCSpeed() ); - - // Check for pump running while commanded off - if ( 0 == targetDialInFlowRate ) - { - if ( measMotorSpeed > DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM ) - { - if ( ++errorDialInMotorOffPersistTimerCtr >= DIP_OFF_ERROR_PERSIST ) + + // Check for pump running while commanded off + if ( 0 == targetDialInFlowRate ) + { + if ( measMotorSpeed > DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM ) + { + if ( ++errorDialInMotorOffPersistTimerCtr >= DIP_OFF_ERROR_PERSIST ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMP_SPEED_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { - SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DIAL_IN_PUMP_OFF_CHECK, measMotorSpeed ); + { + SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DIAL_IN_PUMP_OFF_CHECK, measMotorSpeed ); activateSafetyShutdown(); } - } - } - else - { - errorDialInMotorOffPersistTimerCtr = 0; - } - } - else - { - errorDialInMotorOffPersistTimerCtr = 0; - } - + } + } + else + { + errorDialInMotorOffPersistTimerCtr = 0; + } + } + else + { + errorDialInMotorOffPersistTimerCtr = 0; + } + // Checks that only occur when pump is running (and beyond ramp). - if ( DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE == dialInPumpState ) - { - F32 cmdMotorSpeed = DIP_PWM_TO_MOTOR_SPEED_RPM( dialInPumpPWMDutyCyclePctSet ); - F32 deltaMotorSpeed = fabs( measMotorSpeed - cmdMotorSpeed ); + if ( DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE == dialInPumpState ) + { + F32 cmdMotorSpeed = DIP_PWM_TO_MOTOR_SPEED_RPM( dialInPumpPWMDutyCyclePctSet ); + F32 deltaMotorSpeed = fabs( measMotorSpeed - cmdMotorSpeed ); F32 deltaMCMotorSpeed = fabs( measMCMotorSpeed - cmdMotorSpeed ); - F32 measRotorSpeed = getMeasuredDialInPumpRotorSpeed(); - F32 measMotorSpeedInRotorRPM = measMotorSpeed / DIP_GEAR_RATIO; - F32 deltaRotorSpeed = fabs( measRotorSpeed - measMotorSpeedInRotorRPM ); - - // Check measured motor speed vs. commanded motor speed while controlling to target - if ( ( deltaMotorSpeed > DIP_MAX_MOTOR_SPEED_ERROR_RPM ) || ( deltaMCMotorSpeed > DIP_MAX_MOTOR_SPEED_ERROR_RPM ) ) - { - if ( ++errorDialInMotorSpeedPersistTimerCtr >= DIP_MOTOR_SPEED_ERROR_PERSIST ) + F32 measRotorSpeed = getMeasuredDialInPumpRotorSpeed(); + F32 measMotorSpeedInRotorRPM = measMotorSpeed / DIP_GEAR_RATIO; + F32 deltaRotorSpeed = fabs( measRotorSpeed - measMotorSpeedInRotorRPM ); + + // Check measured motor speed vs. commanded motor speed while controlling to target + if ( ( deltaMotorSpeed > DIP_MAX_MOTOR_SPEED_ERROR_RPM ) || ( deltaMCMotorSpeed > DIP_MAX_MOTOR_SPEED_ERROR_RPM ) ) + { + if ( ++errorDialInMotorSpeedPersistTimerCtr >= DIP_MOTOR_SPEED_ERROR_PERSIST ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMP_SPEED_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { + { SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DIAL_IN_PUMP_MOTOR_SPEED_CHECK, cmdMotorSpeed, measMotorSpeed ); - } - } - } - else - { - errorDialInMotorSpeedPersistTimerCtr = 0; - } - - // Check measured rotor speed vs. measured motor speed while controlling to target - if ( deltaRotorSpeed > DIP_MAX_ROTOR_VS_MOTOR_DIFF_RPM ) - { - if ( ++errorDialInRotorSpeedPersistTimerCtr >= DIP_ROTOR_SPEED_ERROR_PERSIST ) + } + } + } + else + { + errorDialInMotorSpeedPersistTimerCtr = 0; + } + + // Check measured rotor speed vs. measured motor speed while controlling to target + if ( deltaRotorSpeed > DIP_MAX_ROTOR_VS_MOTOR_DIFF_RPM ) + { + if ( ++errorDialInRotorSpeedPersistTimerCtr >= DIP_ROTOR_SPEED_ERROR_PERSIST ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMP_SPEED_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { + { SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DIAL_IN_PUMP_ROTOR_SPEED_CHECK, measRotorSpeed, measMotorSpeed ); - } - } - } - else - { - errorDialInRotorSpeedPersistTimerCtr = 0; - } - - } - else - { - errorDialInMotorSpeedPersistTimerCtr = 0; - errorDialInRotorSpeedPersistTimerCtr = 0; - } -} - -/*********************************************************************//** - * @brief - * The checkDialInPumpFlowRate function checks the measured dialysate flow - * rate is in range. - * @details Inputs: measuredDialInFlowRate - * @details Outputs: alarm may be triggered - * @return none - *************************************************************************/ -static void checkDialInPumpFlowRate( void ) -{ + } + } + } + else + { + errorDialInRotorSpeedPersistTimerCtr = 0; + } + + } + else + { + errorDialInMotorSpeedPersistTimerCtr = 0; + errorDialInRotorSpeedPersistTimerCtr = 0; + } +} + +/*********************************************************************//** + * @brief + * The checkDialInPumpFlowRate function checks the measured dialysate flow + * rate is in range. + * @details Inputs: measuredDialInFlowRate + * @details Outputs: alarm may be triggered + * @return none + *************************************************************************/ +static void checkDialInPumpFlowRate( void ) +{ F32 flow = getMeasuredDialInFlowRate(); // Range check on measure DPi flow rate. @@ -1195,105 +1196,105 @@ } } - // Check only performed while in treatment mode and while we are in control to target state - if ( ( MODE_TREA == getCurrentOperationMode() ) && ( DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE == dialInPumpState ) ) - { - F32 flow = (F32)targetDialInFlowRate; - F32 speed = getMeasuredDialInPumpSpeed(); - F32 impliedSpeed = DIP_PWM_TO_MOTOR_SPEED_RPM( DIP_PWM_FROM_ML_PER_MIN( flow ) ); + // Check only performed while in treatment mode and while we are in control to target state + if ( ( MODE_TREA == getCurrentOperationMode() ) && ( DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE == dialInPumpState ) ) + { + F32 flow = (F32)targetDialInFlowRate; + F32 speed = getMeasuredDialInPumpSpeed(); + F32 impliedSpeed = ( flow * DIP_ML_PER_MIN_TO_PUMP_RPM_FACTOR * DIP_GEAR_RATIO ); F32 delta = fabs( speed - impliedSpeed ); - - if ( delta > DIP_MAX_FLOW_VS_SPEED_DIFF_RPM ) - { - if ( ++errorDialInFlowVsMotorSpeedPersistTimerCtr >= DIP_FLOW_VS_SPEED_PERSIST ) + + if ( delta > DIP_MAX_FLOW_VS_SPEED_DIFF_RPM ) + { + if ( ++errorDialInFlowVsMotorSpeedPersistTimerCtr >= DIP_FLOW_VS_SPEED_PERSIST ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_PUMP_FLOW_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { + { SET_ALARM_WITH_2_F32_DATA( ALARM_ID_DIAL_IN_PUMP_FLOW_VS_MOTOR_SPEED_CHECK, flow, speed ); - } - } - } - else - { - errorDialInFlowVsMotorSpeedPersistTimerCtr = 0; - } - } - else - { - errorDialInFlowVsMotorSpeedPersistTimerCtr = 0; + } + } + } + else + { + errorDialInFlowVsMotorSpeedPersistTimerCtr = 0; + } } -} - -/*********************************************************************//** - * @brief - * The checkDialInPumpMCCurrent function checks the measured MC current vs. - * the set state of the dialIn pump (stopped or running). - * @details Inputs: dialInPumpState, dipCurrErrorDurationCtr, adcDialInPumpMCCurrentmA - * @details Outputs: none - * @return none - *************************************************************************/ -static void checkDialInPumpMCCurrent( void ) -{ - F32 dipCurr; - - // DialIn pump should be off - if ( DIAL_IN_PUMP_OFF_STATE == dialInPumpState ) - { - dipCurr = fabs( getMeasuredDialInPumpMCCurrent() ); - if ( dipCurr > DIP_MAX_CURR_WHEN_STOPPED_MA ) - { - dipCurrErrorDurationCtr += TASK_PRIORITY_INTERVAL; - if ( dipCurrErrorDurationCtr > DIP_MAX_CURR_ERROR_DURATION_MS ) + else + { + errorDialInFlowVsMotorSpeedPersistTimerCtr = 0; + } +} + +/*********************************************************************//** + * @brief + * The checkDialInPumpMCCurrent function checks the measured MC current vs. + * the set state of the dialIn pump (stopped or running). + * @details Inputs: dialInPumpState, dipCurrErrorDurationCtr, adcDialInPumpMCCurrentmA + * @details Outputs: none + * @return none + *************************************************************************/ +static void checkDialInPumpMCCurrent( void ) +{ + F32 dipCurr; + + // DialIn pump should be off + if ( DIAL_IN_PUMP_OFF_STATE == dialInPumpState ) + { + dipCurr = fabs( getMeasuredDialInPumpMCCurrent() ); + if ( dipCurr > DIP_MAX_CURR_WHEN_STOPPED_MA ) + { + dipCurrErrorDurationCtr += TASK_PRIORITY_INTERVAL; + if ( dipCurrErrorDurationCtr > DIP_MAX_CURR_ERROR_DURATION_MS ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_MOTOR_CURRNT_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { + { SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DIAL_IN_PUMP_MC_CURRENT_CHECK, getMeasuredDialInPumpMCCurrent() ); - } - } - } - else - { - dipCurrErrorDurationCtr = 0; - } - } - // DialIn pump should be running - else - { - dipCurr = fabs( getMeasuredDialInPumpMCCurrent() ); - if ( dipCurr > DIP_MAX_CURR_WHEN_RUNNING_MA ) - { - dipCurrErrorDurationCtr += TASK_PRIORITY_INTERVAL; - if ( dipCurrErrorDurationCtr > DIP_MAX_CURR_ERROR_DURATION_MS ) + } + } + } + else + { + dipCurrErrorDurationCtr = 0; + } + } + // DialIn pump should be running + else + { + dipCurr = fabs( getMeasuredDialInPumpMCCurrent() ); + if ( dipCurr > DIP_MAX_CURR_WHEN_RUNNING_MA ) + { + dipCurrErrorDurationCtr += TASK_PRIORITY_INTERVAL; + if ( dipCurrErrorDurationCtr > DIP_MAX_CURR_ERROR_DURATION_MS ) { #ifndef _RELEASE_ if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_MOTOR_CURRNT_CHECKS ) != SW_CONFIG_ENABLE_VALUE ) #endif - { + { SET_ALARM_WITH_1_F32_DATA( ALARM_ID_DIAL_IN_PUMP_MC_CURRENT_CHECK, getMeasuredDialInPumpMCCurrent() ); - } - } - } - else - { - dipCurrErrorDurationCtr = 0; - } - } + } + } + } + else + { + dipCurrErrorDurationCtr = 0; + } + } } -/*********************************************************************//** - * @brief - * The execDialInFlowTest function executes the state machine for the - * DialInFlow self-test. - * @details Inputs: none - * @details Outputs: dialInPumpCalRecord - * @return the current state of the DialInFlow self-test. - *************************************************************************/ -SELF_TEST_STATUS_T execDialInFlowTest( void ) -{ +/*********************************************************************//** + * @brief + * The execDialInFlowTest function executes the state machine for the + * DialInFlow self-test. + * @details Inputs: none + * @details Outputs: dialInPumpCalRecord + * @return the current state of the DialInFlow self-test. + *************************************************************************/ +SELF_TEST_STATUS_T execDialInFlowTest( void ) +{ SELF_TEST_STATUS_T result = SELF_TEST_STATUS_PASSED; BOOL calStatus = getNVRecord2Driver( GET_CAL_PUMPS, (U08*)&dialInPumpCalRecord, sizeof( HD_PUMPS_CAL_RECORD_T ), @@ -1308,88 +1309,88 @@ result = SELF_TEST_STATUS_FAILED; } - return result; -} - - -/************************************************************************* - * TEST SUPPORT FUNCTIONS - *************************************************************************/ + return result; +} - -/*********************************************************************//** - * @brief - * The testSetDialInFlowDataPublishIntervalOverride function overrides the - * dialIn flow data publish interval. - * @details Inputs: none - * @details Outputs: dialInFlowDataPublishInterval - * @param value override dialIn flow data publish interval with (in ms) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetDialInFlowDataPublishIntervalOverride( U32 value ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - U32 intvl = value / TASK_PRIORITY_INTERVAL; - - result = TRUE; - dialInFlowDataPublishInterval.ovData = intvl; - dialInFlowDataPublishInterval.override = OVERRIDE_KEY; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetDialInFlowDataPublishIntervalOverride function resets the override - * of the dialIn flow data publish interval. - * @details Inputs: none - * @details Outputs: dialInFlowDataPublishInterval - * @return TRUE if override reset successful, FALSE if not - *************************************************************************/ -BOOL testResetDialInFlowDataPublishIntervalOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - dialInFlowDataPublishInterval.override = OVERRIDE_RESET; - dialInFlowDataPublishInterval.ovData = dialInFlowDataPublishInterval.ovInitData; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testSetTargetDialInFlowRateOverride function overrides the target - * dialysate inlet flow rate.n - * @details Inputs: none - * @details Outputs: targetDialInFlowRate - * @param value override target dialysate inlet flow rate (in mL/min) + +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/ + + +/*********************************************************************//** + * @brief + * The testSetDialInFlowDataPublishIntervalOverride function overrides the + * dialIn flow data publish interval. + * @details Inputs: none + * @details Outputs: dialInFlowDataPublishInterval + * @param value override dialIn flow data publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetDialInFlowDataPublishIntervalOverride( U32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + U32 intvl = value / TASK_PRIORITY_INTERVAL; + + result = TRUE; + dialInFlowDataPublishInterval.ovData = intvl; + dialInFlowDataPublishInterval.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetDialInFlowDataPublishIntervalOverride function resets the override + * of the dialIn flow data publish interval. + * @details Inputs: none + * @details Outputs: dialInFlowDataPublishInterval + * @return TRUE if override reset successful, FALSE if not + *************************************************************************/ +BOOL testResetDialInFlowDataPublishIntervalOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + dialInFlowDataPublishInterval.override = OVERRIDE_RESET; + dialInFlowDataPublishInterval.ovData = dialInFlowDataPublishInterval.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetTargetDialInFlowRateOverride function overrides the target + * dialysate inlet flow rate.n + * @details Inputs: none + * @details Outputs: targetDialInFlowRate + * @param value override target dialysate inlet flow rate (in mL/min) * @param ctrlMode override pump control mode to this mode (0 = closed loop, 1 = open loop) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetTargetDialInFlowRateOverride( S32 value, U32 ctrlMode ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - MOTOR_DIR_T dir; - - if ( value < 0 ) - { - dir = MOTOR_DIR_REVERSE; - } - else - { - dir = MOTOR_DIR_FORWARD; - } + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetTargetDialInFlowRateOverride( S32 value, U32 ctrlMode ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + MOTOR_DIR_T dir; + + if ( value < 0 ) + { + dir = MOTOR_DIR_REVERSE; + } + else + { + dir = MOTOR_DIR_FORWARD; + } if ( ctrlMode < NUM_OF_PUMP_CONTROL_MODES ) { if ( 0 == value ) @@ -1400,235 +1401,235 @@ { result = setDialInPumpTargetFlowRate( abs(value), dir, (PUMP_CONTROL_MODE_T)ctrlMode ); } - } - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetMeasuredDialInFlowRateOverride function overrides the measured - * dialIn flow rate. - * @details Inputs: none - * @details Outputs: measuredDialInFlowRate - * @param value override measured dialIn flow rate (in mL/min) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetMeasuredDialInFlowRateOverride( F32 value ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - measuredDialInFlowRate.ovData = value; - measuredDialInFlowRate.override = OVERRIDE_KEY; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetMeasuredDialInFlowRateOverride function resets the override of the - * measured dialIn flow rate. - * @details Inputs: none - * @details Outputs: measuredDialInFlowRate - * @return TRUE if reset successful, FALSE if not - *************************************************************************/ -BOOL testResetMeasuredDialInFlowRateOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - measuredDialInFlowRate.override = OVERRIDE_RESET; - measuredDialInFlowRate.ovData = measuredDialInFlowRate.ovInitData; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testSetMeasuredDialInPumpRotorSpeedOverride function overrides the measured - * dialIn pump rotor speed. - * @details Inputs: none - * @details Outputs: dialInPumpRotorSpeedRPM - * @param value override measured dialIn pump rotor speed (in RPM) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetMeasuredDialInPumpRotorSpeedOverride( F32 value ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - dialInPumpRotorSpeedRPM.ovData = value; - dialInPumpRotorSpeedRPM.override = OVERRIDE_KEY; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetMeasuredDialInPumpRotorSpeedOverride function resets the override of the - * measured dialIn pump rotor speed. - * @details Inputs: none - * @details Outputs: dialInPumpRotorSpeedRPM - * @return TRUE if reset successful, FALSE if not - *************************************************************************/ -BOOL testResetMeasuredDialInPumpRotorSpeedOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - dialInPumpRotorSpeedRPM.override = OVERRIDE_RESET; - dialInPumpRotorSpeedRPM.ovData = dialInPumpRotorSpeedRPM.ovInitData; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testSetMeasuredDialInPumpSpeedOverride function overrides the measured - * dialIn pump motor speed. - * @details Inputs: none - * @details Outputs: dialInPumpSpeedRPM - * @param value override measured dialIn pump motor speed (in RPM) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetMeasuredDialInPumpSpeedOverride( F32 value ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - dialInPumpSpeedRPM.ovData = value; - dialInPumpSpeedRPM.override = OVERRIDE_KEY; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetMeasuredDialInPumpSpeedOverride function resets the override of the - * measured dialIn pump motor speed. - * @details Inputs: none - * @details Outputs: dialInPumpSpeedRPM - * @return TRUE if reset successful, FALSE if not - *************************************************************************/ -BOOL testResetMeasuredDialInPumpSpeedOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - dialInPumpSpeedRPM.override = OVERRIDE_RESET; - dialInPumpSpeedRPM.ovData = dialInPumpSpeedRPM.ovInitData; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testSetMeasuredDialInPumpMCSpeedOverride function overrides the measured - * dialIn pump motor speed. - * @details Inputs: none - * @details Outputs: adcDialInPumpMCSpeedRPM - * @param value override measured dialIn pump speed (in RPM) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetMeasuredDialInPumpMCSpeedOverride( F32 value ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - adcDialInPumpMCSpeedRPM.ovData = value; - adcDialInPumpMCSpeedRPM.override = OVERRIDE_KEY; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetMeasuredDialInPumpMCSpeedOverride function resets the override of the - * measured dialIn pump motor speed. - * @details Inputs: none - * @details Outputs: adcDialInPumpMCSpeedRPM - * @return TRUE if reset successful, FALSE if not - *************************************************************************/ -BOOL testResetMeasuredDialInPumpMCSpeedOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - adcDialInPumpMCSpeedRPM.override = OVERRIDE_RESET; - adcDialInPumpMCSpeedRPM.ovData = adcDialInPumpMCSpeedRPM.ovInitData; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testSetMeasuredDialInPumpMCCurrentOverride function overrides the measured - * dialIn pump motor current. - * @details Inputs: none - * @details Outputs: adcDialInPumpMCCurrentmA - * @param value override measured dialIn pump current (in mA) - * @return TRUE if override successful, FALSE if not - *************************************************************************/ -BOOL testSetMeasuredDialInPumpMCCurrentOverride( F32 value ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - adcDialInPumpMCCurrentmA.ovData = value; - adcDialInPumpMCCurrentmA.override = OVERRIDE_KEY; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The testResetMeasuredDialInPumpMCCurrentOverride function resets the override of the - * measured dialIn pump motor current. - * @details Inputs: none - * @details Outputs: adcDialInPumpMCCurrentmA - * @return TRUE if reset successful, FALSE if not - *************************************************************************/ -BOOL testResetMeasuredDialInPumpMCCurrentOverride( void ) -{ - BOOL result = FALSE; - - if ( TRUE == isTestingActivated() ) - { - result = TRUE; - adcDialInPumpMCCurrentmA.override = OVERRIDE_RESET; - adcDialInPumpMCCurrentmA.ovData = adcDialInPumpMCCurrentmA.ovInitData; - } - - return result; -} + } + } -/**@}*/ + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredDialInFlowRateOverride function overrides the measured + * dialIn flow rate. + * @details Inputs: none + * @details Outputs: measuredDialInFlowRate + * @param value override measured dialIn flow rate (in mL/min) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetMeasuredDialInFlowRateOverride( F32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + measuredDialInFlowRate.ovData = value; + measuredDialInFlowRate.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredDialInFlowRateOverride function resets the override of the + * measured dialIn flow rate. + * @details Inputs: none + * @details Outputs: measuredDialInFlowRate + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredDialInFlowRateOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + measuredDialInFlowRate.override = OVERRIDE_RESET; + measuredDialInFlowRate.ovData = measuredDialInFlowRate.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredDialInPumpRotorSpeedOverride function overrides the measured + * dialIn pump rotor speed. + * @details Inputs: none + * @details Outputs: dialInPumpRotorSpeedRPM + * @param value override measured dialIn pump rotor speed (in RPM) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetMeasuredDialInPumpRotorSpeedOverride( F32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + dialInPumpRotorSpeedRPM.ovData = value; + dialInPumpRotorSpeedRPM.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredDialInPumpRotorSpeedOverride function resets the override of the + * measured dialIn pump rotor speed. + * @details Inputs: none + * @details Outputs: dialInPumpRotorSpeedRPM + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredDialInPumpRotorSpeedOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + dialInPumpRotorSpeedRPM.override = OVERRIDE_RESET; + dialInPumpRotorSpeedRPM.ovData = dialInPumpRotorSpeedRPM.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredDialInPumpSpeedOverride function overrides the measured + * dialIn pump motor speed. + * @details Inputs: none + * @details Outputs: dialInPumpSpeedRPM + * @param value override measured dialIn pump motor speed (in RPM) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetMeasuredDialInPumpSpeedOverride( F32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + dialInPumpSpeedRPM.ovData = value; + dialInPumpSpeedRPM.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredDialInPumpSpeedOverride function resets the override of the + * measured dialIn pump motor speed. + * @details Inputs: none + * @details Outputs: dialInPumpSpeedRPM + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredDialInPumpSpeedOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + dialInPumpSpeedRPM.override = OVERRIDE_RESET; + dialInPumpSpeedRPM.ovData = dialInPumpSpeedRPM.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredDialInPumpMCSpeedOverride function overrides the measured + * dialIn pump motor speed. + * @details Inputs: none + * @details Outputs: adcDialInPumpMCSpeedRPM + * @param value override measured dialIn pump speed (in RPM) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetMeasuredDialInPumpMCSpeedOverride( F32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcDialInPumpMCSpeedRPM.ovData = value; + adcDialInPumpMCSpeedRPM.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredDialInPumpMCSpeedOverride function resets the override of the + * measured dialIn pump motor speed. + * @details Inputs: none + * @details Outputs: adcDialInPumpMCSpeedRPM + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredDialInPumpMCSpeedOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcDialInPumpMCSpeedRPM.override = OVERRIDE_RESET; + adcDialInPumpMCSpeedRPM.ovData = adcDialInPumpMCSpeedRPM.ovInitData; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testSetMeasuredDialInPumpMCCurrentOverride function overrides the measured + * dialIn pump motor current. + * @details Inputs: none + * @details Outputs: adcDialInPumpMCCurrentmA + * @param value override measured dialIn pump current (in mA) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetMeasuredDialInPumpMCCurrentOverride( F32 value ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcDialInPumpMCCurrentmA.ovData = value; + adcDialInPumpMCCurrentmA.override = OVERRIDE_KEY; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The testResetMeasuredDialInPumpMCCurrentOverride function resets the override of the + * measured dialIn pump motor current. + * @details Inputs: none + * @details Outputs: adcDialInPumpMCCurrentmA + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetMeasuredDialInPumpMCCurrentOverride( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + result = TRUE; + adcDialInPumpMCCurrentmA.override = OVERRIDE_RESET; + adcDialInPumpMCCurrentmA.ovData = adcDialInPumpMCCurrentmA.ovInitData; + } + + return result; +} + +/**@}*/