37 #include <Rcs_typedef.h> 38 #include <Rcs_macros.h> 39 #include <ControllerBase.h> 60 action->ele[0] = 0.2*std::cos(2.*M_PI*t)*(135*M_PI/180);
72 unsigned int numPosCtrlJoints = 0;
73 RCSGRAPH_TRAVERSE_JOINTS(config->graph) {
74 if (JNT->jacobiIndex != -1) {
75 if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_POSITION) {
80 allJointsPosCtrl = config->graph->nJ == numPosCtrlJoints;
83 currentGraph = config->graph;
84 desiredGraph = RcsGraph_clone(currentGraph);
87 q_ctrl = MatNd_clone(desiredGraph->q);
88 qd_ctrl = MatNd_clone(desiredGraph->q_dot);
89 T_ctrl = MatNd_create(desiredGraph->dof, 1);
93 homePoseFilt =
new Rcs::SecondOrderLPFND(currentGraph->q->ele, tmc, config->dt, desiredGraph->q->m);
94 homePoseFilt->setTarget(currentGraph->q->ele);
97 controlPolicy =
nullptr;
99 observation = config->observationModel->getSpace()->createValueMatrix();
100 action = config->actionModel->getSpace()->createValueMatrix();
103 config->actionModel->reset();
104 config->observationModel->reset();
110 MatNd_destroy(q_ctrl);
111 MatNd_destroy(qd_ctrl);
112 MatNd_destroy(T_ctrl);
114 MatNd_destroy(observation);
115 MatNd_destroy(action);
120 config->graph =
nullptr;
123 RcsGraph_destroy(desiredGraph);
131 if ((controlPolicy ==
nullptr && q_des ==
nullptr) || (controlPolicy !=
nullptr && q_des !=
nullptr)) {
132 throw std::invalid_argument(
"Either controlPolicy or q_des need to be != nullptr, not none or both!");
134 std::unique_lock<std::mutex> lock(controlPolicyMutex);
135 this->controlPolicy = controlPolicy;
137 if (controlPolicy ==
nullptr) {
139 homePoseFilt->setTarget(q_des->ele);
140 MatNd_setZero(qd_ctrl);
141 MatNd_setZero(T_ctrl);
145 config->observationModel->reset();
146 config->actionModel->reset();
149 getConfig()->actionModel->setGraph(RcsGraph_clone(getCurrentGraph()));
152 amIK->
setGraph(RcsGraph_clone(getCurrentGraph()));
159 std::unique_lock<std::mutex> lock(controlPolicyMutex);
162 config->observationModel->computeObservation(observation, action, config->dt);
165 if (controlPolicy !=
nullptr) {
167 controlPolicy->computeAction(action, observation);
170 config->actionModel->computeCommand(q_ctrl, qd_ctrl, T_ctrl, action, getCallbackUpdatePeriod());
174 if (!allJointsPosCtrl) {
175 Rcs::ControllerBase::computeInvDynJointSpace(T_ctrl, config->graph, q_ctrl, 1000.);
178 if (controlPolicy ==
nullptr) {
181 homePoseFilt->iterate();
182 homePoseFilt->getPosition(q_ctrl->ele);
183 homePoseFilt->getVelocity(qd_ctrl->ele);
187 setMotorCommand(q_ctrl, qd_ctrl, T_ctrl);
193 logger.record(observation, action);
208 RcsGraph_setState(desiredGraph, currentGraph->q, currentGraph->q_dot);
210 RcsGraph_setState(getConfig()->graph, currentGraph->q, currentGraph->q_dot);
212 RcsGraph_setState(getConfig()->actionModel->getGraph(), currentGraph->q, currentGraph->q_dot);
216 RcsGraph_setState(amIK->getDesiredGraph(), currentGraph->q, currentGraph->q_dot);
220 homePoseFilt->init(currentGraph->q->ele);
RcsPyBot(PropertySource *propertySource)
MatNd * getAction() const
void syncGraphsToCurrent()
MatNd * getObservation() const
void setGraph(RcsGraph *newGraph)
virtual void computeAction(MatNd *action, const MatNd *observation)
static ExperimentConfig * create(PropertySource *properties)
virtual void updateControl()
void setControlPolicy(ControlPolicy *controlPolicy, const MatNd *q_des=nullptr)