Skip to content

Commit b0c4c0a

Browse files
authored
Merge pull request #36 from florent-lamiraux/devel
[RSSotController] Fix subsampling machinery after testing on UR10.
2 parents e33f740 + 6127055 commit b0c4c0a

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

src/roscontrol-sot-controller.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ RCSotController::RCSotController()
7474
// -> 124 Mo of data.
7575
type_name_("RCSotController"),
7676
simulation_mode_(false),
77-
subSampling_(0),
77+
subSampling_(1),
7878
step_(0),
7979
verbosity_level_(0),
8080
io_service_(),
8181
io_work_(io_service_),
82+
thread_created_(false),
8283
sotComputing_(false) {
8384
RESETDEBUG4();
8485
}
@@ -622,6 +623,9 @@ bool RCSotController::readParamsControlMode(ros::NodeHandle &robot_nh) {
622623
}
623624

624625
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_);
625629
/// Read /sot_controller/dt to know what is the control period
626630
if (robot_nh.hasParam("/sot_controller/dt")) {
627631
robot_nh.getParam("/sot_controller/dt", dt_);
@@ -941,8 +945,8 @@ void RCSotController::readControl(
941945

942946
} else {
943947
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_));
946950
localStandbyPositionControlMode();
947951
}
948952
}
@@ -978,7 +982,7 @@ void RCSotController::one_iteration() {
978982

979983
// Wait until last subsampling step to write result in controlValues_
980984
while (step_ != subSampling_ - 1) {
981-
ros::Duration(.01 * dtRos_).sleep();
985+
ros::Duration(.01 * dt_ / subSampling_).sleep();
982986
}
983987
// mutex
984988
mutex_.lock();
@@ -1082,30 +1086,18 @@ void RCSotController::localStandbyPositionControlMode() {
10821086
first_time = false;
10831087
}
10841088

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_);
11031094
// create thread in this method instead of in constructor to inherit the
11041095
// same priority level.
11051096
// If subsampling is equal to one, the control graph is evaluated in the
11061097
// same thread.
11071098
thread_pool_.create_thread(
11081099
boost::bind(&boost::asio::io_service::run, &io_service_));
1100+
thread_created_ = true;
11091101
}
11101102
}
11111103
void RCSotController::update(const ros::Time &, const ros::Duration &period) {
@@ -1120,6 +1112,7 @@ void RCSotController::update(const ros::Time &, const ros::Duration &period) {
11201112
// control graph in this thread
11211113
if (subSampling_ == 1) {
11221114
one_iteration();
1115+
readControl(controlValues_);
11231116
} else {
11241117
if (step_ == 0) {
11251118
// 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) {
11471140
const std::vector<double> &velocity =
11481141
controlValues_["velocity"].getValues();
11491142
for (std::size_t i = 0; i < control.size(); ++i) {
1150-
control[i] += dtRos_ * velocity[i];
1143+
control[i] += period.toSec() * velocity[i];
11511144
}
11521145
controlValues_["control"].setValues(control);
11531146
}
@@ -1171,7 +1164,7 @@ void RCSotController::update(const ros::Time &, const ros::Duration &period) {
11711164
throw;
11721165
}
11731166
} else {
1174-
computeSubSampling(period);
1167+
computeSubSampling();
11751168
/// Update the sensors.
11761169
fillSensors();
11771170
try {

src/roscontrol-sot-controller.hh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class RCSotController : public lci::ControllerBase, SotLoaderBasic {
7878
rc_sot_system::DataToLog DataOneIter_;
7979

8080
private:
81-
void computeSubSampling(const ros::Duration &period);
81+
void computeSubSampling();
8282
/// @{ \name Ros-control related fields
8383

8484
/// \brief Vector of joint handles.
@@ -146,11 +146,9 @@ class RCSotController : public lci::ControllerBase, SotLoaderBasic {
146146
std::map<std::string, std::string> mapFromRCToSotDevice_;
147147

148148
/// ratio between sot control period and roscontrol control period
149-
std::size_t subSampling_;
149+
int subSampling_;
150150
/// iteration index of the subsampling. Ranges from 0 to subSampling_-1
151-
std::atomic<std::size_t> step_;
152-
/// double roscontrol sampling period
153-
double dtRos_;
151+
std::atomic<int> step_;
154152
/// \brief Verbosity level for ROS messages during initRequest/initialization
155153
/// phase. 0: no messages or error 1: info 2: debug
156154
int verbosity_level_;
@@ -165,6 +163,7 @@ class RCSotController : public lci::ControllerBase, SotLoaderBasic {
165163
boost::asio::io_service io_service_;
166164
boost::asio::io_service::work io_work_;
167165
boost::thread_group thread_pool_;
166+
bool thread_created_;
168167
// mutex to protect access to controlValues_
169168
std::mutex mutex_;
170169
// Flag informing whether the graph computation is running to avoid starting

0 commit comments

Comments
 (0)