Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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,933
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......
 

vdaanen

Newbie level 3
Joined
Sep 1, 2006
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,312
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.
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 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
 

kalyan99kumar999

Newbie level 5
Joined
Feb 25, 2006
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,348
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".
 

artem

Advanced Member level 4
Joined
May 22, 2003
Messages
1,350
Helped
126
Reputation
252
Reaction score
32
Trophy points
1,328
Location
Turkey
Activity points
13,461

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top