-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[drake_cmake_installed] Replace broken particles example (#355)
For now, just sync up with the drake_cmake_installed_apt example. It is not a great demo, but it's what we have.
- Loading branch information
1 parent
b8e2687
commit b200975
Showing
22 changed files
with
150 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
add_subdirectory(find_resource) | ||
add_subdirectory(particles) | ||
add_subdirectory(particle) | ||
add_subdirectory(simple_bindings) | ||
add_subdirectory(simple_continuous_time_system) | ||
|
||
add_test(NAME import_all_test COMMAND | ||
"${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/import_all_test.py" | ||
) | ||
set_tests_properties(import_all_test PROPERTIES | ||
ENVIRONMENT "PYTHONPATH=${PYTHONPATH}" | ||
ENVIRONMENT "PYTHONPATH=${DRAKE_PYTHONPATH}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
add_library(particle particle.cc particle.h) | ||
target_link_libraries(particle drake::drake) | ||
|
||
add_executable(particle_test particle_test.cc) | ||
target_link_libraries(particle_test particle drake::drake gtest) | ||
add_test(NAME cc_particle_test COMMAND particle_test) | ||
set_tests_properties(cc_particle_test PROPERTIES LABELS small TIMEOUT 60) | ||
|
||
add_test(NAME python_particle_test | ||
COMMAND Python3::Interpreter -B -m unittest particle_test | ||
) | ||
set_tests_properties(python_particle_test PROPERTIES | ||
ENVIRONMENT "PYTHONPATH=${DRAKE_PYTHONPATH}" | ||
LABELS small | ||
REQUIRED_FILES "${CMAKE_CURRENT_SOURCE_DIR}/particle_test.py" | ||
TIMEOUT 60 | ||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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)) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
import unittest | ||
|
||
from particle import Particle | ||
|
||
|
||
class TestParticle(unittest.TestCase): | ||
"""A test case for Particle systems.""" | ||
def setUp(self): | ||
# System (aka 'device under test') being tested. | ||
self.dut = Particle() | ||
# Context for the given dut. | ||
self.context = self.dut.CreateDefaultContext() | ||
# Outputs of the given dut. | ||
self.output = self.dut.AllocateOutput() | ||
# Derivatives of the given dut. | ||
self.derivatives = self.dut.AllocateTimeDerivatives() | ||
|
||
def test_output(self): | ||
""" | ||
Makes sure a Particle output is consistent with its state (position and | ||
velocity). | ||
""" | ||
# Initialize state. | ||
continuous_state_vector = \ | ||
self.context.get_mutable_continuous_state_vector() | ||
continuous_state_vector.SetAtIndex(0, 10.0) # x0 = 10 m. | ||
continuous_state_vector.SetAtIndex(1, 1.0) # x1 = 1 m/s. | ||
# Compute outputs. | ||
self.dut.CalcOutput(self.context, self.output) | ||
output_vector = self.output.get_vector_data(0) | ||
# Check results. | ||
self.assertEqual(output_vector.GetAtIndex(0), 10.0) # y0 == x0 | ||
self.assertEqual(output_vector.GetAtIndex(1), 1.0) # y1 == x1 | ||
|
||
def test_derivatives(self): | ||
""" | ||
Makes sure a Particle system state derivatives are consistent with its | ||
state and input (velocity and acceleration). | ||
""" | ||
# Set input. | ||
input_port = self.dut.get_input_port(0) | ||
input_port.FixValue(self.context, 1.0) # u0 = 1 m/s^2 | ||
# Set state. | ||
continuous_state_vector = \ | ||
self.context.get_mutable_continuous_state_vector() | ||
continuous_state_vector.SetAtIndex(0, 0.0) # x0 = 0 m | ||
continuous_state_vector.SetAtIndex(1, 2.0) # x1 = 2 m/s | ||
# Compute derivatives. | ||
self.dut.CalcTimeDerivatives(self.context, self.derivatives) | ||
derivatives_vector = self.derivatives.get_vector() | ||
# Check results. | ||
self.assertEqual(derivatives_vector.GetAtIndex(0), 2.0) # x0dot == x1 | ||
self.assertEqual(derivatives_vector.GetAtIndex(1), 1.0) # x1dot == u0 | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-58 KB
...e_cmake_installed/src/particles/docs/uniformly_accelerated_particle_diagram.png
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.