45 environment.
getController()->getAvailableSignals(signal_target);
53 environment.
getPlant()->getAvailableSignals(signal_target);
60 environment.
getObserver()->getAvailableSignals(signal_target);
78 using ControlVector = Eigen::VectorXd;
79 using StateVector = Eigen::VectorXd;
80 using OutputVector = Eigen::VectorXd;
89 if (!
verify(environment, msg))
return;
96 if (!plant->
output(
y, t, signal_target, ns))
PRINT_ERROR(
"OpenLoopControlTask::performTask(): error while retreiving plant output.");
101 bool controller_success =
102 controller->
step(
x, *
_xreference, *
_ureference,
Duration(0), t, u_sequence, x_sequence, signal_target,
nullptr,
nullptr,
nullptr, ns);
103 if (!controller_success)
106 PRINT_ERROR(
"OpenLoopControlTask::performTask(): controller error.");
109 PRINT_INFO(
"Controller CPU time: " << (
Time::now() - time_pre_step).toSec() * 1000 <<
" ms.");
117 if (!u_sequence || u_sequence->getTimeDimension() < 1)
119 PRINT_ERROR(
"OpenLoopControlTask::performTask(): control does not provide any open-loop control sequence. Canceling.");
124 Time tf(u_sequence->getTime().back() + 1e-12);
131 while (t <= tf &&
ok())
134 if (!plant->
output(
y, t, signal_target, ns))
PRINT_ERROR(
"OpenLoopControlTask::performTask(): error while retreiving plant output.");
143 PRINT_ERROR(
"OpenLoopControlTask::performTask(): control sequence interpolation error.");
148 if (
_dt <= 0 && t_idx < u_sequence->getTimeDimension() - 1)
150 dt.
fromSec(u_sequence->getTimeRef()[t_idx + 1] - u_sequence->getTimeRef()[t_idx]);
155 plant->
control(u, dt, t, signal_target, ns);
162 PRINT_WARNING(
"OpenLoopControlTask(): rate exceeded (" << rate.lastCycleTime().toSec() <<
"s/" << dt <<
"s).");
169 signal_target->
sendMeasurement(ns +
"ctrl_succeess", 0.0, {(double)controller_success});
176 if (msg) msg->clear();
181 if (msg) *msg +=
"The provided controller does not support open loop control tasks.\n";
189 if (msg) *msg +=
"State reference trajectory not specified for OpenLoopControlTask\n";
196 if (msg) *msg +=
"Control reference trajectory not specified for OpenLoopControlTask\n";
200 std::string environment_msg;
201 ret_val = ret_val && environment.
verify(&environment_msg);
202 if (msg) *msg += environment_msg;
203 if (ret_val ==
false)
return false;
210 *msg +=
"State reference trajectory dimension (" + std::to_string(
_xreference->getDimension()) +
211 ") does not match controller state dimension (" + std::to_string(environment.
getController()->getStateDimension()) +
").\n";
217 *msg +=
"Control reference trajectory dimension (" + std::to_string(
_ureference->getDimension()) +
218 ") does not match controller control input dimension (" +
219 std::to_string(environment.
getController()->getControlInputDimension()) +
").\n";
222 if (environment.
getPlant()->requiresFutureControls() && !environment.
getController()->providesFutureControls())
225 if (msg) *msg +=
"Controller does not support control sequences, that are required by the plant";
228 if (environment.
getPlant()->requiresFutureStates() && !environment.
getController()->providesFutureStates())
231 if (msg) *msg +=
"Controller does not support state sequences, that are required by the plant";
237 #ifdef MESSAGE_SUPPORT 238 void OpenLoopControlTask::toMessage(corbo::messages::OpenLoopControlTask& message)
const 245 void OpenLoopControlTask::fromMessage(
const corbo::messages::OpenLoopControlTask& message, std::stringstream* issues)
252 if (message.has_xreference())
256 if (util::get_oneof_field_type(message.xreference(),
"reference", type,
false))
262 xreference->fromMessage(message.xreference(), issues);
270 if (message.has_ureference())
274 if (util::get_oneof_field_type(message.ureference(),
"reference", type,
false))
280 ureference->fromMessage(message.ureference(), issues);
void setControlReference(ReferenceTrajectoryInterface::Ptr ureference)
Set control input reference trajectory.
#define PRINT_FATAL(msg)
Print msg-stream as fatal error msg and exit program execution.
Standard environment for control tasks.
#define PRINT_WARNING(msg)
Print msg-stream.
ReferenceTrajectoryInterface::Ptr _xreference
static Time now()
Retrieve current system time.
OpenLoopControlTask()
Default constructor.
Interpolation
List of available interpolation methods.
virtual void sendSignals(double t, SignalTargetInterface &signal_target, const std::string &ns="") const
void fromSec(double t)
Set duration from seconds (see Duration(double t))
ReferenceTrajectoryInterface::Ptr _ureference
virtual void stop()
Stop plant (you might probably use this to set the plant into a safe setpoint)
virtual bool step(const StateVector &x, ReferenceTrajectoryInterface &xref, ReferenceTrajectoryInterface &uref, const Duration &dt, const Time &t, ControlVector &u, SignalTargetInterface *signal_target=nullptr, const std::string &ns="")
Perform actual controller step / control law computation.
virtual void sendMeasurement(Measurement::ConstPtr measurement)=0
Send a measurement to the target.
Interface class for signal targets.
Representation of time stamps.
void performTask(Environment &environment, SignalTargetInterface *signal_target=nullptr, std::string *msg=nullptr, const std::string &ns="") override
Perform task.
virtual bool observe(const OutputVector &y, StateVector &x, const Duration &dt, const Time &t, SignalTargetInterface *signal_target=nullptr, const std::string &ns="")=0
Perform actual observer step / state estimation.
virtual bool hasPiecewiseConstantControls() const =0
Return true if the controller returns piecewise constant control pieces.
Interface class for observers.
const PlantInterface::Ptr & getPlant() const
Read access to the underlying plant.
virtual void registerMeasurement(const std::string &unique_name, int value_dimension, const std::vector< std::string > &value_labels={}, bool zero_order_hold=false)=0
Register a measurement type at current target.
Rate objects can be used to run loops at a desired rate.
bool verify(std::string *msg=nullptr) const
Check if the environment satisfies all requirements (dimensions, ...)
virtual int getStateDimension() const =0
Return the dimension of the required plant state/output.
void getAvailableSignals(const Environment &environment, SignalTargetInterface &signal_target, const std::string &ns="") const override
Retrieve available signals from the task.
static Factory & instance()
< Retrieve static instance of the factory
const ObserverInterface::Ptr & getObserver() const
Read access to the underlying observer.
virtual int getControlInputDimension() const =0
Return the control input dimension.
constexpr const int INHERITED
Inherit property.
virtual bool initialize()
Initialize plant.
double toSec() const
Cast time stamp to seconds.
virtual int getOutputDimension() const =0
Return the plant output dimension (y)
virtual bool control(const ControlVector &u, const Duration &dt, const Time &t, SignalTargetInterface *signal_target=nullptr, const std::string &ns="")
Send commands to plant.
bool ok()
global method to check whether to proceed or cancel the current action
std::shared_ptr< ReferenceTrajectoryInterface > Ptr
Interface class for controllers.
std::shared_ptr< TimeSeries > Ptr
void setStateReference(ReferenceTrajectoryInterface::Ptr xreference)
Set state reference trajectory.
virtual bool output(OutputVector &output, const Time &t, SignalTargetInterface *signal_target=nullptr, const std::string &ns="")=0
Retrieve current plant output (measurements)
virtual bool initialize(const StateVector &x, ReferenceTrajectoryInterface &expected_xref, ReferenceTrajectoryInterface &expected_uref, const Duration &expected_dt, const Time &t, ReferenceTrajectoryInterface *expected_sref=nullptr)
Initialize the controller.
std::shared_ptr< Derived > create(const std::string &name, bool print_error=true) const
Create a shared instance of the desired object.
The matrix class, also used for vectors and row-vectors.
const ControllerInterface::Ptr & getController() const
Read access to the underlying controller.
Representation of time durations.
bool verify(const Environment &environment, std::string *msg=nullptr) const override
Check if the environment and other settings satisfy all requirements for the given task...
#define PRINT_INFO(msg)
Print msg-stream.
Interface class for plants.
#define PRINT_ERROR(msg)
Print msg-stream as error msg.