Index: firmware/App/Controllers/DialOutFlow.c =================================================================== diff -u -re7e4d0d8f84b61de25fe21ecf9aa11b2732b598d -r316e7117180362d54af53818822694c4f936e550 --- firmware/App/Controllers/DialOutFlow.c (.../DialOutFlow.c) (revision e7e4d0d8f84b61de25fe21ecf9aa11b2732b598d) +++ firmware/App/Controllers/DialOutFlow.c (.../DialOutFlow.c) (revision 316e7117180362d54af53818822694c4f936e550) @@ -55,6 +55,7 @@ #define DIAL_OUT_FLOW_SAMPLE_FREQ ( MS_PER_SECOND / TASK_PRIORITY_INTERVAL ) +#define DIAL_OUT_CONTROLLER_SAMPLE_FREQ ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) // dialOut pump stop and direction macros #define STOP_DO_PUMP_SPI4_PORT_MASK 0x00000200 // (SPI4CLK - re-purposed as output GPIO) @@ -85,10 +86,10 @@ static U32 dialOutFlowDataPublicationTimerCounter = 5; // used to schedule dialIn flow data publication to CAN bus // Rx values -static F32 rxTotalTargetVolumeInMl; -static U32 rxTargetTimeInSamples; -static F32 rxInitialTargetFlowRatePWM; -static F32 targetVolumeUFRatePerSample; +static volatile F32 rxTotalTargetVolumeInMl; +static volatile U32 rxTargetTimeInSamples; +static volatile F32 rxInitialTargetFlowRatePWM; +static volatile F32 targetVolumeUFRatePerSample; // Variables used in loop @@ -100,15 +101,20 @@ static F32 bagMeasuredUFVolumeInMl; static F32 accumulativeTotalUFVolumeInMl; -static F32 totalMeasuredUFVolumeInMl; -static F32 totalTargetUFVolumeInMl; +static volatile F32 totalMeasuredUFVolumeInMl; +static volatile F32 totalTargetUFVolumeInMl; -static F32 sentPWM; +static volatile F32 sentPWM; +static volatile U32 targetVolumeTimer; // Pump variables static F32 dialOutPumpSpeedInRPM; static F32 dialOutPumpCurrentInMA; +// Simulator +static volatile F32 simulator_volume_in_ml_per_min; +static volatile F32 simulator_inlet_flow_rate; + // ********** private function prototypes ********** static DIALOUT_FLOW_STATE_T handleDialOutFlowStopState( void ); @@ -123,7 +129,10 @@ static void publishDialOutFlowData( void ); +static F32 UFSimulator( F32 pwm ); +static void ResetSimulator( void ); + /************************************************************************* * @brief setDialOutFlowNewState * The setDialOutNew function changes the state to STOP, PAUSE or RUN. @@ -137,14 +146,20 @@ switch( newState ) { case DIALOUT_FLOW_RUN_UF_STATE: + break; case DIALOUT_FLOW_PAUSE_UF_STATE: - if( isNewBag ) - { - bagStartVolumeInMl = 0.0; - } + bagStartVolumeInMl = (isNewBag == TRUE) ? 0.0 : bagStartVolumeInMl; break; case DIALOUT_FLOW_STOP_STATE: + // Reset total target volume + totalTargetUFVolumeInMl = 0.0; + accumulativeTotalUFVolumeInMl = 0.0; + bagStartVolumeInMl = 0.0; + sentPWM = 0.0; + bagStartVolumeInMl = (isNewBag == TRUE) ? 0.0 : bagStartVolumeInMl; + ResetSimulator(); + stopDialOutPump(); break; default: @@ -176,7 +191,7 @@ *************************************************************************/ static void updateTargetVolume( void ) { - rxTotalTargetVolumeInMl += targetVolumeUFRatePerSample; + totalTargetUFVolumeInMl += targetVolumeUFRatePerSample; } /************************************************************************* @@ -189,7 +204,7 @@ * @param rxFlowRateinMlPerMin * @return none *************************************************************************/ -BOOL setDialOutFlowRxTotalVolumeAndRxTime( U32 rxTotaVolumeInMl, U32 rxTotalTimeInMinutes, U32 rxFlowRateinMlPerMin) +BOOL setDialOutFlowRxTotalVolumeAndRxTime( U16 rxTotaVolumeInMl, U16 rxTotalTimeInMinutes, U16 rxFlowRateinMlPerMin) { #define SECS_IN_MIN 60 @@ -204,12 +219,32 @@ BOOL returnValue = TRUE; - rxTotalTargetVolumeInMl = rxTotaVolumeInMl; - rxTargetTimeInSamples = rxTotalTimeInMinutes * SEC_PER_MIN * DIAL_OUT_FLOW_SAMPLE_FREQ ; - rxInitialTargetFlowRatePWM = DOP_PWM_FROM_ML_PER_MIN(rxFlowRateinMlPerMin); + totalTargetUFVolumeInMl = 0.0; + simulator_inlet_flow_rate = (F32) rxFlowRateinMlPerMin; + + rxTotalTargetVolumeInMl = (F32) rxTotaVolumeInMl; + rxTargetTimeInSamples = (U32) rxTotalTimeInMinutes * SEC_PER_MIN * DIAL_OUT_CONTROLLER_SAMPLE_FREQ ; + rxInitialTargetFlowRatePWM = RANGE(DOP_PWM_FROM_ML_PER_MIN(rxFlowRateinMlPerMin), MIN_DIAL_OUT_PUMP_PWM_DUTY_CYCLE, MAX_DIAL_OUT_PUMP_PWM_DUTY_CYCLE); + + resetPIController( PI_CONTROLLER_ID_LOAD_CELL, rxInitialTargetFlowRatePWM ); + + targetVolumeUFRatePerSample = (F32) rxTotalTargetVolumeInMl / (F32) rxTargetTimeInSamples; + #ifdef DEBUG_ENABLED + // TODO - temporary debug code - remove later + char debugFlowStr[ 128 ]; + + sprintf( debugFlowStr, "target:%5.4f, time:%5d, InitTarget:%6.4f, RatePerSample:%8.4f", + rxTotalTargetVolumeInMl, + rxTargetTimeInSamples, + rxInitialTargetFlowRatePWM, + targetVolumeUFRatePerSample ); + + sendDebugData( (U08*)debugFlowStr, strlen(debugFlowStr) ); + #endif + return returnValue; } @@ -221,16 +256,12 @@ *************************************************************************/ void initDialOutFlow( void ) { - dialOutFlowState = DIALOUT_FLOW_STOP_STATE; - totalTargetUFVolumeInMl = 0.0; - stopDialOutPump(); setDialOutPumpDirection( MOTOR_DIR_FORWARD ); initializeFilter(FILTER_ID_LOAD_CELL_WEIGHT, 0); - bagStartVolumeInMl = 0.0; - accumulativeTotalUFVolumeInMl = 0.0; + setDialOutFlowNewState( DIALOUT_FLOW_STOP_STATE , TRUE ); // initialize dialysate outlet flow PI controller initializePIController( PI_CONTROLLER_ID_LOAD_CELL, rxInitialTargetFlowRatePWM, @@ -293,9 +324,42 @@ // publish dialIn flow data on interval + bagMeasuredUFVolumeInMl = loadCellVolumeInMl - bagStartVolumeInMl; + totalMeasuredUFVolumeInMl = bagMeasuredUFVolumeInMl + accumulativeTotalUFVolumeInMl; + + totalMeasuredUFVolumeInMl = UFSimulator(sentPWM); + + publishDialOutFlowData(); } +/* + * TODO: Delete UFSimulator when hardware is ready + */ + +F32 UFSimulator(F32 pwm) { + F32 flow; + + if(dialOutFlowState == DIALOUT_FLOW_STOP_STATE) + { + flow = 0; + } + else + { + flow = ( ((678.0*pwm - simulator_inlet_flow_rate - 67.80) * (F32) TASK_PRIORITY_INTERVAL )/ (F32) MS_PER_SECOND ); + } + simulator_volume_in_ml_per_min += flow; + + return simulator_volume_in_ml_per_min; +} + +/* + * TODO: Delete ResetSimulator when hardware is ready. + */ +void ResetSimulator() { + simulator_volume_in_ml_per_min = 0.0; +} + /************************************************************************* * @brief execDialOutFlowController * The execDialOutFlowController function executes the dialIn flow controller. @@ -341,6 +405,8 @@ { DIALOUT_FLOW_STATE_T result = DIALOUT_FLOW_STOP_STATE; + targetVolumeTimer = 0; + stopDialOutPump(); return result; @@ -452,7 +518,6 @@ // publish dialIn flow data on interval if ( ++dialOutFlowDataPublicationTimerCounter > DIAL_OUT_FLOW_DATA_PUB_INTERVAL ) { - //leo-del dialOutFlowState = dialOutFlowState == DIALOUT_FLOW_STOP_STATE ? DIALOUT_FLOW_RUN_UF_STATE : DIALOUT_FLOW_STOP_STATE; #ifdef DEBUG_ENABLED // TODO - temporary debug code - remove later char debugFlowStr[ 128 ];