Example of how to setup and use the general PID implementation in pid.c.
$Name$
Definition in file main.c.
#include <inavr.h>
#include <ioavr.h>
#include "stdint.h"
#include "pid.h"
Include dependency graph for main.c:
Go to the source code of this file.
Data Structures | |
struct | GLOBAL_FLAGS |
Flags for status information. More... | |
Defines | |
#define | K_D 0.00 |
#define | K_I 0.00 |
#define | K_P 1.00 |
#define | TIME_INTERVAL 157 |
Functions | |
int16_t | Get_Measurement (void) |
Read system process value. | |
int16_t | Get_Reference (void) |
Read reference value. | |
void | Init (void) |
Init of PID controller demo. | |
void | main (void) |
Demo of PID controller. | |
void | Set_Input (int16_t inputValue) |
Set control input to system. | |
__interrupt void | TIMER0_OVF_ISR (void) |
Timer interrupt to control the sampling interval. | |
Variables | |
GLOBAL_FLAGS | gFlags |
Flags for status information. | |
PID_DATA | pidData |
Parameters for regulator. |
#define K_P 1.00 |
#define TIME_INTERVAL 157 |
Specify the desired PID sample time interval With a 8-bit counter (255 cylces to overflow), the time interval value is calculated as follows: TIME_INTERVAL = ( desired interval [sec] ) * ( frequency [Hz] ) / 255
Definition at line 57 of file main.c.
Referenced by TIMER0_OVF_ISR().
int16_t Get_Measurement | ( | void | ) |
int16_t Get_Reference | ( | void | ) |
void Init | ( | void | ) |
Init of PID controller demo.
Definition at line 75 of file main.c.
References K_D, K_I, K_P, pid_Init(), pidData, and SCALING_FACTOR.
Referenced by main().
00076 { 00077 pid_Init(K_P * SCALING_FACTOR, K_I * SCALING_FACTOR , K_D * SCALING_FACTOR , &pidData); 00078 00079 // Set up timer, enable timer/counte 0 overflow interrupt 00080 TCCR0A = (1<<CS00); 00081 TIMSK0 = (1<<TOIE0); 00082 TCNT0 = 0; 00083 }
Here is the call graph for this function:
void main | ( | void | ) |
Demo of PID controller.
Definition at line 117 of file main.c.
References FALSE, Get_Measurement(), Get_Reference(), gFlags, Init(), pid_Controller(), pidData, GLOBAL_FLAGS::pidTimer, and Set_Input().
00118 { 00119 int16_t referenceValue, measurementValue, inputValue; 00120 Init(); 00121 __enable_interrupt(); 00122 00123 while(1){ 00124 00125 // Run PID calculations once every PID timer timeout 00126 if(gFlags.pidTimer) 00127 { 00128 referenceValue = Get_Reference(); 00129 measurementValue = Get_Measurement(); 00130 00131 inputValue = pid_Controller(referenceValue, measurementValue, &pidData); 00132 00133 Set_Input(inputValue); 00134 00135 gFlags.pidTimer = FALSE; 00136 } 00137 } 00138 }
Here is the call graph for this function:
void Set_Input | ( | int16_t | inputValue | ) |
__interrupt void TIMER0_OVF_ISR | ( | void | ) |
Timer interrupt to control the sampling interval.
Definition at line 62 of file main.c.
References gFlags, GLOBAL_FLAGS::pidTimer, TIME_INTERVAL, and TRUE.
00063 { 00064 static uint16_t i = 0; 00065 if(i < TIME_INTERVAL) 00066 i++; 00067 else{ 00068 gFlags.pidTimer = TRUE; 00069 i = 0; 00070 } 00071 }
struct GLOBAL_FLAGS gFlags |