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
|
/************************************************************************************************************************
* 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"
////////////////////////////////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
///////////////////////////////////////////////////////////////////////////////////////////
typedef enum
{
NextState = 0,
Repeat,
Inter,
Home,
Stop
} ReturnCode;
/********************************************************************************************
* functions describes motor operation flow and movement state during profile execution
* used to operate in runtime correct profileflow execution
*********************************************************************************************/
static ReturnCode EntryState(void *JobDetails);
static ReturnCode PrepareState(void *JobDetails);
static ReturnCode PreSegmentState(void *JobDetails);
static ReturnCode SegmentState(void *JobDetails);
static ReturnCode EndState(void *JobDetails);
static ReturnCode ExitState(void *JobDetails);
/**********************************************************************
* the array and enum of PrintingState_t below must be in sync order
***********************************************************************/
static ReturnCode (* 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;
ReturnCode m_returnCode;
PrintingState_t m_destinationState;
} Transition_t;
//*************************************************************
/* transitions from end state aren't needed */
//*************************************************************
#define NUM_OF_TRANSITION 17
#define ENTRY_STATE Entry
/*************************************************************
* table which describes fast motors transitions states
* during p_profile / segments execution
*************************************************************/
static Transition_t stateTransitionTable[NUM_OF_TRANSITION] = {};
/*{
{Entry, NextState, HomingStart},
{Entry, Repeat, Entry}, //for homing of dispensers
{HomingStart, NextState, Start},
{HomingStart, Repeat, HomingStart},
{Start, NextState, Segment},
{Start, Repeat, Start},
{Segment, Inter, Intersegment},
{Segment, Repeat, Segment},
{Segment, Home, HomingEnd},
{Intersegment, NextState, Segment},
{Intersegment, Repeat, Intersegment},
{Intersegment, Home, HomingEnd},
{HomingEnd, NextState, End},
{HomingEnd, Repeat, HomingEnd},
{End, NextState, Entry},
{End, Repeat, Entry},
{Exit, Stop, Exit} //for stoping the machine iteration in case of error
};*/
////////////////////////Slow Motor State////////////////////////////////////
static PrintingState_t gPrintingState;
////////////////////////////////////////////////////////////////////////////
//********************************************************************************************************************
/********************************************************************************************************************
*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 ReturnCode EntryState(void *JobDetails)
{
return NextState;
}
//********************************************************************************************************************
static ReturnCode PrepareState(void *JobDetails)
{
return NextState;
}
//********************************************************************************************************************
static ReturnCode PreSegmentState(void *JobDetails)
{
return NextState;
}
//********************************************************************************************************************
static ReturnCode SegmentState(void *JobDetails)
{
return Repeat;
}
//********************************************************************************************************************
static ReturnCode EndState(void *JobDetails)
{
return NextState;
}
//********************************************************************************************************************
static ReturnCode ExitState(void *JobDetails)
{
return Stop;
}
//***********************************************************************************************************************
//this function is responsible for operating and transitioning between the diffrent motor state executions of the profile
//the lower managment level
//***********************************************************************************************************************
static PrintingState_t LookupTransitions(PrintingState_t state,ReturnCode returnCode)
{
char str[80];
uint8_t len = 0;
uint8_t indexInTransitionTable;
for (indexInTransitionTable = 0; indexInTransitionTable < NUM_OF_TRANSITION; ++indexInTransitionTable)
{
if ((stateTransitionTable[indexInTransitionTable].m_sourceState == state) && (stateTransitionTable[indexInTransitionTable].m_returnCode == returnCode))
{
//len = usnprintf(str, 60, "\r\n tick %d state %d return code %d",tick,state, returnCode );
//cb_push_back (str, len);
//in normal execution flow function should not arrive here
//in case it did the meaning is that the entery point was wrong and a bug should be corrected
return stateTransitionTable[indexInTransitionTable].m_destinationState;
}
}
//int tick = UsersysTickGet();
//len = usnprintf(str, 60, "\r\n tick %d state %d return code %d",tick,state, returnCode );
//cb_push_back (str, len);
//in normal execution flow function should not arrive here
//in case it did the meaning is that the entery point was wrong and a bug should be corrected
len = usnprintf(str, 80, "Internal: invalid slow motor transition state %d return code %d",state, returnCode );
return EXIT_STATE;
}
//********************************************************************************
//this function is used to manage and operate the motor managmant state mashine
//the highest managment level
//********************************************************************************
bool PrintingIterate(void *JobDetails)
{
uint32_t tick = 0;
char str[60];
uint8_t len = 0;
PrintingState_t keepstate = gPrintingState;
//
// Disable all interrupts.
//
ROM_IntMasterDisable();
ReturnCode (* state_fun)(void *JobDetails) = state[gPrintingState];
//if (_motorId == SCREW_MOTOR)
// screw_movement[gPrintingState]++;
ReturnCode returnCode = state_fun(JobDetails);
/*if ((_motorId == SCREW_MOTOR)&&(pause_active))
{
tick = UsersysTickGet();
len = usnprintf(str, 60, "\r\n PrintingIterate tick %d state %d retcode %d ",tick, gPrintingState,returnCode);
cb_push_back (str, len);
//SendInterruptMessageToHost(10+gPrintingState,returnCode);
}*/
gPrintingState = LookupTransitions(gPrintingState, returnCode);
if (keepstate != gPrintingState){
tick = UsersysTickGet();
len = usnprintf(str, 60, "\r\n changed state tick %d state %d retcode %d ", tick, gPrintingState,returnCode);
cb_push_back (str, len);
}
//
// Enable all interrupts.
//
ROM_IntMasterEnable();
return (gPrintingState != EXIT_STATE);
}
//********************************************************************************************************************
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;
}
switch(PrtMessage->messageId)
{
case PrintRequest:
break;
case PreSegmentResultsOk:
break;
case PreSegmentResultsFail:
break;
case SegmentResultsOk:
break;
case SegmentResultsFail:
break;
case FinishResultsOk:
break;
case FinishResultsFail:
break;
case PrintSystemFailure:
break;
default:
break;
}
}
|