endeffectors.h
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
30 #ifndef _XPP_STATES_ENDEFFECTORS_H_
31 #define _XPP_STATES_ENDEFFECTORS_H_
32 
33 #include <deque>
34 #include <iostream>
35 #include <vector>
36 
37 #include <xpp_states/state.h>
38 
39 namespace xpp {
40 
41 using EndeffectorID = uint;
42 
58 template<typename T>
59 class Endeffectors {
60 public:
61  using Container = std::deque<T>; // only to avoid faulty std::vector<bool>
62  using EndeffectorsT = Endeffectors<T>;
63 
64  Endeffectors (int n_ee = 0);
65  virtual ~Endeffectors () = default;
66 
70  void SetCount(int n_ee);
71 
75  void SetAll(const T& value);
76 
80  int GetEECount() const;
81 
85  std::vector<EndeffectorID> GetEEsOrdered() const;
86 
91  T& at(EndeffectorID ee);
92 
97  const T& at(EndeffectorID ee) const;
98 
99  const EndeffectorsT operator-(const EndeffectorsT& rhs) const;
100  const EndeffectorsT operator/(double scalar) const;
101 
105  bool operator!=(const Endeffectors& other) const;
106 
110  Container ToImpl() const;
111 
112 private:
113  Container ee_;
114 };
115 
116 // convenience typedefs, can also be extended to derived classes if desired.
120 
125 class EndeffectorsMotion : public Endeffectors<StateLin3d> {
126 public:
132  {
134  for (auto ee : GetEEsOrdered())
135  val.at(ee) = at(ee).GetByIndex(deriv);
136 
137  return val;
138  }
139 };
140 
141 
149 class EndeffectorsContact : public Endeffectors<bool> {
150 public:
156  EndeffectorsContact (int n_ee=0, bool in_contact=false)
157  :Endeffectors(n_ee) { SetAll(in_contact);};
158 
162  int GetContactCount() const
163  {
164  int count = 0;
165  for (auto ee : GetEEsOrdered())
166  if (at(ee))
167  count++;
168 
169  return count;
170  }
171 };
172 
173 
174 // implementations
175 template<typename T>
177 {
178  SetCount(n_ee);
179 }
180 
181 template<typename T>
182 void
184 {
185  ee_.resize(n_ee);
186 }
187 
188 template<typename T>
189 void
190 Endeffectors<T>::SetAll (const T& value)
191 {
192  std::fill(ee_.begin(), ee_.end(), value);
193 }
194 
195 template<typename T>
196 T&
198 {
199  return ee_.at(idx);
200 }
201 
202 template<typename T>
203 const T&
205 {
206  return ee_.at(idx);
207 }
208 
209 template<typename T>
210 int
212 {
213  return ee_.size();
214 }
215 
216 template<typename T>
219 {
220  return ee_;
221 }
222 
223 template<typename T>
224 std::vector<EndeffectorID>
226 {
227  std::vector<EndeffectorID> vec;
228  for (int i=0; i<ee_.size(); ++i)
229  vec.push_back(i);
230 
231  return vec;
232 }
233 
234 template<typename T>
235 const typename Endeffectors<T>::EndeffectorsT
236 Endeffectors<T>::operator - (const EndeffectorsT& rhs) const
237 {
238  EndeffectorsT result(ee_.size());
239  for (auto i : GetEEsOrdered())
240  result.at(i) = ee_.at(i) - rhs.at(i);
241 
242  return result;
243 }
244 
245 template<typename T>
246 const typename Endeffectors<T>::EndeffectorsT
247 Endeffectors<T>::operator / (double scalar) const
248 {
249  EndeffectorsT result(ee_.size());
250  for (auto i : GetEEsOrdered())
251  result.at(i) = ee_.at(i)/scalar;
252 
253  return result;
254 }
255 
256 template <typename T>
257 std::ostream& operator<<(std::ostream& stream, Endeffectors<T> endeffectors)
258 {
259  for (EndeffectorID ee : endeffectors.GetEEsOrdered())
260  stream << endeffectors.at(ee) << ", ";
261 
262  return stream;
263 }
264 
265 template<typename T>
266 bool
267 Endeffectors<T>::operator!=(const Endeffectors& other) const
268 {
269  for (auto ee : GetEEsOrdered()) {
270  if (ee_.at(ee) != other.at(ee))
271  return true;
272  }
273  return false;
274 }
275 
276 } /* namespace xpp */
277 
278 #endif /* _XPP_STATES_ENDEFFECTORS_H_ */
xpp::StateLinXd::GetByIndex
const VectorXd GetByIndex(MotionDerivative deriv) const
Read either position, velocity of acceleration by index.
Definition: state.cc:88
xpp::Endeffectors::EndeffectorsT
Endeffectors< T > EndeffectorsT
Definition: endeffectors.h:89
xpp::EndeffectorsContact
Bundles the contact state of all endeffectors.
Definition: endeffectors.h:176
xpp::operator<<
std::ostream & operator<<(std::ostream &stream, Endeffectors< T > endeffectors)
Definition: endeffectors.h:284
xpp::Endeffectors::GetEEsOrdered
std::vector< EndeffectorID > GetEEsOrdered() const
Definition: endeffectors.h:252
xpp::Endeffectors::Endeffectors
Endeffectors(int n_ee=0)
Definition: endeffectors.h:203
xpp::Endeffectors::Container
std::deque< T > Container
Definition: endeffectors.h:88
xpp::Endeffectors::operator!=
bool operator!=(const Endeffectors &other) const
Definition: endeffectors.h:294
xpp::Endeffectors::ee_
Container ee_
Definition: endeffectors.h:140
xpp::MotionDerivative
MotionDerivative
Definition: state.h:80
xpp::EndeffectorsContact::GetContactCount
int GetContactCount() const
The number of endeffectors in contact with the environment.
Definition: endeffectors.h:189
xpp::Endeffectors::GetEECount
int GetEECount() const
Definition: endeffectors.h:238
xpp::Endeffectors::SetAll
void SetAll(const T &value)
Sets each endeffector to the same value.
Definition: endeffectors.h:217
xpp::Endeffectors
Data structure to assign values to each endeffector.
Definition: endeffectors.h:86
xpp::Endeffectors::SetCount
void SetCount(int n_ee)
Sets the number of endeffectors.
Definition: endeffectors.h:210
xpp
Definition: cartesian_declarations.h:41
xpp::EndeffectorsMotion::Get
Endeffectors< Vector3d > Get(MotionDerivative deriv) const
Extract only either the pos, vel or acc from all endeffectors.
Definition: endeffectors.h:158
xpp::Endeffectors::ToImpl
Container ToImpl() const
Definition: endeffectors.h:245
xpp::EndeffectorsMotion
Bundles the position, velocity and acceleration of all endeffectors. as well as appending a Endeffect...
Definition: endeffectors.h:152
xpp::EndeffectorsContact::EndeffectorsContact
EndeffectorsContact(int n_ee=0, bool in_contact=false)
Constructs a state, the default being 0 feet, none in contact.
Definition: endeffectors.h:183
xpp::Endeffectors::at
T & at(EndeffectorID ee)
Read/write access to the endeffector stored at index ee.
Definition: endeffectors.h:224
state.h
xpp::Endeffectors::operator-
const EndeffectorsT operator-(const EndeffectorsT &rhs) const
Definition: endeffectors.h:263
xpp::Endeffectors::operator/
const EndeffectorsT operator/(double scalar) const
Definition: endeffectors.h:274
xpp::EndeffectorID
uint EndeffectorID
Definition: endeffectors.h:68
xpp::Endeffectors::~Endeffectors
virtual ~Endeffectors()=default


xpp_states
Author(s): Alexander W. Winkler
autogenerated on Wed Mar 2 2022 01:14:14