GCC Code Coverage Report


Directory: ./
File: src/device.cc
Date: 2025-03-07 11:10:46
Exec Total Coverage
Lines: 0 72 0.0%
Branches: 0 110 0.0%

Line Branch Exec Source
1 ///
2 /// Copyright (c) 2015 CNRS
3 /// Authors: Joseph Mirabel
4 ///
5 ///
6
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30
31 #include <boost/serialization/weak_ptr.hpp>
32 #include <hpp/manipulation/device.hh>
33 #include <hpp/manipulation/handle.hh>
34 #include <hpp/manipulation/serialization.hh>
35 #include <hpp/pinocchio/gripper.hh>
36 #include <hpp/pinocchio/joint-collection.hh>
37 #include <hpp/pinocchio/joint.hh>
38 #include <hpp/util/serialization.hh>
39 #include <pinocchio/multibody/geometry.hpp>
40 #include <pinocchio/multibody/model.hpp>
41
42 namespace hpp {
43 namespace manipulation {
44 using ::pinocchio::Frame;
45
46 pinocchio::DevicePtr_t Device::clone() const {
47 Device* ptr = new Device(*this);
48 DevicePtr_t shPtr(ptr);
49 ptr->initCopy(shPtr, *this);
50 return shPtr;
51 }
52
53 void Device::setRobotRootPosition(const std::string& rn, const Transform3s& t) {
54 FrameIndices_t idxs = robotFrames(rn);
55 if (idxs.size() == 0)
56 throw std::invalid_argument("No frame for robot name " + rn);
57
58 pinocchio::Model& m = model();
59 pinocchio::GeomModel& gm = geomModel();
60 // The root frame should be the first frame.
61 Frame& rootFrame = m.frames[idxs[0]];
62 if (rootFrame.type == ::pinocchio::JOINT) {
63 JointIndex jid = m.getJointId(rootFrame.name);
64 m.jointPlacements[jid] = t;
65 return;
66 }
67
68 Transform3s shift(t * rootFrame.placement.inverse());
69 // Find all the frames that have the same parent joint.
70 for (std::size_t i = 1; i < idxs.size(); ++i) {
71 Frame& frame = m.frames[idxs[i]];
72 if (frame.parent == rootFrame.parent) {
73 // frame is between rootFrame and next moving joints.
74 frame.placement = shift * frame.placement;
75 if (frame.type == ::pinocchio::BODY) {
76 // Update the geometry object placement.
77 for (std::size_t k = 0; k < gm.geometryObjects.size(); ++k) {
78 ::pinocchio::GeometryObject& go = gm.geometryObjects[k];
79 if (go.parentFrame == idxs[i]) go.placement = shift * go.placement;
80 }
81 }
82 } else if ((frame.type == ::pinocchio::JOINT) &&
83 (rootFrame.parent == m.parents[frame.parent])) {
84 // frame corresponds to a child joint of rootFrame.parent
85 m.jointPlacements[frame.parent] = shift * m.jointPlacements[frame.parent];
86 }
87 }
88 invalidate();
89 // Update the pool of device data.
90 numberDeviceData(numberDeviceData());
91 }
92
93 std::vector<std::string> Device::robotNames() const {
94 const pinocchio::Model& model = this->model();
95 std::vector<std::string> names;
96
97 for (pinocchio::FrameIndex fi = 1; fi < model.frames.size(); ++fi) {
98 const Frame& frame = model.frames[fi];
99 std::size_t sep = frame.name.find('/');
100 if (sep == std::string::npos) {
101 hppDout(warning,
102 "Frame " << frame.name << " does not belong to any robots.");
103 continue;
104 }
105 std::string name = frame.name.substr(0, sep);
106
107 if (std::find(names.rbegin(), names.rend(), name) != names.rend())
108 names.push_back(name);
109 }
110 return names;
111 }
112
113 FrameIndices_t Device::robotFrames(const std::string& robotName) const {
114 const pinocchio::Model& model = this->model();
115 FrameIndices_t frameIndices;
116
117 for (pinocchio::FrameIndex fi = 1; fi < model.frames.size(); ++fi) {
118 const std::string& name = model.frames[fi].name;
119 if (name.size() > robotName.size() &&
120 name.compare(0, robotName.size(), robotName) == 0 &&
121 name[robotName.size()] == '/') {
122 frameIndices.push_back(fi);
123 }
124 }
125 return frameIndices;
126 }
127
128 void Device::removeJoints(const std::vector<std::string>& jointNames,
129 Configuration_t referenceConfig) {
130 Parent_t::removeJoints(jointNames, referenceConfig);
131
132 for (auto& pair : grippers.map)
133 pair.second = pinocchio::Gripper::create(pair.second->name(), self_);
134 // TODO update handles and jointAndShapes
135 }
136
137 std::ostream& Device::print(std::ostream& os) const {
138 Parent_t::print(os);
139 // print handles
140 os << "Handles:" << std::endl;
141 handles.print(os);
142 // print grippers
143 os << "Grippers:" << std::endl;
144 grippers.print(os);
145 return os;
146 }
147
148 template <class Archive>
149 void Device::serialize(Archive& ar, const unsigned int version) {
150 using namespace boost::serialization;
151 (void)version;
152 auto* har = hpp::serialization::cast(&ar);
153
154 ar& make_nvp("base", base_object<pinocchio::HumanoidRobot>(*this));
155
156 // TODO we should throw if a pinocchio::Device instance with name name_
157 // and not of type manipulation::Device is found.
158 bool written =
159 (!har || har->template getChildClass<pinocchio::Device, Device>(
160 name_, false) != this);
161 ar& BOOST_SERIALIZATION_NVP(written);
162 if (written) {
163 ar& BOOST_SERIALIZATION_NVP(self_);
164 // TODO (easy) add serialization of core::Container ?
165 // ar & BOOST_SERIALIZATION_NVP(handles);
166 // ar & BOOST_SERIALIZATION_NVP(grippers);
167 // ar & BOOST_SERIALIZATION_NVP(jointAndShapes);
168 }
169 }
170
171 HPP_SERIALIZATION_IMPLEMENT(Device);
172 } // namespace manipulation
173 } // namespace hpp
174
175 BOOST_CLASS_EXPORT_IMPLEMENT(hpp::manipulation::Device)
176