source: UH/SAMGAR/SAMGAR LIB/SourceFiles/SAMGARMAIN.cpp @ 121

Revision 121, 11.4 KB checked in by KDucasse, 10 years ago (diff)

Folder and files for the to compile samgar modules

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