8
8
#include <stdint.h>
9
9
#include <stdlib.h>
10
10
11
- #include <pbio/config.h>
12
11
#include <pbio/control_settings.h>
13
12
#include <pbio/integrator.h>
14
13
#include <pbio/int_math.h>
15
14
16
- /* Speed integrator used for speed-based control */
17
-
15
+ /**
16
+ * Pauses the speed integrator at the current position error.
17
+ *
18
+ * @param [inout] itg Speed integrator instance.
19
+ * @param [in] time_now The wall time (ticks).
20
+ * @param [in] position_error Current position error (control units).
21
+ */
18
22
void pbio_speed_integrator_pause (pbio_speed_integrator_t * itg , uint32_t time_now , int32_t position_error ) {
19
23
20
24
// Pause only if running
@@ -28,10 +32,16 @@ void pbio_speed_integrator_pause(pbio_speed_integrator_t *itg, uint32_t time_now
28
32
// Increment the paused integrator state with the integrated amount between the last resume and the newly enforced pause
29
33
itg -> speed_err_integral_paused += position_error - itg -> position_error_resumed ;
30
34
31
- // Store time at which we started pausing
35
+ // Store time at which we started pausing, used only for stall flag hysteresis.
32
36
itg -> time_pause_begin = time_now ;
33
37
}
34
38
39
+ /**
40
+ * Resumes the speed integrator from the current position error.
41
+ *
42
+ * @param [inout] itg Speed integrator instance.
43
+ * @param [in] position_error Current position error (control units).
44
+ */
35
45
void pbio_speed_integrator_resume (pbio_speed_integrator_t * itg , int32_t position_error ) {
36
46
37
47
// Resume only if paused
@@ -47,22 +57,34 @@ void pbio_speed_integrator_resume(pbio_speed_integrator_t *itg, int32_t position
47
57
itg -> position_error_resumed = position_error ;
48
58
}
49
59
60
+ /**
61
+ * Resets the speed integrator state.
62
+ *
63
+ * @param [inout] itg Speed integrator instance.
64
+ * @param [in] settings Control settings instance from which to read stall settings.
65
+ */
50
66
void pbio_speed_integrator_reset (pbio_speed_integrator_t * itg , pbio_control_settings_t * settings ) {
51
67
52
68
// Save reference to settings.
53
69
itg -> settings = settings ;
54
70
55
- // Set integral to 0
71
+ // Reset built up integral to 0.
56
72
itg -> speed_err_integral_paused = 0 ;
57
73
58
- // Set state to paused
74
+ // Set state to paused. It will resume immediately on start.
59
75
itg -> running = false;
60
76
61
77
// Resume integration
62
78
pbio_speed_integrator_resume (itg , 0 );
63
79
}
64
80
65
- // Get reference errors and integrals
81
+ /**
82
+ * Gets the speed error integral accumulated thus far.
83
+ *
84
+ * @param [in] itg Speed integrator instance.
85
+ * @param [in] position_error Current position error (control units).
86
+ * @return Speed error integral (position control units).
87
+ */
66
88
int32_t pbio_speed_integrator_get_error (pbio_speed_integrator_t * itg , int32_t position_error ) {
67
89
68
90
// The speed error integral is at least the value at which we paused it last
@@ -75,6 +97,15 @@ int32_t pbio_speed_integrator_get_error(pbio_speed_integrator_t *itg, int32_t po
75
97
return speed_err_integral ;
76
98
}
77
99
100
+ /**
101
+ * Checks if the speed integrator state indicates stalling.
102
+ *
103
+ * @param [in] itg Speed integrator instance.
104
+ * @param [in] time_now The wall time (ticks).
105
+ * @param [in] speed_now Current speed (control units).
106
+ * @param [in] speed_ref Reference speed (control units).
107
+ * @return True if stalled, false if not.
108
+ */
78
109
bool pbio_speed_integrator_stalled (pbio_speed_integrator_t * itg , uint32_t time_now , int32_t speed_now , int32_t speed_ref ) {
79
110
// If were running, we're not stalled
80
111
if (itg -> running ) {
@@ -101,8 +132,13 @@ bool pbio_speed_integrator_stalled(pbio_speed_integrator_t *itg, uint32_t time_n
101
132
return true;
102
133
}
103
134
104
- /* Count integrator used for position-based control */
105
-
135
+ /**
136
+ * Gets reference time compensated for stall duration of position controller.
137
+ *
138
+ * @param [in] itg Position integrator instance.
139
+ * @param [in] time_now The wall time (ticks).
140
+ * @return Wall time compensated for time spent stalling.
141
+ */
106
142
uint32_t pbio_position_integrator_get_ref_time (pbio_position_integrator_t * itg , uint32_t time_now ) {
107
143
// The wall time at which we are is either the current time, or whenever we stopped last.
108
144
uint32_t real_time = itg -> trajectory_running ? time_now : itg -> time_pause_begin ;
@@ -111,22 +147,40 @@ uint32_t pbio_position_integrator_get_ref_time(pbio_position_integrator_t *itg,
111
147
return real_time - itg -> time_paused_total ;
112
148
}
113
149
150
+ /**
151
+ * Pauses the position integrator at the current time.
152
+ *
153
+ * @param [inout] itg Speed integrator instance.
154
+ * @param [in] time_now The wall time (ticks).
155
+ */
114
156
void pbio_position_integrator_pause (pbio_position_integrator_t * itg , uint32_t time_now ) {
115
157
116
- // Return if already paused
158
+ // Return if already paused.
117
159
if (!itg -> trajectory_running ) {
118
160
return ;
119
161
}
120
162
121
- // Disable the integrator
163
+ // Disable the integrator.
122
164
itg -> trajectory_running = false;
123
165
itg -> time_pause_begin = time_now ;
124
166
}
125
167
168
+ /**
169
+ * Tests if the position integrator is paused.
170
+ *
171
+ * @param [inout] itg Speed integrator instance.
172
+ * @return True if integration is paused, false if not.
173
+ */
126
174
bool pbio_position_integrator_is_paused (pbio_position_integrator_t * itg ) {
127
175
return !itg -> trajectory_running ;
128
176
}
129
177
178
+ /**
179
+ * Resumes the position integrator at the current time.
180
+ *
181
+ * @param [inout] itg Speed integrator instance.
182
+ * @param [in] time_now The wall time (ticks).
183
+ */
130
184
void pbio_position_integrator_resume (pbio_position_integrator_t * itg , uint32_t time_now ) {
131
185
132
186
// Return if already trajectory_running
@@ -141,6 +195,13 @@ void pbio_position_integrator_resume(pbio_position_integrator_t *itg, uint32_t t
141
195
itg -> time_paused_total += time_now - itg -> time_pause_begin ;
142
196
}
143
197
198
+ /**
199
+ * Resets the position integrator state.
200
+ *
201
+ * @param [inout] itg Speed integrator instance.
202
+ * @param [in] settings Control settings instance from which to read stall settings.
203
+ * @param [in] time_now The wall time (ticks).
204
+ */
144
205
void pbio_position_integrator_reset (pbio_position_integrator_t * itg , pbio_control_settings_t * settings , uint32_t time_now ) {
145
206
146
207
// Save reference to settings.
@@ -157,6 +218,14 @@ void pbio_position_integrator_reset(pbio_position_integrator_t *itg, pbio_contro
157
218
158
219
}
159
220
221
+ /**
222
+ * Updates the position integrator state with the latest error.
223
+ *
224
+ * @param [in] itg Speed integrator instance.
225
+ * @param [in] position_error Current position error (position control units).
226
+ * @param [in] target_error Remaining error to the endpoint (position control units).
227
+ * @return Integrator state value (position control units).
228
+ */
160
229
int32_t pbio_position_integrator_update (pbio_position_integrator_t * itg , int32_t position_error , int32_t target_error ) {
161
230
162
231
int32_t error_now = position_error ;
@@ -196,6 +265,15 @@ int32_t pbio_position_integrator_update(pbio_position_integrator_t *itg, int32_t
196
265
return itg -> count_err_integral ;
197
266
}
198
267
268
+ /**
269
+ * Checks if the position integrator state indicates stalling.
270
+ *
271
+ * @param [in] itg Speed integrator instance.
272
+ * @param [in] time_now The wall time (ticks).
273
+ * @param [in] speed_now Current speed (control units).
274
+ * @param [in] speed_ref Reference speed (control units).
275
+ * @return True if stalled, false if not.
276
+ */
199
277
bool pbio_position_integrator_stalled (pbio_position_integrator_t * itg , uint32_t time_now , int32_t speed_now , int32_t speed_ref ) {
200
278
201
279
// Get integral value that would lead to maximum actuation.
0 commit comments