@@ -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+
257328pbio_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+
331403pbio_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
0 commit comments