Skip to content

Commit 279e145

Browse files
author
Enrique Fernández Perdomo
committed
Merge pull request #30 from clearpathrobotics/add_timing_to_state
Add timing to state
2 parents 1ba314f + 79b2936 commit 279e145

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

diff_drive_controller/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ find_package(catkin REQUIRED
1616
trajectory_msgs
1717
roslint)
1818

19-
find_package(Boost REQUIRED)
19+
find_package(Boost REQUIRED
20+
COMPONENTS
21+
timer)
2022

2123
find_package(Eigen REQUIRED)
2224

@@ -54,7 +56,7 @@ roslint_cpp()
5456

5557
add_library(${PROJECT_NAME} src/diff_drive_controller.cpp src/odometry.cpp src/speed_limiter.cpp)
5658
# Note that the entry for ${Ceres_LIBRARIES} was removed as we only used headers from that package
57-
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES})
59+
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${Boost_LIBRARIES})
5860
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${PROJECT_NAME}_gencfg)
5961

6062
install(TARGETS ${PROJECT_NAME}

diff_drive_controller/include/diff_drive_controller/diff_drive_controller.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include <diff_drive_controller/speed_limiter.h>
5858
#include <diff_drive_controller/DiffDriveControllerConfig.h>
5959

60+
#include <boost/timer/timer.hpp>
61+
6062
#include <vector>
6163
#include <string>
6264

@@ -185,6 +187,9 @@ namespace diff_drive_controller
185187
typedef dynamic_reconfigure::Server<DiffDriveControllerConfig> ReconfigureServer;
186188
boost::shared_ptr<ReconfigureServer> cfg_server_;
187189

190+
/// Timing related:
191+
boost::timer::cpu_timer cpu_timer_;
192+
188193
struct DynamicParams
189194
{
190195
bool pose_from_joint_position;

diff_drive_controller/msg/DiffDriveControllerState.msg

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ trajectory_msgs/JointTrajectoryPoint error_estimated_side_average
3131
float64 control_period_desired
3232
float64 control_period_actual
3333
float64 control_period_error
34+
35+
# Control time (of the actual code):
36+
# Note that the resolution of the wall time is approx. 0.5us, but the user and
37+
# system is only 10ms. See:
38+
# http://www.boost.org/doc/libs/1_48_0/libs/timer/doc/cpu_timers.html#Resolution
39+
#
40+
# For this reason, and given that the control time is much less than 10ms,
41+
# the user and system time aren't accurate; note that we can't take an average
42+
# of multiple cycles because we still have to call stop at the end of the cycle,
43+
# so the internal count of the timer would still be wrong.
44+
# Consequently, ignore the user and system time, unless the wall time becomes
45+
# greater than 10ms.
46+
float64 control_time_wall
47+
float64 control_time_user
48+
float64 control_time_system

diff_drive_controller/src/diff_drive_controller.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ namespace diff_drive_controller
466466

467467
void DiffDriveController::update(const ros::Time& time, const ros::Duration& period)
468468
{
469+
// Start/Resume CPU timer to measure the control time:
470+
if (publish_state_)
471+
{
472+
cpu_timer_.start();
473+
}
474+
469475
// UPDATE DYNAMIC PARAMS
470476
// Retreive dynamic params:
471477
DynamicParams dynamic_params = *(dynamic_params_.readFromRT());
@@ -818,6 +824,7 @@ namespace diff_drive_controller
818824
state_pub_->msg_.desired.effort[i] - state_pub_->msg_.actual_estimated_side_average.effort[i];
819825
}
820826

827+
// Set time from start:
821828
state_pub_->msg_.desired.time_from_start = ros::Duration(dt);
822829
state_pub_->msg_.actual.time_from_start = ros::Duration(control_period);
823830
state_pub_->msg_.error.time_from_start = state_pub_->msg_.actual.time_from_start;
@@ -831,10 +838,18 @@ namespace diff_drive_controller
831838
state_pub_->msg_.actual_estimated_side_average.time_from_start = state_pub_->msg_.actual.time_from_start;
832839
state_pub_->msg_.error_estimated_side_average.time_from_start = state_pub_->msg_.actual_estimated_side_average.time_from_start;
833840

841+
// Set control period (update method):
834842
state_pub_->msg_.control_period_desired = control_period_desired_;
835843
state_pub_->msg_.control_period_actual = period.toSec();
836844
state_pub_->msg_.control_period_error = state_pub_->msg_.control_period_desired - state_pub_->msg_.control_period_actual;
837845

846+
// Set control wall, user and system time:
847+
boost::timer::cpu_times control_times = cpu_timer_.elapsed();
848+
849+
state_pub_->msg_.control_time_wall = 1e-9 * control_times.wall;
850+
state_pub_->msg_.control_time_user = 1e-9 * control_times.user;
851+
state_pub_->msg_.control_time_system = 1e-9 * control_times.system;
852+
838853
state_pub_->unlockAndPublish();
839854
}
840855

0 commit comments

Comments
 (0)