CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

C Code explanation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wakaka



Joined: 11 Sep 2006
Posts: 27

View user's profile Send private message

C Code explanation
PostPosted: Wed Sep 20, 2006 2:00 am     Reply with quote

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????
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 20, 2006 2:19 am     Reply with quote

Question 1 and 3 have to do with the concept of pointers, an important part of the C language. It would take a lot of text to explain the working of pointers here, every good C book will describe it in details.

Quote:
What is memset?
A standard function supplied with every C-compiler. Again, check your C language books for a detailed explanation.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group