aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/StateMachines/Printing/PrintingSTM.c
blob: 688f40533b8bfc729fdcd6a2d7938a06340be675 (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
/************************************************************************************************************************
 * Printing.c
 * High managment logical unit of slow motors in the system ( 6 dispensers and the screw motor)
 * profile run up begins from screw homing to begin position and only then from fast motors activation.
 * when every slow motor tuches the limit switch (no matter whether its screw or dispenser)
 * an interrupt occures in the system and as long as its pushing the limit switch all the system is prevented from operation.
 * because of that the work flow with interrupts must be :
     * design a function handle (what to do in the moment the interrupt arrives)
     * configure the wanted interrupt in the cfg file (according to the defined port and pin and its interrupt number and the handler)
     * enable interupt for predefined gpio in the application
     * when the interrupt arrives the handle will be automatically called
     * in case of the limit switches since the operation is continuess the interrupt must be disabled in order to continue the application running.
     * then the operation is not continues (like butten pushing) there is no need in disabling the interrupts

 * Printing module is responsible for :
     * operating diffrent winding algorithms with predefined parameters from the UI
     * operating the dispensers according to predefined dispensing rate from the UI
 **************************************************************************************************************************/
#include "include.h"
#include "./printingSTM.h"
#include "modules/thread/thread_ex.h"
#include "modules/ids/ids_ex.h"
////////////////////////////////State machine operation////////////////////////////////////
//the state machine operation is used to operate in runtime correct profile flow execution
//by recieved esign flow of the user from the UI
///////////////////////////////////////////////////////////////////////////////////////////

/********************************************************************************************
* functions describes motor operation flow and movement state during profile execution
* used to operate in runtime correct profileflow execution
*********************************************************************************************/
static uint32_t EntryState(void *JobDetails);
static uint32_t PrepareState(void *JobDetails);
static uint32_t PreSegmentState(void *JobDetails, int);
static uint32_t SegmentState(void *JobDetails, int);
static uint32_t EndState(void *JobDetails);
static uint32_t ExitState(void *JobDetails);

/**********************************************************************
* the array and enum of PrintingState_t below must be in sync order
***********************************************************************/
//static uint32_t (* state[])(void *JobDetails) = { EntryState, PrepareState, PreSegmentState,  SegmentState, EndState, ExitState};


typedef enum
{
    PrintRequest,
    PreSegmentResultsOk,
    PreSegmentResultsFail,
    SegmentResultsOk,
    SegmentResultsFail,
    FinishResultsOk,
    FinishResultsFail,
    PrintSystemFailure
}PrintSTMEventsEnum;

typedef struct
{
    PrintingState_t m_sourceState;
    uint32_t          m_returnCode;
    PrintingState_t     m_destinationState;
} Transition_t;


////////////////////////Slow Motor State////////////////////////////////////
//static PrintingState_t gPrintingState;
////////////////////////////////////////////////////////////////////////////
ModuleStateEnum SegmentWaiting[MAX_SYSTEM_MODULES] = {ModuleIdle};
ModuleStateEnum PreSegmentWaiting[MAX_SYSTEM_MODULES] = {ModuleIdle};
ModuleStateEnum PrintWaiting[MAX_SYSTEM_MODULES] = {ModuleIdle};


//********************************************************************************************************************
/********************************************************************************************************************
*function describes entry point of motor in profile execution - accelerate from stop position
*function described above used to operate motor operation flow and movement state during profile execution
*********************************************************************************************************************/
static uint32_t EntryState(void *JobDetails)
{
    return OK;
}

uint32_t PreSegmentReady(int ModuleId, ModuleStateEnum result)
{
    int i;
    bool ready = true;
    uint32_t status = OK;

    JobMessageStruc Message;
    PrintMessageStruc *PrtMessage = (PrintMessageStruc *)Message.messageData;

    assert (ModuleId<MAX_SYSTEM_MODULES);
    assert (result<=ModuleFail);


    PreSegmentWaiting[ModuleId] = result;
    if (result == ModuleFail) status = ERROR;
    for (i=0;i<MAX_SYSTEM_MODULES ;i++)
    {
        if (PreSegmentWaiting[i] == ModuleWaiting)
            ready = false;
    }
    if ((ready == false)&&(status == OK)) return OK;
    else
    {
        Message.messageId = PrintMessage;
        if (status == OK)    PrtMessage->messageId = PreSegmentResultsOk;
        else PrtMessage->messageId = PreSegmentResultsFail;
        //memcpy(Message.messageData,JobDetails,MAX_MSG_LEN);
        Message.msglen = 10;
        if (JobmsgQ != NULL)
            Mailbox_post(JobmsgQ , &Message, BIOS_NO_WAIT);
    }
    return 0;

}

//********************************************************************************************************************
static uint32_t PreSegmentState(void *JobDetails, int SegmentId)
{
    PreSegmentWaiting[Module_Thread] = ModuleWaiting;
    ThreadPreSegmentState(JobDetails);
    PreSegmentWaiting[Module_Winder] = ModuleWaiting;
    Winder_Presegment(JobDetails);
    PreSegmentWaiting[Module_IDS] = ModuleWaiting;
    IDSPreSegmentState(JobDetails,SegmentId);
    return OK;
}
//********************************************************************************************************************
uint32_t SegmentReady(int ModuleId, ModuleStateEnum result)
{
    int i;
    bool ready = true;
    uint32_t status = OK;

    JobMessageStruc Message;
    PrintMessageStruc *PrtMessage = (PrintMessageStruc *)Message.messageData;

    assert (ModuleId<MAX_SYSTEM_MODULES);
    assert (result<=ModuleFail);


    SegmentWaiting[ModuleId] = result;
    if (result == ModuleFail) status = ERROR;
    for (i=0;i<MAX_SYSTEM_MODULES ;i++)
    {
        if (SegmentWaiting[i] == ModuleWaiting)
            ready = false;
    }
    if ((ready == false)&&(status == OK)) return OK;
    else
    {
        Message.messageId = PrintMessage;
        if (status == OK)    PrtMessage->messageId = SegmentResultsOk;
        else PrtMessage->messageId = SegmentResultsFail;
        //memcpy(Message.messageData,JobDetails,MAX_MSG_LEN);
        Message.msglen = 10;
        if (JobmsgQ != NULL)
            Mailbox_post(JobmsgQ , &Message, BIOS_NO_WAIT);
    }
    return 0;

}

//********************************************************************************************************************
static uint32_t SegmentState(void *JobDetails, int SegmentId)
{
    SegmentWaiting[Module_Thread] = ModuleWaiting;
    ThreadSegmentState(JobDetails,SegmentId);
    //SegmentWaiting[Module_Winder] = ModuleWaiting;
    //Winder_Segment(JobDetails);
    SegmentWaiting[Module_IDS] = ModuleWaiting;
    IDSSegmentState(JobDetails,SegmentId);
    return OK;
}

//********************************************************************************************************************
static uint32_t EndState(void *JobDetails)
{
    return OK;
}
//********************************************************************************************************************
static uint32_t ExitState(void *JobDetails)
{
    return OK;
}

//********************************************************************************************************************

void PrintingsInit(void)
{
}

//********************************************************************************************************************

void StartPrinting(void)
{
}

//********************************************************************************************************************
//********************************************************************************************************************

void StopPrinting(void)
{
}
void PrintSTMMsgHandler(void * msg)
{
    JobMessageStruc *Message = msg;
    PrintMessageStruc *PrtMessage = (PrintMessageStruc *)Message->messageData;

    Report(REPORT_LINE("PrintSTMMsgHandler"),__FILE__,__LINE__, RpMessage,0x1000,Message->messageId,PrtMessage->messageId);

    if (Message->messageId != PrintMessage)
    {
        //REPORT_ERR ...
        return;
    }
    static int SegmentId = 0;
    switch(PrtMessage->messageId)
    {
        case PrintRequest:
            SegmentId = 0;
            PreSegmentState(&CurrentJob,SegmentId);
            break;
        case PreSegmentResultsOk:
            SegmentState(&CurrentJob,SegmentId);
            break;
        case PreSegmentResultsFail:
            ExitState(Message->messageData);
            break;
        case SegmentResultsOk:
            SegmentId++;
            if (SegmentId == CurrentJob->n_segments)
                {
                  //if (CurrentJob->) handle distance to spool
                    EndState(&CurrentJob);
                }
            else
            {
                PreSegmentState(&CurrentJob,SegmentId);
            }
            break;
        case SegmentResultsFail:
            break;
        case FinishResultsOk:
            break;
        case FinishResultsFail:
            break;
        case PrintSystemFailure:
            break;
        default:
            break;
    }



}