aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Thread/Thread_Winder.c
blob: 6a05b83ce77bb8f8b89c3176750aa38a1aa2e30e (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
/*
 * Thread_Winder.c
 *
 *  Created on: 25 ���� 2018
 *      Author: shlomo
 */
#include"include.h"
#include "thread.h"
#include "Drivers/Peripheral_GPIO/GPIO.h"
#include "drivers/Motors/Motor.h"
#include "StateMachines/Printing/PrintingSTM.h"
bool Winder_ScrewHoming = false;
uint32_t Winder_ScrewAtOffsetCallback(uint32_t NumberOfSteps);
uint32_t Winder_PrepareStage2(void);

double ScrewSpeed = 0;

uint32_t Winder_Init(void)
{
    return OK;
}
uint32_t Winder_Prepare(void)
{
    uint32_t status = 0;
    double ScrewSpeed = (50 *  MotorsCfg[SCREW_MOTOR].pulseperround *  MotorsCfg[SCREW_MOTOR].microstep)/(2*PI* MotorsCfg[SCREW_MOTOR].pulleyradius);

    /*
     * 1. move home to the limit switch (check that the cart is clear from the limit switch, start moving, with acceleration to maximal speed. enable interrupt on the limit switch, upon interrupt stop.
     * 2. move back x steps - according to thehw specifications and bobine definitions in the job. move for a predefined number of steps. get a callback when done
     * report ready to the job STM
     */
    if (PollGPIO(GPI113_LS_SCREW_RIGHT))
    {
        //go to prepare stage 2
        Winder_PrepareStage2();
    }
    else
    {
        Winder_ScrewHoming = true;
        status = MotorSetDirection(MOTOR_SCREW,1);//make sur to move the cart home
        status |=  MotorSetSpeed(MOTOR_SCREW, ScrewSpeed, MotorsCfg[SCREW_MOTOR].microstep);
    }
    return status;
}
uint32_t Winder_PrepareStage2(void)
{
    uint32_t status;
    uint32_t numOfSteps = InternalWinderCfg.segmentoffsetpulses;
    /*
     * 1. move home to the limit switch (check that the cart is clear from the limit switch, start moving, with acceleration to maximal speed. enable interrupt on the limit switch, upon interrupt stop.
     * 2. move back x steps - according to thehw specifications and bobine definitions in the job. move for a predefined number of steps. get a callback when done
     * report ready to the job STM
     */
        status = MotorSetDirection(MOTOR_SCREW,0);//make sur to move the cart out
        //status |= MotorMoveSteps (MOTOR_SCREW, numOfSteps, Winder_ScrewAtOffsetCallback);
        //set motor location 0 here
        return status;

}

uint32_t WinderPresegmentReady(uint32_t deviceID, uint32_t ReadValue)
{
    return PreSegmentReady(Module_Winder,OK);
}
uint32_t Winder_Presegment(void *JobDetails)
{
    JobTicket* JobTicket = JobDetails;
    int process_speed = JobTicket->processparameters->dyeingspeed;
    float screw_speed = 0;
    float RotationsPerSecond;

    double ScrewSpeed = (process_speed *  MotorsCfg[SCREW_MOTOR].pulseperround *  MotorsCfg[SCREW_MOTOR].microstep)/(2*PI* MotorsCfg[SCREW_MOTOR].pulleyradius);

/*typedef struct
{
    uint32_t startoffsetpulses;
    uint32_t spoolbackingrate;
    uint32_t segmentoffsetpulses;
    uint32_t milimetersperrotation;
    int32 SpoolBottomBackingRate;// the angle of the bottom of the spool
    double NumberOfRotationPerPassage;  // how many rotations per spool passage
}InternalWinderConfigStruc;
 * */
//     * speed is set by the winding parameters and by winder rotational speed (read POSITION every 10msec)
//     * calculate
//     * 1. calculate speed according to JobTicket->processparameters->dyeingspeed
    // calculation input: traverse length in milimeters/pulses, number of rotations per traverse ==> length of traverse per rotation.
    screw_speed = InternalWinderCfg.segmentoffsetpulses /  InternalWinderCfg.NumberOfRotationPerPassage;
    // calculation input#2: number of rotations per second - (basically: speed/winder perimeter. later - according to winder actual speed - calculate according to winder position accumulation in the last second.
    RotationsPerSecond = process_speed / (InternalWinderCfg.diameter * 3.1416);
    // calculation input#3: speed = rotation per second * traverse per rotation = traverse per second. speed set: traverse per second (mm) * pulses per mm.
    screw_speed = screw_speed*RotationsPerSecond;
    //screw_speed = InternalWinderCfg.milimetersperrotation
//     * 2. determine optimal micro-step setting
//     * 3. calculate cart travel length from winding parameters
//     * 4. start move of travel length
//     * 5. register motor nBusy callback. this callback will flip between move(traverse length, hardstop) and goto(0), with handline og the coneshape and adjusting maxspeed

    //MotorMove (InternalWinderCfg.segmentoffsetpulses,screw_speed); process: set point 0, set max speed, move to the specified length, return back.
     MotorSetSpeedWithCallback (MOTOR_SCREW, screw_speed, MotorsCfg[SCREW_MOTOR].microstep,WinderPresegmentReady);
    //in a callback: calculate backing rate for top and bottom, update point 0, update passing length, call the appropriate move to 0 / move;

    return OK;
}
uint32_t Winder_End(void)
{
    //stop screw
    return StopMotor (MOTOR_SCREW,Hard_Hiz);
}
void Winder_ScrewHomeLimitSwitchInterrupt(void)
{
    uint32_t status;
    if  (Winder_ScrewHoming)
    {
        StopMotor(MOTOR_SCREW,Hard_Stop); //stop ASAP
    }
    status = MotorSetDirection(MOTOR_SCREW,MotorsCfg[SCREW_MOTOR].directionthreadwize);//make sure to move the cart out
}
void Winder_ScrewOutLimitSwitchInterrupt(void)
{
    uint32_t status;
    status = MotorSetDirection(MOTOR_SCREW,1-MotorsCfg[SCREW_MOTOR].directionthreadwize);//make sure to move the cart out
}
uint32_t Winder_ScrewAtOffsetCallback(uint32_t NumberOfSteps)
{
    if (NumberOfSteps == InternalWinderCfg.segmentoffsetpulses)
    {
        PrepareReady(Module_Winder, OK);
        return OK;
    }
    else
    {
        //do we want to do something?
        PrepareReady(Module_Winder, ERROR);
        return ERROR;
    }

}