aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_print.c
blob: 38ae19f36ea722f7eb7eed657e1ee0ede73319e1 (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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/************************************************************************************************************************
 * Heaters_print.c
 **************************************************************************************************************************/

////////////////////////////////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 "include.h"

#include "PMR/Hardware/HardwarePidControlType.pb-c.h"
#include "PMR/Hardware/HardwarePidControl.pb-c.h"
#include "PMR/common/MessageContainer.pb-c.h"

#include "../control/control.h"
#include "../control/pidalgo.h"
#include "include.h"
#include <driverlib/timer.h>
#include <DataDef.h>
#include <inc/hw_ints.h>
#include <inc/hw_memmap.h>

#include "heaters.h"
#include "Drivers/Heater/Heater.h"
#include "Drivers/Heater/TemperatureSensor.h"

#include "StateMachines/Printing/PrintingSTM.h"


/******************** Data Structures  ********************************************/
typedef enum
{
    HeatersControl,
}HeatersControlMessages;

typedef struct HeatersControlMessage{
    uint16_t messageId;
    uint16_t msglen;
    uint32_t tick;
    uint8_t messageData[20];
}HeatersControlMessageStruc;

/******************** GLOBAL PARAMETERS  ********************************************/
HeaterCommand          HeaterCmd[MAX_HEATERS_NUM] = {};
uint32_t    ControlIdtoHeaterId [MAX_HEATERS_NUM] = {0xFF};
uint32_t    DryerHeaterMaxTempControl = 0xFF;
uint32_t    DryerInternalPT100Id = ANALOG_DRYER_TEMP2;

bool HeatersRestart = false;

uint32_t OutputProportionalSingleStep = 0;  //A/C Heaters step size from one decision point to another - in cpu clocks. 120000 = 1 millisecod
uint32_t  Heater_timerBase = TIMER2_BASE;        //Timer handle
uint32_t OutputProportionalCycleTime = 0; //A/C Heaters Cycle time in milliseconds - one for all heaters
char     TimeSliceAllocation[MAX_TIMESLICES] = {0xFF};
int     DCTimeSliceAllocation[MAX_HEATERS_NUM] = {0};

bool TimerActivated = false;

Mailbox_Handle          HeatersControlMsgQ = NULL;
bool InitialHeating = true;
bool DCInitialHeating[MAX_HEATERS_NUM] = {true};
bool    HeaterReady[MAX_HEATERS_NUM] = {true};

/******************** FUNCTIONS  ********************************************/
uint32_t HeaterMaxTempCBFunction(uint32_t IfIndex, uint32_t readValue);
uint32_t HeaterControlCBFunction(uint32_t deviceID, uint32_t readValue);
uint32_t DCHeaterControlCBFunction(uint32_t IfIndex, uint32_t readValue);
uint32_t PrepareHeater(int HeaterId, uint32_t SetTemperatue);
void HeatersStartControlTimer (void);

//**********************************************************************
//******************** CODE  *******************************************/
//**********************************************************************

void HeatersControlInit(void)
{
    int i;
    HeatersControlMsgQ = Mailbox_create(sizeof(HeatersControlMessageStruc), 20, NULL,NULL);

    ROM_TimerConfigure(Heater_timerBase, TIMER_CFG_PERIODIC);   // 32 bits Timer
    ROM_IntEnable(INT_TIMER2A);
    ROM_TimerIntEnable(Heater_timerBase, TIMER_TIMA_TIMEOUT);

    for (i = 0; i < MAX_HEATERS_NUM; i++)
    {
        DCInitialHeating[i] = true;
        HeaterReady[i] = true;
    }
    HeatersRestart = false;


    return;
}
void HeatersControlStop(void)
{
    Report("HeatersControlStop ", __FILE__,__LINE__,0, RpMessage, 0, 0);
    TimerActivated = false;
    HeatersRestart = false;
}
#warning there is a need to separate the AC and DC heaters preparation!
void HeatersControlStart(void)
{
    Report("HeatersControlStart ", __FILE__,__LINE__,0, RpMessage, 0, 0);
    HeatersRestart = true;
    HeatersStartControlTimer();
}

uint32_t HeatersEnd(void)
{
    int i,status = OK;
    HeatersControlStop();
    for (i=0;i<MAX_HEATERS_NUM;i++)
    {
        if (i > HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2) //DC Heaters
        {
            status |= RemoveControlCallback(ControlIdtoHeaterId [i], DCHeaterControlCBFunction);
        }
        else if (i < HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2) //AC Heaters
        {
            status |=RemoveControlCallback(ControlIdtoHeaterId [i] ,HeaterControlCBFunction);
        }
        if (i == HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w) //DC Heaters
            status |=RemoveControlCallback(DryerHeaterMaxTempControl ,HeaterMaxTempCBFunction);
        DeActivateHeater(i);
    }
    return status;

}


void HeatersStartControlTimer (void)
{
    if (TimerActivated == true)
        return;
    TimerActivated = true;
    ROM_TimerConfigure(Heater_timerBase, TIMER_CFG_PERIODIC);   // 32 bits Timer
    ROM_TimerLoadSet(Heater_timerBase, TIMER_A,OutputProportionalSingleStep/*twelve millisecond???*/);
    ROM_IntEnable(INT_TIMER2A);
    ROM_TimerIntEnable(Heater_timerBase, TIMER_TIMA_TIMEOUT);
    TimerEnable(Heater_timerBase, TIMER_A);
    Report("HeatersStartControlTimer ", __FILE__,__LINE__,0, RpMessage, 0, 0);
    return;
}
bool HeaterReducedPowerStarted = false;
/**************************************************************************************
 * HeaterCommandRequestMessage
 * called by: Communication from host
 * initialized all global data
 *************************************************************************************/
uint32_t  HeaterCommandRequestMessage(int HeaterId, bool OnOff, int Temperature)
{
    //uint32_t status = NOT_SUPPORTED;
    //MessageContainer responseContainer;
    if (HeaterControl[HeaterId].configured == false)
    {
        LOG_ERROR (HeaterId,"HeaterControl not configured");
        return ERROR;
    }
    if (HeaterReducedPowerStarted == false)
    {
        HeaterReducedPowerStarted = true;
        //AddControlCallback( HeaterBalanceLoadControlCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HeaterId2PT100Id[HeaterId],0);
    }

    if (HeaterId< MAX_HEATERS_NUM)
    {
        if (HeaterControl[HeaterId].id != HeaterId) // heater configuration missing
        {
            LOG_ERROR (HeaterId, "Heater is not yet configured");
            return ERROR;
        }
        HeaterCmd[HeaterId].heaterid = HeaterId;
        HeaterCmd[HeaterId].command = OnOff;
        HeaterCmd[HeaterId].targettemperatue = Temperature*100;
    }
    bool HeaterState = GetHeaterState(HeaterId);

    if ((HeaterState == HEATER_OFF)&& (OnOff == HEATER_ON)) //start heating
    {
        //set the heater control parameters
        //set the target operation temperature
        //start the control
        PrepareHeater(HeaterId,Temperature);  //prepare the heaters control info
        //set the power balance handler (if not set yet)
        // if the heater is off (?) start it.
        ActivateHeater(HeaterId);
        //set the heater operation mode to fast heating - depended on the current temperature
        // timers are prepared but not started yet!!! only when the system is hot.

    }
    else if ((HeaterState == HEATER_ON)&& (OnOff == HEATER_ON)) //set temperature
    {
        HeaterPIDConfig[HeaterId].m_SetParam = Temperature;
        // if the heater is not on return (?).
        //set the target operation temperature
        //set the heater operation mode to fast heating - depended on the current temperature
    }
    else if ((HeaterState == HEATER_ON)&& (OnOff == HEATER_OFF)) //stop heating
    {
       //turn off the heater
        //stop the control
        HeaterPIDConfig[HeaterId].m_SetParam  = HeaterId;
    }
    else
    {
        LOG_ERROR (OnOff, "unsupported command id");
        return ERROR;
    }

    return OK;
}
/*
 * PrepareHeater
 * called by: Communication from host
 * initialized all global data
 */
uint32_t PrepareHeater(int HeaterId, uint32_t SetTemperatue)
{
    assert(HeaterId < MAX_HEATERS_NUM);
    assert(SetTemperatue < MAX_HEATERS_TEMPERATURE);

    //start thread control for all motors
    HeaterPIDConfig[HeaterId].m_params.MAX = HeaterControl[HeaterId].outputproportionalpowerlimit;
    HeaterPIDConfig[HeaterId].m_params.MIN = .0;
    HeaterPIDConfig[HeaterId].m_params.Kd = HeaterControl[HeaterId].kd;
    HeaterPIDConfig[HeaterId].m_params.Kp = HeaterControl[HeaterId].kp;
    HeaterPIDConfig[HeaterId].m_params.Ki = HeaterControl[HeaterId].ki;
    HeaterPIDConfig[HeaterId].m_params.dt = 0.1;
    HeaterPIDConfig[HeaterId].m_calculatedError = 0;
    HeaterPIDConfig[HeaterId].m_integral = 0;
    HeaterPIDConfig[HeaterId].m_isEnabled = true;
    HeaterPIDConfig[HeaterId].m_isReady = true;
    HeaterPIDConfig[HeaterId].m_mesuredParam = 0;
    HeaterPIDConfig[HeaterId].m_preError = 0;
    HeaterPIDConfig[HeaterId].m_SetParam = SetTemperatue*100;//need to update SetParams on presegment stage
    if (HeaterId > HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2) //DC Heaters
    {
        ControlIdtoHeaterId [HeaterId] = AddControlCallback( DCHeaterControlCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HeaterId2PT100Id[HeaterId],0);
    }
    else if (HeaterId < HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2) //AC Heaters
    {
        ControlIdtoHeaterId [HeaterId] = AddControlCallback( HeaterControlCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HeaterId2PT100Id[HeaterId],0);
    }
    HeaterReady[HeaterId] = false;
    if (HeaterId == HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w) //Dryer Heaters
        DryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),DryerInternalPT100Id,0);


    return OK;
}
void HeaterPrepareReady(void)
{
    int i;
    for (i=0;i<MAX_HEATERS_NUM;i++)
    {
        if (HeaterReady[i] == false)
        {
            return; //not all configured heaters are ready
        }
    }
    PrepareReady(Module_Heaters,ModuleDone);
}
/*
 * HeaterControlCBFunction
 * called by: Communication from host
 * initialized all global data
 */
//DryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction
uint32_t HeaterMaxTempCBFunction(uint32_t IfIndex, uint32_t readValue)
{
    int index=MAX_HEATERS_NUM;
    /*char str[100];
    uint8_t len = 0;*/
    if (IfIndex>>8 != IfTypeHeaters)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;
    if (index != HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w) //AC Heaters
    {
        LOG_ERROR (IfIndex, "Wrong  Interface ");
        return 0xFFFFFFFF;
    }
    if (readValue >= HeaterControl[index].sensormaxvalue)
    {
        LOG_ERROR (readValue, "Heater Over the max temperature, turned off");
        DeActivateHeater(index);
        return OK;
    }
    if (readValue <= (HeaterControl[index].sensormaxvalue-HeaterControl[index].sensorminvalue))
    {
        if (HeaterControl[index].sensorminvalue > 0)
        {
            ActivateHeater(index);
        }
        LOG_ERROR (readValue, "Heater Cooled Off max temperature, turned on");
        return OK;
    }
}
uint32_t HeaterControlCBFunction(uint32_t IfIndex, uint32_t readValue)
{
    int index=MAX_HEATERS_NUM;
    /*char str[100];
    uint8_t len = 0;*/
    if (IfIndex>>8 != IfTypeHeaters)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;
    if (index >= HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2) //AC Heaters
    {
        LOG_ERROR (IfIndex, "Wrong  Interface ");
        return 0xFFFFFFFF;
    }
    if (HeaterCmd[index].targettemperatue == 0)
    {
        LOG_ERROR (0, "unconfigured");
        return ERROR;
    }
    static uint32_t Temperature[2] = {0};
    if (index<2)
        Temperature[index] =  readValue;
    /*HeatingTestSendResonse(0, false,GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w),GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1),
                           Temperature[0],Temperature[1],
                           HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w].m_calculatedError, HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1].m_calculatedError,"Standard");
*/
    // check if the read value is within the proportional band
    if (InitialHeating)
    {
        if (readValue > ((HeaterCmd[index].targettemperatue * (100+HeaterControl[index].outputproportionalband))/100))
                {
                    DeActivateHeater(index);
                    //Heaters OFF until coming into the proportional band
                    /*
                    len = usnprintf(str, 100, "\r\n HeaterControlCBFunction devId %d temp %d over proportional band 1.0%d of set temp %d  "
                                    ,index, readValue,HeaterControl[index].outputproportionalband,HeaterCmd[index].targettemperatue);
                    Report(str, __FILE__,__LINE__,0, RpMessage, index, deviceID);
                    */
                    return OK;
                }
        if (readValue < ((HeaterCmd[index].targettemperatue * (100-HeaterControl[index].outputproportionalband))/100))
                {
                    /*
                    len = usnprintf(str, 100, "\r\n HeaterControlCBFunction devId %d temp %d below proportional band %d of set temp %d  "
                                    ,index, readValue,HeaterControl[index].outputproportionalband,HeaterCmd[index].targettemperatue);
                    Report(str, __FILE__,__LINE__,0, RpMessage, index, deviceID);
                            //Heaters ON until coming into the proportional band
                    */
                    return OK;
                }
        else
        {
            InitialHeating = false;
            HeatersControlStart();
            HeaterReady[index] = true;
            HeaterPrepareReady();
            HeatingTestSendResonse(0, false,GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w),GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1),
                               Temperature[0],Temperature[1],
                               HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w].m_calculatedError, HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1].m_calculatedError,"End Initial Heating");

        }
    }
    if(HeaterPIDConfig[index].m_isEnabled && (HeaterPIDConfig[index].m_SetParam != 0))
    {
        HeaterPIDConfig[index].m_mesuredParam = readValue;
        HeaterPIDConfig[index].m_calculatedError = PIDAlgorithmCalculation(HeaterPIDConfig[index].m_SetParam , HeaterPIDConfig[index].m_mesuredParam,
                                                                              &HeaterPIDConfig[index].m_params,   &HeaterPIDConfig[index].m_preError, &HeaterPIDConfig[index].m_integral);
/*        if (HeaterPIDConfig[index].m_calculatedError >= HeaterPIDConfig[index].m_params.MAX)
        {
            HeaterPIDConfig[index].m_calculatedError = HeaterPIDConfig[index].m_params.MAX;
        }
        if (HeaterPIDConfig[index].m_calculatedError < HeaterPIDConfig[index].m_params.MIN)
        {
            HeaterPIDConfig[index].m_calculatedError = HeaterPIDConfig[index].m_params.MIN;
        }
*/
        HeaterRecalculateSharedHeatersParams(index, HeaterPIDConfig[index].m_calculatedError);

        //SetMotorFreq (index, HeaterControl[index].m_calculatedError);
    }


 return OK;
}
/*
 * DCHeaterControlCBFunction
 * called by: Communication from host
 * initialized all global data
 */

uint32_t DCHeaterControlCBFunction(uint32_t IfIndex, uint32_t readValue)
{
    int index=MAX_HEATERS_NUM;
    /*char str[100];
    uint8_t len = 0;*/
    if (IfIndex>>8 != IfTypeHeaters)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;
    if (index <= HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2) //AC Heaters
    {
        LOG_ERROR (IfIndex, "Wrong  Interface ");
        return 0xFFFFFFFF;
    }
    if (HeaterCmd[index].targettemperatue == 0)
    {
        LOG_ERROR (0, "unconfigured");
        return ERROR;
    }
    /*static uint32_t Temperature[2] = {0};
    if (index<2)
        Temperature[index] =  readValue;
    HeatingTestSendResonse(0, false,GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w),GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1),
                           Temperature[0],Temperature[1],
                           HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w].m_calculatedError, HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1].m_calculatedError,"Standard");
*/
/*
 Data, 189, 2730, 0, False, True, 50, 0, Standard DC
{
Data, 428, 80, 2641, False, False, 3, 4000, Standard DC
{
  "HeaterGroupId": 0,
  "Zone1Temp": 80,
  "Zone2Temp": 2641,
  "Heater1Active": false,
  "Heater2Active": false,
  "Heater1Percentage": 3,
  "Heater2Percentage": 4000,
  "InfoMessage": "Standard DC"
}  void HeatingTestSendResonse(uint32_t status, bool last,bool heater1Active,bool heater2Active, int temperature1, int temperature2,int Heater1Percentage,int Heater2Percentage, char* Message)

}*/
    static int t=0;
    t++;
    if (t >=11)
    {
        HeatingTestSendResonse(0,false,GetHeaterState(index), DCInitialHeating[index],index, readValue,DCTimeSliceAllocation[index],
                           HeaterPIDConfig[index].m_calculatedError,"Standard DC");
        t=0;
    }
    // check if the read value is within the proportional band
    if (DCInitialHeating[index]==true)
    {
        //int temp = ((HeaterCmd[index].targettemperatue * (100+HeaterControl[index].outputproportionalband))/100);
        if (readValue > ((HeaterCmd[index].targettemperatue * (100+HeaterControl[index].outputproportionalband))/100))
                {
                    DeActivateHeater(index);
                    //Heaters OFF until coming into the proportional band
                    return OK;
                }
        if (readValue < ((HeaterCmd[index].targettemperatue * (100-HeaterControl[index].outputproportionalband))/100))
                {
                    return OK;
                }
        else
        {
            DCInitialHeating[index] = false;
            HeatersControlStart();
            HeaterReady[index] = true;
            HeaterPrepareReady();
            HeatingTestSendResonse(0,false,GetHeaterState(index),DCInitialHeating[index],HeaterCmd[index].targettemperatue, readValue,DCTimeSliceAllocation[index],
                               HeaterPIDConfig[index].m_calculatedError,"End Initial Heating");

        }
    }
    if(HeaterPIDConfig[index].m_isEnabled && (HeaterPIDConfig[index].m_SetParam != 0))
    {
        HeaterPIDConfig[index].m_mesuredParam = readValue;
        HeaterPIDConfig[index].m_calculatedError = PIDAlgorithmCalculation(HeaterPIDConfig[index].m_SetParam , HeaterPIDConfig[index].m_mesuredParam,
                                                                              &HeaterPIDConfig[index].m_params,   &HeaterPIDConfig[index].m_preError, &HeaterPIDConfig[index].m_integral);
/*        if (HeaterPIDConfig[index].m_calculatedError >= HeaterPIDConfig[index].m_params.MAX)
        {
            HeaterPIDConfig[index].m_calculatedError = HeaterPIDConfig[index].m_params.MAX;
        }
        if (HeaterPIDConfig[index].m_calculatedError < HeaterPIDConfig[index].m_params.MIN)
        {
            HeaterPIDConfig[index].m_calculatedError = HeaterPIDConfig[index].m_params.MIN;
        }
*/
        HeaterRecalculateHeaterParams(index, (int)(HeaterPIDConfig[index].m_calculatedError*100));

        //SetMotorFreq (index, HeaterControl[index].m_calculatedError);
    }


 return OK;
}

/*
 * EightMilliSecondHeatersInterrupt - a timer based interrupt, that will handle the time sharing between the A/C heaters
 * There is a need to prevent the system from activating all A/C heaters at the same time, due to electrical current problems
 */
int  SliceCounter = 0;
void EightMilliSecondHeatersInterrupt(UArg arg0)
{

    ROM_TimerIntClear(Heater_timerBase, TIMER_TIMA_TIMEOUT);  // Clear the timer interrupt

    ROM_IntMasterDisable();
    HeatersControlMessageStruc Message;
    bool retcode = false;

    //send message to the HeatersControl task
    Message.messageId = HeatersControl;
    Message.tick = UsersysTickGet();
    Message.msglen = sizeof(HeatersControlMessageStruc);
    if (HeatersControlMsgQ != NULL)
        retcode = Mailbox_post(HeatersControlMsgQ , &Message, BIOS_NO_WAIT);

    if (HeatersRestart == true)
    {
        ROM_TimerLoadSet(Heater_timerBase, TIMER_A,OutputProportionalSingleStep/*twelve millisecond???*/);
    }
    else
        TimerDisable(Heater_timerBase, TIMER_A);

    //
    // Enable all interrupts.
    //
    ROM_IntMasterEnable();
    return ;
}


uint32_t HeatersControlLoop(uint32_t tick)
{
    //char str[100];
    //uint8_t len = 0;
    int DcHeaterId;
    /*len = usnprintf(str, 100, "\r\n EightMilliSecondHeatersInterrupt SliceCounter %d Owner %d H1000 %d  H2000 %d"
                    ,SliceCounter,TimeSliceAllocation[SliceCounter],HeatersRestart,NumberOFSlicesInUse);
    Report(str, __FILE__,__LINE__,0, RpMessage, SliceCounter, TimeSliceAllocation[SliceCounter]);
*/
    static bool first = true;
    if (first == true)
    {
        first = false;
        HeatingTestSendResonse(0, false,GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w),GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1),
                           0,0, HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w].m_calculatedError, HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1].m_calculatedError,"First Control");

    }
    if (TimeSliceAllocation[SliceCounter] == HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w)
    {
        //If HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w should be active
        //Activate HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w
        ActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w);
        //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1
        DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1);
        DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2);
    }
    else if (TimeSliceAllocation[SliceCounter] == HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1)
    {
        //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w
        DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w);
        //If HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1 should be active
        //Activate HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1
        ActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1);
        ActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2);
    }
    else
    {
        //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w
        DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w);
        //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1
        DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater200w1);
        DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeater200w2);
    }

    for ( DcHeaterId = HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ1; DcHeaterId<= HARDWARE_PID_CONTROL_TYPE__MixerHeater;DcHeaterId++)
    {
        if (HeaterReady[DcHeaterId] == false)
            continue;
        if (DCTimeSliceAllocation[DcHeaterId] > 0) //heater active
        {
            if (SliceCounter == 0)
                ActivateHeater (DcHeaterId);
            else if (SliceCounter >= DCTimeSliceAllocation[DcHeaterId]) //turn off
                DeActivateHeater (DcHeaterId);
        }
        else
            DeActivateHeater (DcHeaterId);
    }
    //handle the time sharing module
    SliceCounter++;
    if (SliceCounter >= NumberOFSlicesInUse)
        SliceCounter = 0;

    return OK;
}
/******************************************************************************
 *  ======== messageTsk ========
 *  Task for this function is created statically. See the project's .cfg file.
 *  this message task is created statically in system initialization,
 ******************************************************************************/
void HeatersControlTask(UArg arg0, UArg arg1)
{
    HeatersControlMessageStruc Message;
    //char str[60];
    //uint16_t length;
    //Clock_setTimeout(HostKAClock, 1000);
    //Clock_start(HostKAClock);
    HeatersControlInit();

    while(1)
    {
        Mailbox_pend(HeatersControlMsgQ , &Message, BIOS_WAIT_FOREVER);
        switch (Message.messageId)
        {
            case HeatersControl:
                HeatersControlLoop(Message.tick);
                break;
            default:
                break;
        }
    }
}