Skip to content

Commit 7dd84a3

Browse files
committed
pbio/drivebase: Implement curved motion.
This implements a first version of driving along an arc with a given radius and angle.
1 parent 3a0f6b4 commit 7dd84a3

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎lib/pbio/include/pbio/drivebase.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void pbio_drivebase_claim_servos(pbio_drivebase_t *db, bool claim);
2323

2424
// Finite point to point control
2525

26+
pbio_error_t pbio_drivebase_curve(pbio_drivebase_t *db, int32_t radius, int32_t angle, int32_t drive_speed, int32_t turn_rate, pbio_actuation_t after_stop);
27+
2628
pbio_error_t pbio_drivebase_straight(pbio_drivebase_t *db, int32_t distance, int32_t straight_speed, pbio_actuation_t after_stop);
2729

2830
pbio_error_t pbio_drivebase_turn(pbio_drivebase_t *db, int32_t angle, int32_t turn_rate, pbio_actuation_t after_stop);

‎lib/pbio/src/drivebase.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,77 @@ pbio_error_t pbio_drivebase_update(pbio_drivebase_t *db) {
254254
return pbio_servo_actuate(db->right, dif_actuation, sum_torque / 2 - dif_torque / 2 + feed_forward_right);
255255
}
256256

257+
pbio_error_t pbio_drivebase_curve(pbio_drivebase_t *db, int32_t radius, int32_t angle, int32_t drive_speed, int32_t turn_rate, pbio_actuation_t after_stop) {
258+
259+
// Claim both servos for use by drivebase
260+
pbio_drivebase_claim_servos(db, true);
261+
262+
// Get current time
263+
int32_t time_now = pbdrv_clock_get_us();
264+
265+
// Get drive base state
266+
pbio_control_state_t state_distance;
267+
pbio_control_state_t state_heading;
268+
pbio_error_t err = pbio_drivebase_get_state(db, &state_distance, &state_heading);
269+
if (err != PBIO_SUCCESS) {
270+
return err;
271+
}
272+
273+
// Sum controller drives by the given arc length
274+
int32_t relative_sum_target = pbio_control_user_to_counts(&db->control_distance.settings, (10 * angle * radius) / 573);
275+
int32_t target_sum_rate = pbio_control_user_to_counts(&db->control_distance.settings, drive_speed);
276+
277+
err = pbio_control_start_relative_angle_control(&db->control_distance, time_now, &state_distance, relative_sum_target, target_sum_rate, after_stop);
278+
if (err != PBIO_SUCCESS) {
279+
return err;
280+
}
281+
282+
// Dif controller drives by given angle
283+
int32_t relative_dif_target = pbio_control_user_to_counts(&db->control_heading.settings, angle);
284+
int32_t target_dif_rate = pbio_control_user_to_counts(&db->control_heading.settings, turn_rate);
285+
286+
err = pbio_control_start_relative_angle_control(&db->control_heading, time_now, &state_heading, relative_dif_target, target_dif_rate, after_stop);
287+
if (err != PBIO_SUCCESS) {
288+
return err;
289+
}
290+
291+
// At this point, the two trajectories may have different durations, so they won't complete at the same time
292+
// To account for this, we re-compute the shortest trajectory to have the same duration as the longest.
293+
if (db->control_distance.trajectory.t3 == db->control_heading.trajectory.t3) {
294+
// We're done if they're already the same.
295+
return PBIO_SUCCESS;
296+
}
297+
298+
// Find out which controller takes the lead
299+
pbio_control_t *control_leader;
300+
pbio_control_t *control_follower;
301+
302+
if (db->control_distance.trajectory.t3 > db->control_heading.trajectory.t3) {
303+
// Distance control takes the longest, so it will take the lead
304+
control_leader = &db->control_distance;
305+
control_follower = &db->control_heading;
306+
} else {
307+
// Heading control takes the longest, so it will take the lead
308+
control_leader = &db->control_heading;
309+
control_follower = &db->control_distance;
310+
}
311+
312+
// Revise follower trajectory so it's just as slow as the leader, achieved
313+
// by picking a lower speed that makes the end times match.
314+
int32_t end_time = control_leader->trajectory.t3;
315+
pbio_trajectory_t *trj = &control_follower->trajectory;
316+
err = pbio_trajectory_calc_speed_new(trj, trj->t0, end_time, trj->th0, trj->th3, trj->w0, control_follower->settings.max_rate, control_follower->settings.abs_acceleration);
317+
if (err != PBIO_SUCCESS) {
318+
return err;
319+
}
320+
321+
// The follower trajector holds until the leader trajectory says otherwise
322+
control_follower->after_stop = PBIO_ACTUATION_HOLD;
323+
324+
return PBIO_SUCCESS;
325+
}
326+
327+
257328
pbio_error_t pbio_drivebase_straight(pbio_drivebase_t *db, int32_t distance, int32_t drive_speed, pbio_actuation_t after_stop) {
258329

259330
// Claim both servos for use by drivebase
@@ -328,6 +399,7 @@ pbio_error_t pbio_drivebase_turn(pbio_drivebase_t *db, int32_t angle, int32_t tu
328399

329400
return PBIO_SUCCESS;
330401
}
402+
331403
pbio_error_t pbio_drivebase_drive(pbio_drivebase_t *db, int32_t speed, int32_t turn_rate) {
332404

333405
// Claim both servos for use by drivebase

‎pybricks/robotics/pb_type_drivebase.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ STATIC mp_obj_t robotics_DriveBase_turn(size_t n_args, const mp_obj_t *pos_args,
130130
}
131131
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(robotics_DriveBase_turn_obj, 1, robotics_DriveBase_turn);
132132

133+
// pybricks.robotics.DriveBase.curve
134+
STATIC mp_obj_t robotics_DriveBase_curve(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
135+
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
136+
robotics_DriveBase_obj_t, self,
137+
PB_ARG_REQUIRED(radius),
138+
PB_ARG_REQUIRED(angle),
139+
PB_ARG_DEFAULT_OBJ(then, pb_Stop_HOLD_obj),
140+
PB_ARG_DEFAULT_TRUE(wait));
141+
142+
mp_int_t radius = pb_obj_get_int(radius_in);
143+
mp_int_t angle = pb_obj_get_int(angle_in);
144+
pbio_actuation_t then = pb_type_enum_get_value(then_in, &pb_enum_type_Stop);
145+
146+
pb_assert(pbio_drivebase_curve(self->db, radius, angle, self->straight_speed, self->turn_rate, then));
147+
148+
if (mp_obj_is_true(wait_in)) {
149+
wait_for_completion_drivebase(self->db);
150+
}
151+
152+
return mp_const_none;
153+
}
154+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(robotics_DriveBase_curve_obj, 1, robotics_DriveBase_curve);
155+
133156
// pybricks.robotics.DriveBase.drive
134157
STATIC mp_obj_t robotics_DriveBase_drive(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
135158
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
@@ -249,6 +272,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(robotics_DriveBase_settings_obj, 1, robotics_D
249272

250273
// dir(pybricks.robotics.DriveBase)
251274
STATIC const mp_rom_map_elem_t robotics_DriveBase_locals_dict_table[] = {
275+
{ MP_ROM_QSTR(MP_QSTR_curve), MP_ROM_PTR(&robotics_DriveBase_curve_obj) },
252276
{ MP_ROM_QSTR(MP_QSTR_straight), MP_ROM_PTR(&robotics_DriveBase_straight_obj) },
253277
{ MP_ROM_QSTR(MP_QSTR_turn), MP_ROM_PTR(&robotics_DriveBase_turn_obj) },
254278
{ MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&robotics_DriveBase_drive_obj) },

‎tests/motors/drivebase_curve.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pybricks.pupdevices import Motor
2+
from pybricks.tools import wait
3+
from pybricks.parameters import Port, Direction
4+
from pybricks.robotics import DriveBase
5+
from pybricks import version
6+
7+
print(version)
8+
9+
# Initialize default "Driving Base" with medium motors and wheels.
10+
left_motor = Motor(Port.C, Direction.COUNTERCLOCKWISE)
11+
right_motor = Motor(Port.D)
12+
drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=112)
13+
14+
# Allocate logs for motors and controller signals.
15+
DURATION = 6000
16+
left_motor.log.start(DURATION)
17+
right_motor.log.start(DURATION)
18+
drive_base.distance_control.log.start(DURATION)
19+
drive_base.heading_control.log.start(DURATION)
20+
21+
# Drive straight forward and back again.
22+
drive_base.curve(radius=500, angle=360)
23+
24+
# Wait so we can also log hold capability, then turn off the motor completely.
25+
wait(100)
26+
drive_base.stop()
27+
28+
# Transfer data logs.
29+
print("Transferring data...")
30+
left_motor.log.save("servo_left.txt")
31+
right_motor.log.save("servo_right.txt")
32+
drive_base.distance_control.log.save("control_distance.txt")
33+
drive_base.heading_control.log.save("control_heading.txt")
34+
print("Done")

0 commit comments

Comments
 (0)