source: libs/SAMGAR V2/Module/ExampleOne.h @ 579

Revision 579, 4.1 KB checked in by KDucasse, 9 years ago (diff)
Line 
1#include "SamClass.h"
2
3/****** SAMGAR V2 EXAMPLES******/
4
5/*
6 * a couple of class's showing how to use SAMGAR, they dont have to be done in
7 * header files at all, but it can be better its easer to have a option of
8 * compiling them together or seperately, both of which have there advantages in
9 * differnt situatoins
10 *
11 * if you want to send imgs there is a class in yarp exactly for that, you can
12 * just send a IPL image.
13 *
14 * IMPORTANT: update 1, need to name and create module before opening the
15 * ports!!, due to persistent connections in latest yarp release
16 *
17 */
18
19// This class just sends out some data
20class ExampleOneWrite: public SamClass
21{
22  private:
23   int myint;
24   BufferedPort<Bottle> myfirst,myfirst2; // create buffered ports for bottles like this
25   Network yarp;                                 // make sure the network is ready
26 
27  public:
28   void SamInit(void)
29   {
30      myint=0;
31      RecognisePort("W1");                // name the port to be shown in the gui
32   
33      RecognisePort("W2");                // name the port to be shown in the gui
34      StartModule("/Writer");   
35   
36      myfirst2.open("/Writer_W2");        // open the port
37      myfirst2.setReporter(myPortStatus);         // set reporter
38
39
40      // we weve addid all ports and are happy then send the module name, this
41      // must be done last!!
42
43      myfirst.open("/Writer_W1");                 // open the port with MODULENAME_PORTNAME
44      myfirst.setReporter(myPortStatus);          // set reporter, this is important
45
46      puts("started writer");
47   }
48   void SamIter(void)
49   {
50      Bottle& B = myfirst.prepare();      // prepare the bottle/port
51      B.clear();
52      B.addInt(myint++);
53      myfirst.write();                    // add stuff then send
54      Bottle& C = myfirst2.prepare();
55      C.clear();
56      C.addInt(myint+5);
57      myfirst2.write();
58      puts("running writer");
59   }
60};
61
62class ExampleTwoRead: public SamClass
63{
64  private:
65   int myint;
66   BufferedPort<Bottle> myfirst;
67   Network yarp;
68
69  public:
70   void SamInit(void)
71   {
72      myint=0;
73      RecognisePort("R1");
74      StartModule("/Reader");
75      myfirst.open("/Reader_R1");
76      myfirst.setReporter(myPortStatus);
77
78      puts("started reader");
79   }
80   void SamIter(void)
81   {
82      puts("running reader");
83      Bottle *input = myfirst.read(false); // get in the input from the port, if
84      // you want it to wait use true, else use false
85      if(input!=NULL)                    // check theres data
86      {
87         puts("got a msg");
88         puts(input->toString());
89      }
90      else{puts("didn't get a msg");}
91   }
92};
93
94// A very simple data class
95class DataForm
96{
97  public:
98   int x;
99   int y;
100   int p;
101};
102
103class ExampleThreeSendClass: public SamClass
104{
105  private:
106   int myint;
107   BufferedPort< BinPortable<DataForm> > myfirst; // use BinPortable to make
108   // a class ready to be used on
109   // the network
110   Network yarp;
111  public:
112   void SamInit(void)
113   {
114      myint=0;
115      RecognisePort("Out");
116      StartModule("/SClass");
117      myfirst.open("/SClass_Out");
118      myfirst.setReporter(myPortStatus);
119   }
120   void SamIter(void)
121   {
122      myint++;
123      BinPortable<DataForm> &MyData = myfirst.prepare(); // prepare data/port
124      MyData.content().x=myint;                                         
125      MyData.content().y=myint+5;
126      MyData.content().p=myint+10;
127      myfirst.write();                             // add stuff and write
128   }
129};
130
131
132// a Interupt port, when data hits this port it'll do whatever is onread, be
133// carefull, fast firing interupts can cause big problems as in normal code
134
135class DataPort : public BufferedPort< BinPortable<DataForm> > 
136{
137   virtual void onRead(BinPortable<DataForm>& b)
138   {
139      printf("X %i Y %i P %i \n",b.content().x,b.content().y,b.content().p);
140   }
141};
142
143class ExampleFourReciveClassInterupt: public SamClass
144{
145  private:
146   int myint;
147   DataPort myfirst;
148   Network yarp;
149  public:
150   void SamInit(void)
151   {
152      myint=0;
153      RecognisePort("In");
154      StartModule("/RClass");
155      myfirst.open("/RClass_In");
156      myfirst.setReporter(myPortStatus);
157      myfirst.useCallback();                            // this tells it to use the onread method
158   }   
159};
Note: See TracBrowser for help on using the repository browser.