-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathparticle.py
48 lines (40 loc) · 1.93 KB
/
particle.py
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
# SPDX-License-Identifier: MIT-0
from pydrake.systems.framework import BasicVector
from pydrake.systems.framework import LeafSystem
from pydrake.systems.framework import PortDataType
class Particle(LeafSystem):
"""
A linear 1DOF particle system.
With very simple dynamics xdotdot = a, this system can be described in
terms of its:
Inputs:
linear acceleration (input index 0), in m/s^2 units.
States/Outputs:
linear position (state/output index 0), in m units.
linear velocity (state/output index 1), in m/s units.
"""
def __init__(self):
LeafSystem.__init__(self)
# A 1D input vector for acceleration.
self.DeclareInputPort('acceleration', PortDataType.kVectorValued, 1)
# Adding one generalized position and one generalized velocity.
self.DeclareContinuousState(1, 1, 0)
# A 2D output vector for position and velocity.
self.DeclareVectorOutputPort('postion_and_velocity', BasicVector(2),
self.CopyStateOut)
def CopyStateOut(self, context, output):
# Get current state from context.
continuous_state_vector = context.get_continuous_state_vector()
# Write system output.
output.SetFromVector(continuous_state_vector.CopyToVector())
def DoCalcTimeDerivatives(self, context, derivatives):
# Get current state from context.
continuous_state_vector = x = context.get_continuous_state_vector()
# Obtain the structure we need to write into.
derivatives_vector = derivatives.get_mutable_vector()
# Get current input acceleration value.
input_vector = self.EvalVectorInput(context, 0)
# Set the derivatives. The first one is velocity and the second one is
# acceleration.
derivatives_vector.SetAtIndex(0, continuous_state_vector.GetAtIndex(1))
derivatives_vector.SetAtIndex(1, input_vector.GetAtIndex(0))