discoverpixy
pixy_control.c
Go to the documentation of this file.
1 /**************************************************************************************************************************************
2 * Project: discoverpixy
3 * Website: https://github.com/t-moe/discoverpixy
4 * Authors: Aaron Schmocker, Timo Lang
5 * Institution: BFH Bern University of Applied Sciences
6 * File: common/app/pixy_control.c
7 *
8 * Version History:
9 * Date Autor Email SHA Changes
10 * 2015-06-02 aaron@duckpond.ch e018a75 Implemented basic pi and pid controller
11 * 2015-06-06 aaron@duckpond.ch 8c264c2 Comment refactoring, updated PID values
12 * 2015-06-06 aaron@duckpond.ch a04cda9 Refactured comments and implemented a bugfix for the PID controller
13 * 2015-06-07 aaron@duckpond.ch 802d3df Fixed pid controller and refactored code
14 * 2015-06-07 aaron@duckpond.ch 3d98ca9 Minor changes
15 * 2015-06-07 timolang@gmail.com c87220d Renamed pixy_helper to pixy_frame. Updated docu of appliaction. added doxygen comments to pixy_{frame,control}.h
16 *
17 **************************************************************************************************************************************/
18 
19 /*
20  * pixy_control.c
21  *
22  * Notation
23  * --------
24  *
25  * x : Sollwert (Führgrösse)
26  * w : Istwert (Reglergrösse)
27  * esum : Integralteil
28  * e : Regelabweichung
29  * y : Stellgrösse
30  *
31  *
32  */
33 #include<pixy_control.h>
34 #include<stdint.h>
35 
36 // PID tuning factors
37 #define REG_PID_KP (0.5f)
38 #define REG_PID_KI (0.001f)
39 #define REG_PID_KD (0.001f)
40 #define REG_PID_TA (0.01f)
41 
42 
43 // PID controller implementatoin for the y-axis
44 int16_t pixy_PID_Y(int16_t x, int16_t w)
45 {
46  float e = 0;
47  static float esum = 0;
48  static float eold = 0;
49  float y = 0;
50 
51  e = (float)(x - w); // calculate the controller offset
52 
53  //----PID-control-------------------------------------------------------------------------
54  esum = esum + e; // add e to the current sum
55 
56  y += REG_PID_KP * e; // add the proportional part to the output
57  y += REG_PID_KI * REG_PID_TA * esum; // add the integral part to the output
58  y += REG_PID_KD * (e - eold) / REG_PID_TA; // add the differential part to the output
59  //----------------------------------------------------------------------------------------
60 
61  eold = e; // save the previous value
62 
63  return (int16_t)y;
64 }
65 
66 // PID controller implementation for the x-axis
67 int16_t pixy_PID_X(int16_t x, int16_t w)
68 {
69  float e = 0;
70  static float esum = 0;
71  static float eold = 0;
72  float y = 0;
73 
74  e = (float)(x - w); // calculate the controller offset
75 
76  //----PID-control-------------------------------------------------------------------------
77  esum = esum + e; // add e to the current sum
78 
79  y += REG_PID_KP * e; // add the proportional part to the output
80  y += REG_PID_KI * REG_PID_TA * esum; // add the integral part to the output
81  y += REG_PID_KD * (e - eold) / REG_PID_TA; // add the differential part to the output
82  //----------------------------------------------------------------------------------------
83 
84  eold = e; // save the previous value
85 
86  return (int16_t)y;
87 }
#define REG_PID_KI
Definition: pixy_control.c:38
#define REG_PID_KP
Definition: pixy_control.c:37
#define REG_PID_TA
Definition: pixy_control.c:40
int16_t pixy_PID_X(int16_t x, int16_t w)
Definition: pixy_control.c:67
int16_t pixy_PID_Y(int16_t x, int16_t w)
Definition: pixy_control.c:44
#define REG_PID_KD
Definition: pixy_control.c:39