Index: firmware/App/Controllers/DialOutFlow.c =================================================================== diff -u -r78c03fb021407eaf8d17dd0f74f6969443b397ce -r30f049651877229042e3f8700c8596e5b9a1e0f4 --- firmware/App/Controllers/DialOutFlow.c (.../DialOutFlow.c) (revision 78c03fb021407eaf8d17dd0f74f6969443b397ce) +++ firmware/App/Controllers/DialOutFlow.c (.../DialOutFlow.c) (revision 30f049651877229042e3f8700c8596e5b9a1e0f4) @@ -55,21 +55,21 @@ #define DOP_P_COEFFICIENT 0.0050 ///< P term for dialysate outlet pump control. #define DOP_I_COEFFICIENT 0.0001 ///< I term for dialysate outlet pump control. -#define DOP_HOME_RATE 100 ///< target pump speed (in estimate mL/min) for homing. -#define DOP_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 DOP_HOME_RATE 100 ///< Target pump speed (in estimate mL/min) for homing. +#define DOP_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 DOP_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 DOP_SPEED_CALC_BUFFER__LEN ( 1000 / DOP_SPEED_CALC_INTERVAL / TASK_PRIORITY_INTERVAL ) -#define DOP_HALL_EDGE_COUNTS_PER_REV 48 ///< number of hall sensor edge counts per motor revolution. +#define DOP_HALL_EDGE_COUNTS_PER_REV 48 ///< Number of hall sensor edge counts per motor revolution. -#define DOP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0 ///< maximum motor speed (RPM) while motor is commanded off. -#define DOP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0 ///< maximum difference in speed between motor and rotor (in rotor RPM). -#define DOP_MAX_MOTOR_SPEED_ERROR_RPM 300.0 ///< maximum difference in speed between measured and commanded RPM. -#define DOP_OFF_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) for motor off error condition. -#define DOP_MOTOR_SPEED_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) motor speed error condition. -#define DOP_ROTOR_SPEED_ERROR_PERSIST ((12 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) rotor speed error condition. -#define DOP_DIRECTION_ERROR_PERSIST (250 / TASK_PRIORITY_INTERVAL) ///< persist time (task intervals) pump direction error condition. +#define DOP_MAX_MOTOR_SPEED_WHILE_OFF_RPM 100.0 ///< Maximum motor speed (RPM) while motor is commanded off. +#define DOP_MAX_ROTOR_VS_MOTOR_DIFF_RPM 5.0 ///< Maximum difference in speed between motor and rotor (in rotor RPM). +#define DOP_MAX_MOTOR_SPEED_ERROR_RPM 300.0 ///< Maximum difference in speed between measured and commanded RPM. +#define DOP_OFF_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) for motor off error condition. +#define DOP_MOTOR_SPEED_ERROR_PERSIST ((5 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) motor speed error condition. +#define DOP_ROTOR_SPEED_ERROR_PERSIST ((12 * MS_PER_SECOND) / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) rotor speed error condition. +#define DOP_DIRECTION_ERROR_PERSIST (250 / TASK_PRIORITY_INTERVAL) ///< Persist time (task intervals) pump direction error condition. #define DOP_MAX_CURR_WHEN_STOPPED_MA 150.0 ///< Motor controller current should not exceed this when pump should be stopped. #define DOP_MIN_CURR_WHEN_RUNNING_MA 150.0 ///< Motor controller current should always exceed this when pump should be running. @@ -111,7 +111,7 @@ NUM_OF_DIAL_OUT_PUMP_SELF_TEST_STATES ///< Number of dialysate outlet pump self-test states. } DIAL_OUT_PUMP_SELF_TEST_STATE_T; -// pin assignments and macros for pump stop and direction outputs +// Pin assignments and macros for pump stop and direction outputs #define STOP_DO_PUMP_MIBSPI1_PORT_MASK 0x00000400 ///< MIBSPI1 SIMO[0] - re-purposed as GPIOoutput for pump controller run/stop pin. #define SET_DOP_STOP() {mibspiREG1->PC3 &= ~STOP_DO_PUMP_MIBSPI1_PORT_MASK;} ///< Macro sets pump controller run/stop signal to stop. #define CLR_DOP_STOP() {mibspiREG1->PC3 |= STOP_DO_PUMP_MIBSPI1_PORT_MASK;} ///< Macro sets pump controller run/stop signal to run. @@ -144,18 +144,18 @@ static U32 dopControlTimerCounter = 0; ///< Timer counter to determine when to control dialysate outlet pump. static U32 dopCurrErrorDurationCtr = 0; ///< Timer counter for motor current error persistence. -static U32 dopRotorRevStartTime = 0; ///< dialysate outlet pump rotor rotation start time (in ms) -static BOOL dopStopAtHomePosition = FALSE; ///< stop dialysate outlet pump at next home position -static U32 dopHomeStartTime = 0; ///< when did dialysate outlet pump home command begin? (in ms) +static U32 dopRotorRevStartTime = 0; ///< Dialysate outlet pump rotor rotation start time (in ms) +static BOOL dopStopAtHomePosition = FALSE; ///< Stop dialysate outlet pump at next home position +static U32 dopHomeStartTime = 0; ///< When did dialysate outlet pump home command begin? (in ms) -static U16 dopLastMotorHallSensorCounts[ DOP_SPEED_CALC_BUFFER__LEN ]; ///< last hall sensor count for the dialysate outlet pump motor -static U32 dopMotorSpeedCalcIdx = 0; ///< index into 1 second buffer of motor speed hall sensor counts -static U32 dopMotorSpeedCalcTimerCtr = 0; ///< counter determines interval for calculating dialysate outlet pump motor speed from hall sensor count. +static U16 dopLastMotorHallSensorCounts[ DOP_SPEED_CALC_BUFFER__LEN ]; ///< Last hall sensor count for the dialysate outlet pump motor +static U32 dopMotorSpeedCalcIdx = 0; ///< Index into 1 second buffer of motor speed hall sensor counts +static U32 dopMotorSpeedCalcTimerCtr = 0; ///< Counter determines interval for calculating dialysate outlet pump motor speed from hall sensor count. -static U32 errorDialOutMotorOffPersistTimerCtr = 0; ///< persistence timer counter for motor off check error condition. -static U32 errorDialOutMotorSpeedPersistTimerCtr = 0; ///< persistence timer counter for motor speed error condition. -static U32 errorDialOutRotorSpeedPersistTimerCtr = 0; ///< persistence timer counter for rotor speed error condition. -static U32 errorDialOutPumpDirectionPersistTimerCtr = 0; ///< persistence timer counter for pump direction error condition. +static U32 errorDialOutMotorOffPersistTimerCtr = 0; ///< Persistence timer counter for motor off check error condition. +static U32 errorDialOutMotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for motor speed error condition. +static U32 errorDialOutRotorSpeedPersistTimerCtr = 0; ///< Persistence timer counter for rotor speed error condition. +static U32 errorDialOutPumpDirectionPersistTimerCtr = 0; ///< Persistence timer counter for pump direction error condition. static DIAL_OUT_PUMP_SELF_TEST_STATE_T dialOutPumpSelfTestState = DIAL_OUT_PUMP_SELF_TEST_STATE_START; ///< Current state of the dialysate outlet pump self-test state machine. static U32 dialOutPumpSelfTestTimerCount = 0; ///< Timer counter for time reference during self-test. @@ -196,20 +196,20 @@ stopDialOutPump(); setDialOutPumpDirection( MOTOR_DIR_FORWARD ); - // zero motor hall sensors counts buffer + // Zero motor hall sensors counts buffer dopMotorSpeedCalcIdx = 0; for ( i = 0; i < DOP_SPEED_CALC_BUFFER__LEN; i++ ) { dopLastMotorHallSensorCounts[ i ] = 0; } - // initialize load cell weights + // Initialize load cell weights for ( i = 0; i < NUM_OF_LOAD_CELLS; i++ ) { loadCellWeightInGrams[ i ].data = 0.0; } - // initialize dialysate outlet flow PI controller + // Initialize dialysate outlet flow PI controller initializePIController( PI_CONTROLLER_ID_ULTRAFILTRATION, MIN_DIAL_OUT_PUMP_PWM_DUTY_CYCLE, DOP_P_COEFFICIENT, DOP_I_COEFFICIENT, MIN_DIAL_OUT_PUMP_PWM_DUTY_CYCLE, MAX_DIAL_OUT_PUMP_PWM_DUTY_CYCLE ); @@ -230,10 +230,10 @@ { BOOL result = FALSE; - // direction change while pump is running is not allowed + // Direction change while pump is running is not allowed if ( ( FALSE == isDialOutPumpOn ) || ( 0 == flowRate ) || ( dir == dialOutPumpDirectionSet ) ) { - // verify flow rate + // Verify flow rate if ( flowRate <= MAX_DIAL_OUT_FLOW_RATE ) { F32 adjFlow = (F32)flowRate; @@ -245,24 +245,24 @@ lastGivenRate = flowRate; dialOutPumpDirection = dir; dialOutPumpControlMode = 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'll control to flow when ramp completed dialOutPumpPWMDutyCyclePct = DOP_PWM_FROM_ML_PER_MIN(adjFlow); switch ( dialOutPumpState ) { - case DIAL_OUT_PUMP_RAMPING_UP_STATE: // see if we need to reverse direction of ramp + case DIAL_OUT_PUMP_RAMPING_UP_STATE: // See if we need to reverse direction of ramp if ( dialOutPumpPWMDutyCyclePct < dialOutPumpPWMDutyCyclePctSet ) { dialOutPumpState = DIAL_OUT_PUMP_RAMPING_DOWN_STATE; } break; - case DIAL_OUT_PUMP_RAMPING_DOWN_STATE: // see if we need to reverse direction of ramp + case DIAL_OUT_PUMP_RAMPING_DOWN_STATE: // See if we need to reverse direction of ramp if ( dialOutPumpPWMDutyCyclePct > dialOutPumpPWMDutyCyclePctSet ) { dialOutPumpState = DIAL_OUT_PUMP_RAMPING_UP_STATE; } break; - case DIAL_OUT_PUMP_CONTROL_TO_TARGET_STATE: // start ramp to new target in appropriate direction + case DIAL_OUT_PUMP_CONTROL_TO_TARGET_STATE: // Start ramp to new target in appropriate direction if ( dialOutPumpPWMDutyCyclePctSet > dialOutPumpPWMDutyCyclePct ) { dialOutPumpState = DIAL_OUT_PUMP_RAMPING_DOWN_STATE; @@ -273,7 +273,7 @@ } break; default: - // ok - not all states need to be handled here + // Ok - not all states need to be handled here break; } result = TRUE; @@ -339,7 +339,7 @@ dialOutPumpRotorSpeedRPM.data = ( 1.0 / (F32)deltaTime ) * (F32)MS_PER_SECOND * (F32)SEC_PER_MIN; dopRotorRevStartTime = rotTime; - // if we're supposed to stop pump at home position, stop pump now. + // If we're supposed to stop pump at home position, stop pump now. if ( TRUE == dopStopAtHomePosition ) { signalDialOutPumpHardStop(); @@ -425,7 +425,7 @@ // Calculate dialysate outlet pump motor speed/direction from hall sensor count updateDialOutPumpSpeedAndDirectionFromHallSensors(); - // don't start enforcing checks until out of init/POST mode + // Don't start enforcing checks until out of init/POST mode if ( getCurrentOperationMode() != MODE_INIT ) { // Check pump direction @@ -487,13 +487,13 @@ { DIAL_OUT_PUMP_STATE_T result = DIAL_OUT_PUMP_OFF_STATE; - // if we've been given a flow rate, setup ramp up and transition to ramp up state + // If we've been given a flow rate, setup ramp up and transition to ramp up state if ( lastGivenRate > 0 ) { - // set initial PWM duty cycle + // Set initial PWM duty cycle dialOutPumpPWMDutyCyclePctSet = DOP_PWM_ZERO_OFFSET + MAX_DIAL_OUT_PUMP_PWM_STEP_UP_CHANGE; setDialOutPumpControlSignalPWM( dialOutPumpPWMDutyCyclePctSet ); - // allow dialOut pump to run in requested direction + // Allow dialOut pump to run in requested direction setDialOutPumpDirection( dialOutPumpDirection ); releaseDialOutPumpStop(); isDialOutPumpOn = TRUE; @@ -519,20 +519,20 @@ { DIAL_OUT_PUMP_STATE_T result = DIAL_OUT_PUMP_RAMPING_UP_STATE; - // have we been asked to stop the dialOut pump? + // Have we been asked to stop the dialOut pump? if ( dialOutPumpPWMDutyCyclePct < (MAX_DIAL_OUT_PUMP_PWM_STEP_DN_CHANGE + DOP_PWM_ZERO_OFFSET) ) { - // start ramp down to stop + // Start ramp down to stop dialOutPumpPWMDutyCyclePctSet -= MAX_DIAL_OUT_PUMP_PWM_STEP_DN_CHANGE; setDialOutPumpControlSignalPWM( dialOutPumpPWMDutyCyclePctSet ); result = DIAL_OUT_PUMP_RAMPING_DOWN_STATE; } - // have we reached end of ramp up? + // Have we reached end of ramp up? else if ( dialOutPumpPWMDutyCyclePctSet >= dialOutPumpPWMDutyCyclePct ) { resetPIController( PI_CONTROLLER_ID_ULTRAFILTRATION, dialOutPumpPWMDutyCyclePctSet ); dialOutPumpControlModeSet = dialOutPumpControlMode; - // 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 ( dialOutPumpControlModeSet == PUMP_CONTROL_MODE_OPEN_LOOP ) { dialOutPumpPWMDutyCyclePctSet = dialOutPumpPWMDutyCyclePct; @@ -562,18 +562,18 @@ { DIAL_OUT_PUMP_STATE_T result = DIAL_OUT_PUMP_RAMPING_DOWN_STATE; - // have we essentially reached zero speed + // Have we essentially reached zero speed if ( dialOutPumpPWMDutyCyclePctSet < (MAX_DIAL_OUT_PUMP_PWM_STEP_DN_CHANGE + DOP_PWM_ZERO_OFFSET) ) { stopDialOutPump(); result = DIAL_OUT_PUMP_OFF_STATE; } - // have we reached end of ramp down? + // Have we reached end of ramp down? else if ( dialOutPumpPWMDutyCyclePctSet <= dialOutPumpPWMDutyCyclePct ) { resetPIController( PI_CONTROLLER_ID_ULTRAFILTRATION, dialOutPumpPWMDutyCyclePctSet ); dialOutPumpControlModeSet = dialOutPumpControlMode; - // 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 ( dialOutPumpControlModeSet == PUMP_CONTROL_MODE_OPEN_LOOP ) { dialOutPumpPWMDutyCyclePctSet = dialOutPumpPWMDutyCyclePct; @@ -613,17 +613,17 @@ F32 newPWMDutyCyclePct; F32 deltaPWMDutyCyclePct; - // get new PWM from PI controller + // Get new PWM from PI controller newPWMDutyCyclePct = runPIController( PI_CONTROLLER_ID_ULTRAFILTRATION, refVol, totVol ); - // limit PWM change to max + // Limit PWM change to max deltaPWMDutyCyclePct = newPWMDutyCyclePct - dialOutPumpPWMDutyCyclePctSet; if ( fabs( deltaPWMDutyCyclePct ) > MAX_DIAL_OUT_PUMP_PWM_STEP_UP_CHANGE ) { newPWMDutyCyclePct = ( deltaPWMDutyCyclePct < 0.0 ? \ dialOutPumpPWMDutyCyclePctSet - MAX_DIAL_OUT_PUMP_PWM_STEP_UP_CHANGE : \ dialOutPumpPWMDutyCyclePctSet + MAX_DIAL_OUT_PUMP_PWM_STEP_UP_CHANGE ); } - // set new PWM + // Set new PWM dialOutPumpPWMDutyCyclePctSet = newPWMDutyCyclePct; setDialOutPumpControlSignalPWM( newPWMDutyCyclePct ); } @@ -714,7 +714,7 @@ *************************************************************************/ static void publishDialOutFlowData( void ) { - // publish dialysate outlet pump and UF volume data on interval + // Publish dialysate outlet pump and UF volume data on interval if ( ++dialOutFlowDataPublicationTimerCounter >= getPublishDialOutDataInterval() ) { DIAL_OUT_FLOW_DATA_T dialOutBroadCastVariables; @@ -753,7 +753,7 @@ U16 decDelta = HEX_64_K - incDelta; U16 delta; - // determine dialysate outlet pump speed/direction from delta hall sensor count since last interval + // Determine dialysate outlet pump speed/direction from delta hall sensor count since last interval if ( incDelta < decDelta ) { delta = incDelta; @@ -765,7 +765,7 @@ dialOutPumpSpeedRPM.data = ( (F32)delta / (F32)DOP_HALL_EDGE_COUNTS_PER_REV ) * (F32)SEC_PER_MIN * -1.0; } - // update last count for next time + // Update last count for next time dopLastMotorHallSensorCounts[ nextIdx ] = dopMotorHallSensorCount; dopMotorSpeedCalcIdx = nextIdx; dopMotorSpeedCalcTimerCtr = 0; @@ -783,15 +783,15 @@ *************************************************************************/ static void checkDialOutPumpRotor( void ) { - // if homing, check timeout + // If homing, check timeout if ( ( TRUE == dopStopAtHomePosition ) && ( TRUE == didTimeout( dopHomeStartTime, DOP_HOME_TIMEOUT_MS ) ) ) { signalDialOutPumpHardStop(); dopStopAtHomePosition = 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( dopRotorRevStartTime, DOP_HOME_TIMEOUT_MS ) ) { dialOutPumpRotorSpeedRPM.data = 0.0; @@ -942,7 +942,7 @@ { F32 dopCurr; - // dialOut pump should be off + // DialOut pump should be off if ( DIAL_OUT_PUMP_OFF_STATE == dialOutPumpState ) { dopCurr = fabs( getMeasuredDialOutPumpMCCurrent() ); @@ -961,7 +961,7 @@ dopCurrErrorDurationCtr = 0; } } - // dialOut pump should be running + // DialOut pump should be running else { dopCurr = fabs( getMeasuredDialOutPumpMCCurrent() );