aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/UploadCompletedRequest.cs
blob: b51612603feae27931de8c3fa9aeec4331554e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * InitSequence.c
 *
 *  Created on: Feb 12, 2019
 *      Author: shlomo
 */
#include <Container.h>
#include <DataDef.h>
#include "include.h"

#include  <PMR/Diagnostics/EventType.pb-c.h>
#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( InitSequenceCallBackFunction, 4* eOneSecond, TemplateDataReadCBFunction,0,0, 0 );
    InitSchedulerControlId = AddControlCallback( 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
    }
    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
    }
    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( 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( 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<MAX_SYSTEM_DISPENSERS;Dispenser_i++)
    {
        if (isMotorConfigured(Dispenser_i + HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1)==true)
        {
            IDS_Dispenser_Build_Pressure(Dispenser_i,InitSequenceDispenserPressureBuildUpTestCallBackFunction);
            Report("IDS_Dispenser_Build_Pressure",__FILE__,__LINE__,(int)Dispenser_i,RpWarning,(int)Dispenser_i,0);
            NumOfCheckedDispnsers++;
        }
    }


   return OK;
}
uint32_t InitSequenceThreadDetection(void)
{
    InitStages++;
    MachineStateDetail = MACHINE_STATE_THREAD_DETECTION;
    //InitSequenceStateMachine(InitStages);
    return OK;
}

uint32_t InitSequenceStartHeatingCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag)
{
    if (HeaterCheckReady()==true)
    {
        MachineStateDetail = MACHINE_STATE_MACHINE_READY_TO_DYE;
        if (SafeRemoveControlCallback(HWControlId, InitSequenceStartHeatingCallBackFunction )==OK)
            HWControlId = 0xFF;
        else
            Report("Remove control callback failed",__FILE__,__LINE__,(int)HWControlId,RpWarning,(int)InitSequenceStartHeatingCallBackFunction,0);
        InitStages++;
        //InitSequenceStateMachine(InitStages);
    }
    return OK;
}
bool InitialHeating = false;
void InitSequenceSetStartHeating(bool StartHeating)
{
    REPORT_MSG(StartHeating,"InitSequenceStateMachine InitialHeating set");
    InitialHeating = StartHeating;
}
uint32_t InitSequenceStartHeating(void)
{
    if (InitialHeating)
    {
        if (ProcessParamsInit() ==OK)
        {
            MachineStateDetail = MACHINE_STATE_HEATING_STARTED;
            HWControlId = AddControlCallback( InitSequenceStartHeatingCallBackFunction, 2*eOneSecond, TemplateDataReadCBFunction,0,0, 0 );
            //start heaters with predefined / store process parameters
        }
        else
        {
            MachineStateDetail = MACHINE_STATE_NO_PROCESS_PARAMS;
        }
    }
    else
    {
        REPORT_MSG(InitialHeating,"InitSequenceStateMachine InitialHeating is OFF");
        MachineStateDetail = MACHINE_STATE_MACHINE_READY_TO_DYE;
    }
    if (AutoHoming_Config >= 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);
    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;
}