Index: firmware/App/Controllers/DialInFlow.c =================================================================== diff -u -r40a0bc77c53a4eb538580d44f1117d26f1d37fb2 -ra2bc96881a5fc3d8f779246b2abebf15a8de9384 --- firmware/App/Controllers/DialInFlow.c (.../DialInFlow.c) (revision 40a0bc77c53a4eb538580d44f1117d26f1d37fb2) +++ firmware/App/Controllers/DialInFlow.c (.../DialInFlow.c) (revision a2bc96881a5fc3d8f779246b2abebf15a8de9384) @@ -41,49 +41,49 @@ // ********** private definitions ********** -/// interval (ms/task time) at which the dialIn flow data is published on the CAN bus. +/// 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.0133 ///< max duty cycle change when ramping up ~ 200 mL/min/s. -#define MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE 0.02 ///< max duty cycle change when ramping down ~ 300 mL/min/s. -#define MAX_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.88 ///< controller will error if PWM duty cycle > 90%, so set max to 88%. -#define MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.12 ///< controller will error if PWM duty cycle < 10%, so set min to 12%. +#define MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE 0.0133 ///< Max duty cycle change when ramping up ~ 200 mL/min/s. +#define MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE 0.02 ///< Max duty cycle change when ramping down ~ 300 mL/min/s. +#define MAX_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.88 ///< Controller will error if PWM duty cycle > 90%, so set max to 88%. +#define MIN_DIAL_IN_PUMP_PWM_DUTY_CYCLE 0.12 ///< Controller will error if PWM duty cycle < 10%, so set min to 12%. -#define DIP_CONTROL_INTERVAL ( 10000 / TASK_GENERAL_INTERVAL ) ///< interval (ms/task time) at which the dialIn pump is controlled +#define DIP_CONTROL_INTERVAL ( 10000 / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the dialIn pump is controlled #define DIP_P_COEFFICIENT 0.00035 ///< P term for dialIn pump control. #define DIP_I_COEFFICIENT 0.00035 ///< 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_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. +/// 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_VS_SPEED_DIFF_RPM 200.0 ///< maximum difference between measured motor speed and speed implied by measured flow. -#define DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0 ///< maximum motor speed (RPM) while motor is commanded off. -#define DIP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0 ///< maximum difference in speed between motor and rotor (in rotor RPM). -#define DIP_MAX_MOTOR_SPEED_ERROR_RPM 300.0 ///< maximum difference in speed between measured and commanded RPM. -#define DIP_FLOW_VS_SPEED_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) for flow vs. motor speed error condition. -#define DIP_OFF_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) for motor off error condition. -#define DIP_MOTOR_SPEED_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) motor speed error condition. -#define DIP_ROTOR_SPEED_ERROR_PERSIST ((12 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) rotor speed error condition. -#define DIP_DIRECTION_ERROR_PERSIST (250 / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) pump direction error condition. +#define DIP_MAX_FLOW_VS_SPEED_DIFF_RPM 200.0 ///< Maximum difference between measured motor speed and speed implied by measured flow. +#define DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0 ///< Maximum motor speed (RPM) while motor is commanded off. +#define DIP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0 ///< Maximum difference in speed between motor and rotor (in rotor RPM). +#define DIP_MAX_MOTOR_SPEED_ERROR_RPM 300.0 ///< Maximum difference in speed between measured and commanded RPM. +#define DIP_FLOW_VS_SPEED_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) for flow vs. motor speed error condition. +#define DIP_OFF_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) for motor off error condition. +#define DIP_MOTOR_SPEED_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) motor speed error condition. +#define DIP_ROTOR_SPEED_ERROR_PERSIST ((12 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) rotor speed error condition. +#define DIP_DIRECTION_ERROR_PERSIST (250 / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) pump direction error condition. -#define DIP_MAX_CURR_WHEN_STOPPED_MA 150.0 ///< motor controller current should not exceed this when pump should be stopped. -#define DIP_MIN_CURR_WHEN_RUNNING_MA 150.0 ///< motor controller current should always exceed this when pump should be running. -#define DIP_MAX_CURR_WHEN_RUNNING_MA 2000.0 ///< 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.0 ///< Motor controller current should not exceed this when pump should be stopped. +#define DIP_MIN_CURR_WHEN_RUNNING_MA 150.0 ///< Motor controller current should always exceed this when pump should be running. +#define DIP_MAX_CURR_WHEN_RUNNING_MA 2000.0 ///< 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_SPEED_ADC_TO_RPM_FACTOR 1.280938 ///< conversion factor from ADC counts to RPM for dialIn pump motor. -#define DIP_CURRENT_ADC_TO_MA_FACTOR 3.002 ///< conversion factor from ADC counts to mA for dialIn pump motor. +#define DIP_SPEED_ADC_TO_RPM_FACTOR 1.280938 ///< Conversion factor from ADC counts to RPM for dialIn pump motor. +#define DIP_CURRENT_ADC_TO_MA_FACTOR 3.002 ///< Conversion factor from ADC counts to mA for dialIn pump motor. -#define DIP_REV_PER_LITER 150.0 ///< rotor revolutions per liter. +#define DIP_REV_PER_LITER 150.0 ///< 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.0 ///< dialIn pump motor to dialIn pump gear ratio. +#define DIP_GEAR_RATIO 32.0 ///< DialIn pump motor to dialIn pump gear ratio. #define DIP_MOTOR_RPM_TO_PWM_DC_FACTOR 0.00028 ///< ~28 BP motor RPM = 1% PWM duty cycle. #define DIP_PWM_ZERO_OFFSET 0.1 ///< 10% PWM duty cycle = zero speed. /// Macro converts flow rate to estimate PWM needed to achieve it. @@ -94,10 +94,10 @@ ///< 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 ) -/// measured dialIn flow is filtered w/ moving average. +/// Measured dialIn flow is filtered w/ moving average. #define SIZE_OF_ROLLING_AVG ( ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) * 10 ) -/// dialysate flow sensor signal strength low alarm persistence. +/// Dialysate flow sensor signal strength low alarm persistence. #define FLOW_SIG_STRGTH_ALARM_PERSIST ( 5 * MS_PER_SECOND ) #define MIN_FLOW_SIG_STRENGTH 0.9 ///< Minimum flow sensor signal strength (90%). @@ -120,69 +120,64 @@ 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 +// 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 +// 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. -#ifndef BREADBOARD_TARGET - #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. -#else - #define SET_DIP_STOP() gioSetBit( gioPORTA, STOP_DI_PUMP_GIO_PORT_PIN, PIN_SIGNAL_HIGH ) - #define CLR_DIP_STOP() gioSetBit( gioPORTA, STOP_DI_PUMP_GIO_PORT_PIN, PIN_SIGNAL_LOW ) -#endif +#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 = 5; ///< 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. -static F32 dialInFlowCalGain = 1.0; ///< dialysate flow calibration gain. -static F32 dialInFlowCalOffset = 0.0; ///< dialysate flow calibration offset. +static DIAL_IN_PUMP_STATE_T dialInPumpState = DIAL_IN_PUMP_OFF_STATE; ///< Current state of dialIn flow controller state machine +static U32 dialInFlowDataPublicationTimerCounter = 5; ///< 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. +static F32 dialInFlowCalGain = 1.0; ///< Dialysate flow calibration gain. +static F32 dialInFlowCalOffset = 0.0; ///< Dialysate flow calibration offset. -/// interval (in ms) at which to publish dialIn flow data to CAN bus +/// 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 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 OVERRIDE_F32_T dialInFlowSignalStrength = { 0.0, 0.0, 0.0, 0 }; ///< measured dialysate flow signal strength (%) +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 OVERRIDE_F32_T dialInFlowSignalStrength = { 0.0, 0.0, 0.0, 0 }; ///< Measured dialysate flow signal strength (%) -static U32 dipControlTimerCounter = 0; ///< determines when to perform control on dialIn flow +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 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 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 errorDialInPumpDirectionPersistTimerCtr = 0; ///< persistence timer counter for pump direction error condition. +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 F32 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 F32 flowReadingsTotal = 0.0; ///< rolling total - used to calc average -static U32 flowReadingsCount = 0; ///< number of samples in flow rolling average buffer +static F32 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 F32 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 U32 dipCurrErrorDurationCtr = 0; ///< Used for tracking persistence of dip current errors -static DIAL_IN_FLOW_SELF_TEST_STATE_T dialInPumpSelfTestState = DIAL_IN_FLOW_SELF_TEST_STATE_START; ///< current dialIn pump self-test state -static U32 dialInPumpSelfTestTimerCount = 0; ///< timer counter for dialIn pump self-test +static DIAL_IN_FLOW_SELF_TEST_STATE_T dialInPumpSelfTestState = DIAL_IN_FLOW_SELF_TEST_STATE_START; ///< Current dialIn pump self-test state +static U32 dialInPumpSelfTestTimerCount = 0; ///< Timer counter for dialIn pump self-test // ********** private function prototypes ********** @@ -204,7 +199,7 @@ static void checkDialInPumpFlowAgainstSpeed( void ); static void checkDialInPumpMCCurrent( void ); static void checkDialInFlowSensorSignalStrength( void ); -static DATA_GET_PROTOTYPE( U32, getPublishDialInFlowDataInterval ); +static U32 getPublishDialInFlowDataInterval( void ); /*********************************************************************//** * @brief @@ -220,22 +215,22 @@ stopDialInPump(); setDialInPumpDirection( MOTOR_DIR_FORWARD ); - // zero rolling flow average buffer + // Zero rolling flow average buffer resetDialInFlowMovingAverage(); - // zero motor hall sensors counts buffer + // Zero motor hall sensors counts buffer dipMotorSpeedCalcIdx = 0; for ( i = 0; i < DIP_SPEED_CALC_BUFFER_LEN; i++ ) { dipLastMotorHallSensorCounts[ i ] = 0; } - // initialize dialysate inlet flow PI controller + // 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 + // Initialize persistent alarm for flow sensor signal strength too low initPersistentAlarm( PERSISTENT_ALARM_DIALYSATE_FLOW_SIGNAL_STRENGTH, ALARM_ID_DIALYSATE_FLOW_SIGNAL_STRENGTH_TOO_LOW, FALSE, FLOW_SIG_STRGTH_ALARM_PERSIST, FLOW_SIG_STRGTH_ALARM_PERSIST ); @@ -256,34 +251,34 @@ { BOOL result = FALSE; - // direction change while pump is running is not allowed + // Direction change while pump is running is not allowed if ( ( FALSE == isDialInPumpOn ) || ( 0 == flowRate ) || ( dir == dialInPumpDirectionSet ) ) { - // verify flow rate + // Verify flow rate if ( flowRate <= MAX_DIAL_IN_FLOW_RATE ) { resetDialInFlowMovingAverage(); targetDialInFlowRate = ( dir == MOTOR_DIR_FORWARD ? (S32)flowRate : (S32)flowRate * -1 ); dialInPumpDirection = dir; dialInPumpControlMode = mode; - // set PWM duty cycle target to an estimated initial target to ramp to based on target flow rate - then we'll control to flow when ramp completed + // 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 = DIP_PWM_FROM_ML_PER_MIN((F32)flowRate); // ~ 8% per 100 mL/min with a 10% zero offset added in (e.g. 100 mL/min = 8+10 = 18%) switch ( dialInPumpState ) { - case DIAL_IN_PUMP_RAMPING_UP_STATE: // see if we need to reverse direction of ramp + 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 + 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 + 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; @@ -294,12 +289,12 @@ } break; default: - // ok - not all states need to be handled here + // Ok - not all states need to be handled here break; } result = TRUE; } - else // requested flow rate too high + else // Requested flow rate too high { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIAL_IN_FLOW_SET_TOO_HIGH, flowRate ) } @@ -339,11 +334,11 @@ U32 rotTime = getMSTimerCount(); U32 deltaTime = calcTimeBetween( dipRotorRevStartTime, rotTime ); - // calculate rotor speed (in RPM) + // Calculate rotor speed (in RPM) dialInPumpRotorSpeedRPM.data = ( 1.0 / (F32)deltaTime ) * (F32)MS_PER_SECOND * (F32)SEC_PER_MIN; dipRotorRevStartTime = rotTime; - // if we're supposed to stop pump at home position, stop pump now. + // If we are supposed to stop pump at home position, stop pump now. if ( TRUE == dipStopAtHomePosition ) { signalDialInPumpHardStop(); @@ -405,26 +400,26 @@ filterDialInFlowReadings( dipFlow ); - // calculate dialysate inlet pump motor speed/direction from hall sensor count + // Calculate dialysate inlet pump motor speed/direction from hall sensor count updateDialInPumpSpeedAndDirectionFromHallSensors(); - // don't start enforcing checks until out of init/POST mode + // Do not start enforcing checks until out of init/POST mode if ( opMode != MODE_INIT ) { - // check pump direction + // Check pump direction checkDialInPumpDirection(); - // check pump controller current + // Check pump controller current checkDialInPumpMCCurrent(); - // check pump speeds and flow + // Check pump speeds and flow checkDialInPumpSpeeds(); checkDialInPumpFlowAgainstSpeed(); - // check for home position, zero/low speed + // Check for home position, zero/low speed checkDialInPumpRotor(); - // check flow sensor signal strength + // Check flow sensor signal strength checkDialInFlowSensorSignalStrength(); } - // publish dialIn flow data on interval + // Publish dialIn flow data on interval publishDialInFlowData(); } @@ -473,13 +468,13 @@ { DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_OFF_STATE; - // if we've been given a flow rate, setup ramp up and transition to ramp up 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 + // 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 + // Allow dialIn pump to run in requested direction setDialInPumpDirection( dialInPumpDirection ); releaseDialInPumpStop(); isDialInPumpOn = TRUE; @@ -501,29 +496,29 @@ { DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_RAMPING_UP_STATE; - // have we been asked to stop the dialIn pump? + // Have we been asked to stop the dialIn pump? if ( 0 == targetDialInFlowRate ) { - // start ramp down to stop + // 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? + // Have we reached end of ramp up? else if ( dialInPumpPWMDutyCyclePctSet >= dialInPumpPWMDutyCyclePct ) { resetDialInFlowMovingAverage(); resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, dialInPumpPWMDutyCyclePctSet ); dialInPumpControlModeSet = dialInPumpControlMode; - // if open loop mode, set PWM to requested duty cycle where it will stay during control state + // If open loop mode, set PWM to requested duty cycle where it will stay during control state if ( dialInPumpControlModeSet == PUMP_CONTROL_MODE_OPEN_LOOP ) { dialInPumpPWMDutyCyclePctSet = dialInPumpPWMDutyCyclePct; setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePct ); } result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; } - // continue ramp up + // Continue ramp up else { dialInPumpPWMDutyCyclePctSet += MAX_DIAL_IN_PUMP_PWM_STEP_UP_CHANGE; @@ -545,27 +540,27 @@ { DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_RAMPING_DOWN_STATE; - // have we essentially reached zero speed + // Have we essentially reached zero speed if ( dialInPumpPWMDutyCyclePctSet < (MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE + DIP_PWM_ZERO_OFFSET) ) { stopDialInPump(); result = DIAL_IN_PUMP_OFF_STATE; } - // have we reached end of ramp down? + // Have we reached end of ramp down? else if ( dialInPumpPWMDutyCyclePctSet <= dialInPumpPWMDutyCyclePct ) { resetDialInFlowMovingAverage(); resetPIController( PI_CONTROLLER_ID_DIALYSATE_FLOW, dialInPumpPWMDutyCyclePctSet ); dialInPumpControlModeSet = dialInPumpControlMode; - // if open loop mode, set PWM to requested duty cycle where it will stay during control state + // If open loop mode, set PWM to requested duty cycle where it will stay during control state if ( dialInPumpControlModeSet == PUMP_CONTROL_MODE_OPEN_LOOP ) { dialInPumpPWMDutyCyclePctSet = dialInPumpPWMDutyCyclePct; setDialInPumpControlSignalPWM( dialInPumpPWMDutyCyclePct ); } result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; } - // continue ramp down + // Continue ramp down else { dialInPumpPWMDutyCyclePctSet -= MAX_DIAL_IN_PUMP_PWM_STEP_DN_CHANGE; @@ -587,7 +582,7 @@ { DIAL_IN_PUMP_STATE_T result = DIAL_IN_PUMP_CONTROL_TO_TARGET_STATE; - // control at set interval + // Control at set interval if ( ++dipControlTimerCounter >= DIP_CONTROL_INTERVAL ) { if ( dialInPumpControlModeSet == PUMP_CONTROL_MODE_CLOSED_LOOP ) @@ -840,7 +835,7 @@ *************************************************************************/ static void publishDialInFlowData( void ) { - // publish dialIn flow data on interval + // Publish dialIn flow data on interval if ( ++dialInFlowDataPublicationTimerCounter >= getPublishDialInFlowDataInterval() ) { DIALIN_PUMP_STATUS_PAYLOAD_T payload; @@ -919,7 +914,7 @@ U16 decDelta = HEX_64_K - incDelta; U16 delta; - // determine dialysate inlet pump speed/direction from delta hall sensor count since last interval + // Determine dialysate inlet pump speed/direction from delta hall sensor count since last interval if ( incDelta < decDelta ) { delta = incDelta; @@ -931,7 +926,7 @@ dialInPumpSpeedRPM.data = ( (F32)delta / (F32)DIP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN * -1.0; } - // update last count for next time + // Update last count for next time dipLastMotorHallSensorCounts[ nextIdx ] = dipMotorHallSensorCount; dipMotorSpeedCalcIdx = nextIdx; dipMotorSpeedCalcTimerCtr = 0; @@ -949,15 +944,15 @@ *************************************************************************/ static void checkDialInPumpRotor( void ) { - // if homing, check timeout + // 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 pump is stopped or running very slowly, set rotor speed to zero if ( TRUE == didTimeout( dipRotorRevStartTime, DIP_HOME_TIMEOUT_MS ) ) { dialInPumpRotorSpeedRPM.data = 0.0; @@ -981,7 +976,7 @@ 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 + // Check set direction vs. direction from hall sensors if ( dialInPumpDirectionSet != dipDir ) { if ( ++errorDialInPumpDirectionPersistTimerCtr >= DIP_DIRECTION_ERROR_PERSIST ) @@ -991,7 +986,7 @@ #endif } } - // check set direction vs. direction from sign of motor controller speed + // Check set direction vs. direction from sign of motor controller speed else if ( dialInPumpDirectionSet != dipMCDir ) { if ( ++errorDialInPumpDirectionPersistTimerCtr >= DIP_DIRECTION_ERROR_PERSIST ) @@ -1029,7 +1024,7 @@ F32 measMotorSpeed = getMeasuredDialInPumpSpeed(); S32 cmdRate = targetDialInFlowRate; - // check for pump running while commanded off + // Check for pump running while commanded off if ( 0 == cmdRate ) { if ( measMotorSpeed > DIP_MAX_MOTOR_SPEED_WHILE_OFF_RPM ) @@ -1060,7 +1055,7 @@ F32 measMotorSpeedInRotorRPM = measMotorSpeed / DIP_GEAR_RATIO; F32 deltaRotorSpeed = fabs( measRotorSpeed - measMotorSpeedInRotorRPM ); - // check measured motor speed vs. commanded motor speed while controlling to target + // Check measured motor speed vs. commanded motor speed while controlling to target if ( deltaMotorSpeed > DIP_MAX_MOTOR_SPEED_ERROR_RPM ) { if ( ++errorDialInMotorSpeedPersistTimerCtr >= DIP_MOTOR_SPEED_ERROR_PERSIST ) @@ -1075,7 +1070,7 @@ errorDialInMotorSpeedPersistTimerCtr = 0; } - // check measured rotor speed vs. measured motor speed while controlling to target + // 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 ) @@ -1110,7 +1105,7 @@ *************************************************************************/ static void checkDialInPumpFlowAgainstSpeed( void ) { - // check only performed while in treatment mode and while we're in control to target state + // 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 = getMeasuredDialInFlowRate(); @@ -1150,7 +1145,7 @@ { F32 dipCurr; - // dialIn pump should be off + // DialIn pump should be off if ( DIAL_IN_PUMP_OFF_STATE == dialInPumpState ) { dipCurr = fabs( getMeasuredDialInPumpMCCurrent() ); @@ -1169,7 +1164,7 @@ dipCurrErrorDurationCtr = 0; } } - // dialIn pump should be running + // DialIn pump should be running else { dipCurr = fabs( getMeasuredDialInPumpMCCurrent() ); @@ -1203,7 +1198,7 @@ #ifndef DISABLE_PUMP_FLOW_CHECKS HD_OP_MODE_T opMode = getCurrentOperationMode(); - // check flow sensor signal strength when appropriate TODO - in pre-treatment, must be far enough along for fluid to be in tubing + // Check flow sensor signal strength when appropriate TODO - in pre-treatment, must be far enough along for fluid to be in tubing if ( MODE_TREA == opMode || ( MODE_PRET == opMode && FALSE ) ) { F32 sigStrength = getMeasuredDialInFlowSignalStrength(); @@ -1230,7 +1225,7 @@ switch ( dialInPumpSelfTestState ) { case DIAL_IN_FLOW_SELF_TEST_STATE_START: - // retrieve blood flow sensor calibration data + // Retrieve blood flow sensor calibration data if ( TRUE == getCalibrationData( &cal ) ) { dialInFlowCalGain = cal.dialysateFlowGain; @@ -1279,10 +1274,10 @@ CALIBRATION_DATA_T cal; getCalibrationData( &cal ); - // keep locally and apply immediately + // Keep locally and apply immediately dialInFlowCalGain = gain; dialInFlowCalOffset = offset; - // also update calibration record in non-volatile memory + // Also update calibration record in non-volatile memory cal.dialysateFlowGain = gain; cal.dialysateFlowOffset_mL_min = offset; if ( TRUE == setCalibrationData( cal ) )