All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pidlib.h
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) James Pearman */
4 /* 2013 */
5 /* All Rights Reserved */
6 /* */
7 /*-----------------------------------------------------------------------------*/
8 /* */
9 /* Module: pidlib.h */
10 /* Author: James Pearman */
11 /* Created: 19 June 2013 */
12 /* */
13 /* Revisions: */
14 /* V1.00 4 July 2013 - Initial release */
15 /* */
16 /*-----------------------------------------------------------------------------*/
17 /* */
18 /* This file is part of ConVEX. */
19 /* */
20 /* The author is supplying this software for use with the VEX cortex */
21 /* control system. ConVEX is free software; you can redistribute it */
22 /* and/or modify it under the terms of the GNU General Public License */
23 /* as published by the Free Software Foundation; either version 3 of */
24 /* the License, or (at your option) any later version. */
25 /* */
26 /* ConVEX is distributed in the hope that it will be useful, */
27 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
28 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
29 /* GNU General Public License for more details. */
30 /* */
31 /* You should have received a copy of the GNU General Public License */
32 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 /* */
34 /* A special exception to the GPL can be applied should you wish to */
35 /* distribute a combined work that includes ConVEX, without being obliged */
36 /* to provide the source code for any proprietary components. */
37 /* See the file exception.txt for full details of how and when the */
38 /* exception can be applied. */
39 /* */
40 /* The author can be contacted on the vex forums as jpearman */
41 /* or electronic mail using jbpearman_at_mac_dot_com */
42 /* Mentor for team 8888 RoboLancers, Pasadena CA. */
43 /* */
44 /*-----------------------------------------------------------------------------*/
45 
46 #ifndef __PIDLIB__
47 #define __PIDLIB__
48 
49 /*-----------------------------------------------------------------------------*/
50 /** @file pidlib.h
51  * @brief A port of the ROBOTC pidlib library, macros and prototypes
52 *//*---------------------------------------------------------------------------*/
53 
54 /** @brief Current pidlib Version is 1.02
55  */
56 #define kPidLibVersion 102
57 
58 /** @brief Use heap for pid controller data rather than static data
59  */
60 #define PIDLIB_USE_DYNAMIC 1
61 
62 /*-----------------------------------------------------------------------------*/
63 /** @brief Structure to hold all data for one instance of a PID controller */
64 /*-----------------------------------------------------------------------------*/
65 /** @note
66  * Currently at 64 bytes memory usage
67  */
68 typedef struct _pidController {
69  // Turn on or off the control loop
70  int16_t enabled; ///< enable or diable pid calculations
71  int16_t res1; ///< word alignment of Kp
72 
73  // PID constants, Kbias is used to compensate for gravity or similar
74  float Kp; ///< proportional constant
75  float Ki; ///< integral constant
76  float Kd; ///< derivative constant
77  float Kbias; ///< bias constant
78 
79  // working variables
80  float error; ///< error between actual pisition and target
81  float last_error; ///< error last time update called
82  float integral; ///< integrated error
83  float integral_limit; ///< limit for integrated error
84  float derivative; ///< change in error from last time
85  float error_threshold;///< threshold below which error is ignored
86 
87  // output
88  float drive; ///< calculated motor drive in range +/- 1.0
89  int16_t drive_raw; ///< motor drive in the range +/- 127
90  int16_t drive_cmd; ///< linearized motor drive in the range +/- 127
91 
92  tVexSensors sensor_port; ///< digital or analog port with the position sensor
93  int16_t sensor_reverse; ///< flag indicating the sensor values should be reversed
94  int32_t sensor_value; ///< current value of the position sensor
95 
96  int32_t target_value; ///< the target value
97  } pidController;
98 
99 
100 /*-----------------------------------------------------------------------------*/
101 /** @brief Allow 4 pid controllers */
102 /*-----------------------------------------------------------------------------*/
103 #define MAX_PID 4
104 
105 // lookup table to linearize control
106 
107 /** @brief size of linearizing table
108  */
109 #define PIDLIB_LUT_SIZE 128
110 /** @brief severity pf the linearize power function
111  */
112 #define PIDLIB_LUT_FACTOR 20.0
113 /** @brief offset below which the linearize function is not used
114  */
115 #define PIDLIB_LUT_OFFSET 10
116 
117 /** @brief This causes the motor never to be given more than a 0.25 drive
118  * command due to integral
119  */
120 #define PIDLIB_INTEGRAL_DRIVE_MAX 0.25
121 
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125 
126 pidController *PidControllerInit( float Kp, float Ki, float Kd, tVexSensors port, int16_t sensor_reverse );
127 pidController *PidControllerInitWithBias( float Kp, float Ki, float Kd, float Kbias, tVexSensors port, int16_t sensor_reverse );
128 int16_t PidControllerUpdate( pidController *p );
129 void PidControllerMakeLut(void);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif // __PIDLIB__