-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ball-inl.h
57 lines (46 loc) · 1.76 KB
/
ball-inl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
/// @file
/// Template method implementations for ball.h.
/// Most users should only include that file, not this one.
/// For background, see http://drake.mit.edu/cxx_inl.html.
/* clang-format off to disable clang-format-includes */
#include "drake/examples/bouncing_ball/ball.h"
/* clang-format on */
#include "drake/common/drake_assert.h"
#include "drake/systems/framework/basic_vector.h"
namespace drake {
namespace bouncing_ball {
template <typename T>
Ball<T>::Ball() {
this->DeclareContinuousState(1, 1, 0);
this->DeclareVectorOutputPort(systems::BasicVector<T>(2),
&Ball::CopyStateOut);
}
template <typename T>
void Ball<T>::CopyStateOut(const systems::Context<T>& context,
systems::BasicVector<T>* output) const {
output->get_mutable_value() =
context.get_continuous_state().CopyToVector();
}
template <typename T>
void Ball<T>::DoCalcTimeDerivatives(
const systems::Context<T>& context,
systems::ContinuousState<T>* derivatives) const {
// Obtain the state.
const systems::VectorBase<T>& state = context.get_continuous_state_vector();
// Obtain the structure we need to write into.
DRAKE_ASSERT(derivatives != nullptr);
systems::VectorBase<T>& new_derivatives = derivatives->get_mutable_vector();
new_derivatives.SetAtIndex(0, state.GetAtIndex(1));
new_derivatives.SetAtIndex(1, T(get_gravitational_acceleration()));
}
template <typename T>
void Ball<T>::SetDefaultState(const systems::Context<T>&,
systems::State<T>* state) const {
DRAKE_DEMAND(state != nullptr);
Vector2<T> x0;
x0 << 10.0, 0.0; // initial state values.
state->get_mutable_continuous_state().SetFromVector(x0);
}
} // namespace bouncing_ball
} // namespace drake