Drake
state.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <memory>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 
12 
13 namespace drake {
14 namespace systems {
15 
21 template <typename T>
23  public:
26  explicit ContinuousState(std::unique_ptr<StateVector<T>> state) {
27  state_ = std::move(state);
28  generalized_position_.reset(new StateSubvector<T>(state_.get()));
29  generalized_velocity_.reset(new StateSubvector<T>(state_.get()));
30  misc_continuous_state_.reset(
31  new StateSubvector<T>(state_.get(), 0, state_.size()));
32  }
33 
48  ContinuousState(std::unique_ptr<StateVector<T>> state, int num_q,
49  int num_v, int num_z) {
50  state_ = std::move(state);
51  if (state_->size() != num_q + num_v + num_z) {
52  throw std::out_of_range(
53  "Continuous state of size " + std::to_string(state_->size()) +
54  "cannot be partitioned as" + " q " + std::to_string(num_q) + " v " +
55  std::to_string(num_v) + " z " + std::to_string(num_z));
56  }
57  if (num_v > num_q) {
58  throw std::logic_error("Number of velocity variables " +
59  std::to_string(num_v) +
60  " must not exceed number of position variables " +
61  std::to_string(num_q));
62  }
63  generalized_position_.reset(new StateSubvector<T>(state_.get(), 0, num_q));
64  generalized_velocity_.reset(
65  new StateSubvector<T>(state_.get(), num_q, num_v));
66  misc_continuous_state_.reset(
67  new StateSubvector<T>(state_.get(), num_q + num_v, num_z));
68  }
69 
70  // TODO(david-german-tri): Add a suitable constructor for the continuous
71  // state of a Diagram, using StateSupervectors.
72 
74  const StateVector<T>& get_state() const { return *state_; }
75 
77  StateVector<T>* get_mutable_state() { return state_.get(); }
78 
81  return *generalized_position_;
82  }
83 
87  return generalized_position_.get();
88  }
89 
92  return *generalized_velocity_;
93  }
94 
98  return generalized_velocity_.get();
99  }
100 
103  return *misc_continuous_state_;
104  }
105 
109  return misc_continuous_state_.get();
110  }
111 
112  private:
113  // The entire state vector. May or may not own the underlying data.
114  std::unique_ptr<StateVector<T>> state_;
115 
116  // Generalized coordinates representing System configuration, conventionally
117  // denoted `q`. These are second-order state variables.
118  // This is a subset of state_ and does not own the underlying data.
119  std::unique_ptr<StateVector<T>> generalized_position_;
120 
121  // Generalized speeds representing System velocity. Conventionally denoted
122  // `v`. These are first-order state variables that the System can linearly
123  // map to time derivatives `qdot` of `q` above.
124  // This is a subset of state_ and does not own the underlying data.
125  std::unique_ptr<StateVector<T>> generalized_velocity_;
126 
127  // Additional continuous, first-order state variables not representing
128  // multibody system motion. Conventionally denoted `z`.
129  // This is a subset of state_ and does not own the underlying data.
130  std::unique_ptr<StateVector<T>> misc_continuous_state_;
131 };
132 
139 template <typename T>
140 struct State {
141  std::unique_ptr<ContinuousState<T>> continuous_state;
142  // TODO(david-german-tri): Add discrete state variables.
143 };
144 
145 } // namespace systems
146 } // namespace drake
StateVector< T > * get_mutable_state()
Returns a mutable pointer to the entire state vector, never null.
Definition: state.h:77
const StateVector< T > & get_misc_continuous_state() const
Returns the subset of the state vector that is other continuous state z.
Definition: state.h:102
std::unique_ptr< ContinuousState< T > > continuous_state
Definition: state.h:141
Definition: constants.h:3
const StateVector< T > & get_generalized_position() const
Returns the subset of the state vector that is generalized position q.
Definition: state.h:80
StateVector is an abstract base class template for vector quantities within the state of a System...
Definition: state_vector.h:20
const StateVector< T > & get_state() const
Returns the entire state vector.
Definition: state.h:74
StateVector< T > * get_mutable_generalized_position()
Returns a mutable pointer to the subset of the state vector that is generalized position q...
Definition: state.h:86
const StateVector< T > & get_generalized_velocity() const
Returns the subset of the state vector that is generalized velocity v.
Definition: state.h:91
StateSubvector is a concrete class template that implements StateVector by providing a sliced view of...
Definition: state_subvector.h:19
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
The State is a container for all the data comprising the complete state of a particular System at a p...
Definition: state.h:140
ContinuousState(std::unique_ptr< StateVector< T >> state, int num_q, int num_v, int num_z)
Constructs a ContinuousState that exposes second-order structure.
Definition: state.h:48
The ContinuousState is a container for all the State variables that are unique to continuous Systems...
Definition: state.h:22
ContinuousState(std::unique_ptr< StateVector< T >> state)
Constructs a ContinuousState for a system that does not have second-order structure: All of the state...
Definition: state.h:26
StateVector< T > * get_mutable_generalized_velocity()
Returns a mutable pointer to the subset of the state vector that is generalized velocity v...
Definition: state.h:97
StateVector< T > * get_mutable_misc_continuous_state()
Returns a mutable pointer to the subset of the state vector that is other continuous state z...
Definition: state.h:108