aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Thread/Thread_print.c
blob: 0272255a5768911002c4508709f3d985160351db (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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<UserControl x:Class="Tango.MachineStudio.Common.Controls.MdiContainerControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:local="clr-namespace:Tango.MachineStudio.Common.Controls"
             xmlns:converters="clr-namespace:Tango.MachineStudio.Common.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <converters:PointToMarginConverter x:Key="PointToMarginConverter"></converters:PointToMarginConverter>
    </UserControl.Resources>
    
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},Path=ItemsSource}" x:Name="itemsControl">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas x:Name="canvas"  HorizontalAlignment="Left" VerticalAlignment="Top"></Canvas>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="FrameworkElement">
                        <Setter Property="Canvas.Left" Value="{Binding Location.X}"></Setter>
                        <Setter Property="Canvas.Top" Value="{Binding Location.Y}"></Setter>
                        <EventSetter Event="PreviewMouseDown" Handler="OnControlMouseDown"></EventSetter>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="277*"/>
                                </Grid.RowDefinitions>

                                <Grid Background="{StaticResource AccentColorBrush}">
                                    <Thumb Opacity="0" DragDelta="OnControlDragging" Tag="{Binding}"></Thumb>
                                    <DockPanel LastChildFill="False">
                                        <StackPanel IsHitTestVisible="False" DockPanel.Dock="Left" VerticalAlignment="Center" Orientation="Horizontal" Margin="5 0 0 0">
                                            <materialDesign:PackIcon Kind="{Binding Icon}" Foreground="White"></materialDesign:PackIcon>
                                            <TextBlock Margin="5 0 0 0" VerticalAlignment="Center" Foreground="White" Text="{Binding Header}"></TextBlock>
                                        </StackPanel>

                                        <StackPanel DockPanel.Dock="Right" VerticalAlignment="Center" Orientation="Horizontal">
                                            <Button Foreground="White" Style="{StaticResource MaterialDesignFlatButton}" ToolTip="Close" Width="24" Height="24" Padding="0" Click="OnControlClosed" Tag="{Binding}">
                                                <materialDesign:PackIcon Kind="WindowClose" />
                                            </Button>
                                        </StackPanel>
                                    </DockPanel>
                                </Grid>

                                <Grid Grid.Row="1" MinWidth="100" MinHeight="100">
                                    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                                        <ContentPresenter Content="{Binding View}"></ContentPresenter>
                                    </ScrollViewer>

                                    <Thumb HorizontalAlignment="Right" VerticalAlignment="Stretch" Opacity="0" Width="5" Cursor="SizeWE" DragDelta="OnControlSizeRight" Tag="{Binding}"></Thumb>
                                    <Thumb HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Opacity="0" Height="5" Cursor="SizeNS" DragDelta="OnControlSizeBottom" Tag="{Binding}"></Thumb>
                                    <Thumb HorizontalAlignment="Right" VerticalAlignment="Bottom" Opacity="0" Height="10" Width="10" Cursor="SizeNWSE" DragDelta="OnControlResize" Tag="{Binding}"></Thumb>
                                </Grid>
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>    
    </Grid>
</UserControl>
>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
/************************************************************************************************************************
 * Thread_print.c
 * Printing module is responsible for :
     * operating diffrent winding algorithms with predefined parameters from the UI
     * operating the dispensers according to predefined dispensing rate from the UI
 **************************************************************************************************************************/
#include "include.h"
#include "thread.h"
#include "thread_ex.h"
#include "../control/control.h"
#include "../control/pidalgo.h"
#include "PMR/Hardware/HardwareMotor.pb-c.h"
#include "PMR/Hardware/HardwareMotorType.pb-c.h"
#include "PMR/Hardware/HardwareDancerType.pb-c.h"
#include "PMR/Printing/JobSegment.pb-c.h"
#include "PMR/Printing/JobTicket.pb-c.h"
#include  <PMR/Diagnostics/EventType.pb-c.h>

#include <utils/ustdlib.h>

#include "StateMachines/Printing/PrintingSTM.h"

#include "drivers/Motors/Motor.h"
//#include "drivers/SSI_Comm/ssi_comm.h"
#include "drivers/SSI_Comm/Dancer/Dancer.h"
#include "drivers/Heater/TemperatureSensor.h"
#include "drivers/Heater/Heater.h"
#include "drivers/Motors/Motor.h"
#include "drivers/FPGA/FPGA_GPIO/FPGA_GPIO.h"
#include "modules/heaters/heaters.h"
#include "modules/General/process.h"
#include "Modules/AlarmHandling/AlarmHandling.h"
#include "Control/MillisecTask.h"

////////////////////////////////State machine operation////////////////////////////////////
//the state machine operation is used to operate in runtime correct profile flow execution
//by recieved esign flow of the user from the UI
///////////////////////////////////////////////////////////////////////////////////////////

uint32_t CurrentControlledSpeed[MAX_THREAD_MOTORS_NUM] = {0};

TimerMotors_t ThreadMotorIdToMotorId[MAX_THREAD_MOTORS_NUM] = {HARDWARE_MOTOR_TYPE__MOTO_RDRIVING,HARDWARE_MOTOR_TYPE__MOTO_DRYER_DRIVING,HARDWARE_MOTOR_TYPE__MOTO_LDRIVING,HARDWARE_MOTOR_TYPE__MOTO_WINDER,HARDWARE_MOTOR_TYPE__MOTO_SCREW};
HardwareDancerType ThreadMotorIdToDancerId[MAX_THREAD_MOTORS_NUM] = {FEEDER_DANCER,NUM_OF_DANCERS,POOLER_DANCER,WINDER_DANCER,NUM_OF_DANCERS};
uint32_t    ControlIdtoMotorId [MAX_THREAD_MOTORS_NUM] = {0xFF};
uint32_t    SpeedControlId=0xFF;

double DancerError[NUM_OF_DANCERS] = {0.0};
int OriginalMotorSpd_2PPS[MAX_THREAD_MOTORS_NUM] = {0};
uint32_t JobCounter = 0;
typedef struct
{
    bool                m_isEnabled;
    int32_t             m_SetParam;
    float               m_mesuredParam;
    float               m_preError;
    float               m_integral;
    float               m_calculatedError;
    bool                m_isReady;
    PID_Config_Params   m_params;
}MotorControlConfig_t;

MotorControlConfig_t MotorControlConfig[MAX_THREAD_MOTORS_NUM];
uint32_t DeviceId2Motor[MAX_THREAD_MOTORS_NUM];

uint32_t PreviousPosition = 0, CurrentPosition = 0;
double totalLength = 0.0;
double CurrentRequestedLength = 0.0;
double CurrentProcessedLength = 0.0;
double TotalProcessedLength = 0.0;
double LengthCalculationMultiplier;
bool PrepareState = false;
int CurrentSegmentId = 0;
typedef  void (* ProcessedLengthFunc)(void);
ProcessedLengthFunc ProcessedLengthFuncPtr = NULL;
// segment/intersegment/distance to spool finished
void ThreadSegmentEnded(void);
void ThreadInterSegmentEnded(void);
void ThreadDistanceToSpoolEnded(void);
uint32_t ThreadControlCBFunction(uint32_t IfIndex, uint32_t ReadValue);
void SetOriginMotorSpeed(float process_speed);

double KeepNormalizedError = 0;
bool ThreadControlActive = false;
////////////////////////Slow Motor State////////////////////////////////////
//uint32_t ThreadPreSegmentState(void *JobDetails);

////////////////////////////////////////////////////////////////////////////
/********************************************************************
*
*    Name        : GTIME_Delta_Time_Pass
*
*    Parameters  : start_time.
*
*    Return      : time pass from start time
*
*    Description :
*
*********************************************************************/

uint32_t Control_Delta_Position_Pass(uint32_t Current_Read,uint32_t Previous_Read)
{
    uint32_t Time_Pass;
//  #define   MAX_COUNTER 0x3FFF  //14 bits
    #define   MAX_COUNTER 0x3FFFFF  //22 bits


  if (Current_Read < Previous_Read)
    Time_Pass = (MAX_COUNTER - Previous_Read) + Current_Read + 1;
  else
    Time_Pass = Current_Read - Previous_Read;

  return (Time_Pass);
}
/*****************************************************************************************
 *
 *
 *
 *
 *
 *
 * **************************************************************************************/
uint32_t initialpos = 0xFFFF;

void ThreadUpdateProcessLength (double length, void *Funcptr)
{
    CurrentRequestedLength = length*100;//Centimetres
    CurrentProcessedLength = 0;
    //PreviousPosition = 0;
    //CurrentPosition = 0;
    totalLength = 0;
    ProcessedLengthFuncPtr = (ProcessedLengthFunc)Funcptr;
    initialpos = 0xFFFF;
}
double MotorSentData[100] = {0};
uint32_t PosDif[100] = {0};


int MotorDataIndex = 0;

uint32_t ThreadLengthCBFunction(uint32_t IfIndex, uint32_t ReadValue)
{
    uint32_t positionDiff = 0;
    double length = 0.0;
    char str[150];
    int index = MAX_THREAD_MOTORS_NUM;
    if (IfIndex>>8 != IfTypeThread)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;
//    if (CurrentRequestedLength == 0.0)
//        return OK;
    if (index != FEEDER_MOTOR)
    {
        LOG_ERROR (IfIndex, "Wrong Motor");
        return 0xFFFFFFFF;
    }
    CurrentPosition = MotorGetPosition(ThreadMotorIdToMotorId[index]);
//    if (CurrentPosition == 0)
//        return OK; //unusable data
    if (initialpos == 0xFFFF)
    {
        PreviousPosition = CurrentPosition;
        initialpos = 0;
    }
    positionDiff = Control_Delta_Position_Pass(CurrentPosition,PreviousPosition);
    //positionDiff = positionDiff / MotorsCfg[ThreadMotorIdToMotorId[index]].microstep;
    PreviousPosition = CurrentPosition;

    // total length = (position diff / full cycle) * pulley perimeter
    //(positionDiff/pulseperround)*((2*PI*motor_Radius)

    //positionDiff = positionDiff / MotorsCfg[ThreadMotorIdToMotorId[index]].microstep;
    length = (double)(positionDiff)*LengthCalculationMultiplier;
    if (length > 0.1)
    {
        totalLength+=length;
    }

//#warning control disabled
        CurrentProcessedLength+=length;

    PosDif[MotorDataIndex] = CurrentPosition;
    //PosDif[MotorDataIndex] = positionDiff;
    MotorSentData[MotorDataIndex] = length;
    MotorDataIndex+=1;
    if (MotorDataIndex == 99) MotorDataIndex = 0;
    static int pooler_counter = 0;
    pooler_counter++;
    TotalProcessedLength+= (length/100);
    if (pooler_counter%10 == 0)
    {
        if (PrepareState == true)
        {
            //later - add temperatures
             TemperatureListString(str);

            SendJobProgress(0.0,0,false, str);
        }
        else
        {
            SendJobProgress(TotalProcessedLength,0,false, NULL);
        }

    }
    if (CurrentProcessedLength>=CurrentRequestedLength )
    {
        // segment/intersegment/distance to spool finished
        if (ProcessedLengthFuncPtr)
            ProcessedLengthFuncPtr();
    }
return OK;
}
float SpeedSamples[MAX_CONTROL_SAMPLES] = {0};

uint32_t ThreadSpeedControlCBFunction(uint32_t IfIndex, uint32_t ReadValue)
{
    //read value is the dancer angle
    int index=MAX_THREAD_MOTORS_NUM;
    int32_t i, avreageSampleValue = 0;
    //double tempcalcspeed = 0;
    uint32_t calculated_speed;
    float speed = getSensorSpeedData();
    if (IfIndex>>8 != IfTypeThread)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;
    SpeedSamples[MotorSamplePointer[index]] = speed;//(-1 * TranslatedReadValue);
    MotorSamplePointer[index]++;
    if (MotorSamplePointer[index] >= MotorsControl[index].pvinputfilterfactormode)
        MotorSamplePointer[index] = 0;
    for (i=0;i<MotorsControl[index].pvinputfilterfactormode;i++)
        avreageSampleValue += SpeedSamples[i];
    avreageSampleValue = avreageSampleValue / MotorsControl[index].pvinputfilterfactormode;
    if(MotorControlConfig[index].m_isEnabled && (MotorControlConfig[index].m_SetParam != 0))
    {
        MotorControlConfig[index].m_mesuredParam = ReadValue;
        MotorControlConfig[index].m_calculatedError = PIDAlgorithmCalculation(MotorControlConfig[index].m_SetParam , MotorControlConfig[index].m_mesuredParam,
                                                                              &MotorControlConfig[index].m_params,   &MotorControlConfig[index].m_preError, &MotorControlConfig[index].m_integral);
        //SetMotorFreq (index, MotorControlConfig[index].m_calculatedError);
        calculated_speed = (1-MotorControlConfig[index].m_calculatedError)*OriginalMotorSpd_2PPS[index];
        if (abs(calculated_speed-CurrentControlledSpeed[index])>2)
        {
            CurrentControlledSpeed[index] = calculated_speed;
            MotorSetSpeed(ThreadMotorIdToMotorId[index], calculated_speed);
        }
    }
 return OK;
}
uint32_t _speed;
uint32_t ThreadControlSpeedReadFunction(uint32_t IfIndex, uint32_t ReadValue)
{
    int index;
    if (IfIndex>>8 != IfTypeThread)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;

    if(MotorControlConfig[index].m_isEnabled )
    {
        int MotorId = ThreadMotorIdToMotorId[index];
        _speed = MotorGetSpeedFromFPGA_Res ((TimerMotors_t)MotorId);
    }
    return OK;
}
/*double calculatedError[100];
double eNormalizedError[100];
int    readValue[100];
int    TranslatedreadValue[100];
int    AveragereadValue[100];
int    calculatedspeed[100];
int controlIndex = 0;
int32_t KeepReadValue = 0;
void testDancersControl()
{
    int mm20,mm10,mm5,mm2,mm1;
    mm20 = (20*DancerStopActivityLimit[FEEDER_MOTOR])/(DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].maximalmovementmm*2);
    mm2 = mm20/10;
    mm5 = mm20/4;
    mm10 = mm20/2;
    mm1 = mm20/20;
    ThreadControlActive = true;
    SetOriginMotorSpeed(30.0);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint - mm20);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint - mm10);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint - mm5);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint - mm2);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint - mm1);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint + mm1);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint + mm2);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint + mm5);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint + mm10);
    ThreadControlCBFunction(IfTypeThread*0x100+FEEDER_MOTOR, DancersCfg[HARDWARE_DANCER_TYPE__RightDancer].zeropoint + mm20);
    ThreadControlActive = false;
}*/
bool dancerinvalid = false;
uint32_t ThreadControlCBFunction(uint32_t IfIndex, uint32_t ReadValue)
{
//#define MAX_CONTROL_SAMPLES 6
//extern uint32_t MotorSamples[MAX_THREAD_MOTORS_NUM][MAX_CONTROL_SAMPLES];
//extern int MotorSamplePointer[MAX_THREAD_MOTORS_NUM];

    //read value is the dancer angle
    int i,index=MAX_THREAD_MOTORS_NUM;
    int DancerId;
    int32_t TranslatedReadValue, avreageSampleValue = 0;
    //double tempcalcspeed = 0;
    uint32_t calculated_speed;
    double NormalizedError;
    char Message[60];

    if (ThreadControlActive == false)
        return OK;
    if (IfIndex>>8 != IfTypeThread)
    {
        LOG_ERROR (IfIndex, "Wrong  Interface type");
        return 0xFFFFFFFF;
    }
    index = IfIndex&0xFF;

    /*for (i=0;i<MAX_THREAD_MOTORS_NUM;i++)
        if (ControlIdtoMotorId[i] == MotorId)
        {
            index = i;
            break;
        }
    if (index==MAX_THREAD_MOTORS_NUM)
    {
        LOG_ERROR (MotorId, "No motor  for device");
        return 0xFFFFFFFF;
    }*/

    if(MotorControlConfig[index].m_isEnabled )
    {
        DancerId = ThreadMotorIdToDancerId[index];
        if (ReadValue < 10)
        {
            REPORT_MSG(ReadValue, "Dancer value read too small.");
            return OK;
        }
        if (ReadValue == 0x3FFF)
        {
            if (dancerinvalid == false)
            {
                dancerinvalid = true;
                LOG_ERROR(index, "Dancer value invalid.");
            }
            return OK;
        }
        TranslatedReadValue = ReadValue - DancersCfg[DancerId].zeropoint;
        if (index == POOLER_MOTOR)
        {
			//pooler dancer is right sided: data is opposite
            TranslatedReadValue = (-1*TranslatedReadValue);
            JobCounter++;
        }
        //TranslatedReadValue = 0;//test
        MotorSamples[index][MotorSamplePointer[index]] = TranslatedReadValue;//(-1 * TranslatedReadValue);
        MotorSamplePointer[index]++;
        if (MotorSamplePointer[index] >= MotorsControl[index].pvinputfilterfactormode)
            MotorSamplePointer[index] = 0;
        for (i=0;i<MotorsControl[index].pvinputfilterfactormode;i++)
            avreageSampleValue += MotorSamples[index][i];
        avreageSampleValue = avreageSampleValue / MotorsControl[index].pvinputfilterfactormode;

        if (BreakSensorenabled == true)
        {
            if (index == POOLER_MOTOR)
            {
                if (JobCounter > eOneSecond)
                {
                    if (ReadBreakSensor()==ERROR)
                    {
                        //consider applying the debouce parameters later
                        //BreakSensordebouncetimemilli
                        JobEndReason = JOB_THREAD_BREAK;
                        ThreadControlActive = false;
                        SendJobProgress(0.0,0,false, "ReadBreakSensor Error");
                        SegmentReady(Module_Thread,ModuleFail);
                        AlarmHandlingSetAlarm(EVENT_TYPE__ThreadBreak,true);
                        //EndState(CurrentJob,"ReadBreakSensor Error" );
                        LOG_ERROR(index, "ReadBreakSensor Error");
                        return OK;
                    }
                }
            }
        }

        //Stop Execution if the dancer moves too much
        if ((abs(avreageSampleValue)> DancerStopActivityLimit[index])&&(JobCounter > eOneSecond))
        {
            usnprintf(Message, 60, "Dancer %d limit %d value %d Zero %d",DancerId,DancerStopActivityLimit[index],avreageSampleValue,DancersCfg[DancerId].zeropoint);
            //JobAbortedByUser = true;
            ThreadControlActive = false;
            JobEndReason = JOB_DANCER_FAIL;
            SendJobProgress(0.0,0,false, Message);
            //EndState(CurrentJob,Message );
            SegmentReady(Module_Thread,ModuleFail);
            AlarmHandlingSetAlarm(EVENT_TYPE__ThreadTensionControlFailure,true);
            LOG_ERROR (index, "Dancer Failure");
            return OK;
        }
        NormalizedError = avreageSampleValue*NormalizedErrorCoEfficient[index];
        MotorControlConfig[index].m_mesuredParam = NormalizedError;
        DancerError[DancerId] = NormalizedError;
        MotorControlConfig[index].m_calculatedError = PIDAlgorithmCalculation((float)MotorControlConfig[index].m_SetParam , (float)MotorControlConfig[index].m_mesuredParam,
                                                                              &MotorControlConfig[index].m_params,   &MotorControlConfig[index].m_preError, &MotorControlConfig[index].m_integral);
        if (index != FEEDER_MOTOR) //feeder unit handles errors opposite to left unit
        {
            MotorControlConfig[index].m_calculatedError = (-1*MotorControlConfig[index].m_calculatedError);
        }
        else
        {
            //KeepNormalizedError = NormalizedError;
        }
        calculated_speed = (1-MotorControlConfig[index].m_calculatedError)*OriginalMotorSpd_2PPS[index];
        /*if (index == FEEDER_MOTOR)
        {
            if (KeepReadValue != TranslatedReadValue)
            {
                eNormalizedError[controlIndex] = NormalizedError;
                calculatedError[controlIndex] = MotorControlConfig[index].m_calculatedError;
                readValue[controlIndex] = ReadValue;
                TranslatedreadValue[controlIndex] = TranslatedReadValue;
                AveragereadValue[controlIndex] = avreageSampleValue;
                calculatedspeed[controlIndex] = calculated_speed;
                controlIndex++;
                if (controlIndex >= 99) controlIndex = 0;
                KeepReadValue = TranslatedReadValue;
            }
        }*/
        if (abs(calculated_speed-CurrentControlledSpeed[index])>2)
        {
            CurrentControlledSpeed[index] = calculated_speed;
            MotorSetSpeed(ThreadMotorIdToMotorId[index], calculated_speed);
        }
    }

 return OK;
}

//********************************************************************************************************************
uint32_t ThreadGetMotorSpeed(threadMotorsEnum MotorId)
{
    return  CurrentControlledSpeed[MotorId];
}

//********************************************************************************************************************
uint32_t ThreadInitialTestStub(HardwareMotor * request)
{


    //MotorsConfigMessage(request);
     ThreadPrepareState(request);
     ThreadPreSegmentState(request);
    return OK;
}
bool InitialProcess = false;
uint32_t ThreadEmptyCBFunction(uint32_t IfIndex, uint32_t ReadValue)
{
    return OK;
}

//********************************************************************************************************************
 uint32_t ThreadPrepareState(void *JobDetails)
{
    int Motor_i, HW_Motor_Id, Pid_Id;
    CurrentSegmentId = 0;

    JobCounter = 0;
    TotalProcessedLength = 0.0;
    PrepareState = true;
    AlarmHandlingSetAlarm(EVENT_TYPE__ThreadBreak,false);
    AlarmHandlingSetAlarm(EVENT_TYPE__ThreadTensionControlFailure,false);

    //start thread control for all motors
    for (Motor_i = 0;Motor_i < MAX_THREAD_MOTORS_NUM;Motor_i++)
    {
        HW_Motor_Id = ThreadMotorIdToMotorId[Motor_i];
        Pid_Id = Motor_i;/*ThreadMotorIdToControlId[Motor_i];*/
            MotorControlConfig[Motor_i].m_params.MAX = 1;
            MotorControlConfig[Motor_i].m_params.MIN = MotorsControl[Pid_Id].outputproportionalpowerlimit*-1;
            MotorControlConfig[Motor_i].m_params.Kd = MotorsControl[Pid_Id].derivativetime;
            MotorControlConfig[Motor_i].m_params.Kp = MotorsControl[Pid_Id].proportionalgain;
            MotorControlConfig[Motor_i].m_params.Ki = MotorsControl[Pid_Id].integraltime;
            MotorControlConfig[Motor_i].m_params.epsilon = 0.1;
            MotorControlConfig[Motor_i].m_params.dt = 1000;
            MotorControlConfig[Motor_i].m_calculatedError = 0;
            MotorControlConfig[Motor_i].m_integral = 0;
            MotorControlConfig[Motor_i].m_isEnabled = true;
            MotorControlConfig[Motor_i].m_isReady = true;
            MotorControlConfig[Motor_i].m_mesuredParam = 0;
            MotorControlConfig[Motor_i].m_preError = 0;
            MotorControlConfig[Motor_i].m_SetParam = 0;//need to update SetParams on presegment stage

            MotorSetDirection((TimerMotors_t)HW_Motor_Id,MotorsCfg[HW_Motor_Id].directionthreadwize);

            if (Motor_i == FEEDER_MOTOR) // dryer motor is speed controlled. later a speed sensor will be utilized, but for now it will not be controlled
            {
                if (SpeedControlId != 0xFF)
                {
                    RemoveControlCallback(SpeedControlId,ThreadLengthCBFunction);
                    SpeedControlId = 0xFF;
                }
                //SetMotHome(ThreadMotorIdToMotorId[Motor_i]);
                LengthCalculationMultiplier = (MotorsCfg[ThreadMotorIdToMotorId[Motor_i]].pulleyradius*2*PI)/(MotorsCfg[ThreadMotorIdToMotorId[Motor_i]].pulseperround*MotorsCfg[ThreadMotorIdToMotorId[Motor_i]].microstep);
                SpeedControlId = AddControlCallback(ThreadLengthCBFunction, eHundredMillisecond,MotorGetPositionFromFPGA,(IfTypeThread*0x100+Motor_i),ThreadMotorIdToMotorId[Motor_i],Motor_i);
            }
            if (Motor_i == FEEDER_MOTOR) // dryer motor is speed controlled. later a speed sensor will be utilized, but for now it will not be controlled
            {
                if (ControlIdtoMotorId[Motor_i] != 0xFF)
                {
                    RemoveControlCallback(ControlIdtoMotorId[Motor_i],ThreadControlCBFunction);
                    ControlIdtoMotorId[Motor_i] = 0xFF;
                    CurrentControlledSpeed[Motor_i] = 0;
                }
                ControlIdtoMotorId[Motor_i] = AddControlCallback(ThreadControlCBFunction, eOneMillisecond,Control_Read_Dancer_Position,(IfTypeThread*0x100+Motor_i),ThreadMotorIdToDancerId[Motor_i],Motor_i);
                //AddControlCallback(ThreadControlSpeedReadFunction, eHundredMillisecond,MotorGetSpeedFromFPGA,(IfTypeThread*0x100+Motor_i),ThreadMotorIdToMotorId[Motor_i],Motor_i);
            }
            if (Motor_i == POOLER_MOTOR) // dryer motor is speed controlled. later a speed sensor will be utilized, but for now it will n//ot be controlled
            {
                if (ControlIdtoMotorId[Motor_i] != 0xFF)
                {
                    RemoveControlCallback(ControlIdtoMotorId[Motor_i],ThreadControlCBFunction);
                    CurrentControlledSpeed[Motor_i] = 0;
                    ControlIdtoMotorId[Motor_i] = 0xFF;
                }
                ControlIdtoMotorId[Motor_i] = AddControlCallback(ThreadControlCBFunction, eOneMillisecond,Control_Read_Dancer_Position,(IfTypeThread*0x100+Motor_i),ThreadMotorIdToDancerId[Motor_i],Motor_i);
            }
            if (Motor_i == WINDER_MOTOR) // dryer motor is speed controlled. later a speed sensor will be utilized, but for now it will n//ot be controlled
            {
                if (ControlIdtoMotorId[Motor_i] != 0xFF)
                {
                    RemoveControlCallback(ControlIdtoMotorId[Motor_i],ThreadControlCBFunction);
                    CurrentControlledSpeed[Motor_i] = 0;
                    ControlIdtoMotorId[Motor_i] = 0xFF;
                }
                ControlIdtoMotorId[Motor_i] = AddControlCallback(ThreadControlCBFunction, eOneMillisecond,Control_Read_Dancer_Position,(IfTypeThread*0x100+Motor_i),ThreadMotorIdToDancerId[Motor_i],Motor_i);
            }
//            if (HW_Motor_Id == HARDWARE_MOTOR_TYPE__MOTO_DRYER_DRIVING) // dryer motor is speed controlled. later a speed sensor will be utilized, but for now it will not be controlled
//                AddControlCallback(ThreadSpeedControlCBFunction, eOneMillisecond,ThreadEmptyCBFunction,(IfTypeThread*0x100+Motor_i),ThreadMotorIdToMotorId[Motor_i],0);
            if (Motor_i == HARDWARE_MOTOR_TYPE__MOTO_DRYER_DRIVING) // dryer motor is speed controlled. later a speed sensor will be utilized, but for now it will not be controlled
                continue;
    }
    //testDancersControl();
    PrepareReady(Module_Thread,ModuleDone);
    //set 3 dancers to the profile positions
    InitialProcess = true;
    return OK;
}

void SetOriginMotorSpeed(float process_speed)
{
    int Motor_i, HW_Motor_Id;
    for (Motor_i = 0; Motor_i <= WINDER_MOTOR; Motor_i++)
    {
        HW_Motor_Id = ThreadMotorIdToMotorId[Motor_i];
        //(Speed*uStep*PPR)/((2*PI*motor_Radius)
        //        double motor_speed = (process_speed *  MotorsCfg[HW_Motor_Id].pulseperround *  MotorsCfg[HW_Motor_Id].microstep)/(2*PI* MotorsCfg[HW_Motor_Id].pulleyradius);
        double motor_speed = (process_speed
                * MotorsCfg[HW_Motor_Id].pulseperround)
                / (2 * PI * MotorsCfg[HW_Motor_Id].pulleyradius);
        //MotorControlConfig[Motor_i].m_SetParam = motor_speed;
        OriginalMotorSpd_2PPS[Motor_i] = (int) motor_speed;
        CurrentControlledSpeed[Motor_i] = (int) motor_speed;
    }
}

//********************************************************************************************************************
uint32_t ThreadPreSegmentState(void *JobDetails)
{
//set the speed only before the first segment, speed is constant across all job segments and intersegments
    JobTicket* JobTicket = JobDetails;

    float process_speed = dyeingspeed;
    if (dyeingspeed == 0)
    {
        LOG_ERROR (dyeingspeed," job speed zero");
        return ERROR;
    }

    SetOriginMotorSpeed(process_speed);
    ThreadControlActive = true;
    PrepareState = false;
    // set the new speed in the dryer motor to the speed of the new segment
    MotorSetSpeed(HARDWARE_MOTOR_TYPE__MOTO_DRYER_DRIVING, OriginalMotorSpd_2PPS[DRYER_MOTOR]);
 //only for testing - when control works, these motors will take their speed from the dryer
    //MotorSetSpeed(HARDWARE_MOTOR_TYPE__MOTO_LDRIVING, OriginalMotorSpd_2PPS[POOLER_MOTOR]);
 //only for testing - when control works, these motors will take their speed from the dryer
    //MotorSetSpeed(HARDWARE_MOTOR_TYPE__MOTO_RDRIVING, OriginalMotorSpd_2PPS[FEEDER_MOTOR]);

//#warning rocker disabled
    if (MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_RLOADING].maxfrequency > 0)
    {
        MotorSetDirection((TimerMotors_t)HARDWARE_MOTOR_TYPE__MOTO_RLOADING,MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_RLOADING].directionthreadwize);
        MotorSetSpeed(HARDWARE_MOTOR_TYPE__MOTO_RLOADING, 2);
    }
    if (MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_LLOADING].maxfrequency > 0)
    {
        MotorSetDirection((TimerMotors_t)HARDWARE_MOTOR_TYPE__MOTO_LLOADING,MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_LLOADING].directionthreadwize);
        MotorSetSpeed(HARDWARE_MOTOR_TYPE__MOTO_LLOADING, 2);
    }
 //#warning rocker disabled

//    MotorMovetoLimitSwitch (HARDWARE_MOTOR_TYPE__MOTO_RDRIVING,MotorsCfg[HARDWARE_MOTOR_TYPE__MOTO_RDRIVING].directionthreadwize, 0, GPI_LS_RLOADMOTOR_UP, EndState); //TODO

    // activate control fr all motors
    //set speed for both rocker motors
    //wait for all motors to get to the required speed (set the target speed for the control to check)
    //call the job state machine when the thread system is ready
    if ((InitialProcess==false) && JobTicket->enableintersegment == true)
    {
        ThreadUpdateProcessLength (JobTicket->intersegmentlength,(void *)ThreadInterSegmentEnded);
    }
    else
    {
        ThreadUpdateProcessLength (0,(void *)NULL);
        PreSegmentReady(Module_Thread,ModuleDone);
        JobCounter = 0;
        InitialProcess = false;
    }

    return OK;
}
void ThreadInterSegmentEnded(void)
{
    PreSegmentReady(Module_Thread,ModuleDone);
}
void ThreadSegmentEnded(void)
{
    SegmentReady(Module_Thread,ModuleDone);
}
void ThreadDistanceToSpoolEnded(void)
{
    DistanceToSpoolReady(Module_Thread,ModuleDone);
}
double seglength = 0.0;
//********************************************************************************************************************
uint32_t ThreadSegmentState(void *JobDetails, int SegmentId)
{
    JobTicket* JobTicket = JobDetails;
    seglength = JobTicket->segments[SegmentId]->length;
    CurrentSegmentId = SegmentId;
    ThreadUpdateProcessLength (seglength,(void *)ThreadSegmentEnded);
    return OK;
}

//********************************************************************************************************************
uint32_t ThreadDistanceToSpoolState(void )
{
    seglength = dryerbufferlength;
    ThreadUpdateProcessLength (seglength,(void *)ThreadDistanceToSpoolEnded);
    return OK;
}

//********************************************************************************************************************
 uint32_t ThreadEndState(void *JobDetails)
{
     int Motor_i;
     ThreadControlActive = false;
     ThreadUpdateProcessLength (0.0,(void *)NULL);
    SetOriginMotorSpeed(0);

     if (SpeedControlId != 0xFF)
     {
         RemoveControlCallback(SpeedControlId,ThreadLengthCBFunction);
         SpeedControlId = 0xFF;
     }
     for ( Motor_i = 0;Motor_i < MAX_THREAD_MOTORS_NUM;Motor_i++)
     {
         if (ControlIdtoMotorId[Motor_i] != 0xFF)
         {
             RemoveControlCallback(ControlIdtoMotorId[Motor_i],ThreadControlCBFunction);
         }
        MotorStop(ThreadMotorIdToMotorId[Motor_i],Hard_Hiz);
     }
     MotorStop(HARDWARE_MOTOR_TYPE__MOTO_RLOADING,Hard_Hiz);
     MotorStop(HARDWARE_MOTOR_TYPE__MOTO_LLOADING,Hard_Hiz);

    return OK;
}



//********************************************************************************************************************

void ThreadStartPrinting(void)
{
    //PrintingIterate();
}

//********************************************************************************************************************
//********************************************************************************************************************

void ThreadStopPrinting(void)
{
    //PrintingIterate();
}