/* * PowerOffSequence.c * * Created on: Apr 2, 2019 * Author: shlomo */ #include "modules/General/GeneralHardware.h" #include "modules/General/Safety.h" #include "modules/thread/thread.h" #include "modules/waste/waste.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 "Modules/Thread/Thread_ex.h" #include "Common/SWUpdate/FirmwareUpgrade.h" #include "StateMachines/Printing/PrintingSTM.h" #include "PowerIdle.h" #include "InitSequence.h" #include "drivers/I2C_Communication/DAC/Blower.h" #include "drivers/I2C_Communication/ADC_MUX/ADC_MUX.h" #include "drivers/Heater/TemperatureSensor.h" #include "drivers/Valves/Valve.h" #include "heaters/heaters_ex.h" #include "PowerOffSequence.h" /* typedef enum { POWER_OFF_INIT, POWER_OFF_STOP_RUNNING_JOB, POWER_OFF_HEAD_CLEAN, POWER_OFF_MIXER_FLUSH, POWER_OFF_HEATERS_OFF, POWER_OFF_STORE_DATA, POWER_OFF_SET_VALVE_POSITION, POWER_OFF_WAIT_FOR_TEMPERATURE, POWER_OFF_WAIT_FOR_PROCESSES,//wait for waste emptying, ink filling, thread loading POWER_OFF_TURN_OFF_DRYER_FAN, POWER_OFF_TURN_OFF_COOLER, POWER_OFF_TURN_OFF_BLOWER, POWER_OFF_POWER_OFF, POWER_OFF_MAX, }POWER_OFF_STAGES_ENUM; */ POWER_OFF_STAGES_ENUM PowerOffMachineState = POWER_OFF_INIT,StoredMachineState = POWER_OFF_INIT; uint32_t PowerOffControlId = 0xFF; uint32_t WaitForProcessControlId = 0xFF; uint32_t PowerOffSequenceStateMachine( POWER_OFF_STAGES_ENUM ReadValue); bool PowerOffInProcess = false; #define POWER_OFF_TEMP_THRESHOLD 5000 //50 celsious int32_t PowerOffTemperatureThreshold = POWER_OFF_TEMP_THRESHOLD; bool PowerOffInProcessGetState(void) { return PowerOffInProcess; } void PowerOffSetTemperatureThreshold (int32_t temperature) { if (temperature) { PowerOffTemperatureThreshold = temperature*100; } } /*******************************************************************************************************/ uint32_t PowerOffScheduler(uint32_t IfIndex, uint32_t BusyFlag) { if (PowerOffMachineState > StoredMachineState) { StoredMachineState = PowerOffMachineState; PowerOffSequenceStateMachine (PowerOffMachineState); } return OK; } /*******************************************************************************************************/ uint32_t PowerOffInit(void) { LOG_ERROR(0,"Power Off Init"); PowerOffMachineState++; StopInitSequence(); setmachineActive(true); PowerIdleOutOfIdleState(); PowerOffInProcess = true; PowerOffControlId = AddControlCallback("PowerOff", PowerOffScheduler, eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); SetMachineStatus(MACHINE_STATE__PowerOff); return OK; } /*******************************************************************************************************/ uint32_t PowerOffCancel(void) { if (PowerOffMachineState >= POWER_OFF_HEAD_CLEAN) PowerOffMachineState = POWER_OFF_CANCELLED; PowerOffInProcess = false; return OK; } /************************************************************************shlomo */ uint32_t PowerOffHeadCleanControlId = 0xff; uint32_t PowerOffHeadCleanCallback(uint32_t DispenserId, uint32_t ReadValue) { if ( JobIsActive()== false) { //stop this control loop SafeRemoveControlCallback(PowerOffHeadCleanControlId, PowerOffHeadCleanCallback ); PowerOffHeadCleanControlId = 0xFF; PowerOffMachineState++; SetMachineStatus(MACHINE_STATE__PowerOff); } return OK; } /*******************************************************************************************************/ uint32_t PowerOffHeadClean(void) { //TBD if (AutoHoming_Config >= AutoHoming_PowerOff ) AutoHoming_Config = AutoHoming_JobEnd_PowerOn_off; if (ThreadCleaningJobFunc(50) == OK) { if (PowerOffHeadCleanControlId != 0xFF) { RemoveControlCallback(PowerOffHeadCleanControlId, PowerOffHeadCleanCallback ); PowerOffHeadCleanControlId = 0xFF; //return ERROR; } PowerOffHeadCleanControlId = AddControlCallback(NULL, PowerOffHeadCleanCallback,eOneSecond , TemplateDataReadCBFunction,0,0, 0 ); } else { LOG_ERROR(0,"Clean job failed"); PowerOffMachineState++; SetMachineStatus(MACHINE_STATE__PowerOff); } // if ( PowerOffHeadCleanControlId == 0xFF) // return ERROR; return OK; } /*******************************************************************************************************/ bool DispenserHomingActive[MAX_SYSTEM_DISPENSERS] = {false,false,false,false,false,false,false,false}; uint32_t PowerOffDispenserHomingCallback(uint32_t DispenserId, uint32_t ReadValue) { DispenserHomingActive[DispenserId] = false; REPORT_MSG (DispenserId, "PowerOffDispenserHomingCallback"); return OK; } #define TI_DISPENSER_ID 4 #define DEFAULT_MIXER_CLEANING_SPEED 1000 #define DEFAULT_MIXER_CLEANING_TIMEOUT 10000 uint32_t PowerOffMixerFlushCallback(void) { //TBD int i; REPORT_MSG (PowerOffMachineState, "PowerOffMixerFlushCallback"); SafeRemoveControlCallback(PowerOffHeadCleanControlId, PowerOffMixerFlushCallback ); Control3WayValvesWithCallback ((Valves_t)TI_DISPENSER_ID, MidTank_Dispenser, NULL); //direction: MidTank_Dispenser or Dispenser_Mixer TimerMotors_t HW_Motor_Id = DispenserIdToMotorId[TI_DISPENSER_ID]; MotorStop(HW_Motor_Id,Hard_Hiz); CurrentDispenserSpeed[TI_DISPENSER_ID] = 0; Task_sleep (20); for (i=0;i= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( TEMP_SENSE_ANALOG_DYEINGH_TEMP1); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( TEMP_SENSE_ANALOG_DYEINGH_TEMP2); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( TEMP_SENSE_ANALOG_DYEINGH_TEMP3); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( TEMP_SENSE_ANALOG_DYEINGH_TEMP4); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( TEMP_SENSE_ANALOG_DYEINGH_TEMP5); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( HEAD6_PT100); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; readTemp = TemperatureSensorRead( TEMP_SENSE_ANALOG_DRYER_TEMP1); if ((readTemp>= MaxTemp)&&(readTemp < 28000)) MaxTemp = readTemp; if ((MaxTemp>PowerOffTemperatureThreshold)&&(WaitForProcessCounter++<3600)) { Report("On going cooling down, wait for end of cooling",__FILE__,__LINE__,(int)MaxTemp,RpWarning,(int)WaitForProcessCounter,0); resetIdleCounter(); } else { Report("ended cooling down, wait for end of cooling",__FILE__,__LINE__,(int)MaxTemp,RpWarning,(int)WaitForProcessCounter,0); PowerOffMachineState = POWER_OFF_TURN_OFF_DRYER_FAN; SafeRemoveControlCallback(WaitForProcessControlId, PowerOffWaitForTemperatureCallback); WaitForProcessControlId = 0xFF; } return OK; } /*******************************************************************************************************/ uint32_t PowerOffWaitForTemperature(void) { if (WaitForProcessControlId == 0xFF) { WaitForProcessCounter = 0; WaitForProcessControlId = AddControlCallback("poweroff cooling", PowerOffWaitForTemperatureCallback, eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); } //TBD return OK; } /*******************************************************************************************************/ uint32_t PowerOffTurnOffDryerFan(void) { Control_Dryer_Fan(STOP,75);//use START or STOP, 0 - 100% PowerOffMachineState++; //TBD return OK; } /*******************************************************************************************************/ uint32_t PowerOffTurnOffCooler(void) { uint32_t DeActivateChiller(); PowerOffMachineState++; return OK; } /*******************************************************************************************************/ uint32_t PowerOffTurnOffBlower(void) { Turn_the_Blower_Off();//Turn off PowerOffMachineState++; return OK; } /*******************************************************************************************************/ uint32_t PowerOffPowerOff(void) { int i; for (i = 0;i<10;i++) { REPORT_MSG (10-i, "Power off in x seconds"); Task_sleep (1000); } Power_Off(); return OK; } /*******************************************************************************************************/ uint32_t PowerOffSequenceStateMachine( POWER_OFF_STAGES_ENUM ReadValue) { uint32_t status = OK; REPORT_MSG(ReadValue,"PowerOffSequenceStateMachine"); switch (ReadValue) { case POWER_OFF_INIT: break; case POWER_OFF_HEAD_CLEAN: status = PowerOffHeadClean(); break; case POWER_OFF_MIXER_FLUSH: status = PowerOffMixerFlush(); break; case POWER_OFF_HEATERS_OFF: status = PowerOffHeatersOff(); break; case POWER_OFF_STORE_DATA: status = PowerOffStoreData(); break; case POWER_OFF_WAIT_FOR_PROCESSES: //wait for waste emptying: ink filling: thread loading status = PowerOffWaitForProcesses(); break; case POWER_OFF_STOP_RUNNING_JOB: status = PowerOffStopRunningJob(); break; case POWER_OFF_SET_VALVE_POSITION: status = PowerOffSetValvePosition(); break; case POWER_OFF_WAIT_FOR_TEMPERATURE: status = PowerOffWaitForTemperature(); break; case POWER_OFF_TURN_OFF_DRYER_FAN: status = PowerOffTurnOffDryerFan(); break; case POWER_OFF_TURN_OFF_COOLER: status = PowerOffTurnOffCooler(); break; case POWER_OFF_TURN_OFF_BLOWER: status = PowerOffTurnOffBlower(); break; case POWER_OFF_POWER_OFF: status = PowerOffPowerOff(); break; case POWER_OFF_ERROR: case POWER_OFF_CANCELLED: RemoveControlCallback( PowerOffControlId,PowerOffScheduler); PowerOffControlId = 0xFF; RemoveControlCallback(WaitForProcessControlId, PowerOffWaitForProcessesCallback); RemoveControlCallback(WaitForProcessControlId, PowerOffWaitForTemperatureCallback); WaitForProcessControlId = 0xFF; LOG_ERROR(ReadValue,"POWER OFF Cancelled"); break; default: LOG_ERROR(ReadValue,"ERROR IN POWER OFF STATE MACHINE"); break; } return status; }