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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/************************************************************************************************************************
* 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
**************************************************************************************************************************/
////////////////////////////////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
///////////////////////////////////////////////////////////////////////////////////////////
#include <Container.h>
#include <DataDef.h>
#include "include.h"
#include <ti/sysbios/knl/mailbox.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/task.h>
#include "PMR/Printing/JobSegment.pb-c.h"
#include "PMR/Printing/JobRequest.pb-c.h"
#include "PMR/Printing/JobResponse.pb-c.h"
#include "PMR/Printing/JobStatus.pb-c.h"
#include "PMR/Printing/AbortJobRequest.pb-c.h"
#include "PMR/Printing/AbortJobResponse.pb-c.h"
#include "PMR/Hardware/Hardwaremotor.pb-c.h"
#include "PMR/Hardware/HardwareWinder.pb-c.h"
#include "PMR/common/MessageContainer.pb-c.h"
#include "Modules/General/process.h"
#include "./printingSTM.h"
#include "modules/thread/thread_ex.h"
#include "modules/ids/ids_ex.h"
#define INVALID_MSG_ID 0xFFFF
Mailbox_Handle JobmsgQ = NULL;
//static Clock_Handle JobClock;
//static Clock_Params jobclkParams;
#define MAX_TICKET_SIZE 10000
//char CurrentJobBuffer[MAX_TICKET_SIZE];
JobTicket *CurrentJob = NULL;//(JobTicket *)CurrentJobBuffer;
JobRequest *CurrentJobRequest = NULL;
//char PreviousJobBuffer[MAX_TICKET_SIZE];
//JobTicket *PreviousJob = (JobTicket *)PreviousJobBuffer;
ModuleStateEnum PrepareWaiting[MAX_SYSTEM_MODULES] = {ModuleIdle};
/********************************************************************************************
* functions describes motor operation flow and movement state during profile execution
* used to operate in runtime correct profileflow execution
*********************************************************************************************/
static ReturnCode IdleState(void *JobDetails);
static ReturnCode ValidateState(void *JobDetails);
static ReturnCode PrepareState(void *JobDetails);
static ReturnCode PrintState(void *JobDetails);
static ReturnCode CleanState(void *JobDetails);
static ReturnCode ExitState(void *JobDetails);
/**********************************************************************
* the array and enum of JobState_t below must be in sync order
***********************************************************************/
//static ReturnCode (* state[])(void *JobDetails) = { IdleState, ValidateState, PrepareState, PrintState, CleanState};
void AbortJob(char *Msg);
typedef enum
{
Idle= 0,
Validate,
PrepareJob,
Print,
Clean,
ExitJob
} JobState_t;
typedef struct
{
JobState_t m_sourceState;
ReturnCode m_returnCode;
JobState_t m_destinationState;
} Transition_t;
////////////////////////Slow Motor State////////////////////////////////////
char JobToken[36];
////////////////////////////////////////////////////////////////////////////
void StartJob(void *JobDetails);
//********************************************************************************************************************
/********************************************************************************************************************
*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 IdleState(void *JobDetails)
{
ReturnCode retcode;
retcode = JobSuccess;
return retcode;
}
//********************************************************************************************************************
static ReturnCode ValidateState(void *JobDetails)
{
ReturnCode retcode;
retcode = JobSuccess;
return retcode;
}
//********************************************************************************************************************
static ReturnCode PrepareState(void *JobDetails)
{
ReturnCode retcode;
retcode = JobSuccess;
//start (fast??) heating
if (Configured[Module_IDS])
{
//PrepareWaiting[Module_IDS] = ModuleWaiting;
}
if (Configured[Module_Heaters])
{
PrepareWaiting[Module_Heaters] = ModuleWaiting;
}
if (Configured[Module_Thread])
{
PrepareWaiting[Module_Thread] = ModuleWaiting;
}
if (Configured[Module_Winder])
{
PrepareWaiting[Module_Winder] = ModuleWaiting;
}
//wait for fast heating to end
//start other peripheral systems: chiller, waist handling
//check thread type
if (Configured[Module_IDS])
{
IDSPrepareState(JobDetails);
}
/* if (Configured[Module_Heaters])
{
//heaters preparation starts on process parameters handling
}*/
if (Configured[Module_Thread])
{
ThreadPrepareState(CurrentJob);
}
if (Configured[Module_Winder])
{
Winder_Prepare();
}
return retcode;
}
//********************************************************************************************************************
uint32_t PrepareReady(int ModuleId, ModuleStateEnum result)
{
int i;
bool ready = true;
uint32_t retcode = 0,status = OK;
JobMessageStruc Message;
assert (ModuleId<MAX_SYSTEM_MODULES);
assert (result<=ModuleFail);
if (PrepareWaiting[ModuleId] != ModuleWaiting)
{
LOG_ERROR (ModuleId, "Message from unrelated module!!");
}
PrepareWaiting[ModuleId] = result;
if (result == ModuleFail) status = ERROR;
for (i=0;i<MAX_SYSTEM_MODULES ;i++)
{
if (PrepareWaiting[i] == ModuleWaiting)
ready = false;
}
if (ready == false) return OK;
else
{
if (status == OK) Message.messageId = PreparationResultsOk;
else Message.messageId = PreparationResultsFail;
//memcpy(Message.messageData,JobDetails,MAX_MSG_LEN);
Message.msglen = 10;
if (JobmsgQ != NULL)
retcode = Mailbox_post(JobmsgQ , &Message, BIOS_NO_WAIT);
}
return retcode;
}
//********************************************************************************************************************
static ReturnCode PrintState(void *JobDetails)
{
ReturnCode retcode;
retcode = JobSuccess;
JobMessageStruc Message;
PrintMessageStruc *PrtMessage = (PrintMessageStruc *)Message.messageData;
Message.messageId = PrintMessage;
PrtMessage->messageId = PrintRequest;
Message.msglen = 10;
if (JobmsgQ != NULL)
Mailbox_post(JobmsgQ , &Message, BIOS_NO_WAIT);
return retcode;
}
//********************************************************************************************************************
static ReturnCode CleanState(void *JobDetails)
{
ReturnCode retcode;
retcode = JobSuccess;
return retcode;
}
//********************************************************************************************************************
static ReturnCode ExitState(void *JobDetails)
{
ReturnCode retcode;
retcode = JobStop;
return retcode;
}
/*static void JobClockHandle(UArg arg0)
{
//Clock_setTimeout(JobClock, 1000);
//Clock_start(JobClock);
}*/
//********************************************************************************************************************
/*void JobInit(void)
{
Clock_Params_init(&jobclkParams);
jobclkParams.period = 0;
jobclkParams.startFlag = FALSE;
JobClock = Clock_create(JobClockHandle, 0, &jobclkParams, NULL);
return ;
}*/
//********************************************************************************************************************
void JobAbortFunc(MessageContainer* requestContainer)
{
uint32_t status = NOT_SUPPORTED;
MessageContainer responseContainer;
uint8_t* container_buffer;
AbortJobRequest* request = abort_job_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
EndState(CurrentJob, "Job Aborted by user");
//AbortJob();
AbortJobResponse response = ABORT_JOB_RESPONSE__INIT;
responseContainer = createContainer(MESSAGE_TYPE__AbortJobResponse, JobToken, false, &response, &abort_job_response__pack, &abort_job_response__get_packed_size);
container_buffer = malloc(message_container__get_packed_size(&responseContainer));
size_t container_size = message_container__pack(&responseContainer, container_buffer);
free(responseContainer.data.data);
SendChars((char*)container_buffer, container_size);
abort_job_request__free_unpacked(request,NULL);
//We keep the job request until it is done
if (CurrentJobRequest!= NULL)
job_request__free_unpacked(CurrentJobRequest,NULL);
CurrentJob = NULL;
CurrentJobRequest = NULL;
}
//********************************************************************************************************************
void JobRequestFunc(MessageContainer* requestContainer)
{
uint32_t status = NOT_SUPPORTED;
MessageContainer responseContainer;
uint8_t* container_buffer;
if (CurrentJobRequest!= NULL)
job_request__free_unpacked(CurrentJobRequest,NULL);
JobRequest* request = job_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
strcpy (JobToken, requestContainer->token);
JobTicket *Ticket = request->jobticket;
int TicketSize = job_ticket__get_packed_size(Ticket);
if (TicketSize >= MAX_TICKET_SIZE)
{
LOG_ERROR (TicketSize, "job ticket message too long");
status = FAILED;
}
else
{
//memcpy(CurrentJob, Ticket,TicketSize);
CurrentJob = Ticket;
CurrentJobRequest = request;
status = PASSED;
HandleProcessParameters(Ticket->processparameters);
InternalWindingConfigMessage(Ticket->spool);
// PrepareWaiting[Module_Heaters] = ModuleWaiting;
}
if (status == PASSED)
{
StartJob(CurrentJob);
}
JobResponse response = JOB_RESPONSE__INIT;
JobStatus jobStatus = JOB_STATUS__INIT;
if (status == PASSED)
{
char Msg[13] = "Job Accepted";
jobStatus.message =Msg;
jobStatus.has_progress = true;
jobStatus.progress = 0.0;
jobStatus.has_currentsegmentindex = false;
response.status = &jobStatus;
response.has_canceled = false;
responseContainer = createContainer(MESSAGE_TYPE__JobResponse, JobToken, false, &response, &job_response__pack, &job_response__get_packed_size);
container_buffer = malloc(message_container__get_packed_size(&responseContainer));
}
else
{
char Msg[25] = "Job message too large";
jobStatus.message = Msg;
jobStatus.has_progress = true;
jobStatus.progress = 0.0;
jobStatus.has_currentsegmentindex = false;
response.status = &jobStatus;
response.has_canceled = false;
responseContainer = createContainer(MESSAGE_TYPE__JobResponse, JobToken, true, &response, &job_response__pack, &job_response__get_packed_size);
container_buffer = malloc(message_container__get_packed_size(&responseContainer));
responseContainer.has_error = true;
responseContainer.error = ERROR_CODE__BAD_CRC;
}
size_t container_size = message_container__pack(&responseContainer, container_buffer);
free(responseContainer.data.data);
SendChars((char*)container_buffer, container_size);
//We keep the job request until it is done
// job_request__free_unpacked(request,NULL);
//free(container_buffer);
//free(requestContainer);
}
//********************************************************************************************************************
void SendJobProgress(double percentage, int SegmentId, bool done, char *Message)
{
uint32_t status = NOT_SUPPORTED;
MessageContainer responseContainer;
uint8_t* container_buffer;
JobResponse response = JOB_RESPONSE__INIT;
JobStatus jobStatus = JOB_STATUS__INIT;
if (done == true)
{
jobStatus.message = Message;
jobStatus.has_progress = true;
jobStatus.progress = percentage;
jobStatus.has_currentsegmentindex = false;
response.status = &jobStatus;
response.has_canceled = false;
responseContainer = createContainer(MESSAGE_TYPE__JobResponse, JobToken, true, &response, &job_response__pack, &job_response__get_packed_size);
container_buffer = malloc(message_container__get_packed_size(&responseContainer));
}
else
{
jobStatus.message =Message;
jobStatus.has_progress = true;
jobStatus.progress = percentage;
jobStatus.has_currentsegmentindex = true;
jobStatus.currentsegmentindex = SegmentId;
response.status = &jobStatus;
response.has_canceled = false;
responseContainer = createContainer(MESSAGE_TYPE__JobResponse, JobToken, false, &response, &job_response__pack, &job_response__get_packed_size);
container_buffer = malloc(message_container__get_packed_size(&responseContainer));
}
size_t container_size = message_container__pack(&responseContainer, container_buffer);
free(responseContainer.data.data);
SendChars((char*)container_buffer, container_size);
}
void AbortJob(char *Msg)
{
ReturnCode retcode;
retcode = JobSuccess;
JobMessageStruc Message;
PrintMessageStruc *PrtMessage = (PrintMessageStruc *)Message.messageData;
Message.messageId = Abort;
PrtMessage->messageId = PrintSystemFailure;
strcpy(PrtMessage->messageData,Msg);
Message.msglen = 10;
if (JobmsgQ != NULL)
Mailbox_post(JobmsgQ , &Message, BIOS_NO_WAIT);
}
void StartJob(void *JobDetails)
{
JobMessageStruc Message;
Message.messageId = JobRequestMsg;
Message.msglen = MAX_MSG_LEN;
if (JobmsgQ != NULL)
Mailbox_post(JobmsgQ , &Message, BIOS_NO_WAIT);
}
/******************************************************************************
* ======== messageTsk ========
* Task for this function is created statically. See the project's .cfg file.
* this message task is created statically in system initialization,
******************************************************************************/
Void jobTask(UArg arg0, UArg arg1)
{
JobMessageStruc Message;
//char str[60];
//uint16_t length;
//Clock_setTimeout(HostKAClock, 1000);
//Clock_start(HostKAClock);
JobmsgQ = Mailbox_create(MAX_MSG_LEN, 20, NULL,NULL);
while(1)
{
Mailbox_pend(JobmsgQ , &Message, BIOS_WAIT_FOREVER);
switch (Message.messageId)
{
case JobRequestMsg:
/*ValidateState (CurrentJob);
break;
case ValidationResultsOk:*/
PrepareState (CurrentJob);
break;
case ValidationResultsFail:
//send message data as a validation error message to host
ExitState(Message.messageData);
break;
case PreparationResultsOk:
PrintState(CurrentJob);
break;
case PreparationResultsFail:
//send message data as a validation error message to host
ExitState(Message.messageData);
break;
case PrintMessage:
//send message data as a validation error message to host
PrintSTMMsgHandler(&Message);
break;
case PrintingResultsOk:
//CleanState(CurrentJob);
break;
case PrintingResultsFail:
//send message data as a validation error message to host
ExitState(Message.messageData);
break;
case CleaningResultsOk:
//send message data as a validation error message to host
ExitState(Message.messageData);
break;
case CleaningResultsFail:
//send message data as a validation error message to host
ExitState(Message.messageData);
break;
case SystemFailure:
//send message data as a validation error message to host
ExitState(Message.messageData);
break;
case Abort:
PrintSTMMsgHandler(&Message);
break;
default:
break;
}
}
}
|