Skip to content

Commit d956d1a

Browse files
committed
pbio/control: Rename load attribute to pid_average.
This is a more accurate description and avoids questionable sign definitions. Also add notes in pybricks.common.Control about methods deprecated in future versions.
1 parent f1a8185 commit d956d1a

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
catches the `SystemExit` exception ([pybricks-micropython#117]).
1313
- Changed `PrimeHub.display.image()` to `PrimeHub.display.icon()` and renamed
1414
its kwarg from `image` to `icon` ([support#409]).
15+
- Deprecated `Control.load()`, `Control.stalled()`, and `Control.done()`
16+
methods, but they will continue to exist in the firmware until further
17+
notice ([support#822]). New scripts are encouraged to use the (improved)
18+
variants available directly on `Motor` objects.
1519

1620
### Fixed
1721
- Fixed connecting `Remote` on BOOST move hub ([support#793]).
@@ -23,6 +27,7 @@
2327
[pybricks-micropython#117]: https://github.com/pybricks/pybricks-micropython/pull/117
2428
[support#409]: https://github.com/pybricks/support/issues/409
2529
[support#793]: https://github.com/pybricks/support/issues/793
30+
[support#793]: https://github.com/pybricks/support/issues/822
2631

2732
## [3.2.0b5] - 2022-11-11
2833

lib/pbio/include/pbio/control.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ typedef struct _pbio_control_t {
130130
/**
131131
* Slow moving average of the PID output, which is a measure for load.
132132
*/
133-
int32_t load;
133+
int32_t pid_average;
134134
/**
135135
* Flag that says whether the controller is currently stalled.
136136
*/
@@ -161,7 +161,6 @@ bool pbio_control_type_is_position(pbio_control_t *ctl);
161161
bool pbio_control_type_is_time(pbio_control_t *ctl);
162162
bool pbio_control_is_stalled(pbio_control_t *ctl, uint32_t *stall_duration);
163163
bool pbio_control_is_done(pbio_control_t *ctl);
164-
int32_t pbio_control_get_load(pbio_control_t *ctl);
165164

166165
// Start new control command:
167166

lib/pbio/src/control.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void pbio_control_update(pbio_control_t *ctl, uint32_t time_now, pbio_control_st
172172
ctl->on_target = pbio_control_check_completion(ctl, ref->time, state, &ref_end);
173173

174174
// Save (low-pass filtered) load for diagnostics
175-
ctl->load = (ctl->load * (100 - PBIO_CONFIG_CONTROL_LOOP_TIME_MS) + torque * PBIO_CONFIG_CONTROL_LOOP_TIME_MS) / 100;
175+
ctl->pid_average = (ctl->pid_average * (100 - PBIO_CONFIG_CONTROL_LOOP_TIME_MS) + torque * PBIO_CONFIG_CONTROL_LOOP_TIME_MS) / 100;
176176

177177
// Decide actuation based on whether control is on target.
178178
if (!ctl->on_target) {
@@ -273,7 +273,7 @@ void pbio_control_stop(pbio_control_t *ctl) {
273273
ctl->type = PBIO_CONTROL_NONE;
274274
ctl->on_target = true;
275275
ctl->stalled = false;
276-
ctl->load = 0;
276+
ctl->pid_average = 0;
277277
}
278278

279279
/**
@@ -703,16 +703,3 @@ bool pbio_control_is_stalled(pbio_control_t *ctl, uint32_t *stall_duration) {
703703
bool pbio_control_is_done(pbio_control_t *ctl) {
704704
return ctl->type == PBIO_CONTROL_NONE || ctl->on_target;
705705
}
706-
707-
/**
708-
* Gets the load experienced by the controller.
709-
*
710-
* It is determined as a slow moving average of the PID output, which is a
711-
* measure for how hard the controller must work to stay on target.
712-
*
713-
* @param [in] ctl The control instance.
714-
* @return The approximate load (control units).
715-
*/
716-
int32_t pbio_control_get_load(pbio_control_t *ctl) {
717-
return ctl->type == PBIO_CONTROL_NONE ? 0 : ctl->load;
718-
}

pybricks/common/pb_type_control.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,32 @@ STATIC mp_obj_t common_Control_trajectory(mp_obj_t self_in) {
231231
}
232232
MP_DEFINE_CONST_FUN_OBJ_1(common_Control_trajectory_obj, common_Control_trajectory);
233233

234+
// DELETEME: This method should not be used in V3.2 or later. It may be removed
235+
// in a future version
234236
// pybricks._common.Control.done
235237
STATIC mp_obj_t common_Control_done(mp_obj_t self_in) {
236238
common_Control_obj_t *self = MP_OBJ_TO_PTR(self_in);
237239
return mp_obj_new_bool(pbio_control_is_done(self->control));
238240
}
239241
MP_DEFINE_CONST_FUN_OBJ_1(common_Control_done_obj, common_Control_done);
240242

243+
// DELETEME: This method should not be used in V3.2 or later. It may be removed
244+
// in a future version
241245
// pybricks._common.Control.load
242246
STATIC mp_obj_t common_Control_load(mp_obj_t self_in) {
243247
common_Control_obj_t *self = MP_OBJ_TO_PTR(self_in);
248+
249+
if (!pbio_control_is_active(self->control)) {
250+
return mp_obj_new_int(0);
251+
}
252+
244253
// Read currently applied PID feedback torque and return as mNm.
245-
return mp_obj_new_int(pbio_control_get_load(self->control) / 1000);
254+
return mp_obj_new_int(self->control->pid_average / 1000);
246255
}
247256
MP_DEFINE_CONST_FUN_OBJ_1(common_Control_load_obj, common_Control_load);
248257

258+
// DELETEME: This method should not be used in V3.2 or later. It may be removed
259+
// in a future version
249260
// pybricks._common.Control.stalled
250261
STATIC mp_obj_t common_Control_stalled(mp_obj_t self_in) {
251262
common_Control_obj_t *self = MP_OBJ_TO_PTR(self_in);

0 commit comments

Comments
 (0)