1
//! Basic connector class.
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.
11
* @author Lutz Winkler
15
#ifndef CONNECTOR_BASE_H_
16
#define CONNECTOR_BASE_H_
19
#include <srCore/robot/objectBase.h>
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>
29
#include <dtGame/gamemanager.h>
33
#include <osgDB/ReadFile>
37
#include <dtGame/gameactor.h>
41
//class osgBodyTransform;
43
const float epsilon = 0.61;
45
//class osgBodyNodeVisitor : public osg::NodeVisitor
48
// osgBodyNodeVisitor(BodyBase& root, TraversalMode tm=TRAVERSE_NONE);
49
// osgBodyNodeVisitor (TraversalMode tm=TRAVERSE_NONE);
50
// osgBodyNodeVisitor (VisitorType type, TraversalMode tm=TRAVERSE_NONE);
52
// virtual void apply(osgBodyTransform &node);
53
// virtual void accept(osgBodyNodeVisitor& bnv);
55
// void addChildToBodyRoot (BodyBase* child);
56
// BodyBase* returnBodyRoot ();
58
// virtual ~osgBodyNodeVisitor ();
65
class ConnectorBase : public ObjectBase
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
83
* \brief Initializes the connector. Needs to be called, before using the connector.
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
91
void init(dtCore::RefPtr<BodyBase> body,
92
float anchorX, float anchorY, float anchorZ,
93
float axisX, float axisY, float axisZ);
95
//! Sets scale of the display for the connector
96
void setScale(osg::Vec3 scale);
98
RobotActorBase* getRobot() const {return myBody->getRobot();};
100
//! Returns the unique id of the connector.
101
dtCore::UniqueId* getID();
104
* \brief Writes the parameter value in parameter
106
* The available parameters can be retrieved by calling getParameterList()
107
* See also: ObjectBase::getParameter()
111
virtual void getParameter(const std::string &name, ParameterReturnValue parameter);
114
* \brief Sets a parameter
116
* See also getParameter()
119
virtual void setParameter(const std::string &name, ParameterReturnValue parameter);
121
//! returns the state of the connector
122
inline ConnectorState getState() const {return state;};
124
//! returns the state of the connector
125
inline void setState(ConnectorState state) {this->state = state;};
127
//! returns the connecte robot if connected, otherwise NULL
128
RobotActorBase* getConnectedRobot() const {
129
if (state == CONNECTED) return theOtherConnector->getRobot();
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;
139
void sendLocalMessage(const std::string& message);
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.
147
//! Disconnects from the other connector. The joint will be destroyed.
150
//! Returns the ODE body ID of the main body the connector is attached to.
153
//! Returns the Robot3D body
154
BodyBase* getSRBody() {return myBody;}
156
//! Returns the global position of the connector
157
osg::Vec3f getTranslation();
159
//! Returns the global look-at vector of the connector
160
osg::Vec3f getRotation();
162
//! Returns the up-vector of the connector
163
inline osg::Vec3f getUpVector();
165
//! Returns the look-at-vector of the connector
166
inline osg::Vec3f getLookAtVector();
168
//! Sets ODE-joint. Should only be called within this class while connecting.
169
inline void setConnector(dJointID *connector)
171
if (connector == NULL) state = DISCONNECTED;//connected = false;
173
this->connector = *connector;
180
//! Returns the ODE-joint.
181
inline dJointID getConnector() const {return this->connector;}
183
//! Activate/Deactivate the joint
184
virtual void setActive(bool activate) {}
186
//! Will be called within RobotActorBase::TickLocal
187
virtual inline void update(float deltaSimTme) {
189
// printf("%s, ConnectorState = ", name.c_str());
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;
201
// if (state != DISCONNECTED) std::cout << state << std::endl;
203
if (state == CONNECTING) connecting(theOtherConnector);
205
if (state == CHECK_CONNECTION) {
213
//! Returns the name of the connector
214
inline std::string getName() {return name;}
216
//! Returns the type of the connector
217
inline std::string getType() {return type;}
220
virtual ~ConnectorBase();
222
//! Connects with the other connector. Establish a fixed connection
223
void connect(dtCore::RefPtr<ConnectorBase> theOtherConnector);
225
//! Move robots to the final position.
226
void connecting(dtCore::RefPtr<ConnectorBase> theOtherConnector);
228
//! Get distance to the closest connector
229
osg::Vec3 GetClosestConnector(int &robotId, int &connectorId);
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;
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
242
if ((angle<(-1+epsilon)) || (angle>1-epsilon) || ((angle>-epsilon) && (angle<epsilon))) return true;
249
osg::Vec3f paramAbsTranslation, paramAbsOrientation, paramUpVector;
250
ConnectorState state;
258
#endif /*CONNECTOR_BASE_H_*/