43 if (environment.getController())
45 environment.getController()->getAvailableSignals(signal_target);
48 signal_target.registerMeasurement(ns +
"control_input", environment.getController()->getControlInputDimension(), {},
true);
51 if (environment.getPlant())
53 environment.getPlant()->getAvailableSignals(signal_target);
55 signal_target.registerMeasurement(ns +
"plant_output", environment.getPlant()->getOutputDimension());
58 if (environment.getObserver())
60 environment.getObserver()->getAvailableSignals(signal_target);
62 signal_target.registerMeasurement(ns +
"observed_states", environment.getObserver()->getStateDimension());
74 ControllerInterface* controller = environment.getController().get();
75 PlantInterface* plant = environment.getPlant().get();
76 ObserverInterface* observer = environment.getObserver().get();
78 using ControlVector = Eigen::VectorXd;
79 using StateVector = Eigen::VectorXd;
80 using OutputVector = Eigen::VectorXd;
85 ControlVector u(controller->getControlInputDimension());
86 OutputVector
y(plant->getOutputDimension());
87 StateVector
x(controller->getStateDimension());
89 if (!
verify(environment, msg))
return;
93 if (!plant->initialize())
PRINT_FATAL(
"Plant initialization failed.");
96 if (!plant->output(
y, t, signal_target, ns))
PRINT_ERROR(
"OpenLoopControlTask::performTask(): error while retreiving plant output.");
97 if (!observer->observe(
y,
x,
Duration(0), t, signal_target, ns))
PRINT_ERROR(
"OpenLoopControlTask::performTask(): observer error.");
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.");
115 if (signal_target) controller->sendSignals(t.toSec(), *signal_target, ns);
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.");
135 if (signal_target) signal_target->sendMeasurement(ns +
"plant_output", t.toSec(),
y);
138 if (!observer->observe(
y,
x, Duration(0), t, signal_target))
PRINT_ERROR(
"OpenLoopControlTask::performTask(): observer error.");
139 if (signal_target) signal_target->sendMeasurement(ns +
"observed_states", t.toSec(),
x);
143 PRINT_ERROR(
"OpenLoopControlTask::performTask(): control sequence interpolation error.");
145 if (signal_target) signal_target->sendMeasurement(ns +
"control_input", t.toSec(), u);
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();
178 if (environment.getController() && !environment.getController()->providesFutureControls())
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;
206 if (environment.getController()->getStateDimension() !=
_xreference->getDimension())
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";
213 if (environment.getController()->getControlInputDimension() !=
_ureference->getDimension())
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);