aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_print.c
blob: 2acd8c56f266663b3e3bb2f4efcfb4b170b97bb4 (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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/************************************************************************************************************************
 * 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/Diagnostics/HeaterType.pb-c.h"
#include "PMR/Diagnostics/HeaterState.pb-c.h"
#include "PMR/common/MessageContainer.pb-c.h"

#include "../control/control.h"
#include "../control/pidalgo.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 "Heaters_ex.h"
#include "Drivers/Heater/TemperatureSensor.h"
#include "Drivers/I2C_Communication/DAC/blower.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,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uint32_t    MainDryerHeaterMaxTempControl = 0xFF;
uint32_t    SecondDryerHeaterMaxTempControl = 0xFF;

#define DRYER_AIR_PT100  TEMP_SENSE_ANALOG_DRYER_TEMP1
#define DRYER_MAIN_PT100 TEMP_SENSE_ANALOG_DRYER_TEMP2
#define DRYER_SECONDARY_PT100 TEMP_SENSE_ANALOG_DRYER_TEMP3

uint32_t HeaterId2PT100Id[MAX_HEATERS_NUM] = {DRYER_AIR_PT100,DRYER_MAIN_PT100,DRYER_SECONDARY_PT100,TEMP_SENSE_ANALOG_DYEINGH_TEMP1,TEMP_SENSE_ANALOG_DYEINGH_TEMP2,TEMP_SENSE_ANALOG_DYEINGH_TEMP3,TEMP_SENSE_ANALOG_DYEINGH_TEMP4,TEMP_SENSE_ANALOG_DYEINGH_TEMP5,TEMP_SENSE_ANALOG_MIXCHIP_TEMP,TEMP_SENSE_AN_ENCLOSURETEMP3};
uint32_t    DryerInternalPT100Id = DRYER_AIR_PT100;
bool HeatersRestart = false;

bool HeaterMaxTempFlag[MAX_HEATERS_NUM] = {false,false,false,false,false,false,false,false,false,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);
    HeaterConfigSetSharedHeatersParams(1000,120000*12);

    ROM_TimerConfigure(Heater_timerBase, TIMER_CFG_PERIODIC);   // 32 bits Timer
    ROM_TimerEnable(Heater_timerBase, TIMER_A);
    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)
{
    if (HeatersRestart == true)
    {
        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)
{
    if (HeatersRestart == false)
    {
        Report("HeatersControlStart ", __FILE__,__LINE__,0, RpMessage, 0, 0);
        HeatersRestart = true;
        HeatersStartControlTimer();
    }
}
void LoadHeaterState(HeaterType HeaterType,HeaterState *HeaterState)
{
    HardwarePidControlType HeaterTypeToControlId[HEATER_TYPE__HeaterZone6] = {HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ1,HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ2,HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ3,HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ4,HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ5,HARDWARE_PID_CONTROL_TYPE__HeadHeaterZ6};
    int HeaterId = HeaterTypeToControlId[HeaterType];

    HeaterState->has_heatertype = true;
    HeaterState->heatertype = HeaterType;
    HeaterState->has_setpoint = true;
    HeaterState->setpoint = HeaterCmd[HeaterId].targettemperatue/100;
    HeaterState->has_currentvalue = true;
    HeaterState->currentvalue = TemperatureSensorRead(HeaterId2PT100Id[HeaterId])/100;
    HeaterState->has_isactive = true;
    HeaterState->isactive = GetHeaterState(HeaterId);
    HeaterState->has_isrampingup = true;
    HeaterState->isrampingup = DCInitialHeating[HeaterId];
    HeaterState->has_isinsetpoint = true;
    HeaterState->isinsetpoint = HeaterReady[HeaterId];

    return;
}
uint32_t HeatersSingleHeaterEnd(HardwarePidControlType HeaterId)
{
    int status = OK;
        if (HeaterId >= MAX_AC_HEATERS) //DC Heaters
        {
            if (ControlIdtoHeaterId [HeaterId]!=0xFF)
            {
                status |= RemoveControlCallback(ControlIdtoHeaterId [HeaterId], DCHeaterControlCBFunction);
                ControlIdtoHeaterId [HeaterId]=0xFF;
                HeaterRecalculateHeaterParams(HeaterId, 0);
            }
            DeActivateHeater(HeaterId);
            HeaterReady[HeaterId] = true;
        }
        else if (HeaterId < MAX_AC_HEATERS) //AC Heaters
        {
            if (ControlIdtoHeaterId [HeaterId]!=0xFF)
            {
                status |=RemoveControlCallback(ControlIdtoHeaterId [HeaterId] ,HeaterControlCBFunction);
                HeaterRecalculateSharedHeatersParams(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain, 0);
                HeaterRecalculateSharedHeatersParams(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary, 0);
                ControlIdtoHeaterId [HeaterId]=0xFF;
            }
            if (MainDryerHeaterMaxTempControl)
            {
                status |=RemoveControlCallback(MainDryerHeaterMaxTempControl ,HeaterMaxTempCBFunction);
                MainDryerHeaterMaxTempControl=0xFF;
            }
            if (SecondDryerHeaterMaxTempControl)
            {
                status |=RemoveControlCallback(SecondDryerHeaterMaxTempControl ,HeaterMaxTempCBFunction);
                SecondDryerHeaterMaxTempControl=0xFF;
            }
            DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
            DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
            HeaterReady[HeaterId] = true;
        }

    return status;

}


uint32_t HeatersEnd(void)
{
    HardwarePidControlType i;
    uint32_t status = OK;
    HeatersControlStop();
    for (i=0;i<MAX_HEATERS_NUM;i++)
    {
        status = HeatersSingleHeaterEnd(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_TimerEnable(Heater_timerBase, TIMER_A);
    ROM_IntEnable(INT_TIMER2A);
    ROM_TimerIntEnable(Heater_timerBase, TIMER_TIMA_TIMEOUT);
    Report("HeatersStartControlTimer ", __FILE__,__LINE__,0, RpMessage, 0, 0);
    return;
}
/**************************************************************************************
 * 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;
    }
//    Turn_the_Blower_On();

    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;
    if ( ControlIdtoHeaterId [HeaterId] == 0xFF)
    {
        HeaterState = false;
    }
    else
    {
        HeaterState = HeaterReady[HeaterId];//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
    {
        if (HeaterPIDConfig[HeaterId].m_SetParam < HeaterCmd[HeaterId].targettemperatue)  //#bug 221
        {
            PrepareHeater(HeaterId,Temperature);  //prepare the heaters control info
        }
        HeaterPIDConfig[HeaterId].m_SetParam = HeaterCmd[HeaterId].targettemperatue;
        // 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 (OnOff == HEATER_OFF)//((HeaterState == HEATER_ON)&& (OnOff == HEATER_OFF)) //stop heating
    {
        //stop the control
        HeaterPIDConfig[HeaterId].m_SetParam  = 0;
       //turn off the heater
        HeatersSingleHeaterEnd(HeaterId);
    }
    /*else //heater off, and the heater is not ready yet
    {
        //turn off the heater
         HeatersSingleHeaterEnd(HeaterId);
         //stop the control
         HeaterPIDConfig[HeaterId].m_SetParam  = 0;
    }*/

    return OK;
}
/*
 * PrepareHeater
 * called by: Communication from host
 * initialized all global data
 */
uint32_t PrepareHeater(int HeaterId, uint32_t SetTemperatue)
{
    if(HeaterId >= MAX_HEATERS_NUM)
    {
        LOG_ERROR (HeaterId,"HeaterId too high");
        return ERROR;
    }

    if (SetTemperatue > MAX_HEATERS_TEMPERATURE)
    {
        LOG_ERROR (SetTemperatue,"Control Temperature too high");
        return ERROR;
    }

    //start thread control for all motors
    HeaterPIDConfig[HeaterId].m_params.MAX = HeaterControl[HeaterId].outputproportionalpowerlimit*100;
    HeaterPIDConfig[HeaterId].m_params.MIN = 0.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.01;
    HeaterPIDConfig[HeaterId].m_params.epsilon = 10;
    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 >= MAX_AC_HEATERS) //DC Heaters
    {
        ControlIdtoHeaterId [HeaterId] = AddControlCallback( DCHeaterControlCBFunction, /*eOneSecond*/eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HeaterId2PT100Id[HeaterId],0);
        DCInitialHeating[HeaterId] = true;
        HeaterReady[HeaterId] = false;
    }
    else if (HeaterId == HARDWARE_PID_CONTROL_TYPE__DryerAirTemperature) //AC Heaters
    {
#ifdef DEMO_TEMPERATURE
        ControlIdtoHeaterId [HeaterId] = AddControlCallback( HeaterControlCBFunction, eHundredMillisecond,DemoTemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HARDWARE_PID_CONTROL_TYPE__DryerAirTemperature,0);
        MainDryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,DemoTemperatureSensorRead,(IfTypeHeaters*0x100+HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain),HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain,0);
        SecondDryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,DemoTemperatureSensorRead,(IfTypeHeaters*0x100+HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary),HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary,0);
#else
        ControlIdtoHeaterId [HeaterId] = AddControlCallback( HeaterControlCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),DryerInternalPT100Id,0);
        MainDryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain),HeaterId2PT100Id[HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain],0);
        SecondDryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary),HeaterId2PT100Id[HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary],0);
#endif
        InitialHeating = true;
        HeaterReady[HeaterId] = false;
    }
/*    if (HeaterId == HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain) //Dryer Heaters
    {
        MainDryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HeaterId2PT100Id[HeaterId],0);
    }
    if (HeaterId == HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary) //Dryer Heaters
    {
        SecondDryerHeaterMaxTempControl = AddControlCallback( HeaterMaxTempCBFunction, eHundredMillisecond,TemperatureSensorRead,(IfTypeHeaters*0x100+HeaterId),HeaterId2PT100Id[HeaterId],0);
    }
*/

    return OK;
}
bool HeaterCheckReady(void)
{
    int i;
    for (i=0;i<MAX_HEATERS_NUM;i++)
    {
        if (HeaterReady[i] == false)
        {
            return false; //not all configured heaters are ready
        }
    }
    return true;
}
void HeaterPrepareReady(void)
{
    int i;
    if (GetHeatersPrepareWaiting() == false)
    {
        return;
    }
    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
 */
bool HeaterGetOverTemperatureState(uint8_t HeaterId)
{
    if (HeaterId > MAX_HEATERS_NUM) return false;
    return HeaterMaxTempFlag[HeaterId];
}
uint32_t MainPT100Read = 0,SecondaryPT100Read = 0;
#ifndef max
    #define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
    #define min(a,b) ((a) < (b) ? (a) : (b))
#endif
uint32_t HeaterMaxTempCBFunction(uint32_t IfIndex, uint32_t readValue)
{
    int index=MAX_HEATERS_NUM;
    uint32_t MaxreadValue = max (MainPT100Read,SecondaryPT100Read);
    uint32_t MinreadValue = min (MainPT100Read,SecondaryPT100Read);
    /*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__DryerHeaterMain)&&(index != HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary)) //AC Heaters
    {
        LOG_ERROR (IfIndex, "Wrong  Interface ");
        return 0xFFFFFFFF;
    }
    if (index == HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain)
    {
        MainPT100Read = readValue;
    }
    if (index == HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary)
    {
        SecondaryPT100Read = readValue;
    }
    if ((MaxreadValue/100) >= HeaterControl[index].sensormaxvalue)
    {
        if (HeaterMaxTempFlag[index] == false)
        {
            LOG_ERROR (MaxreadValue/100, "Heater Over the max temperature, turned off");
        }
        DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
        HeaterMaxTempFlag[HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain] = true;
        DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
        HeaterMaxTempFlag[HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary] = true;
        return OK;
    }
    if ((MinreadValue/100) <= (HeaterControl[index].sensormaxvalue-HeaterControl[index].sensorminvalue))
    {
        if (HeaterControl[index].sensorminvalue > 0)
        {
            if (HeaterMaxTempFlag[index] == true)
            {
                ActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
                if (InitialHeating)
                {
                    ActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
                }
                LOG_ERROR ((MinreadValue/100), "Heater Cooled Off max temperature, turned on");
            }
            HeaterMaxTempFlag[HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain] = false;
            HeaterMaxTempFlag[HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary] = false;
        }
        return OK;
    }
    return ERROR;
}
uint32_t HeaterControlCBFunction(uint32_t IfIndex, uint32_t readValue)
{
    int index=MAX_HEATERS_NUM;
    double temperror = 0.0, correction = 0.0;
    /*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__DryerAirTemperature) //AC Heaters
    {
        LOG_ERROR (IfIndex, "Wrong  Interface ");
        return 0xFFFFFFFF;
    }
    if (HeaterCmd[index].targettemperatue == 0)
    {
        DeActivateHeater(index);
        //LOG_ERROR (0, "unconfigured");
        return ERROR;
    }
    // check if the read value is within the proportional band
    if (InitialHeating)
    {
        if (readValue > ((HeaterCmd[index].targettemperatue * (100+HeaterControl[index].outputproportionalband))/100))
                {
                    DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
                    DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
                    //Heaters OFF until coming into the proportional band
                    return OK;
                }
        if (readValue < ((HeaterCmd[index].targettemperatue * (100-HeaterControl[index].outputproportionalband))/100))
                {
                    ActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
                    ActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
                    return OK;
                }

        {
            InitialHeating = false;
            DeActivateHeater(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
            HeaterRecalculateSharedHeatersParams(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary, 0);
            HeatersControlStart();
            HeaterReady[index] = true;
            HeaterPrepareReady();
        }
    }
    if(HeaterPIDConfig[index].m_isEnabled && (HeaterPIDConfig[index].m_SetParam != 0))
    {
        HeaterPIDConfig[index].m_mesuredParam = readValue;
//check only for the proportional band limits
        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);
        //correction = HeaterPIDConfig[index].m_calculatedError;
        HeaterRecalculateSharedHeatersParams(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain, (int)(HeaterPIDConfig[index].m_calculatedError));
    }


 return OK;
}
/*
 * DCHeaterControlCBFunction
 * called by: Communication from host
 * initialized all global data
 */
//float error[HARDWARE_PID_CONTROL_TYPE__MixerHeater][100];
//float integral[HARDWARE_PID_CONTROL_TYPE__MixerHeater][100];
//float output[HARDWARE_PID_CONTROL_TYPE__MixerHeater][100];
//int Counter[HARDWARE_PID_CONTROL_TYPE__MixerHeater] = {0,0,0,0,0,0,0};
//char logmsg[HARDWARE_PID_CONTROL_TYPE__MixerHeater][254];

uint32_t DCHeaterControlCBFunction(uint32_t IfIndex, uint32_t readValue)
{
    int index=MAX_HEATERS_NUM;
    int len;
    /*char str[100];
    uint8_t len = 0;*/
    if (IfIndex>>8 != IfTypeHeaters)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;
    if (index < MAX_AC_HEATERS) //AC Heaters
    {
        LOG_ERROR (IfIndex, "Wrong  Interface ");
        return 0xFFFFFFFF;
    }
    if (HeaterCmd[index].targettemperatue == 0)
    {
        DeActivateHeater(index);
        //LOG_ERROR (index, "unconfigured");
        return ERROR;
    }
/*
    HeatingTestSendResonse(0, false,GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain),GetHeaterState(HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary),
                           Temperature[0],Temperature[1],
                           HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain].m_calculatedError, HeaterPIDConfig[HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary].m_calculatedError,"Standard");
*/
    // 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
                    //Report("DC HEATER Over temperature ", __FILE__,__LINE__,index, RpMessage, readValue, 0);
                    return OK;
                }
        if (readValue < ((HeaterCmd[index].targettemperatue * (100-HeaterControl[index].outputproportionalband))/100))
                {
                    ActivateHeater(index);
                    //Heaters OFF until coming into the proportional band
                    //Report("DC HEATER Under temperature ", __FILE__,__LINE__,index, RpMessage, readValue, 0);
                    return OK;
                }

        {
            DCInitialHeating[index] = false;
            HeatersControlStart();
            HeaterReady[index] = true;
            HeaterPrepareReady();
        }
    }
    if(HeaterPIDConfig[index].m_isEnabled && (HeaterPIDConfig[index].m_SetParam != 0))
    {
        /*if (readValue < ((HeaterCmd[index].targettemperatue * (100-HeaterControl[index].outputproportionalband))/100)) //below proportional band
        {
            HeaterRecalculateHeaterParams(index, 100);
        }
        else if (readValue > ((HeaterCmd[index].targettemperatue * (100+HeaterControl[index].outputproportionalband))/100))
        {
            HeaterRecalculateHeaterParams(index, 0);
        }*/

//check only for the proportional band limits
        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);

//        error[index][Counter[index]]    = HeaterPIDConfig[index].m_preError;
//        integral[index][Counter[index]] = HeaterPIDConfig[index].m_integral;
//        output[index][Counter[index]]   = HeaterPIDConfig[index].m_calculatedError;
//        if (Counter[index]++ >=100)
//            Counter[index] = 0;
//        len = usnprintf(logmsg[index], 254, "PID%d: Temp %d Integral %d Output %d ",index,(int)HeaterPIDConfig[index].m_mesuredParam ,(int)HeaterPIDConfig[index].m_integral,(int)HeaterPIDConfig[index].m_calculatedError);
//        Report(logmsg[index],__FILE__,__LINE__,index,RpWarning,index,  Counter[index]);

        //#warning PID is now only proportional (above)
        HeaterRecalculateHeaterParams(index, (int)(HeaterPIDConfig[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);
    }
    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;
    }
    if (InitialHeating == false)
    {
        if (TimeSliceAllocation[SliceCounter] == HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain)
        {
            if (HeaterMaxTempFlag[HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain] == false)
            {
                //If HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain should be active
                //Activate HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain
                ActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
                //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary
                DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
            }
        }
        else if (TimeSliceAllocation[SliceCounter] == HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary)
        {
            if (HeaterMaxTempFlag[HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary] == false)
            {
                //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain
                DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
                //If HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary should be active
                //Activate HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary
                ActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
            }
        }
        else
        {
                //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain
                DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeaterMain);
                //DeActivate HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary
                DeActivateHeater (HARDWARE_PID_CONTROL_TYPE__DryerHeaterSecondary);
        }
    }
    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)
            {
                if (HeaterMaxTempFlag[DcHeaterId] == false)
                {
                    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;
        }
    }
}