73#include <yarp/os/RFModule.h>
74#include <yarp/os/Network.h>
75#include <yarp/os/PeriodicThread.h>
76#include <yarp/os/RpcClient.h>
78#include <yarp/sig/Vector.h>
79#include <yarp/sig/Matrix.h>
81#include <yarp/math/Math.h>
86#include "iCubBreatherThread.h"
89using namespace yarp::os;
90using namespace yarp::sig;
91using namespace yarp::math;
101class iCubBreather:
public RFModule
104 iCubBreatherThread *iCubBreatherThrd;
113 bool respond(
const Bottle &command, Bottle &reply)
115 int ack =Vocab32::encode(
"ack");
116 int nack=Vocab32::encode(
"nack");
118 if (command.size()>0)
120 switch (command.get(0).asVocab32())
122 case createVocab32(
's',
't',
'a',
'r'):
124 int res=Vocab32::encode(
"started");
125 if (iCubBreatherThrd -> startBreathing())
127 reply.addVocab32(ack);
130 reply.addVocab32(nack);
132 reply.addVocab32(res);
135 case createVocab32(
's',
't',
'o',
'p'):
137 int res=Vocab32::encode(
"stopped");
138 if (iCubBreatherThrd -> stopBreathing())
140 reply.addVocab32(ack);
143 reply.addVocab32(nack);
145 reply.addVocab32(res);
150 return RFModule::respond(command,reply);
154 reply.addVocab32(nack);
158 bool configure(ResourceFinder &rf)
160 string name =
"iCubBreather";
161 string robot =
"icub";
162 string part =
"left_arm";
165 int numWaypoints = 1;
166 bool autoStart =
false;
167 double noiseStd = 0.5;
168 double refSpeeds = 10.0;
171 if (rf.check(
"name"))
173 name = rf.find(
"name").asString();
174 yInfo(
"*** Module name set to %s",name.c_str());
176 else yInfo(
"*** Module name set to default, i.e. %s",name.c_str());
177 setName(name.c_str());
180 if (rf.check(
"rate"))
182 rate = rf.find(
"rate").asInt32();
183 yInfo((
"*** "+name+
": thread working at %i ms").c_str(), rate);
185 else yInfo((
"*** "+name+
": could not find rate in the config file; using %i ms as default").c_str(), rate);
188 if (rf.check(
"verbosity"))
190 verbosity = rf.find(
"verbosity").asInt32();
191 yInfo((
"*** "+name+
": verbosity set to %i").c_str(),verbosity);
193 else yInfo((
"*** "+name+
": could not find verbosity option in the config file; using %i as default").c_str(),verbosity);
196 if (rf.check(
"noiseStd"))
198 noiseStd = rf.find(
"noiseStd").asFloat64();
199 yInfo((
"*** "+name+
": noiseStd set to %g").c_str(),noiseStd);
201 else yInfo((
"*** "+name+
": could not find noiseStd option in the config file; using %g as default").c_str(),noiseStd);
204 if (rf.check(
"refSpeeds"))
206 refSpeeds = rf.find(
"refSpeeds").asFloat64();
207 yInfo((
"*** "+name+
": refSpeeds set to %g").c_str(),refSpeeds);
209 else yInfo((
"*** "+name+
": could not find refSpeeds option in the config file; using %g as default").c_str(),refSpeeds);
212 if (rf.check(
"robot"))
214 robot = rf.find(
"robot").asString();
215 yInfo((
"*** "+name+
": robot is %s").c_str(),robot.c_str());
217 else yInfo((
"*** "+name+
": could not find robot option in the config file; using %s as default").c_str(),robot.c_str());
220 if (rf.check(
"part"))
222 part = rf.find(
"part").asString();
223 yInfo((
"*** "+name+
": part is %s").c_str(),part.c_str());
225 else yInfo((
"*** "+name+
": could not find part option in the config file; using %s as default").c_str(),part.c_str());
228 if (rf.check(
"autoStart"))
234 iCubBreatherThrd =
new iCubBreatherThread(rate, name, robot, part, autoStart,
235 noiseStd, refSpeeds, verbosity, rf);
237 iCubBreatherThrd -> start();
241 delete iCubBreatherThrd;
242 iCubBreatherThrd = 0;
243 yError(
" iCubBreatherThread wasn't instantiated!!");
246 yInfo(
"ICUB BREATHER: iCubBreatherThread istantiated..");
249 rpcSrvr.open((
"/"+name+
"/rpc:i").c_str());
257 yInfo(
"ICUB BREATHER: Stopping threads..");
258 if (iCubBreatherThrd)
260 iCubBreatherThrd->stop();
261 delete iCubBreatherThrd;
268 double getPeriod() {
return 1.0; }
269 bool updateModule() {
return true; }
275int main(
int argc,
char * argv[])
278 rf.setDefaultContext(
"funny-things");
279 rf.setDefaultConfigFile(
"iCubBreather.ini");
280 rf.configure(argc,argv);
282 if (rf.check(
"help"))
284 cout << endl <<
"IMU IDENTIFIER module" << endl;
285 cout << endl <<
"Options:" << endl;
286 cout <<
" --context path: where to find the called resource. Default gazeStabilization." << endl;
287 cout <<
" --from from: the name of the .ini file. Default iCubBreather.ini." << endl;
288 cout <<
" --name name: the name of the module. Default iCubBreather." << endl;
289 cout <<
" --robot robot: the name of the robot. Default icub." << endl;
290 cout <<
" --rate rate: the period used by the thread. Default 500ms." << endl;
291 cout <<
" --noiseStd double: standard deviation of the noise. Default 1.0." << endl;
292 cout <<
" --refSpeeds double: The reference speeds at the joints. Default 5.0." << endl;
293 cout <<
" --verbosity int: verbosity level. Default 0." << endl;
294 cout <<
" --autoStart flag: if to autostart the module or not. Default no." << endl;
300 if (!yarp.checkNetwork())
302 yInfo(
"No Network!!!\n");
306 iCubBreather icubBrthr;
307 return icubBrthr.runModule(rf);