NRP Core  1.4.1
model_datapack_controller.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef MODEL_GRPC_DATAPACK_CONTROLLER_H
23 #define MODEL_GRPC_DATAPACK_CONTROLLER_H
24 
25 #include <gazebo/gazebo.hh>
26 #include <gazebo/physics/Model.hh>
29 #include "nrp_protobuf/engine_grpc.grpc.pb.h"
30 #include "nrp_protobuf/gazebo.pb.h"
31 
32 namespace gazebo
33 {
38  : public DataPackController<google::protobuf::Message>
39  {
40  template<class T>
41  constexpr static float ToFloat(const T &val)
42  { return static_cast<float>(val); }
43 
44  public:
45 
46  ModelGrpcDataPackController(const std::string &modelName, const physics::ModelPtr &model)
47  : _name(modelName),
48  _model(model)
49  {}
50 
51  virtual void handleDataPackData(const google::protobuf::Message &data) override
52  {
53  // throws bad_cast
54  const auto &l = dynamic_cast<const Gazebo::Model &>(data);
55 
56  // Set Pose
57  if(l.position_size() || l.rotation_size()) {
58  auto pose = this->_model->WorldPose();
59 
60  if(l.position_size() && l.position_size() == 3) {
61  pose.Pos().X(l.position().Get(0));
62  pose.Pos().Y(l.position().Get(1));
63  pose.Pos().Z(l.position().Get(2));
64  }
65  else if(l.position_size())
66  throw NRPException::logCreate("Model msg position wrong");
67 
68  if(l.rotation_size() && l.rotation_size() == 4) {
69  pose.Rot().X(l.rotation().Get(0));
70  pose.Rot().Y(l.rotation().Get(1));
71  pose.Rot().Z(l.rotation().Get(2));
72  pose.Rot().W(l.rotation().Get(3));
73  }
74  else if(l.rotation_size())
75  throw NRPException::logCreate("Model msg rotation wrong");
76 
77  this->_model->SetWorldPose(pose);
78  }
79 
80  // Set Linear velocity
81  if(l.linearvelocity_size() && l.linearvelocity_size() == 3) {
82  auto linvel = this->_model->WorldLinearVel();
83  linvel.X(l.linearvelocity().Get(0));
84  linvel.Y(l.linearvelocity().Get(1));
85  linvel.Z(l.linearvelocity().Get(2));
86  this->_model->SetLinearVel(linvel);
87  }
88  else if(l.linearvelocity_size())
89  throw NRPException::logCreate("Model msg linear velocity wrong");
90 
91  // Set Angular velocity
92  if(l.angularvelocity_size() && l.angularvelocity_size() == 3) {
93  auto angvel = this->_model->WorldAngularVel();
94  angvel.X(l.angularvelocity().Get(0));
95  angvel.Y(l.angularvelocity().Get(1));
96  angvel.Z(l.angularvelocity().Get(2));
97  this->_model->SetAngularVel(angvel);
98  }
99  else if(l.angularvelocity_size())
100  throw NRPException::logCreate("Model msg angular velocity wrong");
101  }
102 
103  virtual google::protobuf::Message *getDataPackInformation() override
104  {
105  auto l = new Gazebo::Model();
106 
107  const auto &pose = this->_model->WorldPose();
108  l->add_position(ToFloat(pose.Pos().X()));
109  l->add_position(ToFloat(pose.Pos().Y()));
110  l->add_position(ToFloat(pose.Pos().Z()));
111 
112  l->add_rotation(ToFloat(pose.Rot().X()));
113  l->add_rotation(ToFloat(pose.Rot().Y()));
114  l->add_rotation(ToFloat(pose.Rot().Z()));
115  l->add_rotation(ToFloat(pose.Rot().W()));
116 
117  const auto &linVel = this->_model->WorldLinearVel();
118  l->add_linearvelocity(ToFloat(linVel.X()));
119  l->add_linearvelocity(ToFloat(linVel.Y()));
120  l->add_linearvelocity(ToFloat(linVel.Z()));
121 
122  const auto &angVel = this->_model->WorldAngularVel();
123  l->add_angularvelocity(ToFloat(angVel.X()));
124  l->add_angularvelocity(ToFloat(angVel.Y()));
125  l->add_angularvelocity(ToFloat(angVel.Z()));
126 
127  return l;
128  }
129 
130  private:
134  std::string _name;
135 
139  physics::ModelPtr _model;
140  };
141 }
142 
143 #endif // MODEL_GRPC_DATAPACK_CONTROLLER_H
gazebo
Definition: camera_datapack_controller.h:34
gazebo::ModelGrpcDataPackController
Interface for Models.
Definition: model_datapack_controller.h:37
gazebo::ModelGrpcDataPackController::handleDataPackData
virtual void handleDataPackData(const google::protobuf::Message &data) override
Handle received datapack data.
Definition: model_datapack_controller.h:51
nrp_exceptions.h
gazebo::ModelGrpcDataPackController::getDataPackInformation
virtual google::protobuf::Message * getDataPackInformation() override
Get datapack information to be forwarded to the NRP.
Definition: model_datapack_controller.h:103
DataPackController
Helper class to handle DataPacks on the Engine Server side.
Definition: datapack_controller.h:34
datapack_controller.h
gazebo::ModelGrpcDataPackController::ModelGrpcDataPackController
ModelGrpcDataPackController(const std::string &modelName, const physics::ModelPtr &model)
Definition: model_datapack_controller.h:46
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73