7 bool HomeostaticModule::addNewDrive(
string driveName, yarp::os::Bottle& grpHomeostatic)
9 yDebug() <<
"Add drive " + driveName;
14 drv->
setDecay(grpHomeostatic.check((driveName +
"-decay"), Value(drv->
decay)).asDouble());
16 drv->
setGradient(grpHomeostatic.check((driveName +
"-gradient"), Value(drv->
gradient)).asInt());
17 drv->
setKey(grpHomeostatic.check((driveName +
"-key"), Value(drv->
key)).asString());
19 manager->addDrive(drv);
22 yInfo() <<
"new drive created successfully!";
26 bool HomeostaticModule::addNewDrive(
string driveName)
29 yInfo() <<
"adding a default drive..." ;
40 manager->addDrive(drv);
42 yDebug() <<
"default drive added. Opening ports...";
44 yInfo() <<
"new drive created successfully!";
53 outputm_ports.push_back(
new BufferedPort<Bottle>());
54 outputM_ports.push_back(
new BufferedPort<Bottle>());
56 if (!input_port.open(
"/"+moduleName+
"/fromSensations:i"))
58 yInfo() << getName() <<
": Unable to open port " <<
"/"<<moduleName<<
"/fromSensations:i" ;
60 string portName =
"/" + moduleName +
"/" + driveName;
62 pn = portName +
"/min:o";
63 yInfo() <<
"Configuring port " <<
" : "<< pn <<
" ..." ;
64 if (!outputm_ports.back()->open(pn))
66 yInfo() << getName() <<
": Unable to open port " << pn ;
69 pn = portName +
"/max:o";
70 yInfo() <<
"Configuring port " <<
" : "<< pn <<
" ..." ;
71 if (!outputM_ports.back()->open(pn))
73 yInfo() << getName() <<
": Unable to open port " << pn ;
81 moduleName = rf.check(
"name",Value(
"homeostasis")).asString();
82 setName(moduleName.c_str());
84 verbose = rf.check(
"verbose",Value(
"false")).asBool();
85 yInfo()<<
"-- Set verbose mode to "<<verbose<<
" -- ";
87 yInfo()<<moduleName<<
": finding configuration files...";
88 period = rf.check(
"period",Value(0.1)).asDouble();
92 yInfo() <<
"Initializing drives";
93 Bottle grpHomeostatic = rf.findGroup(
"HOMEOSTATIC");
94 Bottle *drivesList = grpHomeostatic.find(
"drives").asList();
97 yInfo() <<
"Configuration: Found " << drivesList->size() <<
" drives. " ;
98 for (
unsigned int d = 0; d<drivesList->size(); d++)
101 string driveName = drivesList->get(d).asString();
102 addNewDrive(driveName, grpHomeostatic);
107 yInfo() <<
"Opening RPC...";
109 rpc.open ( (
"/"+moduleName+
"/rpc"));
112 yInfo()<<
"Configuration done.";
116 bool HomeostaticModule::removeDrive(
int d)
119 manager->removeDrive(d);
121 input_ports[d]->close();
122 outputm_ports[d]->close();
123 outputM_ports[d]->close();
124 delete input_ports[d];
125 delete outputm_ports[d];
126 delete outputM_ports[d];
128 input_ports.erase(input_ports.begin()+d);
129 outputm_ports.erase(outputm_ports.begin()+d);
130 outputM_ports.erase(outputM_ports.begin()+d);
137 yInfo() <<
"RPC received: " << cmd.toString();
138 bool all_drives =
false;
139 if (cmd.get(0).asString() ==
"help" )
140 {
string help =
"\n";
141 help +=
" ['par'] ['drive'] ['val'/'min'/'max'/'dec'] [value] : Assigns a value to a specific parameter \n";
142 help +=
" ['delta'] ['drive'] ['val'/'min'/'max'/'dec'] [value] : Adds a value to a specific parameter \n";
143 help +=
" ['add'] ['conf'] [drive Bottle] : Adds a drive to the manager as a drive directly read from conf-file \n";
144 help +=
" ['add'] ['botl'] [drive Bottle] : Adds a drive to the manager as a Bottle of values of shape \n";
145 help +=
" ['add'] ['new'] [drive name] : Adds a default drive to the manager \n";
146 help +=
" ['rm'] [drive name] : removes a drive from the manager \n";
147 help +=
" ['sleep'] [drive name] [time] : prevent drive update for a certain time (in seconds) \n";
148 help +=
" ['sleep'] ['all'] [time] : prevent all drive updates for a certain time (in seconds) \n";
149 help +=
" ['names'] : returns an ordered list of the drives in the manager \n";
150 help +=
" ['verbose'] [true/false] : change verbosity \n";
151 help +=
" : (string name, double value, double homeo_min, double homeo_max, double decay = 0.05, bool gradient = true) \n";
152 reply.addString(help);
155 else if (cmd.get(0).asString() ==
"par" )
157 if (cmd.get(1).asString() ==
"all") {
160 for (
unsigned int d = 0; d<manager->drives.size();d++)
162 if (all_drives || cmd.get(1).asString() == manager->drives[d]->name)
164 if (cmd.get(2).asString()==
"val")
166 manager->drives[d]->setValue(cmd.get(3).asDouble());
168 else if (cmd.get(2).asString()==
"min")
170 manager->drives[d]->setHomeostasisMin(cmd.get(3).asDouble());
172 else if (cmd.get(2).asString()==
"max")
174 manager->drives[d]->setHomeostasisMax(cmd.get(3).asDouble());
176 else if (cmd.get(2).asString()==
"dec")
178 manager->drives[d]->setDecay(cmd.get(3).asDouble());
180 else if (cmd.get(2).asString()==
"decaymult")
182 manager->drives[d]->setDecayMultiplier(cmd.get(3).asDouble());
184 else if (cmd.get(2).asString()==
"reset")
186 manager->drives[d]->reset();
190 reply.addString(
"Format is: \n - ['par'] [drive_name] [val/min/max/dec/reset] [value]");
193 reply.addString(
"ack");
197 reply.addString(
"nack: wrong drive name");
199 else if (cmd.get(0).asString() ==
"delta" )
201 for (
unsigned int d = 0; d<manager->drives.size();d++)
203 if (cmd.get(1).asString() == manager->drives[d]->name)
205 if (cmd.get(2).asString()==
"val")
207 manager->drives[d]->deltaValue(cmd.get(3).asDouble());
209 else if (cmd.get(2).asString()==
"min")
211 manager->drives[d]->deltaHomeostasisMin(cmd.get(3).asDouble());
213 else if (cmd.get(2).asString()==
"max")
215 manager->drives[d]->deltaHomeostasisMax(cmd.get(3).asDouble());
217 else if (cmd.get(2).asString()==
"dec")
219 manager->drives[d]->deltaDecay(cmd.get(3).asDouble());
223 reply.addString(
"nack");
224 yInfo() <<
"Format is: \n - ['par'] [drive_name] [val/min/max/dec] [value]";
226 reply.addString(
"ack");
230 else if (cmd.get(0).asString()==
"add")
232 if (cmd.get(1).asString()==
"conf")
234 Bottle *ga = cmd.get(2).asList();
235 Bottle grpAllostatic = ga->findGroup(
"ALLOSTATIC");
236 addNewDrive(cmd.get(2).check(
"name",yarp::os::Value(
"")).asString(), grpAllostatic);
237 reply.addString(
"add drive from config bottle: ack");
240 else if (cmd.get(1).asString()==
"botl")
242 Drive d = bDrive(cmd.get(2).asList());
243 manager->addDrive(&d);
245 reply.addString(
"add drive from bottle: ack");
247 else if (cmd.get(1).asString()==
"new")
249 string d_name = cmd.get(2).asString();
250 yInfo() <<
"adding new drive... " ;
251 bool b = addNewDrive(d_name);
253 reply.addString(
"add new drive: ack");
255 reply.addString(
"ack. drive not created");
259 reply.addString(
"nack");
263 else if (cmd.get(0).asString()==
"rm")
265 for (
unsigned int d = 0; d<manager->drives.size();d++)
267 if (cmd.get(1).asString() == manager->drives[d]->name)
269 bool b = removeDrive(d);
271 reply.addString(
"ack: Successfuly removed");
273 reply.addString(
"ack: Could not remove the drive");
277 reply.addString(
"nack");
279 else if (cmd.get(0).asString()==
"freeze" || cmd.get(0).asString()==
"unfreeze") {
280 if (cmd.get(1).asString() ==
"all") {
283 for (
unsigned int d = 0; d<manager->drives.size();d++)
285 if (all_drives || cmd.get(1).asString() == manager->drives[d]->name)
287 if (cmd.get(0).asString()==
"freeze") {
288 manager->drives[d]->freeze();
291 manager->drives[d]->unfreeze();
295 reply.addString(
"ack");
297 else if (cmd.get(0).asString()==
"sleep")
299 if (cmd.get(1).asString() !=
"all")
301 for (
unsigned int d = 0; d<manager->drives.size();d++)
303 if (cmd.get(1).asString() == manager->drives[d]->name)
305 double t = cmd.get(2).asDouble();
306 manager->sleep(d, t);
307 reply.addString(
"ack: Drive sleep");
313 for (
unsigned int d = 0; d<manager->drives.size();d++)
315 double t = cmd.get(2).asDouble();
316 manager->sleep(d, t);
317 std::stringstream ss;
318 ss <<
"ack: "<< manager->drives[d]->name <<
" sleep for " << t <<
"s";
319 reply.addString(ss.str());
322 reply.addString(
"nack");
324 else if (cmd.get(0).asString()==
"names")
328 for (
unsigned int i=0;i<manager->drives.size();i++)
330 nms.addString(manager->drives[i]->name);
334 else if (cmd.get(0).asString() ==
"ask")
336 for (
auto drive: manager->drives)
338 if (cmd.get(1).asString() == drive->name)
340 if (cmd.get(2).asString() ==
"min") {
341 reply.addDouble(drive->homeostasisMin);
342 }
else if (cmd.get(2).asString() ==
"max") {
343 reply.addDouble(drive->homeostasisMax);
345 reply.addString(
"nack");
349 if (reply.size() == 0) {
350 reply.addString(
"nack");
353 else if (cmd.get(0).asString() ==
"force")
355 for (
unsigned int d = 0; d<manager->drives.size();d++)
357 manager->drives[d]->freeze();
358 manager->drives[d]->reset();
359 if (cmd.get(1).asString() == manager->drives[d]->name)
361 if (cmd.get(2).asString()==
"bottom")
362 manager->drives[d]->setValue(0.);
363 else if (cmd.get(2).asString()==
"top")
364 manager->drives[d]->setValue(1.);
367 reply.addString(
"Format is: \n - ['force'] [drive_name] [top/bottom]");
369 reply.addString(
"ack");
373 reply.addString(
"Format is: \n - ['force'] [drive_name] [top/bottom]");
378 else if (cmd.get(0).asString() ==
"verbose")
381 yWarning()<<
"The command should be 'verbose true/false'";
384 verbose = cmd.get(1).asBool();
389 reply.addString(
"nack");
396 yarp::os::Bottle* sens_input = input_port.read(
false);
397 if (sens_input == 0) {
398 yDebug()<<
"Waiting for sensations";
403 for(
unsigned int d = 0; d<manager->drives.size();d++)
407 yInfo() <<
"Going by drive #"<<d <<
" with name "<< manager->drives[d]->name ;
411 inp = sens_input->check(manager->drives[d]->key,Value(
"None")).asDouble();
413 if(manager->drives[d]->gradient ==
true)
417 manager->drives[d]->setValue(inp);
424 manager->drives[d]->update();
426 yarp::os::Bottle &out1 = outputm_ports[d]->prepare();
428 out1.addDouble(-manager->drives[d]->getValue()+manager->drives[d]->homeostasisMin);
429 outputm_ports[d]->write();
431 yarp::os::Bottle &out2 = outputM_ports[d]->prepare();
433 out2.addDouble(+manager->drives[d]->getValue()-manager->drives[d]->homeostasisMax);
434 outputM_ports[d]->write();
438 yInfo() <<
"Drive value: " << manager->drives[d]->value;
439 yInfo() <<
"Drive decay: " << manager->drives[d]->decay;
440 yInfo() <<
"Drive homeostasisMin: " << manager->drives[d]->homeostasisMin;
441 yInfo() <<
"Drive homeostasisMax: " << manager->drives[d]->homeostasisMax;
442 yInfo() <<
"Drive gradient: " << manager->drives[d]->gradient;
443 yInfo() <<
"Drive period: " << manager->drives[d]->period;
444 yInfo() << out1.get(0).asDouble();
453 for (
unsigned int d=0; d<manager->drives.size(); d++)
455 input_ports[d]->interrupt();
456 input_ports[d]->close();
457 delete input_ports[d];
459 outputM_ports[d]->interrupt();
460 outputM_ports[d]->close();
461 delete outputM_ports[d];
463 outputm_ports[d]->interrupt();
464 outputm_ports[d]->close();
465 delete outputm_ports[d];
467 delete manager->drives[d];
471 outputM_ports.clear();
472 outputm_ports.clear();
void setValue(double d_value)
void setHomeostasisMax(double d_homeo_max)
void setHomeostasisMin(double d_homeo_min)
std::string name
Name of the drive.
std::string key
Deprecated, not used anymore.
bool configure(yarp::os::ResourceFinder &rf)
void setDecay(double d_decay)
int openPorts(std::string driveName)
openPorts opens default ports for external communication
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
void setKey(std::string d_key)