source: level2/competencies/RobotHouse/ExampleOne.h @ 532

Revision 532, 3.7 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
13IMPORTANT: update 1, need to name and create module before opening the ports!!, due to persistent connections in latest yarp release
14*/
15
16
17// This class just sends out some data //
18
19class ExampleOneWrite: public SamClass
20{
21private:
22int myint;
23BufferedPort<Bottle> myfirst,myfirst2; // create buffered ports for bottles like this
24Network yarp;                                              // make sure the network is ready
25
26public:
27        void SamInit(void)
28        {
29        myint=0;
30        RecognisePort("W1");                            // name the port to be shown in the gui
31       
32        RecognisePort("W2");                            // name the port to be shown in the gui
33        StartModule("/Writer");
34
35        myfirst2.open("/Writer_W2");            // open the port
36        myfirst2.setReporter(myPortStatus);     // set reporter
37
38
39                                // we weve addid all ports and are happy then send the module name, this must be done last!!
40
41        myfirst.open("/Writer_W1");                     // open the port with MODULENAME_PORTNAME
42        myfirst.setReporter(myPortStatus);      // set reporter, this is important
43
44        puts("started writer");
45        }
46        void SamIter(void)
47        {
48        Bottle& B = myfirst.prepare();          // prepare the bottle/port
49        B.clear();
50        B.addInt(myint++);
51        myfirst.write();                                        // add stuff then send
52        Bottle& C = myfirst2.prepare();
53        C.clear();
54        C.addInt(myint+5);
55        myfirst2.write();       
56        puts("running writer");
57        }
58};
59
60
61class ExampleTwoRead: public SamClass
62{
63private:
64int myint;
65BufferedPort<Bottle> myfirst;
66Network yarp;
67
68public:
69        void SamInit(void)
70        {
71        myint=0;
72        RecognisePort("R1");
73        StartModule("/Reader");
74        myfirst.open("/Reader_R1");
75        myfirst.setReporter(myPortStatus);
76
77        puts("started reader");
78        }
79        void SamIter(void)
80        {
81                puts("running reader");
82                Bottle *input = myfirst.read(false); // get in the input from the port, if you want it to wait use true, else use false
83                if(input!=NULL)                                          // check theres data
84                {
85                puts("got a msg");
86                puts(input->toString());
87                }
88                else{puts("didn't get a msg");}
89        }
90};
91
92// A very simple data class
93class DataForm
94{
95public:
96int x;
97int y;
98int p;
99};
100
101
102class ExampleThreeSendClass: public SamClass
103{
104private:
105int myint;
106BufferedPort<BinPortable<DataForm>> myfirst; // use BinPortable to make a class ready to be used on the network
107Network yarp;
108public:
109        void SamInit(void)
110        {
111        myint=0;
112        RecognisePort("Out");
113        myfirst.open("/SClass_Out");
114        myfirst.setReporter(myPortStatus);
115        StartModule("/SClass");
116        }
117        void SamIter(void)
118        {
119                myint++;
120                BinPortable<DataForm> &MyData = myfirst.prepare(); // prepare data/port
121                MyData.content().x=myint;                                               
122                MyData.content().y=myint+5;
123                MyData.content().p=myint+10;
124                myfirst.write();                                                                   // add stuff and write
125        }
126};
127
128
129// 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
130
131class DataPort : public BufferedPort<BinPortable<DataForm>> 
132{
133     virtual void onRead(BinPortable<DataForm>& b)
134         {
135                 printf("X %i Y %i P %i \n",b.content().x,b.content().y,b.content().p);
136     }
137};
138class ExampleFourReciveClassInterupt: public SamClass
139{
140private:
141int myint;
142DataPort myfirst;
143Network yarp;
144public:
145        void SamInit(void)
146        {
147        myint=0;
148        RecognisePort("In");
149        myfirst.open("/RClass_In");
150        myfirst.setReporter(myPortStatus);
151        myfirst.useCallback();                          // this tells it to use the onread method
152        StartModule("/RClass");
153        }
154       
155};
Note: See TracBrowser for help on using the repository browser.