/* * PowerIdle.c * * Created on: Apr 3, 2019 * Author: shlomo */ #include "modules/General/GeneralHardware.h" #include "modules/General/Safety.h" #include "modules/thread/thread.h" #include "modules/ids/ids.h" #include "modules/control/control.h" #include "modules/AlarmHandling/AlarmHandling.h" #include "modules/heaters/heaters_ex.h" #include "modules/Diagnostics/Diagnostics.h" #include "Modules/General/process.h" #include "PMR/MachineStatus/MachineStatus.pb-c.h" #include "Modules/General/MachineStatus.h" #include "StateMachines/Printing/PrintingSTM.h" #include "InitSequence.h" #include "drivers/I2C_Communication/DAC/Blower.h" #include "drivers/I2C_Communication/ADC_MUX/ADC_MUX.h" #include "drivers/Valves/Valve.h" #include "Modules/General/buttons.h" #include "heaters/heaters_ex.h" #define DEFAULT_IDLE_TIME_LIMIT 3600 int powerIdleSecondsCounter = 0; int powerIdleSecondsLimit = DEFAULT_IDLE_TIME_LIMIT; uint32_t IdleDrierTemperature = 8000; uint32_t IdleHeadTemperature = 8000; uint32_t IdleMixerTemperature = 0; bool powerIdleState = false; bool machineActive = false; uint32_t IdleControlId = 0xFF; uint32_t IdleHeatingControlId = 0xFF; ProcessParameters ActiveProcessParameters,ProcessParametersClear; void PowerIdleSetIdle(void) { memcpy (&ActiveProcessParameters,&ProcessParametersKeep,sizeof(ProcessParameters)); ProcessParameters ProcessParametersClear; ProcessParametersClear.dryerzone1temp = (ActiveProcessParameters.dryerzone1temp<80)? ActiveProcessParameters.dryerzone1temp:IdleDrierTemperature; ProcessParametersClear.dryerzone2temp = (ActiveProcessParameters.dryerzone2temp<80)? ActiveProcessParameters.dryerzone2temp:IdleDrierTemperature; ProcessParametersClear.dryerzone3temp = (ActiveProcessParameters.dryerzone3temp<80)? ActiveProcessParameters.dryerzone3temp:IdleDrierTemperature; ProcessParametersClear.mixertemp = (ActiveProcessParameters.mixertemp <80)? ActiveProcessParameters.mixertemp :IdleMixerTemperature; ProcessParametersClear.headzone1temp = (ActiveProcessParameters.headzone1temp <80)? ActiveProcessParameters.headzone1temp :IdleHeadTemperature; ProcessParametersClear.headzone2temp = (ActiveProcessParameters.headzone2temp <80)? ActiveProcessParameters.headzone2temp :IdleHeadTemperature; ProcessParametersClear.headzone3temp = (ActiveProcessParameters.headzone3temp <80)? ActiveProcessParameters.headzone3temp :IdleHeadTemperature; ProcessParametersClear.headzone4temp = (ActiveProcessParameters.headzone4temp <80)? ActiveProcessParameters.headzone4temp :IdleHeadTemperature; ProcessParametersClear.headzone5temp = (ActiveProcessParameters.headzone5temp <80)? ActiveProcessParameters.headzone5temp :IdleHeadTemperature; ProcessParametersClear.headzone6temp = (ActiveProcessParameters.headzone6temp <80)? ActiveProcessParameters.headzone6temp :IdleHeadTemperature; ProcessParametersClear.dyeingspeed = (ActiveProcessParameters.dyeingspeed <40)? ActiveProcessParameters.dyeingspeed :IdleHeadTemperature; if (HandleProcessParameters(&ProcessParametersClear)!= OK) { LOG_ERROR (1, "Turn Heaters idle failed"); return; } if (BlowerCfg.enabled == true) { Turn_the_Blower_On();//Turn on with the Default_Voltage if (BlowerCfg.heatingvoltage) Control_Voltage_To_Blower(BlowerCfg.heatingvoltage); else Control_Voltage_To_Blower(BlowerCfg.voltage-500); } SetMachineStatus(MACHINE_STATE__Sleep); } uint32_t PowerIdleCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag) { if (machineActive == true) return OK; if ((powerIdleSecondsCounter%100 == 0)&&(powerIdleSecondsCounter)) Report("PowerIdle Idle counter ",__FILE__,__LINE__,(int)powerIdleSecondsLimit,RpWarning,(int)powerIdleSecondsCounter,0); if (++powerIdleSecondsCounter>=powerIdleSecondsLimit) { if (powerIdleState == false) { Report("PowerIdle Idle activated ",__FILE__,__LINE__,(int)powerIdleSecondsLimit,RpWarning,(int)powerIdleSecondsCounter,0); powerIdleState = true; SetPowerMachineState(sttIDLE); //move to idle state PowerIdleSetIdle(); if (SafeRemoveControlCallback(IdleControlId, PowerIdleCallBackFunction )==OK) IdleControlId = 0xFF; } } return OK; } void PowerIdleInit(void) { IdleControlId = AddControlCallback("IdleCounter", PowerIdleCallBackFunction, eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); if (IdleControlId == 0xFF) Report("AddControlCallback failed",__FILE__,__LINE__,(int)IdleControlId,RpWarning,(int)0,0); Report("PowerIdleInit ",__FILE__,__LINE__,(int)powerIdleSecondsLimit,RpWarning,(int)0,0); } void PowerIdleSetParameters(uint32_t switchtoidletimeinseconds,uint32_t idledriertemperature,uint32_t idleheadtemperature,uint32_t idlemixertemperature) { powerIdleSecondsLimit = switchtoidletimeinseconds; IdleDrierTemperature = idledriertemperature; IdleHeadTemperature = idleheadtemperature; IdleMixerTemperature = idlemixertemperature; Report("Power Idle Set Parameters",__FILE__,powerIdleSecondsLimit,(int)idledriertemperature,RpWarning,(int)idleheadtemperature,0); } int getIdleCounter (void) {return powerIdleSecondsCounter;} void setIdleLimit (int Limit) {powerIdleSecondsLimit = Limit;} void setmachineActive(bool Active){machineActive = Active;} void resetIdleCounter (void) {powerIdleSecondsCounter = 0;} bool getIdleState (void) {return powerIdleState;} uint32_t PowerIdleStartHeatingCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag) { if (HeaterCheckReady()==true) { //MachineStateDetail = MACHINE_STATE_MACHINE_READY_TO_DYE; if (SafeRemoveControlCallback(IdleHeatingControlId, PowerIdleStartHeatingCallBackFunction )==OK) IdleHeatingControlId = 0xFF; else Report("Remove control callback failed",__FILE__,__LINE__,(int)IdleHeatingControlId,RpWarning,(int)PowerIdleStartHeatingCallBackFunction,0); //InitSequenceStateMachine(InitStages); SetMachineStatus(MACHINE_STATE__Ready); } return OK; } void PowerIdleOutOfIdleState(void) { Report("PowerIdle Out of Idle ",__FILE__,__LINE__,(int)powerIdleSecondsLimit,RpWarning,(int)powerIdleSecondsCounter,0); powerIdleSecondsCounter = 0; if (powerIdleState == true) { powerIdleState = false; SetPowerMachineState(sttON); if (HandleProcessParameters(&ActiveProcessParameters)!= OK) { LOG_ERROR (1, "Turn Heaters active failed"); return ; } if (IdleControlId == 0xFF) IdleControlId = AddControlCallback( "NULL",PowerIdleCallBackFunction, eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); if (IdleControlId == 0xFF) Report("AddControlCallback failed",__FILE__,__LINE__,(int)IdleControlId,RpWarning,(int)0,0); SetMachineStatus(MACHINE_STATE__Initializing); IdleHeatingControlId = AddControlCallback("Heating Out of Idle", PowerIdleStartHeatingCallBackFunction, 2*eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); } } void PowerIdleActivateIdleState(void) { Report("PowerIdle Activate Idle ",__FILE__,__LINE__,(int)machineActive,RpWarning,(int)powerIdleSecondsCounter,0); if (machineActive == true) { LOG_ERROR(machineActive, "machine is active, cannot switch on idle state"); return; } powerIdleSecondsCounter = 0; if (powerIdleState == false) { powerIdleState = true; PowerIdleSetIdle(); } }