34 #include <Rcs_macros.h> 35 #include <Rcs_basicMath.h> 44 unsigned int i, mn =
self->m*
self->n;
45 for (i = 0; i < mn; i++) {
46 if (self->ele[i] < 0){
47 self->ele[i] = std::min(self->ele[i], negUpperBound);
49 else if (self->ele[i] > 0){
50 self->ele[i] = std::max(self->ele[i], posLowerBound);
53 self->ele[i] = posLowerBound;
60 MatNd* signs = MatNd_createLike(src);
61 for (
unsigned int i = 0; i < src->m; i++) {
62 for (
unsigned int j = 0; j < src->n; j++) {
63 double val = (MatNd_get(src, i, j) >= 0) ? 1.0 : -1.0;
64 MatNd_set(signs, i, j, val);
99 for (
unsigned int i = 0; i <
getDim(); i++) {
107 std::vector<std::string> names;
108 for (
unsigned int i = 0; i <
getDim(); ++i) {
109 names.push_back(
"a_" + std::to_string(i));
122 MatNd* q_des, MatNd* q_dot_des, MatNd* T_des,
const MatNd* action,
double dt)
124 RCHECK(action->n == 1);
133 action_tmp = MatNd_clone(action);
134 action_abs = MatNd_clone(action);
135 MatNd_fabsEleSelf(action_abs);
142 MatNd_constMulSelf(action_tmp, 1./MatNd_sumEle(action_abs));
147 MatNd_softMax(action_tmp, action, action->m);
152 for (
unsigned int i = 0; i < action->m; i++) {
155 MatNd_clone2(otherActions, action);
156 MatNd_deleteRow(otherActions, i);
160 for (
unsigned int idx = 0; idx < otherActions->m; idx++) {
162 std::cout <<
"factor " << (1 - otherActions->ele[idx]) << std::endl;
165 prod *= (1 - otherActions->ele[idx]);
167 MatNd_destroy(otherActions);
170 std::cout <<
"prod " << prod << std::endl;
173 MatNd_set(action_tmp, i, 0, action->ele[i]*prod);
181 if (MatNd_minEle(action_abs) < 1e-6) {
184 std::cout <<
"Clipped the activations to ]-inf, -1e-6] u [1e-6, inf[" << std::endl;
189 MatNd_copyRows(
activation, 0, action_tmp, 0, action_tmp->m);
190 for (
unsigned int i = action_tmp->m; i < activation->m; i++) {
193 MatNd_destroy(action_tmp);
203 MatNd_printComment(
"q_des", q_des);
204 MatNd_printComment(
"q_dot_des", q_dot_des);
206 MatNd_printComment(
"T_des", T_des);
214 MatNd_setZero(action);
220 if (tcmName ==
"sum") {
223 else if (tcmName ==
"mean") {
226 else if (tcmName ==
"softmax") {
229 else if (tcmName ==
"product") {
233 std::ostringstream os;
234 os <<
"Unsupported task combination method: " << tcmName <<
"! Supported methods: sum, mean, softmax, product.";
235 throw std::invalid_argument(os.str());
272 this->x_des = MatNd_createLike(x_des);
273 MatNd_copy(this->x_des, x_des);
278 MatNd*
x_des = MatNd_create(1, 1);
280 if (taskSpec.size() !=
getController()->getNumberOfTasks()) {
281 std::ostringstream os;
282 os <<
"Received " << taskSpec.size() <<
" elements in taskSpec, but there are " 283 <<
getController()->getNumberOfTasks() <<
" Rcs controller tasks!";
284 throw std::runtime_error(os.str());
288 MatNd* x_des_temp =
nullptr;
289 if (!ts->getProperty(x_des_temp,
"x_des")) {
290 throw std::invalid_argument(
"Field x_des is missing for at least one task specification!");
292 MatNd_appendRows(x_des, x_des_temp);
294 MatNd_deleteRows(x_des, 0, 0);
296 MatNd_destroy(x_des);
MatNd * MatNd_signs(const MatNd *src)
MatNd * x_des
The goal in task space.
unsigned int dimAlwaysActiveTasks
Store the cumulative number of task dimensions which are always active.
MatNd * activation
The activation resulting from the action and the task combination method (used for logging) ...
const char * getTaskCombinationMethodName() const
virtual std::vector< std::string > getNames() const
virtual unsigned int getDim() const
Get the number of tasks multiplied by their individual dimension, owned by the action model...
virtual ~AMIKControllerActivation()
virtual void getStableAction(MatNd *action) const
static TaskCombinationMethod checkTaskCombinationMethod(std::string tcmName)
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)
void MatNd_fabsClipEleSelf(MatNd *self, double negUpperBound, double posLowerBound)
void ikFromDX(MatNd *q_des, MatNd *q_dot_des, double dt) const
AMIKControllerActivation(RcsGraph *graph, TaskCombinationMethod tcm)
TaskCombinationMethod taskCombinationMethod
Way to combine the tasks' contribution.
MatNd * getActivation() const
void setXdes(const MatNd *x_des)
void addAlwaysActiveTask(Task *task)
Add Rcs controller tasks that are always active. Do this after adding the regular tasks...
virtual void getMinMax(double *min, double *max) const
const ControllerBase * getController() const
void setXdesFromTaskSpec(std::vector< PropertySource *> &taskSpec)