Index: sources/drydemo/StateControllerTreatment.cpp =================================================================== diff -u -r61e26e538aae580369385eb78a966e74240d79c4 -r756496932decbaae7bb3c9f694afc0402eeb1349 --- sources/drydemo/StateControllerTreatment.cpp (.../StateControllerTreatment.cpp) (revision 61e26e538aae580369385eb78a966e74240d79c4) +++ sources/drydemo/StateControllerTreatment.cpp (.../StateControllerTreatment.cpp) (revision 756496932decbaae7bb3c9f694afc0402eeb1349) @@ -3,11 +3,23 @@ #include "ApplicationController.h" #include "types.h" -void StateController::handleBloodPrimeBroadcastData(float accumulatedVolML, quint32 primeTimeOutS, quint32 pauseCountDownS, bool initBC) +float StateController::getBloodPrimeRampStepML() { + qint32 setBPRateMLPM = _treatmentParams.bloodFlowRateMLPM - BLOOD_PRIME_START_FLOW_MLPM; + float rampRateS = ((BLOOD_PRIME_VOLUME_ML / (setBPRateMLPM + BLOOD_PRIME_VOLUME_ML) / BLOOD_PRIME_START_FLOW_MLPM) / 2.0) * SECONDS_PER_MINUTE; + qDebug() << "Step cal" << _treatmentParams.bloodFlowRateMLPM << setBPRateMLPM << rampRateS; + rampRateS = (rampRateS < SECONDS_PER_MINUTE ? SECONDS_PER_MINUTE : rampRateS); + float rampStepML = setBPRateMLPM / rampRateS; + + return rampStepML; +} + + +void StateController::handleBloodPrimeBroadcastData(const float accumulatedVolML, const quint32 primeTimeOutS, const quint32 pauseCountDownS, const Broadcast_Type BCType) +{ static quint32 broadcastCounter = 0; - if ((++broadcastCounter % NUM_OF_COUNTS_TIMER_BC_EMIT != 0) && (!initBC)) { return; } + if ((++broadcastCounter % NUM_OF_COUNTS_TIMER_BC_EMIT != 0) && (BCType == BC_DELAYED)) { return; } // Broadcast QVariantList msg; @@ -22,68 +34,51 @@ _isBroadcastListReady = true; } - -void StateController::handleBloodPrimeVolML(bool resume, quint32 &currBloodFlowMLPM, float &accumBloodVolML) +void StateController::handleBloodPrimeVolML(const bool resume, const float rampStepML, const bool hasFlowChanged, quint32 &currBloodFlowMLPM, + float &accumBloodVolML) { static quint32 primeTimeoutS = 0; static quint32 pauseCountDownS = 0; static quint32 elapsedTimeSincePauseS = 0; + static quint32 controlCounter = 0; if (!resume) { + // If the user has pressed pause, then do the countdown and broadcast + // TODO should we exit is the timeout has happened? elapsedTimeSincePauseS += (QOBJECT_TIMER_TIMEOUT_MS); - primeTimeoutS = BLOOD_PRIME_PAUSE_TIMEOUT_S; - pauseCountDownS = ( elapsedTimeSincePauseS / MILLISECONDS_PER_SECOND < BLOOD_PRIME_PAUSE_TIMEOUT_S ? BLOOD_PRIME_PAUSE_TIMEOUT_S - elapsedTimeSincePauseS / MILLISECONDS_PER_SECOND : pauseCountDownS ); - handleBloodPrimeBroadcastData(accumBloodVolML, primeTimeoutS, pauseCountDownS, false); - qDebug() << "Count" << primeTimeoutS << pauseCountDownS << elapsedTimeSincePauseS; + primeTimeoutS = BLOOD_PRIME_PAUSE_TIMEOUT_S; + pauseCountDownS = ( elapsedTimeSincePauseS / MILLISECONDS_PER_SECOND < BLOOD_PRIME_PAUSE_TIMEOUT_S ? BLOOD_PRIME_PAUSE_TIMEOUT_S - elapsedTimeSincePauseS / MILLISECONDS_PER_SECOND : pauseCountDownS ); + handleBloodPrimeBroadcastData(accumBloodVolML, primeTimeoutS, pauseCountDownS, BC_DELAYED); return; } - //prepareOcclusionBroadcastData(); - //prepareTreatmentParamsRangesBroadcastData(true); // Reset the params and get ready for a new treatment + accumBloodVolML += (currBloodFlowMLPM * FLOW_INTEGRATOR); - if (currBloodFlowMLPM < _treatmentParams.bloodFlowRateMLPM) { - // If interim blood flow is less than the target blood flow, keep adding - // to the blood flow ramp - currBloodFlowMLPM += BLOOD_PRIME_FLOW_CHNG_MLPM; - } + if (++controlCounter % NUM_OF_COUNTS_TIMER_2_CONTROL != 0) { return; } - if (currBloodFlowMLPM >= _treatmentParams.bloodFlowRateMLPM) { - // If the interim blood flow is >= target blood flow: - // If target blood flow is less than 250 which is the starting flow in blood prime, - // then keep subtracting the target (e.g. target 110 and we start from 250 mL/min) - // If the target blood flow is > than 250 mL/min set the interim flow to target blood flow - if (_treatmentParams.bloodFlowRateMLPM < BLOOD_PRIME_START_FLOW_MLPM) { - currBloodFlowMLPM -= BLOOD_PRIME_FLOW_CHNG_MLPM; + if (!hasFlowChanged) { + // If the user has changed the flow then do not control (ramp) the steps + currBloodFlowMLPM += rampStepML; - if (currBloodFlowMLPM <= _treatmentParams.bloodFlowRateMLPM) { currBloodFlowMLPM = _treatmentParams.bloodFlowRateMLPM; } + qDebug() << "Flow calc" << currBloodFlowMLPM; + + if (rampStepML < 0){ + // If the ramp is negative, check the current flow not be less than target and if it is, set it to min + // e.g. current flow calculated = 30 mL/min, treatment params = 50 mL/min, Set it to 50 mL/min + currBloodFlowMLPM = (currBloodFlowMLPM < _treatmentParams.bloodFlowRateMLPM ? _treatmentParams.bloodFlowRateMLPM : currBloodFlowMLPM); } - else { currBloodFlowMLPM = _treatmentParams.bloodFlowRateMLPM; } + else { + currBloodFlowMLPM = (currBloodFlowMLPM > _treatmentParams.bloodFlowRateMLPM ? _treatmentParams.bloodFlowRateMLPM : currBloodFlowMLPM); + } } - accumBloodVolML += (currBloodFlowMLPM * FLOW_INTEGRATOR); - - primeTimeoutS = 0; - pauseCountDownS = 0; + primeTimeoutS = 0; + pauseCountDownS = 0; elapsedTimeSincePauseS = 0; - handleBloodPrimeBroadcastData(accumBloodVolML, primeTimeoutS, pauseCountDownS, false); - //qDebug() << "Current" << currBloodFlowMLPM << _treatmentParams.bloodFlowRateMLPM; + handleBloodPrimeBroadcastData(accumBloodVolML, primeTimeoutS, pauseCountDownS, BC_IMMEDIATELY); + qDebug() << "Current" << currBloodFlowMLPM << _treatmentParams.bloodFlowRateMLPM << controlCounter << accumBloodVolML; } -void StateController::handleBloodPrimeFlowChangeRequest(quint32 requestedFlowMLPM, quint32 &currBloodFlowMLPM) -{ - currBloodFlowMLPM = requestedFlowMLPM; - - QVariantList resp; - resp.append(static_cast(ID_TD_TREATMENT_SET_POINT_BLOOD_FLOW_CHANGE_RESP)); - resp.append(Can_Id::eChlid_TD_UI); - resp.append(ACCEPT_VALUE); - resp.append(0); - _isSendListReady = false; - _sendMessages.append(resp); - _isSendListReady = true; - -} - void StateController::setTreatmentParametersToDefault() { // TODO complete the list as we go @@ -96,7 +91,7 @@ _treatmentParams.treatmentDurationMin = DEF_TX_PARAM_PRESCRIBED_DUR_MIN; } -void StateController::handleTreatmentSetpointsBroadcastData(quint32 bloodFlowMLPM, quint32 dialFlowMLPM, float dialTempC) +void StateController::handleTreatmentSetpointsBroadcastData(const quint32 bloodFlowMLPM, const quint32 dialFlowMLPM, const float dialTempC) { static quint32 broadcastCounter = 0; @@ -111,14 +106,18 @@ _isBroadcastListReady = true; } -void StateController::handleTreatmentStatesBroadcastData(const QList &txStates) +void StateController::handleTreatmentStatesBroadcastData(const QList &txStates, const Broadcast_Type BCType) { + static quint32 broadcastCounter = 0; + + if ((++broadcastCounter % NUM_OF_COUNTS_TIMER_BC_EMIT != 0) && (BCType == BC_DELAYED)) { return; } + QVariantList msg; msg.append(static_cast(ID_TD_TREATMENT_STATE_DATA)); msg.append(Can_Id::eChlid_TD_Sync); for (auto item: txStates) { - msg.append(item); + msg.append(item); } _isBroadcastListReady = false; _broadcastMessages.append(msg);