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>
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:
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
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
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_ */
int GetEECount() const
Definition: endeffectors.h:211
void SetAll(const T &value)
Sets each endeffector to the same value.
Definition: endeffectors.h:190
const EndeffectorsT operator-(const EndeffectorsT &rhs) const
Definition: endeffectors.h:236
std::vector< EndeffectorID > GetEEsOrdered() const
Definition: endeffectors.h:225
const EndeffectorsT operator/(double scalar) const
Definition: endeffectors.h:247
int GetContactCount() const
The number of endeffectors in contact with the environment.
Definition: endeffectors.h:162
T & at(EndeffectorID ee)
Read/write access to the endeffector stored at index ee.
Definition: endeffectors.h:197
Endeffectors< Vector3d > Get(MotionDerivative deriv) const
Extract only either the pos, vel or acc from all endeffectors.
Definition: endeffectors.h:131
Container ToImpl() const
Definition: endeffectors.h:218
void SetCount(int n_ee)
Sets the number of endeffectors.
Definition: endeffectors.h:183
virtual ~Endeffectors()=default
std::deque< Vector3d > Container
Definition: endeffectors.h:61
MotionDerivative
Definition: state.h:53
Bundles the contact state of all endeffectors.
Definition: endeffectors.h:149
Bundles the position, velocity and acceleration of all endeffectors. as well as appending a Endeffect...
Definition: endeffectors.h:125
uint EndeffectorID
Definition: endeffectors.h:41
bool operator!=(const Endeffectors &other) const
Definition: endeffectors.h:267
EndeffectorsContact(int n_ee=0, bool in_contact=false)
Constructs a state, the default being 0 feet, none in contact.
Definition: endeffectors.h:156
Data structure to assign values to each endeffector.
Definition: endeffectors.h:59
Endeffectors(int n_ee=0)
Definition: endeffectors.h:176


xpp_states
Author(s): Alexander W. Winkler
autogenerated on Sun Apr 7 2019 02:13:41