17
17
*
18
18
*/
19
19
20
- #include < stdexcept>
20
+ #include < stdexcept>
21
21
22
22
#include < gazebo/sensors/sensors.hh>
23
23
@@ -54,62 +54,65 @@ void RobotController::Load(
54
54
::gazebo::physics::ModelPtr _parent,
55
55
sdf::ElementPtr _sdf)
56
56
{
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 ;
106
82
}
107
- catch (const std::exception &e)
83
+
84
+ auto robotConfiguration = _sdf->GetElement (" rv:robot_config" );
85
+
86
+ if (robotConfiguration->HasElement (" rv:update_rate" ))
108
87
{
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;
112
90
}
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
+ }
113
116
}
114
117
115
118
// ///////////////////////////////////////////////
@@ -143,8 +146,7 @@ void RobotController::UpdateBattery(ConstRequestPtr &_request)
143
146
// ///////////////////////////////////////////////
144
147
void RobotController::LoadActuators (const sdf::ElementPtr _sdf)
145
148
{
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" ))
148
150
{
149
151
return ;
150
152
}
@@ -166,8 +168,7 @@ void RobotController::LoadActuators(const sdf::ElementPtr _sdf)
166
168
// ///////////////////////////////////////////////
167
169
void RobotController::LoadSensors (const sdf::ElementPtr _sdf)
168
170
{
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" ))
171
172
{
172
173
return ;
173
174
}
@@ -218,8 +219,9 @@ void RobotController::LoadBrain(const sdf::ElementPtr _sdf)
218
219
}
219
220
else if (" rlpower" == learner and " spline" == controller_type)
220
221
{
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_));
223
225
}
224
226
}
225
227
else if (" bo" == learner and " cpg" == controller_type)
@@ -228,16 +230,16 @@ void RobotController::LoadBrain(const sdf::ElementPtr _sdf)
228
230
}
229
231
else if (" offline" == learner and " cpg" == controller_type)
230
232
{
231
- brain_.reset (new DifferentialCPGClean (brain_sdf, motors_));
233
+ brain_.reset (new DifferentialCPGClean (brain_sdf, motors_));
232
234
}
233
235
else if (" offline" == learner and " cppn-cpg" == controller_type)
234
236
{
235
- brain_.reset (new DifferentialCPPNCPG (brain_sdf, motors_));
237
+ brain_.reset (new DifferentialCPPNCPG (brain_sdf, motors_));
236
238
}
237
239
else if (" offline" == learner and " fixed-angle" == controller_type)
238
240
{
239
241
double angle = std::stod (
240
- brain_sdf->GetElement (" rv:controller" )->GetAttribute (" angle" )->GetAsString ());
242
+ brain_sdf->GetElement (" rv:controller" )->GetAttribute (" angle" )->GetAsString ());
241
243
brain_.reset (new FixedAngleController (angle));
242
244
}
243
245
else
@@ -295,7 +297,7 @@ double RobotController::BatteryLevel()
295
297
return 0.0 ;
296
298
}
297
299
298
- return batteryElem_->GetElement (" rv:level" )->Get < double >();
300
+ return batteryElem_->GetElement (" rv:level" )->Get <double >();
299
301
}
300
302
301
303
// ///////////////////////////////////////////////
0 commit comments