funny-things
All Modules
iCubBreatherModule.cpp
1/*
2 * Copyright (C) 2014 iCub Facility - Istituto Italiano di Tecnologia
3 * Author: Alessandro Roncone & Ugo Pattacini
4 * email: alessandro.roncone@iit.it
5 * Permission is granted to copy, distribute, and/or modify this program
6 * under the terms of the GNU General Public License, version 2 or any
7 * later version published by the Free Software Foundation.
8 *
9 * A copy of the license can be found at
10 * http://www.robotcub.org/icub/license/gpl.txt
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 * Public License for more details
16*/
17
73#include <yarp/os/RFModule.h>
74#include <yarp/os/Network.h>
75#include <yarp/os/PeriodicThread.h>
76#include <yarp/os/RpcClient.h>
77
78#include <yarp/sig/Vector.h>
79#include <yarp/sig/Matrix.h>
80
81#include <yarp/math/Math.h>
82
83#include <iostream>
84#include <string.h>
85
86#include "iCubBreatherThread.h"
87
88using namespace yarp;
89using namespace yarp::os;
90using namespace yarp::sig;
91using namespace yarp::math;
92
93using namespace std;
94
101class iCubBreather: public RFModule
102{
103 private:
104 iCubBreatherThread *iCubBreatherThrd;
105 RpcServer rpcSrvr;
106
107 public:
108 iCubBreather()
109 {
110 iCubBreatherThrd=0;
111 }
112
113 bool respond(const Bottle &command, Bottle &reply)
114 {
115 int ack =Vocab32::encode("ack");
116 int nack=Vocab32::encode("nack");
117
118 if (command.size()>0)
119 {
120 switch (command.get(0).asVocab32())
121 {
122 case createVocab32('s','t','a','r'):
123 {
124 int res=Vocab32::encode("started");
125 if (iCubBreatherThrd -> startBreathing())
126 {
127 reply.addVocab32(ack);
128 }
129 else
130 reply.addVocab32(nack);
131
132 reply.addVocab32(res);
133 return true;
134 }
135 case createVocab32('s','t','o','p'):
136 {
137 int res=Vocab32::encode("stopped");
138 if (iCubBreatherThrd -> stopBreathing())
139 {
140 reply.addVocab32(ack);
141 }
142 else
143 reply.addVocab32(nack);
144
145 reply.addVocab32(res);
146 return true;
147 }
148 //-----------------
149 default:
150 return RFModule::respond(command,reply);
151 }
152 }
153
154 reply.addVocab32(nack);
155 return true;
156 }
157
158 bool configure(ResourceFinder &rf)
159 {
160 string name = "iCubBreather";
161 string robot = "icub";
162 string part = "left_arm";
163 int verbosity = 0; // verbosity
164 int rate = 2000; // rate of the iCubBreatherThread
165 int numWaypoints = 1;
166 bool autoStart = false;
167 double noiseStd = 0.5;
168 double refSpeeds = 10.0;
169
170 //******************* NAME ******************
171 if (rf.check("name"))
172 {
173 name = rf.find("name").asString();
174 yInfo("*** Module name set to %s",name.c_str());
175 }
176 else yInfo("*** Module name set to default, i.e. %s",name.c_str());
177 setName(name.c_str());
178
179 //****************** rate ******************
180 if (rf.check("rate"))
181 {
182 rate = rf.find("rate").asInt32();
183 yInfo(("*** "+name+": thread working at %i ms").c_str(), rate);
184 }
185 else yInfo(("*** "+name+": could not find rate in the config file; using %i ms as default").c_str(), rate);
186
187 //******************* VERBOSE ******************
188 if (rf.check("verbosity"))
189 {
190 verbosity = rf.find("verbosity").asInt32();
191 yInfo(("*** "+name+": verbosity set to %i").c_str(),verbosity);
192 }
193 else yInfo(("*** "+name+": could not find verbosity option in the config file; using %i as default").c_str(),verbosity);
194
195 //******************* NOISESTD ******************
196 if (rf.check("noiseStd"))
197 {
198 noiseStd = rf.find("noiseStd").asFloat64();
199 yInfo(("*** "+name+": noiseStd set to %g").c_str(),noiseStd);
200 }
201 else yInfo(("*** "+name+": could not find noiseStd option in the config file; using %g as default").c_str(),noiseStd);
202
203 //******************* REFPEEDS ******************
204 if (rf.check("refSpeeds"))
205 {
206 refSpeeds = rf.find("refSpeeds").asFloat64();
207 yInfo(("*** "+name+": refSpeeds set to %g").c_str(),refSpeeds);
208 }
209 else yInfo(("*** "+name+": could not find refSpeeds option in the config file; using %g as default").c_str(),refSpeeds);
210
211 //******************* ROBOT ******************
212 if (rf.check("robot"))
213 {
214 robot = rf.find("robot").asString();
215 yInfo(("*** "+name+": robot is %s").c_str(),robot.c_str());
216 }
217 else yInfo(("*** "+name+": could not find robot option in the config file; using %s as default").c_str(),robot.c_str());
218
219 //******************* PART *******************
220 if (rf.check("part"))
221 {
222 part = rf.find("part").asString();
223 yInfo(("*** "+name+": part is %s").c_str(),part.c_str());
224 }
225 else yInfo(("*** "+name+": could not find part option in the config file; using %s as default").c_str(),part.c_str());
226
227 //************* AUTOSTART *******************
228 if (rf.check("autoStart"))
229 {
230 autoStart = true;
231 }
232
233 //****************** THREAD ******************
234 iCubBreatherThrd = new iCubBreatherThread(rate, name, robot, part, autoStart,
235 noiseStd, refSpeeds, verbosity, rf);
236
237 iCubBreatherThrd -> start();
238 bool strt = 1;
239 if (!strt)
240 {
241 delete iCubBreatherThrd;
242 iCubBreatherThrd = 0;
243 yError(" iCubBreatherThread wasn't instantiated!!");
244 return false;
245 }
246 yInfo("ICUB BREATHER: iCubBreatherThread istantiated..");
247
248 //************************ RPC ***********************
249 rpcSrvr.open(("/"+name+"/rpc:i").c_str());
250 attach(rpcSrvr);
251
252 return true;
253 }
254
255 bool close()
256 {
257 yInfo("ICUB BREATHER: Stopping threads..");
258 if (iCubBreatherThrd)
259 {
260 iCubBreatherThrd->stop();
261 delete iCubBreatherThrd;
262 iCubBreatherThrd=0;
263 }
264
265 return true;
266 }
267
268 double getPeriod() { return 1.0; }
269 bool updateModule() { return true; }
270};
271
275int main(int argc, char * argv[])
276{
277 ResourceFinder rf;
278 rf.setDefaultContext("funny-things");
279 rf.setDefaultConfigFile("iCubBreather.ini");
280 rf.configure(argc,argv);
281
282 if (rf.check("help"))
283 {
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;
295 cout << endl;
296 return 0;
297 }
298
299 Network yarp;
300 if (!yarp.checkNetwork())
301 {
302 yInfo("No Network!!!\n");
303 return 1;
304 }
305
306 iCubBreather icubBrthr;
307 return icubBrthr.runModule(rf);
308}
309// empty line to make gcc happy