aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Diagnostics/DiagnosticsHoming.c
blob: fecfa5057d34f93e4fd78049ded0efef52d05154 (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
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
/*
 * DiagnosticsHoming.c

 *
 *  Created on: July 29 2018
 *      Author: shlomo
 */
#include <DataDef.h>
#include "include.h"

#include <inc/hw_ints.h>
#include <Container.h>

#include "diagnostics.h"
#include "drivers/Motors/Motor.h"

#include  <PMR/Diagnostics/DispenserHomingRequest.pb-c.h>
#include  <PMR/Diagnostics/DispenserHomingResponse.pb-c.h>
#include  <PMR/Diagnostics/DispenserAbortHomingRequest.pb-c.h>
#include  <PMR/Diagnostics/DispenserAbortHomingResponse.pb-c.h>

#include  <PMR/Diagnostics/MotorHomingRequest.pb-c.h>
#include  <PMR/Diagnostics/MotorHomingResponse.pb-c.h>
#include  <PMR/Diagnostics/MotorAbortHomingRequest.pb-c.h>
#include  <PMR/Diagnostics/MotorAbortHomingResponse.pb-c.h>

#include "Modules/Control/control.h"

#include "drivers/Motors/Motor.h"
#include "drivers/Valves/Valve.h"
#include "Drivers/I2C_Communication/DAC/blower.h"
#include "drivers/Flash_ram/MCU_E2Prom.h"

#include "ids/ids_ex.h"
#include "General/process.h"

#include "StateMachines/Printing/PrintingSTM.h"

char HomingToken[NUM_OF_MOTORS][36+1]={0};
int HomingCounter[NUM_OF_MOTORS];
uint32_t HomingControlId[NUM_OF_MOTORS];
int NumberOfCycles;

/********************************************************************************
 * Motor Homing
 ********************************************************************************/
//int32_t keepkvalright,keepkvalleft;
uint32_t MotorHomingProgressReport(uint32_t deviceID, uint32_t ReadValue)
{
    MessageContainer responseContainer;
    MotorHomingResponse response = MOTOR_HOMING_RESPONSE__INIT;
    bool last = false;
    response.has_progress = true;
    response.has_maxprogress = true;
    response.maxprogress = 1000;
    if (HomingCounter[deviceID]>=1000)
    {
        last = true;
        SafeRemoveControlCallback(HomingControlId[deviceID],MotorHomingProgressReport);
        Report("MotorHomingProgressReport stopped 1000",__FILE__,__LINE__,deviceID,RpMessage,HomingCounter[deviceID],0);
        HomingControlId[deviceID] = 0xff;
    }
    response.progress = HomingCounter[deviceID]++;
    Report("MotorHomingProgressReport",__FILE__,__LINE__,deviceID,RpMessage,HomingCounter[deviceID],0);
    responseContainer = createContainer(MESSAGE_TYPE__MotorHomingResponse, HomingToken[deviceID], last, &response, &motor_homing_response__pack, &motor_homing_response__get_packed_size);
    responseContainer.has_continuous = true;
    responseContainer.continuous = true;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

    return OK;

}

uint32_t MotorHomingRequestCallback(uint32_t deviceID, uint32_t ReadValue)
{
    MessageContainer responseContainer;
    MotorHomingResponse response = MOTOR_HOMING_RESPONSE__INIT;

    if(deviceID == HARDWARE_MOTOR_TYPE__MOTO_RLOADING)
    {
        MotorSetKvalRun(HARDWARE_MOTOR_TYPE__MOTO_RLOADING, 24);
    }
    if(deviceID == HARDWARE_MOTOR_TYPE__MOTO_LLOADING)
    {
        MotorSetKvalRun(HARDWARE_MOTOR_TYPE__MOTO_LLOADING, 24);
    }
    if (HomingControlId[deviceID] != 0xff)
    {
        Report("MotorHomingProgressReport stopped",__FILE__,__LINE__,deviceID,RpMessage,HomingCounter[deviceID],0);
        RemoveControlCallback(HomingControlId[deviceID],MotorHomingProgressReport);
        HomingControlId[deviceID] = 0xff;
    }
    if (deviceID == HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM)
    {
        MCU_E2PromProgram(EEPROM_STORAGE_DRYER_CYCLES,NumberOfCycles);//it takes two cycles to identify a stop of the arm
    }
    responseContainer = createContainer(MESSAGE_TYPE__MotorHomingResponse, HomingToken[deviceID], true, &response, &motor_homing_response__pack, &motor_homing_response__get_packed_size);
    responseContainer.has_continuous = true;
    responseContainer.continuous = true;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);
    HomingToken[deviceID][0] = 0;

return OK;

}
uint32_t MotorHomingRequestFunc(MessageContainer* requestContainer)
{
    uint32_t numberOfSteps = 0;
    uint32_t LoadArmRounds = (int)dryerbufferlength;
    uint32_t status = FAILED;
    MessageContainer responseContainer;

    MotorHomingRequest* request = motor_homing_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    MotorHomingResponse response = MOTOR_HOMING_RESPONSE__INIT;

    int direction;
    TimerMotors_t MotorId = (TimerMotors_t)request->motortype;
    if (LoadArmRounds <= 2)
            LoadArmRounds = 30;

    int speed = request->speed;
    if (speed == 0) speed = 150;
    if (MotorId <= NUM_OF_MOTORS)
    {
        if ((JobIsActive() == false)&&(isMotorConfigured(MotorId) == true))
        {
            ustrncpy (HomingToken[MotorId], requestContainer->token,36);
            if (request->direction == MOTOR_DIRECTION__Backward) //opening the loading system
            {
                if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID)
                {
                    if (MotorDriverResponse[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID].DriverType == CombinrdMotDriver)//reverse direction for powerstep
                    {
                        direction = DRIER_LID_CLOSE;
                    }
                    else
                    {
                        direction = DRIER_LID_OPEN;
                    }
                    Report("MotorHomingRequestFunc Dryer lid open",__FILE__,MotorDriverResponse[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID].DriverType,DRIER_LID_OPEN,RpMessage,GPI_LS_DRYER_LID_OPEN,0);
                    MotorGotoWithCallback(HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID, direction, GPI_LS_DRYER_LID_OPEN, MotorHomingRequestCallback,4000);
                    status = OK;
                }
                else
                {
                    if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM)
                    {
                        numberOfSteps = MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].pulseperround*LoadArmRounds*MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].microstep*MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].pulleyradius;
                        Report("MotorHomingRequestFunc Set_Load_Arm_To_Start_Position",__FILE__,__LINE__,numberOfSteps,RpMessage,LoadArmRounds,0);
                        MotorMoveToStopper(HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM, (1-MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].directionthreadwize),
                                                        MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].pulseperround/2, MotorHomingRequestCallback,0,1000);
                        NumberOfCycles = 0;

                        //MotorMoveWithCallback(HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM, (1-MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].directionthreadwize),
                        //                                numberOfSteps, MotorHomingRequestCallback,1000);
                        status = OK;
                    }
                    if ( Motor_Id_to_LS_IdDown[MotorId] != MAX_GPI)
                    {
                        if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_RLOADING)
                        {
                            //keepkvalright = MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_RLOADING].kvalrun;
                            MotorSetKvalRun(HARDWARE_MOTOR_TYPE__MOTO_RLOADING, 70);
                        }
                        if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_LLOADING)
                        {
                            //keepkvalleft = MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_LLOADING].kvalrun;
                            MotorSetKvalRun(HARDWARE_MOTOR_TYPE__MOTO_LLOADING, 70);
                        }
                        MotorMovetoLimitSwitch (MotorId,1-MotorsCfg[MotorId].directionthreadwize, speed,  Motor_Id_to_LS_IdUp[MotorId], MotorHomingRequestCallback,30000);
                        status = OK;
                    }

                }
            }
            else // closing the system into normal work
            {
                if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID)
                {
                    if (MotorDriverResponse[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID].DriverType == CombinrdMotDriver) //reverse direction for powerstep
                    {
                        direction = DRIER_LID_OPEN;
                    }
                    else
                    {
                        direction = DRIER_LID_CLOSE;
                    }
                    Report("MotorHomingRequestFunc Dryer lid close",__FILE__,MotorDriverResponse[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID].DriverType,DRIER_LID_CLOSE,RpMessage,GPI_LS_DRYER_LID_CLOSED,0);
                    MotorGotoWithCallback(HARDWARE_MOTOR_TYPE__MOTO_DRYER_LID, direction, GPI_LS_DRYER_LID_CLOSED, MotorHomingRequestCallback,4000);
                    status = OK;
                }
                else
                {
                    if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM)
                    {
                        if (LoadArmRounds <= 2)
                                LoadArmRounds = 30;

                        numberOfSteps = MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].pulseperround*LoadArmRounds*MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].microstep*MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].pulleyradius;
                        numberOfSteps -= 100;
                        NumberOfCycles = LoadArmRounds;
                        Report("MotorHomingRequestFunc Set_Load_Arm_To_Start_Position",__FILE__,__LINE__,numberOfSteps,RpMessage,LoadArmRounds,0);

                        MotorSetMaxSpeed (HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM, MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].pulseperround/4);

                        MotorMoveWithCallback (HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM, MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_DRYER_LOADARM].directionthreadwize,
                                               numberOfSteps,    MotorHomingRequestCallback, 100000);
                        status = OK;
                    }
                    if ( Motor_Id_to_LS_IdUp[MotorId] != MAX_GPI)
                    {
                        if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_RLOADING)
                        {
                            //keepkvalright = MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_RLOADING].kvalrun;
                            MotorSetKvalRun(HARDWARE_MOTOR_TYPE__MOTO_RLOADING, 70);
                        }
                        if(MotorId == HARDWARE_MOTOR_TYPE__MOTO_LLOADING)
                        {
                            //keepkvalleft = MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_LLOADING].kvalrun;
                            MotorSetKvalRun(HARDWARE_MOTOR_TYPE__MOTO_LLOADING, 70);
                        }
                        MotorMovetoLimitSwitch (MotorId,MotorsCfg[MotorId].directionthreadwize, speed,  Motor_Id_to_LS_IdDown[MotorId], MotorHomingRequestCallback,30000);
                        status = OK;
                    }
                }
            }
        }
    }
    if (status == OK)
    {
        HomingControlId[MotorId] = AddControlCallback(NULL, MotorHomingProgressReport,  2*eOneSecond, TemplateDataReadCBFunction,MotorId, 0, 0 );
        HomingCounter[MotorId] = 0;
        Report("MotorHomingProgressReport started",__FILE__,__LINE__,MotorId,RpMessage,HomingControlId[MotorId],0);
    }
    else
    {
        responseContainer.has_error = true;
        responseContainer.error = ERROR_CODE__INVALID_PROCESS_ID;
        responseContainer.errormessage = "Job Active or incorrect motor ID";
        responseContainer = createContainer(MESSAGE_TYPE__MotorHomingResponse,  requestContainer->token, true, &response, &motor_homing_response__pack, &motor_homing_response__get_packed_size);
        responseContainer.has_continuous = true;
        responseContainer.continuous = true;
        uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
        size_t container_size = message_container__pack(&responseContainer, container_buffer);
        my_free(responseContainer.data.data);
        SendChars(container_buffer, container_size);
    }

return OK;
}
uint32_t MotorAbortHomingRequestFunc(MessageContainer* requestContainer)
{

    MessageContainer responseContainer;

    MotorAbortHomingRequest* request = motor_abort_homing_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    MotorAbortHomingResponse response = MOTOR_ABORT_HOMING_RESPONSE__INIT;

    TimerMotors_t MotorId = (TimerMotors_t)request->motortype;
    MotorAbortMovetoLimitSwitch(MotorId);
    MotorHomingRequestCallback(MotorId,0);

    responseContainer = createContainer(MESSAGE_TYPE__MotorAbortHomingResponse,  requestContainer->token, false, &response, &motor_abort_homing_response__pack, &motor_abort_homing_response__get_packed_size);
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

return OK;
}

/********************************************************************************
 * Dispenser Homing
 ********************************************************************************/
uint32_t DispenserHomingProgressReport(uint32_t deviceID, uint32_t ReadValue)
{
    MessageContainer responseContainer;
    DispenserHomingResponse response = DISPENSER_HOMING_RESPONSE__INIT;

    bool last = false;
    response.has_progress = true;
    response.has_maxprogress = true;
    response.maxprogress = 5800000;
    response.progress = IDS_Dispenser_Data[deviceID].consumedinnanolitter;
    uint8_t MotorId = deviceID+HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1;

    responseContainer = createContainer(MESSAGE_TYPE__DispenserHomingResponse, HomingToken[MotorId], last, &response, &dispenser_homing_response__pack, &dispenser_homing_response__get_packed_size);
    Report("DispenserHomingProgressReport",__FILE__,__LINE__,deviceID,RpMessage,IDS_Dispenser_Data[deviceID].consumedinnanolitter,0);
    responseContainer.has_continuous = true;
    responseContainer.continuous = true;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

    return OK;

}
uint32_t DispenserHomingRequestCallback(uint32_t deviceID, uint32_t ReadValue)
{
    MessageContainer responseContainer;
    DispenserHomingResponse response = DISPENSER_HOMING_RESPONSE__INIT;

    uint8_t MotorId = deviceID+HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1;
    if (HomingControlId[MotorId] != 0xff)
    {
        RemoveControlCallback(HomingControlId[MotorId],DispenserHomingProgressReport);
        HomingControlId[MotorId] = 0xff;
        Report("DispenserHomingProgressReport stopped",__FILE__,__LINE__,deviceID,RpMessage,HomingControlId[MotorId],0);
    }

    //close dry air valve in the dispenser
    //Valve_Set((Valves_t) (DispenserId+VALVE_2W_MID_AIR_8), Atm_MidTank_OFF);
    //MotorSetMicroStep(deviceID, MotorsCfg[deviceID].microstep);

    responseContainer = createContainer(MESSAGE_TYPE__DispenserHomingResponse, HomingToken[MotorId], true, &response, &dispenser_homing_response__pack, &dispenser_homing_response__get_packed_size);
    responseContainer.has_continuous = true;
    responseContainer.continuous = true;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);
    HomingToken[MotorId][0] = 0;

return OK;

}
uint32_t DispenserHomingRequestFunc(MessageContainer* requestContainer)
{

    uint32_t status = OK;

    MessageContainer responseContainer;

    DispenserHomingRequest* request = dispenser_homing_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    DispenserHomingResponse response = DISPENSER_HOMING_RESPONSE__INIT;

    TimerMotors_t MotorId = (request->index)+HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1;

    ustrncpy (HomingToken[MotorId], requestContainer->token,36);
    int speed = request->speed;
    if (speed == 0) speed = 500;

    responseContainer = createContainer(MESSAGE_TYPE__DispenserHomingResponse,  requestContainer->token, false, &response, &dispenser_homing_response__pack, &dispenser_homing_response__get_packed_size);
    responseContainer.has_continuous = true;
    responseContainer.continuous = true;
    if (MotorId <= NUM_OF_MOTORS)
    {
        //if ((JobIsActive() == false)&&(isMotorConfigured(MotorId) == true))
        if (isMotorConfigured(MotorId) == true)
        {
            if (request->direction == MOTOR_DIRECTION__Backward) //dispenser homing
                status = IDS_HomeDispenser(request->index,speed,DispenserHomingRequestCallback);
            else //empty dispenser -
                status = IDS_EmptyDispenser(request->index,speed,DispenserHomingRequestCallback);//return ERROR;
            if (status == OK)
            {
                HomingControlId[MotorId] = AddControlCallback(NULL, DispenserHomingProgressReport,  2*eOneSecond, TemplateDataReadCBFunction,request->index, 0, 0 );
                Report("DispenserHomingProgressReport started",__FILE__,__LINE__,request->index,RpMessage,HomingControlId[MotorId],0);
            }
        }
        else
        {
            responseContainer.error = ERROR_CODE__GENERAL_ERROR;
            responseContainer.errormessage = "Invalid Id or dispenser not configured";
        }
    }
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    //USBCDCD_sendData(container_buffer, container_size,10);
    SendChars(container_buffer, container_size);

return OK;
}
uint32_t DispenserAbortHomingRequestFunc(MessageContainer* requestContainer)
{

    MessageContainer responseContainer;

    DispenserAbortHomingRequest* request = dispenser_abort_homing_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    DispenserAbortHomingResponse response = DISPENSER_ABORT_HOMING_RESPONSE__INIT;

    IDS_StopHomeDispenser(request->index);

    TimerMotors_t MotorId = (request->index)+HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1;
    if (HomingControlId[MotorId] != 0xff)
    {
        RemoveControlCallback(HomingControlId[MotorId],DispenserHomingProgressReport);
        HomingControlId[MotorId] = 0xff;
        Report("DispenserHomingProgressReport stopped",__FILE__,__LINE__,request->index,RpMessage,HomingControlId[MotorId],0);
    }
    /*
    TimerMotors_t MotorId = (request->index)+HARDWARE_MOTOR_TYPE__MOTO_DISPENSER_1;
    MotorAbortMovetoLimitSwitch(MotorId);
    //close dry air valve in the dispenser
    Valve_Set((Valves_t) ((request->index)+VALVE_2W_MID_AIR_8), Atm_MidTank_OFF);
*/
    responseContainer = createContainer(MESSAGE_TYPE__DispenserAbortHomingResponse,  requestContainer->token, false, &response, &dispenser_abort_homing_response__pack, &dispenser_abort_homing_response__get_packed_size);
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

return OK;
}