28 template <
class CONTROLLED_SYSTEM>
32 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 static const size_t STATE_DIM = CONTROLLED_SYSTEM::STATE_DIM;
35 static const size_t CONTROL_DIM = CONTROLLED_SYSTEM::CONTROL_DIM;
37 using SCALAR =
typename CONTROLLED_SYSTEM::SCALAR;
45 std::shared_ptr<CONTROLLED_SYSTEM> controlled_system,
51 throw std::runtime_error(
"Step sizes must be positive.");
53 throw std::runtime_error(
"Simulation step must be smaller than the control step.");
62 system_ = std::shared_ptr<Controller<STATE_DIM, CONTROL_DIM, SCALAR>>(arg.
system_->clone());
112 while (std::chrono::duration<double>(std::chrono::high_resolution_clock::now() -
sim_start_time_).count() <
120 if (std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - wall_time).count() >=
124 std::cerr <<
"Simulation running too slow. Please increase the step size!" << std::endl;
127 std::this_thread::sleep_until(wall_time + std::chrono::duration<double>(
control_dt_));
129 wall_time += std::chrono::duration_cast<std::chrono::system_clock::duration>(
136 sim_time = std::chrono::duration<double>(wall_time -
sim_start_time_).count();
139 }
catch (std::exception& e)
141 std::cout << e.what() << std::endl;
142 throw(std::runtime_error(
"Control Simulator failed."));
152 while (std::chrono::duration<double>(std::chrono::high_resolution_clock::now() -
sim_start_time_).count() <
157 std::chrono::duration<double>(std::chrono::high_resolution_clock::now() -
sim_start_time_).count();
161 std::chrono::duration<double>(std::chrono::high_resolution_clock::now() -
sim_start_time_).count();
164 }
catch (std::exception& e)
166 std::cout << e.what() << std::endl;
167 throw(std::runtime_error(
"Control Simulator failed."));
174 std::shared_ptr<Controller<STATE_DIM, CONTROL_DIM, SCALAR>>
controller_;
void stop()
stops the simulation
Definition: ControlSimulator.h:98
void integrate_n_steps(StateVector< STATE_DIM, SCALAR > &state, const SCALAR &startTime, size_t numSteps, SCALAR dt, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory)
Equidistant integration based on number of time steps and step length.
Definition: Integrator-impl.h:50
virtual void prepareControllerIteration(Time sim_time)
During controller update, this method does processing before the state measurement arrives...
Definition: ControlSimulator.h:75
StateVector< STATE_DIM > x0_
Definition: ControlSimulator.h:176
std::shared_ptr< CONTROLLED_SYSTEM > system_
Definition: ControlSimulator.h:173
void simulate(Time duration, const IntegrationType &intType=IntegrationType::EULERCT)
spawns the two threads in a nonblocking way
Definition: ControlSimulator.h:79
ControlSimulator(Time sim_dt, Time control_dt, const StateVector< STATE_DIM > &x0, std::shared_ptr< CONTROLLED_SYSTEM > controlled_system, bool verbose=false)
constructor
Definition: ControlSimulator.h:42
std::atomic< bool > stop_
Definition: ControlSimulator.h:182
typename CONTROLLED_SYSTEM::SCALAR SCALAR
Definition: ControlSimulator.h:37
virtual ~ControlSimulator()
destructor
Definition: ControlSimulator.h:71
std::shared_ptr< Controller< STATE_DIM, CONTROL_DIM, SCALAR > > controller_
Definition: ControlSimulator.h:174
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t STATE_DIM
Definition: ControlSimulator.h:34
std::mutex control_mtx_
Definition: ControlSimulator.h:181
A class for simulating controlled systems in a general way.
Definition: ControlSimulator.h:29
virtual void finishSystemIteration(Time sim_time)
Gets called after the integrator step.
Definition: ControlSimulator.h:73
virtual void simulateController(Time duration)
run by the thread that updates the controller
Definition: ControlSimulator.h:147
std::thread sys_thread_
Definition: ControlSimulator.h:178
std::thread control_thread_
Definition: ControlSimulator.h:179
Time sim_dt_
Definition: ControlSimulator.h:171
Standard Integrator.
Definition: Integrator.h:62
Definition: Integrator.h:39
const bool verbose
Definition: JacobianCGTest.h:19
static const size_t CONTROL_DIM
Definition: ControlSimulator.h:35
ControlSimulator(const ControlSimulator &arg)
copy constructor
Definition: ControlSimulator.h:57
ControlSimulator()=default
default constructor
virtual void finishControllerIteration(Time sim_time)
During controller update, this method does processing once the state measurement arrives.
Definition: ControlSimulator.h:77
Time control_dt_
Definition: ControlSimulator.h:172
std::chrono::time_point< std::chrono::high_resolution_clock > sim_start_time_
Definition: ControlSimulator.h:175
bool verbose_
Definition: ControlSimulator.h:183
virtual void simulateSystem(Time duration, const IntegrationType &intType=IntegrationType::EULERCT)
run by the thread that simulates the system
Definition: ControlSimulator.h:101
StateVector< STATE_DIM > x_
Definition: ControlSimulator.h:177
std::mutex state_mtx_
Definition: ControlSimulator.h:180
double Time
Definition: Time.h:11
IntegrationType
The available integration types.
Definition: Integrator.h:30
void finish()
waits for the simulation threads to finish
Definition: ControlSimulator.h:89