@@ -74,11 +74,12 @@ RCSotController::RCSotController()
74
74
// -> 124 Mo of data.
75
75
type_name_ (" RCSotController" ),
76
76
simulation_mode_ (false ),
77
- subSampling_ (0 ),
77
+ subSampling_ (1 ),
78
78
step_ (0 ),
79
79
verbosity_level_ (0 ),
80
80
io_service_ (),
81
81
io_work_ (io_service_),
82
+ thread_created_ (false ),
82
83
sotComputing_ (false ) {
83
84
RESETDEBUG4 ();
84
85
}
@@ -622,6 +623,9 @@ bool RCSotController::readParamsControlMode(ros::NodeHandle &robot_nh) {
622
623
}
623
624
624
625
bool RCSotController::readParamsdt (ros::NodeHandle &robot_nh) {
626
+ // Get subsampling ratio of the stack of task: sot period / roscontrol period
627
+ // If param does not exist default value is 1
628
+ robot_nh.getParam (" /sot_controller/subsampling" , subSampling_);
625
629
// / Read /sot_controller/dt to know what is the control period
626
630
if (robot_nh.hasParam (" /sot_controller/dt" )) {
627
631
robot_nh.getParam (" /sot_controller/dt" , dt_);
@@ -941,8 +945,8 @@ void RCSotController::readControl(
941
945
942
946
} else {
943
947
ROS_INFO_STREAM (" no control." );
944
- localStandbyEffortControlMode (ros::Duration (dtRos_ ));
945
- localStandbyVelocityControlMode (ros::Duration (dtRos_ ));
948
+ localStandbyEffortControlMode (ros::Duration (dt_ / subSampling_ ));
949
+ localStandbyVelocityControlMode (ros::Duration (dt_ / subSampling_ ));
946
950
localStandbyPositionControlMode ();
947
951
}
948
952
}
@@ -978,7 +982,7 @@ void RCSotController::one_iteration() {
978
982
979
983
// Wait until last subsampling step to write result in controlValues_
980
984
while (step_ != subSampling_ - 1 ) {
981
- ros::Duration (.01 * dtRos_ ).sleep ();
985
+ ros::Duration (.01 * dt_ / subSampling_ ).sleep ();
982
986
}
983
987
// mutex
984
988
mutex_.lock ();
@@ -1082,30 +1086,18 @@ void RCSotController::localStandbyPositionControlMode() {
1082
1086
first_time = false ;
1083
1087
}
1084
1088
1085
- void RCSotController::computeSubSampling (const ros::Duration &period) {
1086
- if (subSampling_ != 0 ) return ; // already computed
1087
- dtRos_ = period.toSec ();
1088
- double ratio = dt_ / dtRos_;
1089
- if (fabs (ratio - std::round (ratio)) > 1e-8 ) {
1090
- std::ostringstream os;
1091
- os << " SoT period (" << dt_ << " ) is not a multiple of roscontrol period ("
1092
- << dtRos_
1093
- << " ). Modify rosparam /sot_controller/dt to a "
1094
- " multiple of roscontrol period." ;
1095
- throw std::runtime_error (os.str ().c_str ());
1096
- }
1097
- subSampling_ = static_cast <std::size_t >(std::round (ratio));
1098
- // First time method update is called, step_ will turn to 1.
1099
- step_ = subSampling_ - 1 ;
1100
- ROS_INFO_STREAM (" Subsampling SoT graph computation by ratio "
1101
- << subSampling_);
1102
- if (subSampling_ != 1 ) {
1089
+ void RCSotController::computeSubSampling () {
1090
+ if ((subSampling_ != 1 ) && !thread_created_) {
1091
+ step_ = subSampling_ - 1 ;
1092
+ ROS_INFO_STREAM (" Subsampling SoT graph computation by ratio "
1093
+ << subSampling_);
1103
1094
// create thread in this method instead of in constructor to inherit the
1104
1095
// same priority level.
1105
1096
// If subsampling is equal to one, the control graph is evaluated in the
1106
1097
// same thread.
1107
1098
thread_pool_.create_thread (
1108
1099
boost::bind (&boost::asio::io_service::run, &io_service_));
1100
+ thread_created_ = true ;
1109
1101
}
1110
1102
}
1111
1103
void RCSotController::update (const ros::Time &, const ros::Duration &period) {
@@ -1120,6 +1112,7 @@ void RCSotController::update(const ros::Time &, const ros::Duration &period) {
1120
1112
// control graph in this thread
1121
1113
if (subSampling_ == 1 ) {
1122
1114
one_iteration ();
1115
+ readControl (controlValues_);
1123
1116
} else {
1124
1117
if (step_ == 0 ) {
1125
1118
// If previous graph evaluation is not finished, do not start a new
@@ -1147,7 +1140,7 @@ void RCSotController::update(const ros::Time &, const ros::Duration &period) {
1147
1140
const std::vector<double > &velocity =
1148
1141
controlValues_[" velocity" ].getValues ();
1149
1142
for (std::size_t i = 0 ; i < control.size (); ++i) {
1150
- control[i] += dtRos_ * velocity[i];
1143
+ control[i] += period. toSec () * velocity[i];
1151
1144
}
1152
1145
controlValues_[" control" ].setValues (control);
1153
1146
}
@@ -1171,7 +1164,7 @@ void RCSotController::update(const ros::Time &, const ros::Duration &period) {
1171
1164
throw ;
1172
1165
}
1173
1166
} else {
1174
- computeSubSampling (period );
1167
+ computeSubSampling ();
1175
1168
// / Update the sensors.
1176
1169
fillSensors ();
1177
1170
try {
0 commit comments