-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpid-controller.h
More file actions
119 lines (105 loc) · 2.4 KB
/
Copy pathpid-controller.h
File metadata and controls
119 lines (105 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* A PID controller.
*
* @author Connor Imes
* @date 2017-03-18
*/
#ifndef _PID_CONTROLLER_H_
#define _PID_CONTROLLER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <inttypes.h>
#include <stdio.h>
// Stores user-defined parameters
typedef struct pid_controller_context {
double reference;
double min;
double max;
double K_I;
} pid_controller_context;
// Log file fields
typedef struct pid_controller_log_buffer {
uint64_t id;
uint64_t uid;
double measured;
double e;
double u;
} pid_controller_log_buffer;
// Maintains logging config and state
typedef struct pid_controller_log_state {
uint64_t id;
uint32_t lb_length;
pid_controller_log_buffer* lb;
FILE* lf;
} pid_controller_log_state;
// The top-level context/state struct
typedef struct pid_controller {
pid_controller_context ctx;
pid_controller_log_state ls;
double u;
} pid_controller;
/**
* Initialize the controller.
*
* @param state
* not NULL
* @param reference
* reference > 0
* @param min
* min > 0
* @param max
* max >= min
* @param initial
* min <= initial <= max
* @param k_i
* ratio of control change s.t. k_i > 0. If <= 0, it will be estimated in the first iteration
* @return 0 on success, -1 otherwise (errno will be set)
*/
int pid_controller_init(pid_controller* state, double reference, double min, double max, double initial, double k_i);
/**
* Destroy the controller.
*
* @param state
*/
void pid_controller_destroy(pid_controller* state);
/**
* Get the new control signal.
*
* @param state
* not NULL
* @param uid
* A user-specified identifier
* @param measured
* The measured output
* @return the new control signal
*/
double pid_controller_adapt(pid_controller* state, uint64_t uid, double measured);
/**
* Enable/disable logging.
*
* @param state
* not NULL
* @param lb
* the log buffer (NULL to disable)
* @param lb_length
* the log buffer length (0 to disable)
* @param lf
* the log file (NULL to disable)
* @return 0 on success, -1 otherwise (errno will be set)
*/
int pid_controller_set_logging(pid_controller* state, pid_controller_log_buffer* lb, uint32_t lb_length, FILE* lf);
/**
* Change the reference input.
*
* @param state
* not NULL
* @param reference
* reference > 0
* @return 0 on success, -1 otherwise (errno will be set)
*/
int pid_controller_set_reference(pid_controller* state, double reference);
#ifdef __cplusplus
}
#endif
#endif