aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/StateMachines/Initialization/InitSequence.c
blob: a6e0f7d12e6b4244302c94d2aed51accd72016e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * 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("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<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("Init Heating", InitSequenceStartHeatingCallBackFunction, 2*eOneSecond, TemplateDataReadCBFunction,0,0, 0 );
            //start heaters with predefined / store process parameters
        }
        else
        {
            MachineStateDetail = MACHINE_STATE_NO_PROCESS_PARAMS;
		    SetMachineStatus(MACHINE_STATE__Ready);
	        REPORT_MSG(MachineStateDetail,"InitSequenceStateMachine no processparameters file");	
        }
    }
    else
    {
        REPORT_MSG(InitialHeating,"InitSequenceStateMachine InitialHeating is OFF");
        MachineStateDetail = MACHINE_STATE_MACHINE_READY_TO_DYE;
        InitStages++;
    }
    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);
    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;
}