demo_state.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George ran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #include <tuw_control/utils.h>
38 
40 
41 #include <iostream>
42 
43 using namespace tuw;
44 
46 enum class StateWheel
47 {
48  Steer,
49  Revol,
50  ENUM_SIZE
51 };
53 class OneWheelType : public StateArrayScoped<StateWheel>
54 {
55 public:
57  OneWheelType(double steering, double revolute) : StateArrayScoped<StateWheel>()
58  {
59  steer() = steering, revol() = revolute;
60  }
61  const double &steer() const
62  {
63  return values_[0];
64  }
65  const double &revol() const
66  {
67  return values_[1];
68  }
69  double &steer()
70  {
71  return values_[0];
72  }
73  double &revol()
74  {
75  return values_[1];
76  }
77 };
78 
79 class StateWhInpType : public StateNestedVector<OneWheelType>
80 {
81 public:
83  const OneWheelType &wheel(size_t i) const
84  {
85  return *states_[i];
86  }
87  OneWheelType &wheel(size_t i)
88  {
89  return *states_[i];
90  }
91 };
92 
93 using StateWhInpPtrType = std::shared_ptr<StateWhInpType>;
94 
95 int main(int argc, char **argv)
96 {
97  std::cout << "iws utils test" << std::endl;
98  StateWhInpPtrType tuw_i2ws_wheel_state = std::shared_ptr<StateWhInpType>(new StateWhInpType);
99  tuw_i2ws_wheel_state->resize(2);
100  tuw_i2ws_wheel_state->wheel(0).steer() = 10;
101  tuw_i2ws_wheel_state->wheel(0).revol() = 11;
102  tuw_i2ws_wheel_state->wheel(1).steer() = 12;
103  tuw_i2ws_wheel_state->wheel(1).revol() = 13;
105  std::cout << "tuw : " << *tuw_i2ws_wheel_state << std::endl;
106 
108  std::vector<double> values;
109  tuw_i2ws_wheel_state->toSTLVec(values);
110 
112  std::cout << "tuw : [" << *tuw_i2ws_wheel_state->state(0) << ", " << *tuw_i2ws_wheel_state->state(1) << "]"
113  << std::endl;
114 
116  std::cout << "tuw tuw_i2ws_wheel_state->stateScoped ( 1 )->value<StateWheel::Steer>() = "
117  << tuw_i2ws_wheel_state->stateScoped(1)->value<StateWheel::Steer>() << std::endl;
118 
120  std::cout << "tuw ( *tuw_i2ws_wheel_state ) [1].steer() = " << (*tuw_i2ws_wheel_state)[1].steer()
121  << std::endl;
123  std::cout << "tuw tuw_i2ws_wheel_state->wheel ( 1 ).steer() = " << tuw_i2ws_wheel_state->wheel(1).steer()
124  << std::endl;
125 
126  using LeafArr3 = LeafAccessTreeBase<double, double, 3>;
127  using LeafVec = LeafAccessTreeBase<double, double, -1>;
128 
130  LeafAccessTreeBaseVirt &state0Virt = state0;
131  // LeafAccessTreeBaseVirt& subState0Virt = state0.sub(0);
132  // LeafAccessTreeBaseVirt& subState1Virt = state0.sub<0>();
133  Eigen::VectorXd xx;
134  xx.resize(5);
135  xx(0) = 0;
136  xx(1) = 1;
137  xx(2) = 2;
138  xx(3) = 3;
139  xx(4) = 4;
140 
141  std::cout << std::endl;
142  std::cout << xx.block(2, 0, 2, 1) << std::endl
143  << std::endl;
144 
145  Eigen::VectorXd yy(3);
146  yy(0) = 9;
147  yy(1) = 8;
148  yy(2) = 7;
149 
150  // xx.resize(6);
151  auto _blockRef = xx.block(2, 0, 2, 1);
152  //_blockRef.resize(3);
153  //_blockRef = yy;
154 
155  // xx.block(2,0,2,1) = yy;
156  std::cout << _blockRef << std::endl
157  << std::endl;
158  std::cout << xx << std::endl
159  << std::endl;
160 
161  typedef Eigen::Map<Eigen::VectorXd> MapType;
162 
163  MapType xMap0(&xx(0), xx.rows());
164  MapType xMap2(&xx(2), xx.rows() - 3);
165  MapType xMap4(&xx(4), xx.rows() - 4);
166 
167  std::cout << "xx=" << std::endl
168  << xx << std::endl
169  << std::endl;
170  std::cout << "xMap0=" << std::endl
171  << xMap0 << std::endl
172  << std::endl;
173  std::cout << "xMap2=" << std::endl
174  << xMap2 << std::endl
175  << std::endl;
176  std::cout << "xMap4=" << std::endl
177  << xMap4 << std::endl
178  << std::endl;
179 
180  xx.conservativeResize(8);
181  double *p;
182 
183  new (&xMap2) MapType(&xx(2), xMap2.outerStride() + 3);
184  new (&xMap4) MapType(&xMap2(2), xMap4.outerStride());
185  double *p2;
186  // xMap2.cast_to_pointer_type(p2);
187  // xMap4.cast_to_pointer_type(p); p =&xx(2)+7;+xMap2.outerStride();
188  // xMap4.
189 
190  std::cout << "xx=" << std::endl
191  << xx << std::endl
192  << std::endl;
193  std::cout << "xMap0=" << std::endl
194  << xMap0 << std::endl
195  << std::endl;
196  std::cout << "xMap2=" << std::endl
197  << xMap2 << std::endl
198  << std::endl;
199  std::cout << "xMap4=" << std::endl
200  << xMap4 << std::endl
201  << std::endl;
202  // x.resize(5);
203  // xMap.cast_to_pointer_type(p);
204  // p = &x(0);
205 
206  // std::cout<<x<<std::endl<<std::endl;
207 
208  // MatrixType m1(n_dims), m2(n_dims);
209  // m1.setRandom();
210  // m2.setRandom();
211  // float *p = &m2(0); // get the address storing the data for m2
212  // MapType m2map(p,m2.size()); // m2map shares data with m2
213  // MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
214  // cout << "m1: " << m1 << endl;
215  // cout << "m2: " << m2 << endl;
216  // cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
217  // cout << "Squared euclidean distance, using map: " <<
218  // (m1-m2map).squaredNorm() << endl;
219  // m2map(3) = 7; // this will change m2, since they share the same array
220  // cout << "Updated m2: " << m2 << endl;
221  // cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
222  // /* m2mapconst(2) = 5; */ // this yields a compile-time error
223 }
OneWheelType & wheel(size_t i)
Definition: demo_state.cpp:87
const double & revol() const
Definition: demo_state.cpp:65
double & revol()
Definition: demo_state.cpp:73
const OneWheelType & wheel(size_t i) const
Definition: demo_state.cpp:83
std::shared_ptr< StateWhInpType > StateWhInpPtrType
Definition: demo_state.cpp:93
OneWheelType(double steering, double revolute)
Definition: demo_state.cpp:57
Extension of StateArray providing value access based on a scoped enumeration (compile-time).
StateWheel
Enum defining the semantics of the Iws wheel state.
Definition: demo_state.cpp:46
const double & steer() const
Definition: demo_state.cpp:61
double & steer()
Definition: demo_state.cpp:69
int main(int argc, char **argv)
Definition: demo_state.cpp:95
OneWheelType with access via varialbe and enums.
Definition: demo_state.cpp:53


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:21