Skip to content

Commit 42a1a06

Browse files
committedMar 8, 2023
pbio/integrator: Add docstrings.
1 parent ae177ea commit 42a1a06

File tree

2 files changed

+109
-21
lines changed

2 files changed

+109
-21
lines changed
 

‎lib/pbio/include/pbio/integrator.h

+20-10
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
#include <pbio/control_settings.h>
2121

2222
typedef struct _pbio_speed_integrator_t {
23-
bool running; // Whether the integrator is running (1) or paused (0)
24-
uint32_t time_pause_begin; // Time at which we began pausing, stopping integration
25-
int32_t position_error_resumed; // Position error when integration resumed again
26-
int32_t speed_err_integral_paused; // Total integrated state up to previous pause.
27-
pbio_control_settings_t *settings; // Control settings, which includes integrator settings.
23+
/** Whether the integrator is running (true) or paused (false). */
24+
bool running;
25+
/** Time at which it began pausing, stopping integration */
26+
uint32_t time_pause_begin;
27+
/** Position error when integration resumed again. */
28+
int32_t position_error_resumed;
29+
/** Total integrated state up to previous pause. */
30+
int32_t speed_err_integral_paused;
31+
/** Control settings, which includes integrator settings. */
32+
pbio_control_settings_t *settings;
2833
} pbio_speed_integrator_t;
2934

3035
// Speed integrator functions:
@@ -36,11 +41,16 @@ int32_t pbio_speed_integrator_get_error(pbio_speed_integrator_t *itg, int32_t po
3641
bool pbio_speed_integrator_stalled(pbio_speed_integrator_t *itg, uint32_t time_now, int32_t speed_now, int32_t speed_ref);
3742

3843
typedef struct _pbio_position_integrator_t {
39-
bool trajectory_running; // Whether the trajectory is running (1) or paused (0)
40-
uint32_t time_pause_begin; // Time at which we began pausing most recently, stopping integration
41-
uint32_t time_paused_total; // Total time we spent in a paused state
42-
int32_t count_err_integral; // Ongoing integral of position error
43-
pbio_control_settings_t *settings; // Control settings, which includes integrator settings.
44+
/** Whether the trajectory is running (1) or paused (0). */
45+
bool trajectory_running;
46+
/** Time at which it began pausing most recently, stopping integration. */
47+
uint32_t time_pause_begin;
48+
/** Total time spent in a paused state. */
49+
uint32_t time_paused_total;
50+
/** Ongoing integral of position error. */
51+
int32_t count_err_integral;
52+
/** Control settings, which includes integrator settings. */
53+
pbio_control_settings_t *settings;
4454
} pbio_position_integrator_t;
4555

4656
// Position integrator functions:

‎lib/pbio/src/integrator.c

+89-11
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
#include <stdint.h>
99
#include <stdlib.h>
1010

11-
#include <pbio/config.h>
1211
#include <pbio/control_settings.h>
1312
#include <pbio/integrator.h>
1413
#include <pbio/int_math.h>
1514

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+
*/
1822
void pbio_speed_integrator_pause(pbio_speed_integrator_t *itg, uint32_t time_now, int32_t position_error) {
1923

2024
// Pause only if running
@@ -28,10 +32,16 @@ void pbio_speed_integrator_pause(pbio_speed_integrator_t *itg, uint32_t time_now
2832
// Increment the paused integrator state with the integrated amount between the last resume and the newly enforced pause
2933
itg->speed_err_integral_paused += position_error - itg->position_error_resumed;
3034

31-
// Store time at which we started pausing
35+
// Store time at which we started pausing, used only for stall flag hysteresis.
3236
itg->time_pause_begin = time_now;
3337
}
3438

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+
*/
3545
void pbio_speed_integrator_resume(pbio_speed_integrator_t *itg, int32_t position_error) {
3646

3747
// Resume only if paused
@@ -47,22 +57,34 @@ void pbio_speed_integrator_resume(pbio_speed_integrator_t *itg, int32_t position
4757
itg->position_error_resumed = position_error;
4858
}
4959

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+
*/
5066
void pbio_speed_integrator_reset(pbio_speed_integrator_t *itg, pbio_control_settings_t *settings) {
5167

5268
// Save reference to settings.
5369
itg->settings = settings;
5470

55-
// Set integral to 0
71+
// Reset built up integral to 0.
5672
itg->speed_err_integral_paused = 0;
5773

58-
// Set state to paused
74+
// Set state to paused. It will resume immediately on start.
5975
itg->running = false;
6076

6177
// Resume integration
6278
pbio_speed_integrator_resume(itg, 0);
6379
}
6480

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+
*/
6688
int32_t pbio_speed_integrator_get_error(pbio_speed_integrator_t *itg, int32_t position_error) {
6789

6890
// 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
7597
return speed_err_integral;
7698
}
7799

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+
*/
78109
bool pbio_speed_integrator_stalled(pbio_speed_integrator_t *itg, uint32_t time_now, int32_t speed_now, int32_t speed_ref) {
79110
// If were running, we're not stalled
80111
if (itg->running) {
@@ -101,8 +132,13 @@ bool pbio_speed_integrator_stalled(pbio_speed_integrator_t *itg, uint32_t time_n
101132
return true;
102133
}
103134

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+
*/
106142
uint32_t pbio_position_integrator_get_ref_time(pbio_position_integrator_t *itg, uint32_t time_now) {
107143
// The wall time at which we are is either the current time, or whenever we stopped last.
108144
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,
111147
return real_time - itg->time_paused_total;
112148
}
113149

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+
*/
114156
void pbio_position_integrator_pause(pbio_position_integrator_t *itg, uint32_t time_now) {
115157

116-
// Return if already paused
158+
// Return if already paused.
117159
if (!itg->trajectory_running) {
118160
return;
119161
}
120162

121-
// Disable the integrator
163+
// Disable the integrator.
122164
itg->trajectory_running = false;
123165
itg->time_pause_begin = time_now;
124166
}
125167

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+
*/
126174
bool pbio_position_integrator_is_paused(pbio_position_integrator_t *itg) {
127175
return !itg->trajectory_running;
128176
}
129177

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+
*/
130184
void pbio_position_integrator_resume(pbio_position_integrator_t *itg, uint32_t time_now) {
131185

132186
// Return if already trajectory_running
@@ -141,6 +195,13 @@ void pbio_position_integrator_resume(pbio_position_integrator_t *itg, uint32_t t
141195
itg->time_paused_total += time_now - itg->time_pause_begin;
142196
}
143197

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+
*/
144205
void pbio_position_integrator_reset(pbio_position_integrator_t *itg, pbio_control_settings_t *settings, uint32_t time_now) {
145206

146207
// Save reference to settings.
@@ -157,6 +218,14 @@ void pbio_position_integrator_reset(pbio_position_integrator_t *itg, pbio_contro
157218

158219
}
159220

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+
*/
160229
int32_t pbio_position_integrator_update(pbio_position_integrator_t *itg, int32_t position_error, int32_t target_error) {
161230

162231
int32_t error_now = position_error;
@@ -196,6 +265,15 @@ int32_t pbio_position_integrator_update(pbio_position_integrator_t *itg, int32_t
196265
return itg->count_err_integral;
197266
}
198267

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+
*/
199277
bool pbio_position_integrator_stalled(pbio_position_integrator_t *itg, uint32_t time_now, int32_t speed_now, int32_t speed_ref) {
200278

201279
// Get integral value that would lead to maximum actuation.

0 commit comments

Comments
 (0)
Please sign in to comment.