0% found this document useful (0 votes)
35 views3 pages

Pid Position Controller Code

The document describes an ANSI C program that implements a PID controller to control the speed of a motor using encoder feedback. The program reads encoder values from an NI DAQ device and calculates the control signal to drive the motor's speed to a desired setpoint by minimizing error over time. It continuously calculates the proportional, integral, and derivative terms and outputs PWM pulses to the motor in a feedback loop.

Uploaded by

phandu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Pid Position Controller Code

The document describes an ANSI C program that implements a PID controller to control the speed of a motor using encoder feedback. The program reads encoder values from an NI DAQ device and calculates the control signal to drive the motor's speed to a desired setpoint by minimizing error over time. It continuously calculates the proportional, integral, and derivative terms and outputs PWM pulses to the motor in a feedback loop.

Uploaded by

phandu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

* ANSI C Example program:


* one motor pid controller + Read Encoder
*********************************************************************/

#include <stdio.h>
#include <NIDAQmx.h>
#include <math.h>
#include <windows.h> // for timer
#define pi 3.14159265358979

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) )


goto Error; else

int main(void)
{
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1,t0; // ticks
double Time;

int32 error=0;
char errBuff[2048]={'\0'};

int i,j;

TaskHandle taskHandle=0;
uInt8 data[8]={0};

TaskHandle taskHandle1=0;
int32 read1[1000];
float64 data1[1000]={0};

double d=0 ,dd=0,ddd=0 ;


double result,err,act=0,act2=0,sum=0,eold=0,derror,dt=0,told=0;

// DAQmx Configure Code


DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk
(DAQmxCreateDOChan(taskHandle,"Dev3/port0/line0:7","",DAQmx_Val_ChanForAllLin
es));

DAQmxErrChk (DAQmxCreateTask("",&taskHandle1));
DAQmxErrChk
(DAQmxCreateCIAngEncoderChan(taskHandle1,"Dev3/ctr0","",DAQmx_Val_X4,0,0.0,DA
Qmx_Val_AHighBHigh,DAQmx_Val_Degrees,1000,0.0,""));
DAQmxErrChk
(DAQmxCfgSampClkTiming(taskHandle1,"/Dev3/PFI8",1000.0,DAQmx_Val_Rising,DAQmx
_Val_ContSamps,1000.0));

// DAQmx Start Code

DAQmxErrChk (DAQmxStartTask(taskHandle));
DAQmxErrChk (DAQmxStartTask(taskHandle1));

QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t0);

while(1){
QueryPerformanceCounter(&t1);

Time = (t1.QuadPart - t0.QuadPart) *1.0 / frequency.QuadPart;

dt=Time-told; //delta t

d=10*sin(2*pi*0.25*Time); // desired mm

result=d/0.35; //convert mm to rev


DAQmxErrChk (DAQmxReadCounterF64(taskHandle1,-1,-
1.0,&data1,1000,&read1,NULL));//read encoder
act=data1[0]/360; //convert degrees to rev
act2=act*0.35; //convert rev to mm

printf( "d1=%f ",act2); // print actual mm

err=result-act;

sum=sum+err;
derror=err-eold;//change of error

ddd=err*0.6*0.058*2000+(0.058*0.6*0.2*sum*dt/0.02)+0.058*0.6*0.02*derror/((dt
)*8); //no of pulses or control signal

if(ddd>0)
{
data[1]=1;
dd=ddd;
}
else {
data[1]=0;
dd=-ddd;
}

for(i=1;i<=dd;i++)
{
if(i%2==1)
data[0]=0;
else
data[0]=1;

for(j=0;j<2000;j++); //Delay(3 micro second);

//write pulses
DAQmxErrChk
(DAQmxWriteDigitalLines(taskHandle,1,1,10.0,DAQmx_Val_GroupByChannel,data,NUL
L,NULL));
}
told=Time;
eold=err;

printf( "time=%f \n",Time);

Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");

getchar();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy