Skip to content

Commit 56046f5

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent c4e8ebe commit 56046f5

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

src/roscontrol-sot-controller.cpp

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ void ControlPDMotorControlData::read_from_xmlrpc_value(
7070
}
7171

7272
RCSotController::RCSotController()
73-
: // Store 32 DoFs for 5 minutes (1 Khz: 5*60*1000)
74-
// -> 124 Mo of data.
75-
type_name_("RCSotController"), simulation_mode_(false),
76-
subSampling_(0), step_(0), verbosity_level_(0), io_service_(),
77-
io_work_(io_service_), sotComputing_(false)
78-
{
73+
: // Store 32 DoFs for 5 minutes (1 Khz: 5*60*1000)
74+
// -> 124 Mo of data.
75+
type_name_("RCSotController"),
76+
simulation_mode_(false),
77+
subSampling_(0),
78+
step_(0),
79+
verbosity_level_(0),
80+
io_service_(),
81+
io_work_(io_service_),
82+
sotComputing_(false) {
7983
RESETDEBUG4();
8084
}
8185

@@ -935,7 +939,7 @@ void RCSotController::readControl(
935939
DataOneIter_.controls[i] = command_[i];
936940
}
937941

938-
} else{
942+
} else {
939943
ROS_INFO_STREAM("no control.");
940944
localStandbyEffortControlMode(ros::Duration(dtRos_));
941945
localStandbyVelocityControlMode(ros::Duration(dtRos_));
@@ -973,8 +977,8 @@ void RCSotController::one_iteration() {
973977
}
974978

975979
// Wait until last subsampling step to write result in controlValues_
976-
while(step_ != subSampling_ - 1){
977-
ros::Duration(.01*dtRos_).sleep();
980+
while (step_ != subSampling_ - 1) {
981+
ros::Duration(.01 * dtRos_).sleep();
978982
}
979983
// mutex
980984
mutex_.lock();
@@ -1078,79 +1082,79 @@ void RCSotController::localStandbyPositionControlMode() {
10781082
first_time = false;
10791083
}
10801084

1081-
void RCSotController::computeSubSampling(const ros::Duration &period)
1082-
{
1083-
if (subSampling_ != 0) return; // already computed
1085+
void RCSotController::computeSubSampling(const ros::Duration &period) {
1086+
if (subSampling_ != 0) return; // already computed
10841087
dtRos_ = period.toSec();
1085-
double ratio = dt_/dtRos_;
1086-
if (fabs(ratio - std::round(ratio)) > 1e-8){
1088+
double ratio = dt_ / dtRos_;
1089+
if (fabs(ratio - std::round(ratio)) > 1e-8) {
10871090
std::ostringstream os;
10881091
os << "SoT period (" << dt_ << ") is not a multiple of roscontrol period ("
1089-
<< dtRos_ << "). Modify rosparam /sot_controller/dt to a "
1090-
"multiple of roscontrol period.";
1092+
<< dtRos_
1093+
<< "). Modify rosparam /sot_controller/dt to a "
1094+
"multiple of roscontrol period.";
10911095
throw std::runtime_error(os.str().c_str());
10921096
}
10931097
subSampling_ = static_cast<std::size_t>(std::round(ratio));
10941098
// First time method update is called, step_ will turn to 1.
1095-
step_ = subSampling_ -1;
1099+
step_ = subSampling_ - 1;
10961100
ROS_INFO_STREAM("Subsampling SoT graph computation by ratio "
1097-
<< subSampling_);
1098-
if (subSampling_ != 1){
1101+
<< subSampling_);
1102+
if (subSampling_ != 1) {
10991103
// create thread in this method instead of in constructor to inherit the
11001104
// same priority level.
11011105
// If subsampling is equal to one, the control graph is evaluated in the
11021106
// same thread.
1103-
thread_pool_.create_thread(boost::bind(&boost::asio::io_service::run,
1104-
&io_service_));
1107+
thread_pool_.create_thread(
1108+
boost::bind(&boost::asio::io_service::run, &io_service_));
11051109
}
1106-
11071110
}
11081111
void RCSotController::update(const ros::Time &, const ros::Duration &period) {
11091112
// Do not send any control if the dynamic graph is not started
11101113
if (!isDynamicGraphStopped()) {
11111114
// Increment step at beginning of this method since the end time may
11121115
// vary a lot.
1113-
++ step_;
1116+
++step_;
11141117
if (step_ == subSampling_) step_ = 0;
11151118
try {
11161119
// If subsampling is equal to 1, i.e. no subsampling, evaluate
11171120
// control graph in this thread
1118-
if (subSampling_ == 1){
1119-
one_iteration();
1121+
if (subSampling_ == 1) {
1122+
one_iteration();
11201123
} else {
1121-
if (step_ == 0){
1122-
// If previous graph evaluation is not finished, do not start a new
1123-
// one.
1124-
if (!sotComputing_){
1125-
io_service_.post(boost::bind(&RCSotController::one_iteration,this));
1126-
} else{
1127-
ROS_INFO_STREAM("Sot computation not finished yet.");
1128-
}
1129-
// protected read control
1130-
mutex_.lock();
1131-
/// Read the control values
1132-
readControl(controlValues_);
1133-
mutex_.unlock();
1134-
} else{
1135-
// protected read control
1136-
mutex_.lock();
1137-
// If the robot is controlled in position, update the reference
1138-
// posture with the latest velocity.
1139-
// Note that the entry "velocity" only exists if the robot is
1140-
// controlled in position.
1141-
if (controlValues_.find("velocity") != controlValues_.end()){
1142-
std::vector<double> control = controlValues_["control"].getValues();
1143-
const std::vector<double>& velocity = controlValues_["velocity"].
1144-
getValues();
1145-
for (std::size_t i=0; i<control.size(); ++i){
1146-
control[i] += dtRos_ * velocity[i];
1147-
}
1148-
controlValues_["control"].setValues(control);
1149-
}
1150-
/// Read the control values
1151-
readControl(controlValues_);
1152-
mutex_.unlock();
1153-
}
1124+
if (step_ == 0) {
1125+
// If previous graph evaluation is not finished, do not start a new
1126+
// one.
1127+
if (!sotComputing_) {
1128+
io_service_.post(
1129+
boost::bind(&RCSotController::one_iteration, this));
1130+
} else {
1131+
ROS_INFO_STREAM("Sot computation not finished yet.");
1132+
}
1133+
// protected read control
1134+
mutex_.lock();
1135+
/// Read the control values
1136+
readControl(controlValues_);
1137+
mutex_.unlock();
1138+
} else {
1139+
// protected read control
1140+
mutex_.lock();
1141+
// If the robot is controlled in position, update the reference
1142+
// posture with the latest velocity.
1143+
// Note that the entry "velocity" only exists if the robot is
1144+
// controlled in position.
1145+
if (controlValues_.find("velocity") != controlValues_.end()) {
1146+
std::vector<double> control = controlValues_["control"].getValues();
1147+
const std::vector<double> &velocity =
1148+
controlValues_["velocity"].getValues();
1149+
for (std::size_t i = 0; i < control.size(); ++i) {
1150+
control[i] += dtRos_ * velocity[i];
1151+
}
1152+
controlValues_["control"].setValues(control);
1153+
}
1154+
/// Read the control values
1155+
readControl(controlValues_);
1156+
mutex_.unlock();
1157+
}
11541158
}
11551159

11561160
} catch (std::exception const &exc) {

src/roscontrol-sot-controller.hh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
#ifndef RC_SOT_CONTROLLER_H
66
#define RC_SOT_CONTROLLER_H
77

8-
#include <map>
9-
#include <string>
108
#include <atomic>
11-
12-
#include <boost/thread/thread.hpp>
139
#include <boost/asio/io_service.hpp>
10+
#include <boost/thread/thread.hpp>
11+
#include <map>
12+
#include <string>
1413

1514
#pragma GCC diagnostic push
1615
#pragma GCC system_header
@@ -78,8 +77,7 @@ class RCSotController : public lci::ControllerBase, SotLoaderBasic {
7877
/// Data log.
7978
rc_sot_system::DataToLog DataOneIter_;
8079

81-
private:
82-
80+
private:
8381
void computeSubSampling(const ros::Duration &period);
8482
/// @{ \name Ros-control related fields
8583

@@ -150,7 +148,7 @@ private:
150148
/// ratio between sot control period and roscontrol control period
151149
std::size_t subSampling_;
152150
/// iteration index of the subsampling. Ranges from 0 to subSampling_-1
153-
std::atomic <std::size_t> step_;
151+
std::atomic<std::size_t> step_;
154152
/// double roscontrol sampling period
155153
double dtRos_;
156154
/// \brief Verbosity level for ROS messages during initRequest/initialization
@@ -172,7 +170,8 @@ private:
172170
// Flag informing whether the graph computation is running to avoid starting
173171
// a computation if the previous one has no finished.
174172
bool sotComputing_;
175-
public:
173+
174+
public:
176175
RCSotController();
177176

178177
virtual ~RCSotController();

0 commit comments

Comments
 (0)