Skip to content

Commit 38c99db

Browse files
author
Enrique Fernández Perdomo
committed
Merge pull request #31 from clearpathrobotics/add_limited_state
Add limited state
2 parents 279e145 + 4c30982 commit 38c99db

File tree

3 files changed

+63
-27
lines changed

3 files changed

+63
-27
lines changed

diff_drive_controller/include/diff_drive_controller/diff_drive_controller.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ namespace diff_drive_controller
180180
double left_velocity_estimated_average_previous_;
181181
double right_velocity_estimated_average_previous_;
182182

183-
double left_velocity_command_previous_;
184-
double right_velocity_command_previous_;
183+
double left_velocity_desired_previous_;
184+
double right_velocity_desired_previous_;
185+
186+
double left_velocity_limited_previous_;
187+
double right_velocity_limited_previous_;
185188

186189
/// Dynamic reconfigure server related:
187190
typedef dynamic_reconfigure::Server<DiffDriveControllerConfig> ReconfigureServer;

diff_drive_controller/msg/DiffDriveControllerState.msg

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
# This initial part is the same as JointTrajectoryControllerState.msg:
1+
# This initial part is the same as JointTrajectoryControllerState.msg.
2+
# Note theat error = desired - actual, as it's generally done in any controller:
23
Header header
34
string[] joint_names
45
trajectory_msgs/JointTrajectoryPoint desired
56
trajectory_msgs/JointTrajectoryPoint actual
67
trajectory_msgs/JointTrajectoryPoint error # Redundant, but useful
78

9+
# Limited state, which might be different from the desired because of the
10+
# velocity/acceleration/jerk limits enforced, when they're enabled:
11+
trajectory_msgs/JointTrajectoryPoint limited
12+
813
# Actual wheel joint trajectory controller state estimated from the positions,
914
# i.e. the velocities are NOT the ones directly reported from the joints, but
1015
# they're differentiated from the positions instead:

diff_drive_controller/src/diff_drive_controller.cpp

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ namespace diff_drive_controller
384384
state_pub_->msg_.actual.accelerations.resize(2 * wheel_joints_size_);
385385
state_pub_->msg_.actual.effort.resize(2 * wheel_joints_size_);
386386

387+
state_pub_->msg_.limited.positions.resize(2 * wheel_joints_size_);
388+
state_pub_->msg_.limited.velocities.resize(2 * wheel_joints_size_);
389+
state_pub_->msg_.limited.accelerations.resize(2 * wheel_joints_size_);
390+
state_pub_->msg_.limited.effort.resize(2 * wheel_joints_size_);
391+
387392
state_pub_->msg_.error.positions.resize(2 * wheel_joints_size_);
388393
state_pub_->msg_.error.velocities.resize(2 * wheel_joints_size_);
389394
state_pub_->msg_.error.accelerations.resize(2 * wheel_joints_size_);
@@ -446,8 +451,11 @@ namespace diff_drive_controller
446451
left_velocities_estimated_previous_.resize(wheel_joints_size_, 0.0);
447452
right_velocities_estimated_previous_.resize(wheel_joints_size_, 0.0);
448453

449-
left_velocity_command_previous_ = 0.0;
450-
right_velocity_command_previous_ = 0.0;
454+
left_velocity_desired_previous_ = 0.0;
455+
right_velocity_desired_previous_ = 0.0;
456+
457+
left_velocity_limited_previous_ = 0.0;
458+
right_velocity_limited_previous_ = 0.0;
451459

452460
// Get the joint object to use in the realtime loop
453461
for (int i = 0; i < wheel_joints_size_; ++i)
@@ -664,6 +672,10 @@ namespace diff_drive_controller
664672
curr_cmd.ang = 0.0;
665673
}
666674

675+
// Compute desired (not limited) wheel velocity:
676+
const double left_velocity_desired = (curr_cmd.lin - curr_cmd.ang * ws / 2.0)/wrl;
677+
const double right_velocity_desired = (curr_cmd.lin + curr_cmd.ang * ws / 2.0)/wrl;
678+
667679
// Limit velocities and accelerations:
668680
// @todo add an option to limit the velocity considering the actual velocity
669681
limiter_lin_.limit(curr_cmd.lin, last0_cmd_.lin, last1_cmd_.lin, control_period);
@@ -673,14 +685,14 @@ namespace diff_drive_controller
673685
last0_cmd_ = curr_cmd;
674686

675687
// Compute wheels velocities:
676-
const double left_velocity_command = (curr_cmd.lin - curr_cmd.ang * ws / 2.0)/wrl;
677-
const double right_velocity_command = (curr_cmd.lin + curr_cmd.ang * ws / 2.0)/wrr;
688+
const double left_velocity_limited = (curr_cmd.lin - curr_cmd.ang * ws / 2.0)/wrl;
689+
const double right_velocity_limited = (curr_cmd.lin + curr_cmd.ang * ws / 2.0)/wrr;
678690

679691
// Set wheels velocities:
680692
for (size_t i = 0; i < wheel_joints_size_; ++i)
681693
{
682-
left_wheel_joints_[i].setCommand(left_velocity_command);
683-
right_wheel_joints_[i].setCommand(right_velocity_command);
694+
left_wheel_joints_[i].setCommand(left_velocity_limited);
695+
right_wheel_joints_[i].setCommand(right_velocity_limited);
684696
}
685697

686698
// Publish limited velocity command:
@@ -701,13 +713,13 @@ namespace diff_drive_controller
701713
{
702714
state_pub_->msg_.header.stamp = time;
703715

704-
// Set left wheel joints desired, actual (and actual estimated) state:
716+
// Set left wheel joints desired, actual (estimated), limited state:
705717
for (size_t i = 0; i < wheel_joints_size_; ++i)
706718
{
707719
// Desired state:
708-
state_pub_->msg_.desired.accelerations[i] = (left_velocity_command - left_velocity_command_previous_) * control_frequency;
709-
state_pub_->msg_.desired.velocities[i] = left_velocity_command;
710-
state_pub_->msg_.desired.positions[i] += left_velocity_command * control_period;
720+
state_pub_->msg_.desired.accelerations[i] = (left_velocity_desired - left_velocity_desired_previous_) * control_frequency;
721+
state_pub_->msg_.desired.velocities[i] = left_velocity_desired;
722+
state_pub_->msg_.desired.positions[i] += left_velocity_desired * control_period;
711723
state_pub_->msg_.desired.effort[i] = std::numeric_limits<double>::quiet_NaN();
712724

713725
// Actual state:
@@ -725,15 +737,21 @@ namespace diff_drive_controller
725737
state_pub_->msg_.actual_estimated.velocities[i] = left_velocities_estimated_[i];
726738
state_pub_->msg_.actual_estimated.positions[i] = left_positions_estimated_[i];
727739
state_pub_->msg_.actual_estimated.effort[i] = state_pub_->msg_.actual.effort[i];
740+
741+
// Limited state:
742+
state_pub_->msg_.limited.accelerations[i] = (left_velocity_limited - left_velocity_limited_previous_) * control_frequency;
743+
state_pub_->msg_.limited.velocities[i] = left_velocity_limited;
744+
state_pub_->msg_.limited.positions[i] += left_velocity_limited * control_period;
745+
state_pub_->msg_.limited.effort[i] = std::numeric_limits<double>::quiet_NaN();
728746
}
729747

730-
// Set right wheel joints desired, actual (and actual estimated) state:
748+
// Set right wheel joints desired, actual (estimated), limited state:
731749
for (size_t i = 0, j = wheel_joints_size_; i < wheel_joints_size_; ++i, ++j)
732750
{
733751
// Desired state:
734-
state_pub_->msg_.desired.accelerations[j] = (right_velocity_command - right_velocity_command_previous_) * control_frequency;
735-
state_pub_->msg_.desired.velocities[j] = right_velocity_command;
736-
state_pub_->msg_.desired.positions[j] += right_velocity_command * control_period;
752+
state_pub_->msg_.desired.accelerations[j] = (right_velocity_desired - right_velocity_desired_previous_) * control_frequency;
753+
state_pub_->msg_.desired.velocities[j] = right_velocity_desired;
754+
state_pub_->msg_.desired.positions[j] += right_velocity_desired * control_period;
737755
state_pub_->msg_.desired.effort[j] = std::numeric_limits<double>::quiet_NaN();
738756

739757
// Actual state:
@@ -751,6 +769,12 @@ namespace diff_drive_controller
751769
state_pub_->msg_.actual_estimated.velocities[j] = right_velocities_estimated_[i];
752770
state_pub_->msg_.actual_estimated.positions[j] = right_positions_estimated_[i];
753771
state_pub_->msg_.actual_estimated.effort[j] = state_pub_->msg_.actual.effort[j];
772+
773+
// Limited state:
774+
state_pub_->msg_.limited.accelerations[j] = (right_velocity_limited - right_velocity_limited_previous_) * control_frequency;
775+
state_pub_->msg_.limited.velocities[j] = right_velocity_limited;
776+
state_pub_->msg_.limited.positions[j] += right_velocity_limited * control_period;
777+
state_pub_->msg_.limited.effort[j] = std::numeric_limits<double>::quiet_NaN();
754778
}
755779

756780
// Set left wheel joints actual (and actual estimated) side average
@@ -785,6 +809,7 @@ namespace diff_drive_controller
785809
state_pub_->msg_.actual_estimated_side_average.positions[1] = right_position_estimated_average;
786810
state_pub_->msg_.actual_estimated_side_average.effort[1] = state_pub_->msg_.actual.effort[wheel_joints_size_];
787811

812+
// Set state error:
788813
for (size_t i = 0; i < 2 * wheel_joints_size_; ++i)
789814
{
790815
state_pub_->msg_.error.positions[i] =
@@ -805,14 +830,14 @@ namespace diff_drive_controller
805830
state_pub_->msg_.error_estimated.effort[i] =
806831
state_pub_->msg_.desired.effort[i] - state_pub_->msg_.actual_estimated.effort[i];
807832

808-
state_pub_->msg_.error_estimated.positions[i] =
809-
state_pub_->msg_.desired.positions[i] - state_pub_->msg_.actual_estimated.positions[i];
810-
state_pub_->msg_.error_estimated.velocities[i] =
811-
state_pub_->msg_.desired.velocities[i] - state_pub_->msg_.actual_estimated.velocities[i];
812-
state_pub_->msg_.error_estimated.accelerations[i] =
813-
state_pub_->msg_.desired.accelerations[i] - state_pub_->msg_.actual_estimated.accelerations[i];
814-
state_pub_->msg_.error_estimated.effort[i] =
815-
state_pub_->msg_.desired.effort[i] - state_pub_->msg_.actual_estimated.effort[i];
833+
state_pub_->msg_.error_side_average.positions[i] =
834+
state_pub_->msg_.desired.positions[i] - state_pub_->msg_.actual_side_average.positions[i];
835+
state_pub_->msg_.error_side_average.velocities[i] =
836+
state_pub_->msg_.desired.velocities[i] - state_pub_->msg_.actual_side_average.velocities[i];
837+
state_pub_->msg_.error_side_average.accelerations[i] =
838+
state_pub_->msg_.desired.accelerations[i] - state_pub_->msg_.actual_side_average.accelerations[i];
839+
state_pub_->msg_.error_side_average.effort[i] =
840+
state_pub_->msg_.desired.effort[i] - state_pub_->msg_.actual_side_average.effort[i];
816841

817842
state_pub_->msg_.error_estimated_side_average.positions[i] =
818843
state_pub_->msg_.desired.positions[i] - state_pub_->msg_.actual_estimated_side_average.positions[i];
@@ -871,8 +896,11 @@ namespace diff_drive_controller
871896
left_velocity_estimated_average_previous_ = left_velocity_estimated_average;
872897
right_velocity_estimated_average_previous_ = right_velocity_estimated_average;
873898

874-
left_velocity_command_previous_ = left_velocity_command;
875-
right_velocity_command_previous_ = right_velocity_command;
899+
left_velocity_desired_previous_ = left_velocity_desired;
900+
right_velocity_desired_previous_ = right_velocity_desired;
901+
902+
left_velocity_limited_previous_ = left_velocity_limited;
903+
right_velocity_limited_previous_ = right_velocity_limited;
876904
}
877905

878906
// Save wheel joints positions, needed to estimate the velocities:

0 commit comments

Comments
 (0)