~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srCore/actuator/connectorBase.h

  • Committer: Anne van Rossum
  • Date: 2010-08-10 15:58:55 UTC
  • Revision ID: anne@gamix-20100810155855-kve7x2vwouagdij9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//! Basic connector class.
 
2
 
 
3
/*!
 
4
 *
 
5
 * This class simulates a connector in a very simple way. If two connectors
 
6
 * are close enough, have the right orientation and if both want to connect,
 
7
 * a fixed joint will be created that connects the two bodies, where the
 
8
 * connectors are attached to. If one of the connectors wants to disconnect,
 
9
 * the joint will be destroyed.
 
10
 *
 
11
 * @author Lutz Winkler
 
12
 *
 
13
 */
 
14
 
 
15
#ifndef CONNECTOR_BASE_H_
 
16
#define CONNECTOR_BASE_H_
 
17
 
 
18
 
 
19
#include <srCore/robot/objectBase.h>
 
20
 
 
21
#include <dtCore/dt.h>
 
22
//#include <dtCore/object.h>
 
23
#include <dtCore/globals.h>
 
24
#include <dtCore/transform.h>
 
25
#include <dtGame/gameactor.h>
 
26
#include <dtCore/refptr.h>
 
27
#include <dtCore/uniqueid.h>
 
28
 
 
29
#include <dtGame/gamemanager.h>
 
30
 
 
31
 
 
32
#include <osg/Node>
 
33
#include <osgDB/ReadFile>
 
34
 
 
35
#include <ode/ode.h>
 
36
 
 
37
#include <dtGame/gameactor.h>
 
38
 
 
39
namespace srCore {
 
40
 
 
41
//class osgBodyTransform;
 
42
 
 
43
const float epsilon = 0.61;
 
44
 
 
45
//class osgBodyNodeVisitor : public osg::NodeVisitor
 
46
//{
 
47
//public:
 
48
//      osgBodyNodeVisitor(BodyBase& root, TraversalMode tm=TRAVERSE_NONE);
 
49
//      osgBodyNodeVisitor (TraversalMode tm=TRAVERSE_NONE);
 
50
//      osgBodyNodeVisitor (VisitorType type, TraversalMode tm=TRAVERSE_NONE);
 
51
//
 
52
//      virtual void apply(osgBodyTransform &node);
 
53
//      virtual void accept(osgBodyNodeVisitor& bnv);
 
54
//
 
55
//      void addChildToBodyRoot (BodyBase* child);
 
56
//      BodyBase* returnBodyRoot ();
 
57
//
 
58
//      virtual ~osgBodyNodeVisitor ();
 
59
//
 
60
//private:
 
61
//      BodyBase* parent;
 
62
//
 
63
//};
 
64
 
 
65
class ConnectorBase : public ObjectBase
 
66
{
 
67
public:
 
68
 
 
69
        enum ConnectorState {
 
70
 
 
71
                DISCONNECTED,                                                                                           //not connected, do not wish to connect
 
72
                CHECK_CONNECTION,                                                                                       //automatic connection, only during start to connect robots
 
73
                WANT_TO_CONNECT,                                                                                        //not connected
 
74
                CONNECTING,                                                                                                     //connecting, but not connection not finished
 
75
                CONNECTED
 
76
 
 
77
        };
 
78
 
 
79
        //! Constructor
 
80
        ConnectorBase();
 
81
 
 
82
        /**
 
83
        * \brief Initializes the connector. Needs to be called, before using the connector.
 
84
        *
 
85
        * \param                body                                            Body, where the connector is attached to
 
86
        * \param                        anchorX, anchorY, anchorZ       local position of the connector
 
87
        * \param                        axisX, axisY, axisZ                     local orientation of the connector
 
88
        * \param                        gm                                                      The GameManager, which handles the corresponding robot actor
 
89
        *
 
90
        */
 
91
        void init(dtCore::RefPtr<BodyBase> body,
 
92
                        float anchorX, float anchorY, float anchorZ,
 
93
                        float axisX, float axisY, float axisZ);
 
94
 
 
95
        //! Sets scale of the display for the connector
 
96
        void setScale(osg::Vec3 scale);
 
97
 
 
98
        RobotActorBase* getRobot() const {return myBody->getRobot();};
 
99
 
 
100
        //! Returns the unique id of the connector.
 
101
        dtCore::UniqueId* getID();
 
102
 
 
103
        /**
 
104
         * \brief  Writes the parameter value in parameter
 
105
         *
 
106
         *              The available parameters can be retrieved by calling getParameterList()
 
107
         *              See also: ObjectBase::getParameter()
 
108
         *
 
109
         *
 
110
         */
 
111
        virtual void getParameter(const std::string &name, ParameterReturnValue  parameter);
 
112
 
 
113
        /**
 
114
         * \brief  Sets a parameter
 
115
         *
 
116
         *              See also getParameter()
 
117
         *
 
118
         */
 
119
        virtual void setParameter(const std::string &name, ParameterReturnValue  parameter);
 
120
 
 
121
        //! returns the state of the connector
 
122
        inline ConnectorState getState() const {return state;};
 
123
 
 
124
        //! returns the state of the connector
 
125
        inline void setState(ConnectorState state) {this->state =  state;};
 
126
 
 
127
        //! returns the connecte robot if connected, otherwise NULL
 
128
        RobotActorBase* getConnectedRobot() const {
 
129
                if (state == CONNECTED) return theOtherConnector->getRobot();
 
130
                else return NULL;
 
131
        };
 
132
 
 
133
        //! returns the connector of the robot that is connected to. If not connected, it return NULL
 
134
        inline dtCore::RefPtr<ConnectorBase> getOtherConnector() const {
 
135
                if (state == CONNECTED) return theOtherConnector;
 
136
                else return NULL;
 
137
        };
 
138
 
 
139
        void sendLocalMessage(const std::string& message);
 
140
 
 
141
        /** sets the connector to be ready for connection. If another connector has the right transformation towards this connector
 
142
         *      and is ready for connection, the will be connected together.
 
143
         */
 
144
        void connect();
 
145
 
 
146
 
 
147
        //! Disconnects from the other connector. The joint will be destroyed.
 
148
        void disconnect();
 
149
 
 
150
        //! Returns the ODE body ID of the main body the connector is attached to.
 
151
        dBodyID getBodyID();
 
152
 
 
153
        //! Returns the Robot3D body
 
154
        BodyBase* getSRBody() {return myBody;}
 
155
 
 
156
        //! Returns the global position of the connector
 
157
        osg::Vec3f getTranslation();
 
158
 
 
159
        //! Returns the global look-at vector of the connector
 
160
        osg::Vec3f getRotation();
 
161
 
 
162
        //! Returns the up-vector of the connector
 
163
        inline osg::Vec3f getUpVector();
 
164
 
 
165
        //! Returns the look-at-vector of the connector
 
166
        inline osg::Vec3f getLookAtVector();
 
167
 
 
168
        //! Sets ODE-joint. Should only be called within this class while connecting.
 
169
        inline void setConnector(dJointID *connector)
 
170
        {
 
171
                if (connector == NULL) state = DISCONNECTED;//connected = false;
 
172
                else {
 
173
                        this->connector = *connector;
 
174
                        //connected = true;
 
175
                        state = CONNECTED;
 
176
                }
 
177
 
 
178
        }
 
179
 
 
180
        //! Returns the ODE-joint.
 
181
        inline dJointID getConnector() const {return this->connector;}
 
182
 
 
183
        //! Activate/Deactivate the joint
 
184
        virtual void setActive(bool activate) {}
 
185
 
 
186
        //! Will be called within RobotActorBase::TickLocal
 
187
        virtual inline void update(float deltaSimTme) {
 
188
 
 
189
//              printf("%s, ConnectorState = ", name.c_str());
 
190
//
 
191
//              switch(state) {
 
192
//
 
193
//              case DISCONNECTED: printf("Disconnected \n");break;
 
194
//              case CHECK_CONNECTION: printf("check connection\n");break;
 
195
//              case WANT_TO_CONNECT: printf("want to connect\n");break;
 
196
//              case CONNECTING: printf("connecting\n");break;
 
197
//              case CONNECTED: printf("connected\n");break;
 
198
//
 
199
//
 
200
//              }
 
201
//              if (state != DISCONNECTED) std::cout << state << std::endl;
 
202
 
 
203
                if (state == CONNECTING) connecting(theOtherConnector);
 
204
 
 
205
                if (state == CHECK_CONNECTION) {
 
206
 
 
207
                        connect();
 
208
 
 
209
                }
 
210
 
 
211
        }
 
212
 
 
213
        //! Returns the name of the connector
 
214
        inline std::string getName() {return name;}
 
215
 
 
216
        //! Returns the type of the connector
 
217
        inline std::string getType() {return type;}
 
218
 
 
219
protected:
 
220
        virtual ~ConnectorBase();
 
221
 
 
222
        //! Connects with the other connector. Establish a fixed connection
 
223
        void connect(dtCore::RefPtr<ConnectorBase> theOtherConnector);
 
224
 
 
225
        //! Move robots to the final position.
 
226
        void connecting(dtCore::RefPtr<ConnectorBase> theOtherConnector);
 
227
 
 
228
        //! Get distance to the closest connector
 
229
        osg::Vec3 GetClosestConnector(int &robotId, int &connectorId);
 
230
 
 
231
        std::string name;
 
232
        std::string type;
 
233
        dtCore::UniqueId* myID;
 
234
        dJointID connector;                                                                             // will be type of fixedJoint, if connected
 
235
        dtCore::RefPtr<ConnectorBase> theOtherConnector;                // NULL: if not connected, otherwise the
 
236
                                                                                                                        // pointer to the other connector
 
237
        dtCore::RefPtr<dtGame::GameManager> gm;
 
238
 
 
239
        //! function to check if connectors have the right alignment to connect to each other
 
240
        virtual inline bool checkAngle(float angle, float epsilon) const
 
241
        {
 
242
                if ((angle<(-1+epsilon)) || (angle>1-epsilon) || ((angle>-epsilon) && (angle<epsilon))) return true;
 
243
                else return false;
 
244
 
 
245
        }
 
246
 
 
247
private:
 
248
 
 
249
        osg::Vec3f paramAbsTranslation, paramAbsOrientation, paramUpVector;
 
250
        ConnectorState state;
 
251
 
 
252
        bool firstRun;
 
253
};
 
254
 
 
255
}
 
256
 
 
257
 
 
258
#endif /*CONNECTOR_BASE_H_*/