/* * InitSequence.c * * Created on: Feb 12, 2019 * Author: shlomo */ #include #include #include "include.h" #include #include "PMR/MachineStatus/MachineStatus.pb-c.h" #include "Modules/General/MachineStatus.h" #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 "Modules/Waste/Waste.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 "heaters/heaters_ex.h" typedef enum { INIT_SEQUENCE_INIT, INIT_SEQUENCE_RESET_REASON, INIT_SEQUENCE_BUILT_IN_TEST, INIT_SEQUENCE_INITIAL_BLOWER_ACTIVATION, //INIT_SEQUENCE_DISPENSER_PRESSURE_BUILDUP_TEST, INIT_SEQUENCE_THREAD_DETECTION, INIT_SEQUENCE_START_HEATING, INIT_SEQUENCE_MACHINE_READY_TO_DYE, INIT_SEQUENCE_END, }INIT_SEQUENCE_STAGES_ENUM; INIT_SEQUENCE_STAGES_ENUM InitStages = INIT_SEQUENCE_INIT, StoredInitStages = INIT_SEQUENCE_INIT; MACHINE_STATE_STAGES_ENUM MachineStateDetail = MACHINE_STATE_INIT; MACHINE_STATE_STAGES_ENUM GetMachineState(void) { return MachineStateDetail; } void SetMachineState(MACHINE_STATE_STAGES_ENUM NewState) { MachineStateDetail = NewState; } uint32_t HWControlId,InitSchedulerControlId; uint32_t MidTankControlId; uint32_t RESET_Cause = 0; void InitSequenceResetReason(void); uint32_t InitSequenceBuiltInTest(void); uint32_t InitSequenceInitialBlowerActivation(void); uint32_t InitSequenceDispenserPressureBuildUpTest(void); uint32_t InitSequenceThreadDetection(void); uint32_t InitSequenceStartHeating(void); uint32_t InitSequenceMachineReadyToDye(void); uint32_t InitSequenceStateMachine( INIT_SEQUENCE_STAGES_ENUM ReadValue); void StopInitSequence(void) { InitStages = INIT_SEQUENCE_END; InitSequenceStateMachine(InitStages); } uint32_t InitSequenceCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag) { if (SafeRemoveControlCallback(HWControlId, InitSequenceCallBackFunction )==OK) HWControlId = 0xFF; else Report("Remove control callback failed",__FILE__,__LINE__,(int)HWControlId,RpWarning,(int)InitSequenceCallBackFunction,0); if (HWConfigurationInit() == OK) { ActivateHeadMagnet(); InitStages++; //InitSequenceStateMachine(InitStages); } else { //if (MachineStateDetail == MACHINE_STATE_HW_CONFIG) { Report("HWConfigurationInit failed",__FILE__,__LINE__,(int)HWControlId,RpWarning,(int)InitSequenceCallBackFunction,0); MachineStateDetail = MACHINE_STATE_NO_CFG_FILE; AlarmHandlingSetAlarm (EVENT_TYPE__MACHINE_STATE_HW_CONFIG_FAILED,ON); SetMachineStatus(MACHINE_STATE__Error); } } return OK; } /*******************************************************************************************************/ uint32_t InitScheduler(uint32_t IfIndex, uint32_t BusyFlag) { if (InitStages > StoredInitStages) { StoredInitStages = InitStages; InitSequenceStateMachine(InitStages); } return OK; } /*******************************************************************************************************/ uint32_t Start_InitSequence(void) { MachineStateDetail = MACHINE_STATE_HW_CONFIG; SetMachineStatus(MACHINE_STATE__Initializing); HWControlId = AddControlCallback("Init HW Init", InitSequenceCallBackFunction, 4* eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); InitSchedulerControlId = AddControlCallback("InitScheduler", InitScheduler, eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); return OK; } void InitSequenceResetReason(void) { ReportResetReason(); InitStages++; //InitSequenceStateMachine(InitStages); //return OK; } void InitSequenceBuiltInTestCallBack(uint32_t IfIndex, uint32_t BusyFlag) { if (1)//BIT OK { InitStages++; //InitSequenceStateMachine(InitStages); } else { MachineStateDetail = MACHINE_STATE_BUILT_IN_FAILED; } //return OK; } uint32_t InitSequenceBuiltInTest(void) { MachineStateDetail = MACHINE_STATE_BUILT_IN_TEST; InitSequenceBuiltInTestCallBack(0,0); return OK; } int MidTankOperationCounter = 0; uint32_t InitSequenceMidTankCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag) { int MidTankOpenAir = 8; int MidTankReadPressure = 16; int MidTankCloseAir = 24; int MidTankEnd = 32; int portId; if (MidTankOperationCounter >= MidTankEnd) { SafeRemoveControlCallback(MidTankControlId, InitSequenceMidTankCallBackFunction); } else if (MidTankOperationCounter >= MidTankCloseAir) { //close air valve for midtank (MidTankOperationCounter-MidTankCloseAir) portId = (MidTankOperationCounter - MidTankCloseAir) ; //0-7 Disable_MidTank_Pressure_Reading(portId); Valve_Set(IDS_Id_to_AirValve[portId], Atm_MidTank_OFF ); //Atm_MidTank_OFF/ON Control3WayValvesWithCallback ((Valves_t)portId, MidTank_Dispenser, NULL); //direction: MidTank_Dispenser or Dispenser_Mixer } else if (MidTankOperationCounter >= MidTankReadPressure) { //read pressure for midtank (MidTankOperationCounter-MidTankCloseAir) for (portId = 0;portId < MAX_SYSTEM_DISPENSERS;portId++) { Read_MidTank_Pressure_Sensor(portId); } } else if (MidTankOperationCounter >= MidTankOpenAir) { //open air valve for midtank (MidTankOperationCounter-MidTankOpenAir) portId = (MidTankOperationCounter - MidTankOpenAir) ; //0-7 Enable_MidTank_Pressure_Reading(portId); Valve_Set(IDS_Id_to_AirValve[portId], Atm_MidTank_ON ); //Atm_MidTank_OFF/ON Control3WayValvesWithCallback ((Valves_t)portId, Dispenser_Mixer, NULL); //direction: MidTank_Dispenser or Dispenser_Mixer } MidTankOperationCounter++; return OK; } uint32_t InitSequenceBlowerCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag) { if (SafeRemoveControlCallback(HWControlId, InitSequenceBlowerCallBackFunction )==OK) HWControlId = 0xFF; else Report("Remove control callback failed",__FILE__,__LINE__,(int)HWControlId,RpWarning,(int)InitSequenceBlowerCallBackFunction,0); if (BlowerCfg.heatingvoltage) Control_Voltage_To_Blower(BlowerCfg.heatingvoltage); else Control_Voltage_To_Blower(3000); Safety_Init(); InitStages++; //InitSequenceStateMachine(InitStages); return OK; } uint32_t InitSequenceInitialBlowerActivation(void) { MachineStateDetail = MACHINE_STATE_INITIAL_BLOWER_ACTIVATION; Turn_the_Blower_On();//Turn on with the Default_Voltage if (BlowerCfg.voltage) Control_Voltage_To_Blower(BlowerCfg.voltage); else Control_Voltage_To_Blower(3000); HWControlId = AddControlCallback("Init Blower", InitSequenceBlowerCallBackFunction, 10* eOneSecond, TemplateDataReadCBFunction,0,0, 0 ); /*if (RdInkCartridgeSensor()) //if there is a cartridge in the ink slot skip the valves procedure { MidTankOperationCounter = 32; Report("There is a cartridge in the ink slot. skipping the valves procedure",__FILE__,__LINE__,(int)MidTankOperationCounter,RpWarning,(int)InitStages,0); }*/ MidTankControlId = AddControlCallback("Init Midtank", InitSequenceMidTankCallBackFunction, 300/*eHundredMillisecond*/, TemplateDataReadCBFunction,0,0, 0 ); return OK; } int NumOfCheckedDispnsers = 0; uint32_t InitSequenceDispenserPressureBuildUpTestCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag) { NumOfCheckedDispnsers--; if(NumOfCheckedDispnsers==0) { InitStages++; //InitSequenceStateMachine(InitStages); MachineStateDetail = MACHINE_STATE_DISPENSER_PRESSURE_BUILDUP_TEST; } return OK; } uint32_t InitSequenceDispenserPressureBuildUpTest(void) { int Dispenser_i; Report("InitSequenceDispenserPressureBuildUpTest",__FILE__,__LINE__,(int)0,RpWarning,(int)0,0); for (Dispenser_i = 0;Dispenser_i= AutoHoming_PowerOn_off ) { int Dispenser_i; for ( Dispenser_i = 0;Dispenser_i < MAX_SYSTEM_DISPENSERS;Dispenser_i++) { if (Dispenser_i!=LUBRICANT_DISPENSER) IDS_HomeDispenser (Dispenser_i, 1000 , NULL); } } //activate second pump for 10 minutes PumpActivation(600); return OK; } uint32_t InitSequenceMachineReadyToDye(void) { SetMachineStatus(MACHINE_STATE__Ready); InitStages++; return OK; } uint32_t InitSequenceInitEnd(void) { RemoveControlCallback( InitSchedulerControlId,InitScheduler); return OK; } uint32_t InitSequenceStateMachine( INIT_SEQUENCE_STAGES_ENUM ReadValue) { REPORT_MSG(ReadValue,"InitSequenceStateMachine"); switch (ReadValue) { case INIT_SEQUENCE_INIT: MachineStateDetail = MACHINE_STATE_HW_CONFIG; Start_InitSequence(); break; case INIT_SEQUENCE_RESET_REASON: InitSequenceResetReason(); break; case INIT_SEQUENCE_BUILT_IN_TEST: InitSequenceBuiltInTest(); break; case INIT_SEQUENCE_INITIAL_BLOWER_ACTIVATION: InitSequenceInitialBlowerActivation(); break; /*case INIT_SEQUENCE_DISPENSER_PRESSURE_BUILDUP_TEST: InitSequenceDispenserPressureBuildUpTest(); break;*/ case INIT_SEQUENCE_THREAD_DETECTION: InitSequenceThreadDetection(); break; case INIT_SEQUENCE_START_HEATING: InitSequenceStartHeating(); break; case INIT_SEQUENCE_MACHINE_READY_TO_DYE: InitSequenceMachineReadyToDye(); break; case INIT_SEQUENCE_END: InitSequenceInitEnd(); break; default: LOG_ERROR(ReadValue,"ERROR IN INIT STATE MACHINE"); RemoveControlCallback( InitSchedulerControlId,InitScheduler); break; } return OK; }