Skip to content

Commit 3fce2b0

Browse files
author
Aart Stuurman
committed
Format
1 parent f949242 commit 3fce2b0

File tree

1 file changed

+66
-64
lines changed

1 file changed

+66
-64
lines changed

cpprevolve/revolve/gazebo/plugin/RobotController.cpp

+66-64
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*/
1919

20-
#include <stdexcept>
20+
#include <stdexcept>
2121

2222
#include <gazebo/sensors/sensors.hh>
2323

@@ -54,62 +54,65 @@ void RobotController::Load(
5454
::gazebo::physics::ModelPtr _parent,
5555
sdf::ElementPtr _sdf)
5656
{
57-
try {
58-
// Store the pointer to the model / world
59-
this->model_ = _parent;
60-
this->world_ = _parent->GetWorld();
61-
this->initTime_ = this->world_->SimTime().Double();
62-
63-
// Create transport node
64-
this->node_.reset(new gz::transport::Node());
65-
this->node_->Init();
66-
67-
// Subscribe to robot battery state updater
68-
this->batterySetSub_ = this->node_->Subscribe(
69-
"~/battery_level/request",
70-
&RobotController::UpdateBattery,
71-
this);
72-
this->batterySetPub_ = this->node_->Advertise<gz::msgs::Response>(
73-
"~/battery_level/response");
74-
75-
if (not _sdf->HasElement("rv:robot_config")) {
76-
std::cerr
77-
<< "No `rv:robot_config` element found, controller not initialized."
78-
<< std::endl;
79-
return;
80-
}
81-
82-
auto robotConfiguration = _sdf->GetElement("rv:robot_config");
83-
84-
if (robotConfiguration->HasElement("rv:update_rate")) {
85-
auto updateRate = robotConfiguration->GetElement("rv:update_rate")->Get<double>();
86-
this->actuationTime_ = 1.0 / updateRate;
87-
}
88-
89-
// Load motors
90-
this->motorFactory_ = this->MotorFactory(_parent);
91-
this->LoadActuators(robotConfiguration);
92-
93-
// Load sensors
94-
this->sensorFactory_ = this->SensorFactory(_parent);
95-
this->LoadSensors(robotConfiguration);
96-
97-
// Load brain, this needs to be done after the motors and sensors so they
98-
// can potentially be reordered.
99-
this->LoadBrain(robotConfiguration);
100-
101-
// Call the battery loader
102-
this->LoadBattery(robotConfiguration);
103-
104-
// Call startup function which decides on actuation
105-
this->Startup(_parent, _sdf);
57+
try
58+
{
59+
// Store the pointer to the model / world
60+
this->model_ = _parent;
61+
this->world_ = _parent->GetWorld();
62+
this->initTime_ = this->world_->SimTime().Double();
63+
64+
// Create transport node
65+
this->node_.reset(new gz::transport::Node());
66+
this->node_->Init();
67+
68+
// Subscribe to robot battery state updater
69+
this->batterySetSub_ = this->node_->Subscribe(
70+
"~/battery_level/request",
71+
&RobotController::UpdateBattery,
72+
this);
73+
this->batterySetPub_ = this->node_->Advertise<gz::msgs::Response>(
74+
"~/battery_level/response");
75+
76+
if (not _sdf->HasElement("rv:robot_config"))
77+
{
78+
std::cerr
79+
<< "No `rv:robot_config` element found, controller not initialized."
80+
<< std::endl;
81+
return;
10682
}
107-
catch (const std::exception &e)
83+
84+
auto robotConfiguration = _sdf->GetElement("rv:robot_config");
85+
86+
if (robotConfiguration->HasElement("rv:update_rate"))
10887
{
109-
std::cerr << "Error Loading the Robot Controller, exception: " << std::endl
110-
<< e.what() << std::endl;
111-
throw;
88+
auto updateRate = robotConfiguration->GetElement("rv:update_rate")->Get<double>();
89+
this->actuationTime_ = 1.0 / updateRate;
11290
}
91+
92+
// Load motors
93+
this->motorFactory_ = this->MotorFactory(_parent);
94+
this->LoadActuators(robotConfiguration);
95+
96+
// Load sensors
97+
this->sensorFactory_ = this->SensorFactory(_parent);
98+
this->LoadSensors(robotConfiguration);
99+
100+
// Load brain, this needs to be done after the motors and sensors so they
101+
// can potentially be reordered.
102+
this->LoadBrain(robotConfiguration);
103+
104+
// Call the battery loader
105+
this->LoadBattery(robotConfiguration);
106+
107+
// Call startup function which decides on actuation
108+
this->Startup(_parent, _sdf);
109+
}
110+
catch (const std::exception &e)
111+
{
112+
std::cerr << "Error Loading the Robot Controller, exception: " << std::endl
113+
<< e.what() << std::endl;
114+
throw;
115+
}
113116
}
114117

115118
/////////////////////////////////////////////////
@@ -143,8 +146,7 @@ void RobotController::UpdateBattery(ConstRequestPtr &_request)
143146
/////////////////////////////////////////////////
144147
void RobotController::LoadActuators(const sdf::ElementPtr _sdf)
145148
{
146-
if (not _sdf->HasElement("rv:brain")
147-
or not _sdf->GetElement("rv:brain")->HasElement("rv:actuators"))
149+
if (not _sdf->HasElement("rv:brain") or not _sdf->GetElement("rv:brain")->HasElement("rv:actuators"))
148150
{
149151
return;
150152
}
@@ -166,8 +168,7 @@ void RobotController::LoadActuators(const sdf::ElementPtr _sdf)
166168
/////////////////////////////////////////////////
167169
void RobotController::LoadSensors(const sdf::ElementPtr _sdf)
168170
{
169-
if (not _sdf->HasElement("rv:brain")
170-
or not _sdf->GetElement("rv:brain")->HasElement("rv:sensors"))
171+
if (not _sdf->HasElement("rv:brain") or not _sdf->GetElement("rv:brain")->HasElement("rv:sensors"))
171172
{
172173
return;
173174
}
@@ -218,8 +219,9 @@ void RobotController::LoadBrain(const sdf::ElementPtr _sdf)
218219
}
219220
else if ("rlpower" == learner and "spline" == controller_type)
220221
{
221-
if (not motors_.empty()) {
222-
brain_.reset(new RLPower(this->model_, brain_sdf, motors_, sensors_));
222+
if (not motors_.empty())
223+
{
224+
brain_.reset(new RLPower(this->model_, brain_sdf, motors_, sensors_));
223225
}
224226
}
225227
else if ("bo" == learner and "cpg" == controller_type)
@@ -228,16 +230,16 @@ void RobotController::LoadBrain(const sdf::ElementPtr _sdf)
228230
}
229231
else if ("offline" == learner and "cpg" == controller_type)
230232
{
231-
brain_.reset(new DifferentialCPGClean(brain_sdf, motors_));
233+
brain_.reset(new DifferentialCPGClean(brain_sdf, motors_));
232234
}
233235
else if ("offline" == learner and "cppn-cpg" == controller_type)
234236
{
235-
brain_.reset(new DifferentialCPPNCPG(brain_sdf, motors_));
237+
brain_.reset(new DifferentialCPPNCPG(brain_sdf, motors_));
236238
}
237239
else if ("offline" == learner and "fixed-angle" == controller_type)
238240
{
239241
double angle = std::stod(
240-
brain_sdf->GetElement("rv:controller")->GetAttribute("angle")->GetAsString());
242+
brain_sdf->GetElement("rv:controller")->GetAttribute("angle")->GetAsString());
241243
brain_.reset(new FixedAngleController(angle));
242244
}
243245
else
@@ -295,7 +297,7 @@ double RobotController::BatteryLevel()
295297
return 0.0;
296298
}
297299

298-
return batteryElem_->GetElement("rv:level")->Get< double >();
300+
return batteryElem_->GetElement("rv:level")->Get<double>();
299301
}
300302

301303
/////////////////////////////////////////////////

0 commit comments

Comments
 (0)