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

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