simulated_plant_threaded.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_PLANTS_INCLUDE_CORBO_PLANTS_SIMULATED_PLANT_THREADED_H_
26 #define SRC_PLANTS_INCLUDE_CORBO_PLANTS_SIMULATED_PLANT_THREADED_H_
27 
29 
30 #include <memory>
31 #include <mutex>
32 #include <thread>
33 
34 namespace corbo {
35 
53 {
54  public:
55  using StateVector = Eigen::VectorXd;
56 
61 
62  virtual ~SimulatedPlantThreaded();
63 
64  // implements interface method
65  Ptr getInstance() const override { return std::make_shared<SimulatedPlantThreaded>(); }
66 
67  // implements interface method
68  bool initialize() override;
69 
70  // implements interface method
71  void stop() override;
72 
73  // implements interface method
74  void reset() override;
75 
76  // implements interface method
77  bool control(const TimeSeries::ConstPtr& u_sequence, const TimeSeries::ConstPtr& x_sequence, const Duration& dt, const Time& t,
78  SignalTargetInterface* signal_target = nullptr, const std::string& ns = "") override;
79 
80  // implements interface method
81  bool output(OutputVector& output, const Time& t, SignalTargetInterface* signal_target = nullptr, const std::string& ns = "") override;
82 
83  // implements interface method
84  bool setState(const Eigen::Ref<const Eigen::VectorXd>& state) override;
85 
87  void setSimulationRate(double sim_rate) { _sim_rate = sim_rate; }
88 
89 #ifdef MESSAGE_SUPPORT
90  void toMessage(messages::SimulatedPlantThreaded& message) const;
93  void fromMessage(const messages::SimulatedPlantThreaded& message, std::stringstream* issues = nullptr);
94 
95  // implements interface method
96  void toMessage(messages::Plant& message) const override { toMessage(*message.mutable_simulated_plant_threaded()); }
97  // implements interface method
98  void fromMessage(const messages::Plant& message, std::stringstream* issues = nullptr) override
99  {
100  fromMessage(message.simulated_plant_threaded(), issues);
101  }
102 #endif
103 
104  protected:
105  void simulate();
106 
107  private:
108  double _sim_rate = 10;
109 
110  std::thread _worker_thread;
111  bool _stop_thread = false;
112 
113  std::mutex _control_mutex;
115 
117 };
118 
120 
121 } // namespace corbo
122 
123 #endif // SRC_PLANTS_INCLUDE_CORBO_PLANTS_SIMULATED_PLANT_THREADED_H_
Ptr getInstance() const override
Return a newly created shared instance of the implemented class.
bool setState(const Eigen::Ref< const Eigen::VectorXd > &state) override
Set/move plant to a desired state (if possible)
Interface class for signal targets.
Representation of time stamps.
Definition: time.h:251
Adapter class for plant simulation.
std::shared_ptr< SystemOutputInterface > Ptr
bool output(OutputVector &output, const Time &t, SignalTargetInterface *signal_target=nullptr, const std::string &ns="") override
Retrieve current plant output (measurements)
void setSimulationRate(double sim_rate)
Specify rate for the simulation thread.
Eigen::VectorXd StateVector
Eigen::VectorXd ControlVector
#define FACTORY_REGISTER_PLANT(type)
std::shared_ptr< PlantInterface > Ptr
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
SimulatedPlantThreaded()
Default constructor.
bool initialize() override
Initialize plant.
bool control(const TimeSeries::ConstPtr &u_sequence, const TimeSeries::ConstPtr &x_sequence, const Duration &dt, const Time &t, SignalTargetInterface *signal_target=nullptr, const std::string &ns="") override
Send commands to plant.
Adapter class for plant simulation (threaded version)
Eigen::VectorXd OutputVector
void stop() override
Stop plant (you might probably use this to set the plant into a safe setpoint)
std::shared_ptr< const TimeSeries > ConstPtr
Definition: time_series.h:65
Representation of time durations.
Definition: time.h:106
std::shared_ptr< SystemDynamicsInterface > Ptr


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:20