7 #include "drake/drakeSystemFramework_export.h" 16 class AbstractContext3;
31 : subsystem_num(subsystem_num), port_num(port_num), port(port) {}
40 : subsystem_num(subsystem_num), port_num(port_num), port(port) {}
89 const int my_port_num = (int)input_ports_.size();
90 port->set_owner(
this, my_port_num);
91 input_ports_.emplace_back(-1, my_port_num, port.get());
92 owned_input_ports_.push_back(std::move(port));
100 const int my_port_num = (int)output_ports_.size();
101 port->set_owner(
this, my_port_num);
102 output_ports_.emplace_back(-1, my_port_num, port.get());
103 owned_output_ports_.push_back(std::move(port));
114 return *get_input_port_finder(port_num).port;
120 return get_input_port_finder(port_num).port;
131 return *get_output_port_finder(port_num).port;
137 return get_output_port_finder(port_num).port;
146 DRAKESYSTEMFRAMEWORK_EXPORT std::unique_ptr<AbstractContext3>
147 CreateDefaultContext()
const;
158 DRAKESYSTEMFRAMEWORK_EXPORT
const AbstractValue& EvalOutputPort(
182 template <
class ConcreteSystem>
183 ConcreteSystem*
AddSubsystem(std::unique_ptr<ConcreteSystem> subsystem) {
184 ConcreteSystem* concrete = subsystem.get();
185 subsystem->set_owner(
this, get_num_subsystems());
186 subsystems_.push_back(std::move(subsystem));
198 const int my_port_num = (int)input_ports_.size();
200 input_port_num, sub_port);
212 const int my_port_num = (int)output_ports_.size();
214 output_port_num, sub_port);
269 std::string path_name;
270 if (get_parent_system())
271 path_name = get_parent_system()->GetSubsystemPathName();
272 path_name += (
"/" + get_name());
281 std::vector<int> path = get_path_from_root_system();
284 return my_subcontext;
293 return const_cast<AbstractContext3*
>(&find_my_subcontext(*some_subcontext));
300 if (!get_parent_system())
return *
this;
314 DRAKE_ASSERT(0 <= port_num && port_num < (
int)input_ports_.size());
319 DRAKE_ASSERT(0 <= port_num && port_num < (
int)output_ports_.size());
328 using namespace std::placeholders;
336 const int n = get_num_output_ports();
337 if (!(0 <= port_num && port_num < n))
338 throw std::out_of_range(
"AbstractSystem3::CalcOutputPort(): port " +
340 " is out of range. There are " +
342 DoCalcOutputPort(context, port_num, result);
356 DRAKESYSTEMFRAMEWORK_EXPORT
const AbstractValue& EvalInputPort(
361 virtual std::unique_ptr<AbstractContext3> DoCreateEmptyContext()
const = 0;
373 virtual void DoCalcOutputPort(
const AbstractContext3& context,
int port_num,
382 DRAKESYSTEMFRAMEWORK_EXPORT std::unique_ptr<AbstractContext3>
383 CreateEmptyContext()
const;
388 DRAKESYSTEMFRAMEWORK_EXPORT
void AllocateOutputPorts(
394 DRAKESYSTEMFRAMEWORK_EXPORT
void AllocateAndConnectInputPorts(
403 int start = 0)
const {
404 if (start == (
int)path.size())
return *
this;
405 return get_subsystem(path[start]).find_subsystem(path, start + 1);
410 AbstractSystem3* find_mutable_subsystem(
const std::vector<int>& path) {
421 std::vector<int> get_path_from_root_system()
const {
422 std::vector<int> path;
423 if (get_parent_system()) {
424 path = get_parent_system()->get_path_from_root_system();
426 path.push_back(get_subsystem_num());
452 std::vector<InputPortFinder> input_ports_;
453 std::vector<OutputPortFinder> output_ports_;
456 std::vector<std::unique_ptr<InputPort3>> owned_input_ports_;
457 std::vector<std::unique_ptr<OutputPort3>> owned_output_ports_;
461 std::vector<std::unique_ptr<AbstractSystem3>> subsystems_;
466 int subsystem_num_{-1};
480 template <
typename T>
494 std::unique_ptr<Context3<T>> context(
495 dynamic_cast<Context3<T>*
>(abstract_context.release()));
505 int port_num)
const {
507 to_vector_interface<T>(EvalOutputPort(context, port_num));
530 return DoEvalPotentialEnergy(context);
537 return DoEvalKineticEnergy(context);
554 Eigen::VectorBlock<const VectorX<T>> EvalTimeDerivatives(
566 return DoEvalConservativePower(context);
578 return DoEvalNonConservativePower(context);
601 const Eigen::Ref<
const VectorX<T>>& generalized_velocity,
602 Eigen::Ref<
VectorX<T>>* configuration_derivatives)
const {
604 DoMapVelocityToConfigurationDerivatives(context, generalized_velocity,
605 configuration_derivatives);
630 const Eigen::Ref<
const VectorX<T>>& generalized_acceleration,
631 Eigen::Ref<
VectorX<T>>* configuration_second_derivatives)
const {
633 DoMapAccelerationToConfigurationSecondDerivatives(
634 context, generalized_acceleration, configuration_second_derivatives);
645 void UpdateDiscreteVariables(
Context3<T>& context,
int sample_key)
const;
654 std::pair<double,int> GetNextSampleTime(
const Context3<T>& context)
const;
669 int port_num)
const {
671 to_vector_interface<T>(EvalInputPort(context, port_num));
681 static const T zero(0);
691 static const T zero(0);
711 void DoMapVelocityToConfigurationDerivatives(
713 const Eigen::Ref<
const VectorX<T>>& generalized_velocity,
714 Eigen::Ref<
VectorX<T>>* configuration_derivatives)
const;
717 void DoMapAccelerationToConfigurationSecondDerivatives(
719 const Eigen::Ref<
const VectorX<T>>& generalized_acceleration,
720 Eigen::Ref<
VectorX<T>>* configuration_second_derivatives)
const;
735 static const T zero(0);
748 static const T zero(0);
DRAKESYSTEMFRAMEWORK_EXPORT std::unique_ptr< AbstractContext3 > CreateDefaultContext() const
Returns a default context, initialized with run time mutable memory for the correct number and type o...
Definition: system3.cc:11
const AbstractSystem3 & get_root_system() const
Find the root system of the tree of which this subsystem is a member.
Definition: system3.h:299
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
AbstractSystem3 * get_mutable_parent_system()
Returns a mutable pointer to the parent system that owns this one, or nullptr if this is a root syste...
Definition: system3.h:259
An OutputPort3 represents a data output from a System.
Definition: system3_output.h:35
int get_num_input_ports() const
Returns the current number of input ports in this system.
Definition: system3.h:109
An abstract superclass for the Context3 objects for dynamical systems, encapsulating functionality th...
Definition: context3.h:87
A superclass template for systems that use a specified scalar type T for numerical values...
Definition: system3.h:481
const AbstractContext3 & get_root_context() const
Find the root context of the tree of which this subcontext is a member.
Definition: context3.h:215
void Connect(AbstractSystem3 *source_subsystem, int output_port_num, AbstractSystem3 *sink_subsystem, int input_port_num)
Connect the given output port of subsystem 1 into the given input port of subsystem 2...
Definition: system3.h:224
int get_num_output_ports() const
Returns the current number of output ports in this system.
Definition: system3.h:126
Definition: constants.h:3
AbstractSystem3 * get_mutable_root_system()
Get a mutable pointer to the root system of the tree of which this subsystem is a member...
Definition: system3.h:307
OutputPort3 * get_mutable_output_port(int port_num)
Returns a mutable pointer to the OutputPort3 with the given port number.
Definition: system3.h:136
virtual const T & DoEvalConservativePower(const Context3< T > &context) const
Return the rate at which mechanical energy is being converted from potential energy to kinetic energy...
Definition: system3.h:734
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
std::string get_name() const
Returns the name of this system.
Definition: system3.h:77
int InheritInputPort(AbstractSystem3 *child_subsystem, int input_port_num)
The given subsystem's InputPort3 becomes the next InputPort3 of this system diagram.
Definition: system3.h:194
System3(const std::string &name)
Creates a System3 with no ports.
Definition: system3.h:660
const T & EvalNonConservativePower(const Context3< T > &context) const
For continuous, physical systems only, returns the rate at which energy is being added to (positive) ...
Definition: system3.h:577
DerivativeBlock
Definition: system3.h:544
ConcreteSystem * AddSubsystem(std::unique_ptr< ConcreteSystem > subsystem)
Takes ownership of the given system and returns an unowned, raw pointer to the concrete type for conv...
Definition: system3.h:183
const AbstractSystem3 & get_subsystem(int index) const
Returns a const reference to one of the contained subsystems, using the index reflecting the order in...
Definition: system3.h:240
InputPort3 * get_mutable_input_port(int port_num)
Returns a mutable pointer to the InputPort3 with the given port number.
Definition: system3.h:119
CacheEntry::Calculator get_output_port_calculator(int port_num) const
Given an output port number, return a function that knows how to calculate the value of that port whe...
Definition: system3.h:327
#define DRAKE_ASSERT(condition)
DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cas...
Definition: drake_assert.h:35
const T & EvalPotentialEnergy(const Context3< T > &context) const
Returns the potential energy currently stored in the configuration provided in the given Context3...
Definition: system3.h:528
const T & EvalConservativePower(const Context3< T > &context) const
For continuous, physical systems only, returns the rate at which energy is being converted from poten...
Definition: system3.h:565
Provides Drake's assertion implementation.
#define DRAKE_ABORT_UNLESS(condition)
Evaluates condition and iff the value is false will ::abort() the program with a message showing at l...
Definition: drake_assert.h:39
See InputPortFinder for information.
Definition: system3.h:38
int AddInputPort(std::unique_ptr< InputPort3 > port)
Add an input port that is to be owned by this system.
Definition: system3.h:88
std::string GetSubsystemPathName() const
Determine the full path name of this subsystem in a form like /rootname/parentname/myname.
Definition: system3.h:268
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
const InputPortFinder & get_input_port_finder(int port_num) const
Definition: system3.h:313
int AddOutputPort(std::unique_ptr< OutputPort3 > port)
Add an output port that is to be owned by this System.
Definition: system3.h:99
void MapAccelerationToConfigurationSecondDerivatives(const Context3< T > &context, const Eigen::Ref< const VectorX< T >> &generalized_acceleration, Eigen::Ref< VectorX< T >> *configuration_second_derivatives) const
Transforms a given generalized acceleration vdot into qdotdot, the second time derivative of the gene...
Definition: system3.h:628
An abstract superclass for dynamical systems, encapsulating functionality that is independent of the ...
Definition: system3.h:68
AbstractContext3 * find_my_mutable_subcontext(AbstractContext3 *some_subcontext) const
Get mutable access to this subsystem's subcontext given mutable access to any other subsystem's subco...
Definition: system3.h:291
int InheritOutputPort(AbstractSystem3 *child_subsystem, int output_port_num)
The given subsystem's OutputPort3 becomes the next OutputPort3 of this system diagram.
Definition: system3.h:208
std::vector< Number > result
Definition: IpoptSolver.cpp:170
const InputPort3 & get_input_port(int port_num) const
Returns a const reference to the InputPort3 with the given port number.
Definition: system3.h:113
const OutputPort3 & get_output_port(int port_num) const
Returns a const reference to the OutputPort3 with the given port number.
Definition: system3.h:130
AbstractSystem3 * get_mutable_subsystem(int index)
Returns a mutable pointer to one of the contained subsystems, using the index reflecting the order in...
Definition: system3.h:249
A fully type-erased container class.
Definition: value.h:22
virtual const T & DoEvalNonConservativePower(const Context3< T > &context) const
Return the rate at which mechanical energy is being generated (positive) or dissipated (negative) oth...
Definition: system3.h:747
virtual const T & DoEvalKineticEnergy(const Context3< T > &context) const
If your system models energy of motion, implement this method to return the kinetic energy currently ...
Definition: system3.h:690
int get_subsystem_num() const
If this system is a subsystem of a parent system, return the subsystem number by which that parent kn...
Definition: system3.h:263
virtual void DoAcquireContextResources(AbstractContext3 *context) const
Acquire any private Context resources your concrete System needs, and assign them default values...
Definition: system3.h:367
const T & EvalKineticEnergy(const Context3< T > &context) const
Return the kinetic energy currently present in the motion provided in the given Context3.
Definition: system3.h:535
const AbstractSystem3 * get_parent_system() const
Returns a const pointer to the parent system that owns this one, or nullptr if this is a root system...
Definition: system3.h:255
std::function< void(const class AbstractSystem3 &, const class AbstractContext3 &, AbstractValue *)> Calculator
This is the type of a function that unconditionally computes this cache entry's value with respect to...
Definition: cache3.h:35
void MapVelocityToConfigurationDerivatives(const Context3< T > &context, const Eigen::Ref< const VectorX< T >> &generalized_velocity, Eigen::Ref< VectorX< T >> *configuration_derivatives) const
Transforms a given generalized velocity v into qdot, the time derivative of the generalized configura...
Definition: system3.h:599
const VectorInterface< T > & EvalVectorInputPort(const Context3< T > &context, int port_num) const
Convenience method for obtaining the up-to-date value for an input port which is known to be vector v...
Definition: system3.h:668
The context is a container for all data necessary to uniquely determine the results of computations p...
Definition: context3.h:371
void CalcOutputPort(const AbstractContext3 &context, int port_num, AbstractValue *result) const
Unconditionally calculate what would be the output port value into an already-allocated appropriate r...
Definition: system3.h:334
virtual const T & DoEvalPotentialEnergy(const Context3< T > &context) const
If your system is capable of storing energy, implement this method to return the potential energy cur...
Definition: system3.h:680
const AbstractContext3 & find_my_subcontext(const AbstractContext3 &some_subcontext) const
Get const access to this subsystem's subcontext given const access to any other subsystem's subcontex...
Definition: system3.h:278
int get_num_subsystems() const
Returns the current number of subsystems contained in this system diagram.
Definition: system3.h:236
const VectorInterface< T > & EvalVectorOutputPort(const Context3< T > &context, int port_num) const
Convenience method for obtaining the up-to-date value for an output port which is known to be vector ...
Definition: system3.h:504
std::unique_ptr< Context3< T > > CreateDefaultContext() const
Get a default Context<T> compatible with this System<T>.
Definition: system3.h:492
virtual ~AbstractSystem3()
Definition: system3.h:70
AbstractSystem3(const std::string &name)
Definition: system3.h:346
OutputPortFinder(int subsystem_num, int port_num, OutputPort3 *port)
Definition: system3.h:39
const OutputPortFinder & get_output_port_finder(int port_num) const
Definition: system3.h:318