aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Thread/Thread_print.c
blob: 89ff633638cc59cee1c0c78dd6ba3ab06b7c1d46 (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
/************************************************************************************************************************
 * 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 "../control/control.h"
#include "../control/pidalgo.h"
#include "PMR/Hardware/HardwareMotor.pb-c.h"
#include "PMR/Hardware/HardwareMotorType.pb-c.h"
#include "drivers/Motors/Motor.h"
#include "drivers/Heater/TemperatureSensor.h"
#include "drivers/Heater/Heater.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
///////////////////////////////////////////////////////////////////////////////////////////
typedef enum
{
    NextState = 0,
    Repeat,
    Inter,
    Home,
    Stop
} ReturnCode;


uint32_t ThreadMotorIdToMotorId[MAX_THREAD_MOTORS_NUM] = {MOTOR_RDRIVING,MOTOR_DRYER_DRIVING,MOTOR_LDRIVING,MOTOR_WINDER,MOTOR_SCREW};

/********************************************************************************************
* functions describes motor operation flow and movement state during profile execution
* used to operate in runtime correct profileflow execution
*********************************************************************************************/
static ReturnCode EntryState(void *JobDetails);
static ReturnCode PrepareState(void *JobDetails);
static ReturnCode PreSegmentState(void *JobDetails);
static ReturnCode SegmentState(void *JobDetails);
static ReturnCode EndState(void *JobDetails);
static ReturnCode ExitState(void *JobDetails);
;

/**********************************************************************
* the array and enum of PrintingState_t below must be in sync order
***********************************************************************/
static ReturnCode (* state[])(void *JobDetails) = { EntryState, PrepareState, PreSegmentState,  SegmentState, EndState, ExitState};

typedef enum
{
    Entry= 0,
    Prepare,
    PreSegment,
    Segment,
    End,
    Exit
} PrintingState_t;

typedef struct
{
    PrintingState_t m_sourceState;
    ReturnCode          m_returnCode;
    PrintingState_t     m_destinationState;
} Transition_t;

//*************************************************************
/* transitions from end state aren't needed */
//*************************************************************
#define NUM_OF_TRANSITION   17
#define EXIT_STATE          Exit
#define ENTRY_STATE         Entry
/*************************************************************
 * table which describes fast motors transitions states
 * during p_profile / segments execution
 *************************************************************/
static Transition_t stateTransitionTable[NUM_OF_TRANSITION] =
{};
/*    {Entry,         NextState,  HomingStart},
    {Entry,         Repeat,     Entry},         //for homing of dispensers
    {HomingStart,   NextState,  Start},
    {HomingStart,   Repeat,     HomingStart},
    {Start,         NextState,  Segment},
    {Start,         Repeat,     Start},
    {Segment,       Inter,      Intersegment},
    {Segment,       Repeat,     Segment},
    {Segment,       Home,       HomingEnd},
    {Intersegment,  NextState,  Segment},
    {Intersegment,  Repeat,     Intersegment},
    {Intersegment,  Home,       HomingEnd},
    {HomingEnd,     NextState,  End},
    {HomingEnd,     Repeat,     HomingEnd},
    {End,           NextState,  Entry},
    {End,           Repeat,     Entry},
    {Exit,          Stop,       Exit}           //for stoping the machine iteration in case of error
};*/
typedef struct
{
    bool                m_isEnabled;
    uint32_t            m_SetParam;
    uint32_t            m_mesuredParam;
    float               m_preError;
    float               m_integral;
    float               m_calculatedError;
    bool                m_isReady;
    PID_Config_Params   m_params;
}MotorControlConfig_t;
/*typedef struct
{
    float epsilon;
    float dt;
    float MAX;
    float MIN;
    float Kp;
    float Kd;
    float Ki;
}PID_Config_Params;
#define epsilon 0.01
#define dt 0.01 //100ms loop time
#define MAX 4 //For Current Saturation
#define MIN -4
#define Kp 0.1
#define Kd 0.01
#define Ki 0.005
*/
MotorControlConfig_t MotorControlConfig[MAX_THREAD_MOTORS_NUM];
uint32_t DeviceId2Motor[MAX_THREAD_MOTORS_NUM];
////////////////////////Slow Motor State////////////////////////////////////
static PrintingState_t gPrintingState;
////////////////////////////////////////////////////////////////////////////

uint32_t ThreadControlCBFunction(uint32_t deviceID, uint32_t ReadValue)
{
    int i,index=MAX_THREAD_MOTORS_NUM;
    for (i=0;i<MAX_THREAD_MOTORS_NUM;i++)
        if (DeviceId2Motor[i] == deviceID)
        {
            index = i;
            break;
        }
    if (index==MAX_THREAD_MOTORS_NUM)
    {
        LOG_ERROR (deviceID, "No motor  for device");
        return 0xFFFFFFFF;
    }
    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);
        if (MotorControlConfig[index].m_calculatedError >= MotorControlConfig[index].m_params.MAX)
        {
            MotorControlConfig[index].m_calculatedError = MotorControlConfig[index].m_params.MAX;
        }
        if (MotorControlConfig[index].m_calculatedError < MotorControlConfig[index].m_params.MIN)
        {
            MotorControlConfig[index].m_calculatedError = MotorControlConfig[index].m_params.MIN;
        }

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

 return OK;
}

//********************************************************************************************************************
/********************************************************************************************************************
*function describes entry point of motor in profile execution - accelerate from stop position
*function described above used to operate motor operation flow and movement state during profile execution
*********************************************************************************************************************/
static ReturnCode EntryState(void *JobDetails)
{
    return NextState;
}

//********************************************************************************************************************
static ReturnCode PrepareState(void *JobDetails)
{
    int Motor_i;
    //start thread control for all motors
    for (Motor_i = 0;Motor_i < MAX_THREAD_MOTORS_NUM;Motor_i++)
    {
        MotorControlConfig[Motor_i].m_params.MAX = MotorsCfg[Motor_i].maxfreq;
        MotorControlConfig[Motor_i].m_params.MIN = MotorsCfg[Motor_i].minfreq;
        MotorControlConfig[Motor_i].m_params.Kd = MotorsCfg[Motor_i].kd;
        MotorControlConfig[Motor_i].m_params.Kp = MotorsCfg[Motor_i].kp;
        MotorControlConfig[Motor_i].m_params.Ki = MotorsCfg[Motor_i].ki;
        MotorControlConfig[Motor_i].m_params.dt = eOneMillisecond;
        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
        AddControlCallback(ThreadControlCBFunction, eOneMillisecond,TemplateDataReadCBFunction,Motor_i,0);
    }

    //set 3 dancers to the profile positions
    return NextState;
}

//********************************************************************************************************************
static ReturnCode PreSegmentState(void *JobDetails)
{

    TimerMotors_t Motor_i;
    for (Motor_i = 0;Motor_i < MAX_THREAD_MOTORS_NUM;Motor_i++)
    {
        MotorControlConfig[Motor_i].m_SetParam = MotorGetSpeed(getMotorId(Motor_i));//need to update SetParams on presegment stage
    }
    // set the new speed in the dryer motor to the speed of the new segment
    // 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
    return NextState;
}

//********************************************************************************************************************
static ReturnCode SegmentState(void *JobDetails)
{
    return Repeat;
}

//********************************************************************************************************************
static ReturnCode EndState(void *JobDetails)
{
    return NextState;
}
//********************************************************************************************************************
static ReturnCode ExitState(void *JobDetails)
{
    return Stop;
}


//***********************************************************************************************************************
//this function is responsible for operating and transitioning between the diffrent motor state executions of the profile
//the lower managment level
//***********************************************************************************************************************
static PrintingState_t LookupTransitions(PrintingState_t state,ReturnCode returnCode)
{
    char str[80];
    uint8_t len = 0;
    uint8_t indexInTransitionTable;
    for (indexInTransitionTable = 0; indexInTransitionTable < NUM_OF_TRANSITION; ++indexInTransitionTable)
    {
        if ((stateTransitionTable[indexInTransitionTable].m_sourceState == state) && (stateTransitionTable[indexInTransitionTable].m_returnCode == returnCode))
        {
            //len = usnprintf(str, 60, "\r\n  tick %d state %d return code %d",tick,state, returnCode );
            //cb_push_back (str, len);

            //in normal execution flow function should not arrive here
            //in case it did the meaning is that the entery point was wrong and a bug should be corrected
            return stateTransitionTable[indexInTransitionTable].m_destinationState;
        }
    }
    //int tick = UsersysTickGet();
    //len = usnprintf(str, 60, "\r\n  tick %d state %d return code %d",tick,state, returnCode );
    //cb_push_back (str, len);

    //in normal execution flow function should not arrive here
    //in case it did the meaning is that the entery point was wrong and a bug should be corrected
    len = usnprintf(str, 80, "Internal: invalid slow motor transition state %d return code %d",state, returnCode );

    return EXIT_STATE;
}
//********************************************************************************
//this function is used to manage and operate the motor managmant state mashine
//the highest managment level
//********************************************************************************
bool ThreadPrintingIterate(void *JobDetails)
{
    uint32_t tick = 0;
    char str[60];
    uint8_t len = 0;
    PrintingState_t keepstate  =  gPrintingState;
    //
    // Disable all interrupts.
    //
    ROM_IntMasterDisable();

    ReturnCode (* state_fun)(void *JobDetails) = state[gPrintingState];
    //if (_motorId == SCREW_MOTOR)
    //    screw_movement[gPrintingState[_motorId]]++;
    ReturnCode returnCode = state_fun(JobDetails);

    gPrintingState = LookupTransitions(gPrintingState, returnCode);
//    if (keepstate != gPrintingState){
//    }

    //
    // Enable all interrupts.
    //
    ROM_IntMasterEnable();
    return (gPrintingState != EXIT_STATE);
}


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

void ThreadPrintingsInit(void)
{
//        gPrintingState = Start;
}

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

void ThreadStartPrinting(void)
{
    gPrintingState = ENTRY_STATE;
    //PrintingIterate();
}

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

void ThreadStopPrinting(void)
{
    gPrintingState = EXIT_STATE;
    //PrintingIterate();
}