Explanation of a C code: pp, &sPID, memset

Status
Not open for further replies.

wakaka

Full Member level 4
Joined
Dec 7, 2005
Messages
237
Helped
10
Reputation
20
Reaction score
6
Trophy points
1,298
Activity points
2,931
Code:
#include <string.h> 
#include <stdio.h> 
/*===================================================== 
PID Function 

The PID function is used in mainly 
control applications. PIDCalc performs one iteration of the PID 
algorithm. 

While the PID function works, main is just a dummy program showing 
a typical usage. 
======================================================*/ 

typedef struct PID { 

double SetPoint; // Desired Value 

double Proportion; // Proportional Const 
double Integral; // Integral Const 
double Derivative; // Derivative Const 

double LastError; // Error[-1] 
double PrevError; // Error[-2] 
double SumError; // Sums of Errors 

} PID; 

/*===================================================== 
PID 
======================================================*/ 

double PIDCalc( PID *pp, double NextPoint ) 
{ 
double dError, 
Error; 

Error = pp->SetPoint - NextPoint; 
pp->SumError += Error; 
dError = pp->LastError - pp->PrevError; 
pp->PrevError = pp->LastError; 
pp->LastError = Error; 
return (pp->Proportion * Error 
+ pp->Integral * pp->SumError 
+ pp->Derivative * dError 
); 
} 

/*===================================================== 
Initialize PID Structure 
======================================================*/ 

void PIDInit (PID *pp) 
{ 
memset ( pp,0,sizeof(PID)); 
} 

/*===================================================== 
Main Program 
======================================================*/ 

double sensor (void) // Dummy Sensor Function 
{ 
return 100.0; 
} 

void actuator(double rDelta) // Dummy Actuator Function 
{} 

void main(void) 
{ 
PID sPID; // PID Control Structure 
double rOut; // PID Response (Output) 
double rIn; // PID Feedback (Input) 

PIDInit ( &sPID ); // Initialize Structure 
sPID.Proportion = 0.5; // Set PID Coefficients 
sPID.Integral = 0.5; 
sPID.Derivative = 0.0; 
sPID.SetPoint = 100.0; // Set PID Setpoint 

for (;;) { // Mock Up of PID Processing 

rIn = sensor (); // Read Input 
rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation 
actuator ( rOut ); // Effect Needed Changes 
} 
}

I not quite understand in some parts this code, can some explain on it??

1.
Code:
double PIDCalc( PID *pp, double NextPoint ) 
{ 
double dError, 
Error; 

Error = pp->SetPoint - NextPoint; 
pp->SumError += Error; 
dError = pp->LastError - pp->PrevError; 
pp->PrevError = pp->LastError; 
pp->LastError = Error; 
return (pp->Proportion * Error 
+ pp->Integral * pp->SumError 
+ pp->Derivative * dError 
); 
}
What is the pp?? *pp?

2.
Code:
void PIDInit (PID *pp) 
{ 
memset ( pp,0,sizeof(PID)); 
}
What is memset?

3.
Code:
void main(void) 
{ 
PID sPID; // PID Control Structure 
double rOut; // PID Response (Output) 
double rIn; // PID Feedback (Input) 

PIDInit ( &sPID ); // Initialize Structure 
sPID.Proportion = 0.5; // Set PID Coefficients 
sPID.Integral = 0.5; 
sPID.Derivative = 0.0; 
sPID.SetPoint = 100.0; // Set PID Setpoint 

for (;;) { // Mock Up of PID Processing 

rIn = sensor (); // Read Input 
rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation 
actuator ( rOut ); // Effect Needed Changes 
} 
}
PIDInit ( &sPID ), wat is &sPID ??
sPID.Proportion , the '.' means????

if possible do explain the whole thing, thanx......
 

avr221 code

What is the pp?? *pp?

PID is a structure (a way to pack variables and give them a name), *pp means that when calling the function, you pass the adress of the structure, not the data.
It is used because pp will be modified.

2.
Code:
void PIDInit (PID *pp) 
{ 
memset ( pp,0,sizeof(PID)); 
}

What is memset?
memset sets 'sizeof(PID)' (3rd arg) 0 (2nd arg) in the memory located a pp (1st arg).
It is used to initalized everything to 0

3.

&sPID is the adress (in memory) of the sPID

sPID.Proportion , the '.' means????

the pid structure is, as I already wrote it, a way to pack some variables. To access to a variable you specify it by .'its name'

if possible do explain the whole thing, thanx......
It will take too much time !

You should read a book about C langage (and maybe also PID controller ?)

vince
 

pid.setpoint

The (&sPID) may mean the sPID is passed to the function by reference or address.The changes are effected permanently because the operations are through "&"operator.
The "spid.proportion" means proportion is the member of the structure "spid".
 

pidcalc

question 1 is closely related to pid theory. As a practical introduction, study this application note from atmel - it will answer your current question regarding PID output value calculation (meaning of question 1):
**broken link removed**
**broken link removed**
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…