source: UH/SAMGAR/SAMGAR LIB/Trunk/SourceFiles/SAMGARMAIN.cpp @ 188

Revision 188, 11.7 KB checked in by KDucasse, 10 years ago (diff)
Line 
1
2
3#include <SamgarMainClass.h>
4
5using namespace std;
6using namespace yarp::os;
7using namespace yarp::sig;
8
9/*!
10Constructor class, needs four varibles
11first is the name of module on the network
12secound is the main catagory of the module
13third is the sub catagory of the module
14forth is the type of module either -run -interupt
15!*/
16SamgarModule::SamgarModule(string NameOfModule,string Catagory,string SubCatagory,int Type)
17{
18        Network yarp;
19MyName                  =NameOfModule;
20MyCatagory              =Catagory;
21MySubCatagory   =SubCatagory;
22modulemode              =Type;
23string Tempname = "/Main_" + MyName;
24DoIWantModules = false;
25while(Network::getNameServerName()=="/global"){;}
26open(Tempname.c_str());
27currentmode=running;
28useCallback();
29
30while(getInputCount()<1){;}
31yarp::os::Time::delay(3);
32// upon innit give its data to the gui
33Bottle& MyBottle =prepare();
34MyBottle.addInt(10); // code for gui
35MyBottle.addString(MyName.c_str());
36MyBottle.addString(MyCatagory.c_str());
37MyBottle.addString(MySubCatagory.c_str());
38write();
39
40//setstrict
41//this
42}
43/*!
44A method which enables the module list to be updated when new data is available or called for on the network
45SendAllModulesCommand can be used to update the module list
46!*/
47void SamgarModule::TurnOnModuleListener(void){DoIWantModules=true;}
48
49/*!
50Sends a query to SAMGAR Key to get available platforms
51!*/
52void SamgarModule::GetAvailPlatforms(void)
53{
54Bottle& MyBottle =prepare();
55MyBottle.clear();
56MyBottle.addInt(40);
57write();
58}
59
60
61
62/*!
63This function sends commands to all available modules within the group
640 = stop module , 1 = start module , 2 = get modules to respond with name and type
65!*/
66void SamgarModule::SendAllModulesCommand(int cm)
67{
68Bottle& MyBottle =prepare();
69MyBottle.clear();
70MyBottle.addInt(cm);
71write();
72}
73
74
75/*!
76This function sends commands to a singuler module
773 = stop module , 4 = start module , 5 = get modules to respond with name and type
78!*/
79void SamgarModule::SendModuleCommand(string name,int cm)
80{
81Bottle& MyBottle =prepare();
82MyBottle.clear();
83MyBottle.addInt(cm);
84MyBottle.addString(name.c_str()); // always adds to end of current list
85write();
86}
87
88/*!
89This function is a interupt driven module for the main port of the module
90DO NOT MANUALLY CALL
91!*/
92
93void SamgarModule::onRead(Bottle& b)
94{
95int myswitch = b.get(0).asInt();
96Bottle& MyBottle =prepare();
97MyBottle.clear();
98
99
100//printf("got some data with int %i \n",myswitch);
101
102/// have here a statment that catchs 10's is the user has specified it
103if(b.get(0).asInt()==105)
104{
105
106if(DoIWantModules==true)
107{
108        ModuleStruct TempStruct;
109        ListOfKnownModules.clear();
110        int FF;
111        FF=1;
112        while(FF<=b.size())
113        {
114                TempStruct.name        =b.get(FF).asString().c_str();FF++;
115                TempStruct.catagory    =b.get(FF).asString().c_str();FF++;
116                TempStruct.subcatagory =b.get(FF).asString().c_str();FF++;
117                ListOfKnownModules.push_front(TempStruct);
118        }
119
120}
121}
122if(b.get(0).asInt()==50 && DoIWantModules==true)
123{
124        ListOfKnownPlatforms.clear();
125
126        for(int hh =1;hh<b.size();hh++)
127        {
128                ListOfKnownPlatforms.push_front(b.get(hh).asString().c_str());
129        }
130}
131
132
133switch (myswitch)
134{
135case 0 :
136        currentmode=fullstop;
137//      printf("mode changed to stop \n");
138        break; // get all modules to stop
139case 1 :
140        currentmode=running;
141//      printf("mode changed to running \n");
142        break;  // get all modules to go again
143case 2 :
144//        printf("Sent some catagory data from a module \n");
145      MyBottle.addInt(10); // code for gui
146          MyBottle.addString(MyName.c_str());
147          MyBottle.addString(MyCatagory.c_str());
148          MyBottle.addString(MySubCatagory.c_str());
149      write();
150          break;
151}
152
153
154string mestring = b.get(1).asString().c_str();
155//if(myswitch>2 && b.get(1).asString()/*.c_str()*/ == MyName/*.c_str()*/) // for personal commands
156if(myswitch>2 && mestring.compare(MyName)==0)
157{
158//      printf("got into secound statment to turn off modules \n");
159        switch (myswitch)
160        {
161        case 3 :currentmode=fullstop;break; // get all modules to stop
162        case 4 :currentmode=running; break;  // get all modules to go again
163        case 5 :
164      MyBottle.addInt(10); // code for gui
165          MyBottle.addString(MyName.c_str());
166          MyBottle.addString(MyCatagory.c_str());
167          MyBottle.addString(MySubCatagory.c_str());
168      write();
169          break;
170        }
171}
172}
173/*! Deleates copys of varibles in the known module list !*/
174bool ListDeleatingFunction(ModuleStruct x,ModuleStruct y)
175{
176        if(x.name==y.name){return true;}
177        return false;
178}
179
180/*! Sorts the varibles in the known module list!*/
181bool ListSortingFunction(ModuleStruct x,ModuleStruct y)
182{
183        if(x.name>y.name){return true;}
184        return false;
185}
186
187
188
189/*! Setups a special port for images only !*/
190void SamgarModule::SetupImagePort(string outputname)
191{
192string Tempname = "/Port_" + MyName + "_" + outputname;
193Time::delay(1);
194YarpImagePort.open(Tempname.c_str());
195}
196
197/*! sends a picture out in the yarp native format !*/
198void SamgarModule::SendPictureYarpNative(ImageOf<PixelBgr> Image)
199{
200YarpImagePort.prepare() =Image; // no longer likes this line
201YarpImagePort.write();
202}
203/*! Recives a picture in the yarp native format !*/
204ImageOf<PixelBgr> SamgarModule::RecivePictureYarpNative(void)
205{
206ImageOf<PixelBgr> TempImage = *YarpImagePort.read();
207return TempImage;
208}
209
210/*! Sends a picture in the OpenCV native format IplImage !*/
211void SamgarModule::SendPictureOCVNative(IplImage* Image)
212{
213        ImageOf<PixelBgr> yarpReturnImage;
214    yarpReturnImage.wrapIplImage(Image);
215        SendPictureYarpNative(yarpReturnImage);
216}
217/*! Revices a picture in the OpenCV native formate IplImage !*/
218IplImage* SamgarModule::RecivePictureOCVNative(void)
219{
220if(YarpImagePort.getInputCount()>0)
221        {
222        ImageOf<PixelBgr> *img = YarpImagePort.read();
223        IplImage *frame2=(IplImage*) img->getIplImage();
224        return frame2;
225        }
226return false;
227}
228
229
230
231/*!
232Very important function, allows all modules to sleep/start from send commands
233Also if the module has been designated as a interupt module then it will send back
234false for failure also can have additional data on how well the module has done
235!*/
236
237
238void SamgarModule::SucceedFail(bool Awns,double additionaldata)
239{
240
241if(modulemode==interupt )// only send it when you got worthwhile data
242        {
243        Bottle& MyBottle =prepare();
244        MyBottle.clear();
245        string Tempstring;
246        MyBottle.addInt(20); // so it knows its a log report
247        MyBottle.addString(MyName.c_str());
248        MyBottle.addDouble(additionaldata);
249        if(getOutputCount()>0){write();}
250        currentmode = stoped;
251        }
252
253while(currentmode!=running)
254        {
255        yarp::os::Time::delay(0.01);
256        }
257
258
259}
260
261
262
263/*!
264Sends a message for the log in the gui
265!*/
266void SamgarModule::SendToLog(string LogData,int priority)
267{
268Bottle& MyBottle =prepare();
269MyBottle.clear();
270MyBottle.addInt(30);
271MyBottle.addString(MyName.c_str());
272MyBottle.addString(LogData.c_str());
273MyBottle.addInt(priority);
274write();
275
276}
277/*!
278Just adds a port to the module
279!*/
280void SamgarModule::AddPortS(string outputname)
281{
282        string Tempname = "/Port_" + MyName + "_" + outputname;
283        PortList.push_front(new DataPort(&currentmode));
284        Time::delay(1);
285        while(Network::getNameServerName()=="/global"){;}
286        PortList.front()->open(Tempname.c_str());
287        PortList.front()->useCallback();
288}
289
290
291
292
293void SamgarModule::SendBottleData(string port,Bottle data){SendData(TypeBottle,port,0,0,0,0,data);}
294/*! simpler function calls to the bigger functions !*/
295void SamgarModule::SendIntData(string port,int data){SendData(TypeInt,port," ",data,0,0,0);}
296/*! simpler function calls to the bigger functions !*/
297void SamgarModule::SendFloatData(string port,float data){SendData(TypeFloat,port," ",0,0,data,0);}
298/*! simpler function calls to the bigger functions !*/
299void SamgarModule::SendDoubleData(string port,double data){SendData(TypeDouble,port," ",0,data,0,0);}
300/*! simpler function calls to the bigger functions !*/
301void SamgarModule::SendStringData(string port,string data){SendData(TypeString,port,data,0,0,0,0);}
302
303/*! private function, DO NOT CALL DIRECTLY !*/
304void SamgarModule::SendData(int type,string port,string S,int I ,double D,float F,Bottle B)
305{
306        BufferedPort<Bottle> *MyTempPort;
307        string TempName;
308        bool FoundPort;
309
310        TempName="/Port_" + MyName + "_" + port.c_str();
311
312        FoundPort=false;
313        for ( ItPort=PortList.begin() ; ItPort != PortList.end(); ItPort++ )
314        {
315                MyTempPort=*ItPort;
316                if(MyTempPort->getName()==TempName.c_str())
317                {
318                        FoundPort=true;
319                break; // we have the port so break the loop
320                }
321        }
322if(FoundPort==true)
323{
324        Bottle& MyBottle =MyTempPort-> prepare();
325        MyBottle.clear();
326             if(type==TypeInt   ) {MyBottle.addInt   (I);}
327        else if(type==TypeFloat ) {MyBottle.addDouble(F);}
328        else if(type==TypeDouble) {MyBottle.addDouble(D);}
329        else if(type==TypeString) {MyBottle.addString(S.c_str());}
330        else if(type==TypeBottle) {MyBottle=B;}
331        MyTempPort->write();
332}
333
334}
335
336
337
338//These are just window dressing to make it easer for the user, instead of using GetDataFromPort
339
340/*! Gets int data from port, you give it the int you want changed and it changes it, it also replys with weather the port has been updated True/False !*/
341bool SamgarModule::GetIntData   (string NameOfPort,int    *I){return GetDataFromPort(NameOfPort,TypeInt     ,I,0,0,0,0);}
342/*! Gets float data from port, you give it the int you want changed and it changes it, it also replys with weather the port has been updated True/False !*/
343bool SamgarModule::GetFloatData (string NameOfPort,float  *I){return GetDataFromPort(NameOfPort,TypeFloat   ,0,I,0,0,0);}
344/*! Gets double data from port, you give it the int you want changed and it changes it, it also replys with weather the port has been updated True/False !*/
345bool SamgarModule::GetDoubleData(string NameOfPort,double *I){return GetDataFromPort(NameOfPort,TypeDouble  ,0,0,I,0,0);}
346/*! Gets string data from port, you give it the int you want changed and it changes it, it also replys with weather the port has been updated True/False !*/
347bool SamgarModule::GetStringData(string NameOfPort,string *I){return GetDataFromPort(NameOfPort,TypeString  ,0,0,0,I,0);}
348/*! Gets Bottle data from port, you give it the int you want changed and it changes it, it also replys with weather the port has been updated True/False !*/
349bool SamgarModule::GetBottleData(string NameOfPort,Bottle *I){return GetDataFromPort(NameOfPort,TypeBottle  ,0,0,0,0,I);}
350
351
352/*! DO NOT CALL DIRECTLY !*/
353bool SamgarModule::GetDataFromPort(string NameOfPort,int TypeOfData, int *I ,float *F ,double *D, string *S ,Bottle *B)
354{
355
356//BufferedPort<Bottle> *MyTempPort;
357
358DataPort *MyTempPort;
359
360bool HaveIfoundPort;
361
362
363
364
365NameOfPort="/Port_" + MyName + "_" + NameOfPort.c_str();
366HaveIfoundPort=false;
367
368for ( ItPort=PortList.begin() ; ItPort != PortList.end(); ItPort++ ) // find the right port loop
369        {
370        MyTempPort=*ItPort;
371        if(MyTempPort->getName()==NameOfPort.c_str()){HaveIfoundPort=true;break;}// have the data we need so break the loop
372        }
373
374if(MyTempPort->getInputCount()==0){return false;} // if nothings connected to it       
375
376Bottle MyBottle;
377
378//Bottle *MyBottle = MyTempPort->read();
379if(MyTempPort->istherebottle==1)
380{
381 MyBottle = MyTempPort->SavedBottle;
382MyTempPort->istherebottle=0;
383}
384else
385{
386return false;
387}
388if(MyBottle==NULL){return false;}
389if(HaveIfoundPort==true)
390{
391
392
393if(MyBottle.isNull()==true){return false;}
394if(MyBottle==NULL){return false;}
395
396if     (TypeOfData==TypeInt   &&MyBottle.get(0).isInt()   !=false) {*I=MyBottle.get(0).asInt()   ;return true;}
397else if(TypeOfData==TypeFloat &&MyBottle.get(0).isDouble()!=false)      {*F=MyBottle.get(0).asDouble();return true;}
398else if(TypeOfData==TypeString&&MyBottle.get(0).isString()!=false) {*S=MyBottle.get(0).asString();return true;}
399else if(TypeOfData==TypeDouble&&MyBottle.get(0).isDouble()!=false)      {*D=MyBottle.get(0).asDouble();return true;}
400else if(TypeOfData==TypeBottle)                                                                         {*B=MyBottle;return true;}
401}
402//MyTempPort->release();       
403return false;
404}
405
Note: See TracBrowser for help on using the repository browser.