#include "StateController.h" #include "ApplicationController.h" #include "types.h" 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) && (BCType == BC_DELAYED)) { return; } // Broadcast QVariantList msg; msg.append(static_cast(ID_TD_BLOOD_PRIME_PROGRESS_DATA)); msg.append(Can_Id::eChlid_TD_Sync); msg.append(BLOOD_PRIME_VOLUME_ML); msg.append(accumulatedVolML); msg.append(primeTimeOutS); msg.append(pauseCountDownS); _isBroadcastListReady = false; _broadcastMessages.append(msg); _isBroadcastListReady = true; } 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, BC_DELAYED); return; } accumBloodVolML += (currBloodFlowMLPM * FLOW_INTEGRATOR); if (++controlCounter % NUM_OF_COUNTS_TIMER_2_CONTROL != 0) { return; } if (!hasFlowChanged) { // If the user has changed the flow then do not control (ramp) the steps currBloodFlowMLPM += rampStepML; 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 = (currBloodFlowMLPM > _treatmentParams.bloodFlowRateMLPM ? _treatmentParams.bloodFlowRateMLPM : currBloodFlowMLPM); } } primeTimeoutS = 0; pauseCountDownS = 0; elapsedTimeSincePauseS = 0; handleBloodPrimeBroadcastData(accumBloodVolML, primeTimeoutS, pauseCountDownS, BC_IMMEDIATELY); qDebug() << "Current" << currBloodFlowMLPM << _treatmentParams.bloodFlowRateMLPM << controlCounter << accumBloodVolML; } void StateController::setTreatmentParametersToDefault() { // TODO complete the list as we go _treatmentParams.finalConfirmation = true; _treatmentParams.treatmentModality = 0; // TODO set the actual enum value _treatmentParams.bloodFlowRateMLPM = DEF_TX_PARAM_BLOOD_FLOW_RATE_MLPM; _treatmentParams.dialysateFlowRateMLPM = DEF_TX_PARAM_DIAL_FLOW_RATE_MLPM; _treatmentParams.treatmentDurationMin = DEF_TX_PARAM_PRESCRIBED_DUR_MIN; _treatmentParams.dialysateTemperatureC = DEF_TX_PARAM_DIA_TEMPERATURE_C; _treatmentParams.treatmentDurationMin = DEF_TX_PARAM_PRESCRIBED_DUR_MIN; } void StateController::handleTreatmentSetpointsBroadcastData(const quint32 bloodFlowMLPM, const quint32 dialFlowMLPM, const float dialTempC) { static quint32 broadcastCounter = 0; if (++broadcastCounter % NUM_OF_COUNTS_TIMER_BC_EMIT != 0) { return; } QVariantList msg; msg.append(static_cast(ID_TD_TREATMENT_SET_POINTS)); msg.append(Can_Id::eChlid_TD_Sync); msg.append(bloodFlowMLPM); msg.append(dialFlowMLPM); msg.append(dialTempC); _isBroadcastListReady = false; _broadcastMessages.append(msg); _isBroadcastListReady = true; } 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); } _isBroadcastListReady = false; _broadcastMessages.append(msg); _isBroadcastListReady = true; }