aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/StateMachines/Initialization/PowerOffSequence.c
blob: d19be0f29d8a50b05eb53551e8b3b64269e5a824 (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
/*
 * 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 "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_HEAD_CLEAN,
        POWER_OFF_MIXER_FLUSH,
        POWER_OFF_HEATERS_OFF,
        POWER_OFF_STORE_DATA,
        POWER_OFF_WAIT_FOR_PROCESSES,//wait for waste emptying, ink filling, thread loading
        POWER_OFF_STOP_RUNNING_JOB,
        POWER_OFF_SET_VALVE_POSITION,
        POWER_OFF_WAIT_FOR_TEMPERATURE,
        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);

#define POWER_OFF_TEMP_THRESHOLD 5000  //50 celsious
int32_t PowerOffTemperatureThreshold = POWER_OFF_TEMP_THRESHOLD;

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 = POWER_OFF_HEAD_CLEAN;
    PowerOffControlId = AddControlCallback( PowerOffScheduler, eOneSecond, TemplateDataReadCBFunction,0,0, 0 );
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffCancel(void)
{
    if (PowerOffMachineState >= POWER_OFF_HEAD_CLEAN)
        PowerOffMachineState = POWER_OFF_CANCELLED;
    return OK;
}

/*******************************************************************************************************/
uint32_t PowerOffHeadClean(void)
{
    //TBD
    PowerOffMachineState = POWER_OFF_MIXER_FLUSH;
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffMixerFlush(void)
{
    //TBD
    PowerOffMachineState = POWER_OFF_HEATERS_OFF;
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffHeatersOff(void)
{
    ProcessParameters ProcessParametersClear;
    ProcessParametersClear.dryerzone1temp = 0;
    ProcessParametersClear.dryerzone2temp = 0;
    ProcessParametersClear.dryerzone3temp = 0;
    ProcessParametersClear.mixertemp      = 0;
    ProcessParametersClear.headzone1temp  = 0;
    ProcessParametersClear.headzone2temp  = 0;
    ProcessParametersClear.headzone3temp  = 0;
    ProcessParametersClear.headzone4temp  = 0;
    ProcessParametersClear.headzone5temp  = 0;
    ProcessParametersClear.headzone6temp  = 0;
    ProcessParametersClear.dyeingspeed = 40;
    if (HandleProcessParameters(&ProcessParametersClear)!= OK)
    {
        LOG_ERROR (PowerOffMachineState, "Turn Off Heaters  failed");
        return ERROR;
    }

    PowerOffMachineState = POWER_OFF_STORE_DATA;

    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffStoreData(void)
{
    //TBD
    REPORT_MSG (PowerOffMachineState, "Store Data");
    IDS_Dispenser_Store_Data();
    PowerOffMachineState = POWER_OFF_WAIT_FOR_PROCESSES;
    return OK;
}
/*******************************************************************************************************/
int WaitForProcessCounter = 0;
uint32_t PowerOffWaitForProcessesCallback(uint32_t IfIndex, uint32_t BusyFlag)
{
    if ((WHS_IsEmptying()||
        //MidTankFillingActive()||
        ThreadLoadingActive()||
        SwUpgradeActive())&&
        (WaitForProcessCounter++<900))
    {
        REPORT_MSG (PowerOffMachineState, "On going processes, wait for end of process");
        resetIdleCounter();
    }
    else
    {
        PowerOffMachineState = POWER_OFF_SET_VALVE_POSITION;
        SafeRemoveControlCallback(WaitForProcessControlId, PowerOffWaitForProcessesCallback);
        WaitForProcessControlId = 0xFF;
    }
 return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffWaitForProcesses(void)
{
    int i;
    REPORT_MSG (PowerOffMachineState, "Stop dispensers homing");
    for (i=0;i<MAX_SYSTEM_DISPENSERS;i++)
        IDS_StopHomeDispenser(i);
    if (WHS_IsEmptying()||
        //MidTankFillingActive()||
        ThreadLoadingActive()||
        SwUpgradeActive())
    {
        WaitForProcessCounter = 0; //15 minutes wait
        REPORT_MSG (PowerOffMachineState, "On going processes, wait for end of process");
        WaitForProcessControlId = AddControlCallback( PowerOffWaitForProcessesCallback, eOneSecond, TemplateDataReadCBFunction,0,0, 0 );
    }
    else
    {
        PowerOffMachineState = POWER_OFF_STOP_RUNNING_JOB;
    }
    //TBD
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffStopRunningJob(void)
{
    if (JobIsActive())
    {
        REPORT_MSG (PowerOffMachineState, "Stop running job");
        AbortJob("Power off pressed");
    }
    PowerOffMachineState = POWER_OFF_SET_VALVE_POSITION;
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffSetValvePosition(void)
{
    //TBD
    PowerOffMachineState = POWER_OFF_WAIT_FOR_TEMPERATURE;
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffWaitForTemperatureCallback(uint32_t IfIndex, uint32_t BusyFlag)
{
    uint32_t MaxTemp = 0,readTemp;
    readTemp = TemperatureSensorRead( MIXER_PT100);
    if ((readTemp>= 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++<3500))
    {
        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( 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 = POWER_OFF_TURN_OFF_COOLER;

    //TBD
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffTurnOffCooler(void)
{
    uint32_t DeActivateChiller();
    PowerOffMachineState = POWER_OFF_TURN_OFF_BLOWER;
    return OK;
}
/*******************************************************************************************************/
uint32_t PowerOffTurnOffBlower(void)
{
    Turn_the_Blower_Off();//Turn off
    PowerOffMachineState = POWER_OFF_POWER_OFF;
    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 OK;
}