~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srInterface/Agent.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
/* -*-c++-*-
 
2
 * Delta3D Open Source Game and Simulation Engine
 
3
 * Copyright (C) 2005, BMH Associates, Inc.
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or modify it under
 
6
 * the terms of the GNU Lesser General Public License as published by the Free
 
7
 * Software Foundation; either version 2.1 of the License, or (at your option)
 
8
 * any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
12
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
13
 * details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public License
 
16
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 
17
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * @author Curtiss Murphy
 
20
 */
 
21
#ifndef AGENT
 
22
#define AGENT
 
23
 
 
24
// General files
 
25
#include <map>
 
26
 
 
27
// Delta3D files
 
28
#include <dtCore/refptr.h>
 
29
#include <dtDAL/gameevent.h>
 
30
#include <dtDAL/gameeventmanager.h>
 
31
#include <dtGame/gmcomponent.h>
 
32
#include <dtUtil/log.h>
 
33
 
 
34
// Robot3D files
 
35
#include <srCore/robot/robotActorBase.h>
 
36
#include <srCore/export.h>
 
37
#include <srCore/simulationEvents.h>
 
38
#include <srCore/simulationUtils.h>
 
39
 
 
40
using namespace srCore;
 
41
 
 
42
namespace srInterface {
 
43
 
 
44
/*
 
45
 * Class: Agent
 
46
 *
 
47
 * An agent descends from a Delta3D game component. This can be seen as a wrapper that exposes
 
48
 * a robot to the world outside of the Robot3D simulator.
 
49
 */
 
50
class ROBOT_EXPORT Agent : public dtGame::GMComponent {
 
51
 
 
52
        friend class SimulationArenaComponent;
 
53
 
 
54
public:
 
55
        // Inner class for simulation messages
 
56
        // Todo: Check if this is still needed
 
57
        struct SimMessage {
 
58
                // String with data
 
59
                std::string data;
 
60
                // Type
 
61
                SimulationMessageType::commType type;
 
62
                // Port (what port?)
 
63
                int port;
 
64
 
 
65
        };
 
66
 
 
67
        //! Constructor for a Controller that controls one specific robot
 
68
        Agent(const std::string &name, RobotActorBase* robotActor);
 
69
 
 
70
        //! colors the collisionMesh of the Robot. This function will not be available on the real robot!
 
71
        void __colorRobot(osg::Vec4 color);
 
72
 
 
73
        /** moves the robot forward in 2D mode - physics and collision hadling still activated
 
74
        *@param name velocity: velocity vector, robot will be moved by ds = dt * velocity in its own coordinate system, only for this time step
 
75
        */
 
76
        void move2D(osg::Vec2 velocity);
 
77
 
 
78
        /** rotates the robot in 2D mode - physics and collision hadling still activated
 
79
        *@param name angular_v: angular velocity, robot will be rotate by delta alpha = dt * angular_v.
 
80
        */
 
81
        void rotate2D(float angular_v);
 
82
 
 
83
        //! returns the collision ID
 
84
        int getRobotID() {return robot->getID(); };
 
85
 
 
86
        virtual void ProcessMessage(const dtGame::Message &message);
 
87
 
 
88
        inline void setParamMap(std::map<std::string, std::string> paramMap) { this->paramMap = paramMap; } ;
 
89
 
 
90
protected:
 
91
 
 
92
        osg::Vec3f getRobotPosition() const {return robot->getTranslation();};
 
93
        osg::Vec3f getRobotRotation() const {return robot->getRotation();};
 
94
 
 
95
 
 
96
        //get robot state:
 
97
 
 
98
        ParameterReturnValue getSensorValue(dtCore::RefPtr<SensorBase> sensor);
 
99
        float getActuatorPosition(dtCore::RefPtr<ActuatorBase> actuator);
 
100
        float getActuatorVelocity(dtCore::RefPtr<ActuatorBase> actuator);
 
101
        //         float getActuatorAcceleration(dtCore::RefPtr<ActuatorBase> actuator);
 
102
 
 
103
        int getConnectorState(dtCore::RefPtr<ConnectorBase> connector);
 
104
 
 
105
 
 
106
        /**
 
107
         * Connect a robot to another robot. If there is no connector on a robot (except for this
 
108
         * very one) close enough, only WANTS_TO_CONNECT is set.
 
109
         */
 
110
        void connect(dtCore::RefPtr<ConnectorBase> connector);
 
111
 
 
112
        /**
 
113
         * Disconnect a robot from another robot.
 
114
         */
 
115
        void disconnect(dtCore::RefPtr<ConnectorBase> connector);
 
116
 
 
117
//      void setActuatorPosition(dtCore::RefPtr<ActuatorBase> actuator, float value);
 
118
 
 
119
        void setActuatorVelocity(dtCore::RefPtr<ActuatorBase> actuator, float value);
 
120
        //         void setActuatorAcceleration(ActuatorBase* actuator, float value);
 
121
 
 
122
        //! Returns sensor with the given id that is attached to the robot
 
123
        inline dtCore::RefPtr<SensorBase> getSensor(unsigned int id) const {
 
124
                if (id < robot->getSensors().size()) return robot->getSensors()[id];
 
125
                else {
 
126
                        std::ostringstream msg; msg.clear(); msg.str("");
 
127
                        msg << "Sensor with id " << id << " doesn't exist!";
 
128
                        LOG_WARNING(msg.str());
 
129
                        return NULL;
 
130
                }
 
131
        }
 
132
 
 
133
        //! Returns the amount of sensors
 
134
        inline size_t getSensorSize() const {return robot->getSensors().size();};
 
135
 
 
136
        //! Returns the amount of actuators (excluding the connectors)
 
137
        inline size_t getActuatorSize() const {return robot->getActuators().size();};
 
138
 
 
139
        //! Returns the amount of connectors
 
140
        inline size_t getConnectorSize() const {return robot->getConnectors().size();};
 
141
 
 
142
        //! Returns the amount of objects
 
143
        inline size_t getObjectSize() const {return robot->getObjects().size();};
 
144
 
 
145
        //! Returns sensor with the given name that is attached to the robot
 
146
        inline dtCore::RefPtr<SensorBase> getSensor(const std::string name) const {
 
147
                for (unsigned int i=0; i<robot->getSensors().size(); i++)
 
148
                        if (robot->getSensors()[i]->getName() == name) return robot->getSensors()[i];
 
149
 
 
150
                std::ostringstream msg; msg.clear(); msg.str("");
 
151
                msg << "Sensor with name " << name << " doesn't exist";
 
152
                LOG_WARNING(msg.str());
 
153
                return NULL;
 
154
        }
 
155
 
 
156
        //! Returns actuator with the given id that is attached to the robot
 
157
        inline dtCore::RefPtr<ActuatorBase> getActuator(unsigned int id) const {
 
158
                if (id < robot->getActuators().size()) return robot->getActuators()[id];
 
159
                else {
 
160
                        std::ostringstream msg; msg.clear(); msg.str("");
 
161
                        msg << "Actuator with id " << id << " doesn't exist!";
 
162
                        LOG_WARNING(msg.str());
 
163
                        return NULL;
 
164
                }
 
165
        }
 
166
 
 
167
        //! Returns actuator with the given name that is attached to the robot
 
168
        inline dtCore::RefPtr<ActuatorBase> getActuator(const std::string name) const {
 
169
 
 
170
                for (unsigned int i=0; i<robot->getActuators().size(); i++)
 
171
                        if (robot->getActuators()[i]->getName() == name) return robot->getActuators()[i];
 
172
 
 
173
                std::ostringstream msg; msg.clear(); msg.str("");
 
174
                msg << "Sensor with name " << name << " doesn't exist!";
 
175
                LOG_WARNING(msg.str());
 
176
                return NULL;
 
177
        }
 
178
 
 
179
        //! Returns actuator with the given id that is attached to the robot
 
180
        inline dtCore::RefPtr<ConnectorBase> getConnector(unsigned int id) const {
 
181
                if (id < robot->getConnectors().size()) return robot->getConnectors()[id];
 
182
                else {
 
183
                        std::ostringstream msg; msg.clear(); msg.str("");
 
184
                        msg << "Connector with id " << id << " doesn't exist!";
 
185
                        LOG_WARNING(msg.str());
 
186
                        return NULL;
 
187
                }
 
188
        }
 
189
 
 
190
        //! Returns actuator with the given name that is attached to the robot
 
191
        inline dtCore::RefPtr<ConnectorBase> getConnector(const std::string name) const {
 
192
 
 
193
                for (unsigned int i=0; i<robot->getConnectors().size(); i++)
 
194
                        if (robot->getConnectors()[i]->getName() == name) return robot->getConnectors()[i];
 
195
 
 
196
                std::ostringstream msg; msg.clear(); msg.str("");
 
197
                msg << "Connector with name " << name << " doesn't exist!";
 
198
                LOG_WARNING(msg.str());
 
199
                return NULL;
 
200
        }
 
201
 
 
202
        //! Returns object with the given id that is attached to the robot
 
203
        inline dtCore::RefPtr<ObjectBase> getObject(unsigned int id) const {
 
204
                if (id < robot->getObjects().size()) return robot->getObjects()[id];
 
205
                else {
 
206
                        std::ostringstream msg; msg.clear(); msg.str("");
 
207
                        msg << "Object with id " << id << " doesn't exist!";
 
208
                        LOG_WARNING(msg.str());
 
209
                        return NULL;
 
210
                }
 
211
        }
 
212
 
 
213
        //! Returns object with the given name that is attached to the robot
 
214
        inline dtCore::RefPtr<ObjectBase> getObject(const std::string name) const {
 
215
 
 
216
                for (unsigned int i=0; i<robot->getObjects().size(); i++)
 
217
                        if (robot->getObjects()[i]->getName() == name) return robot->getObjects()[i];
 
218
 
 
219
                std::ostringstream msg; msg.clear(); msg.str("");
 
220
                msg << "Object with name " << name << " doesn't exist!";
 
221
                LOG_WARNING(msg.str());
 
222
                return NULL;
 
223
        }
 
224
 
 
225
        inline float getDeltaSimTime() const {return deltaSimTime;};
 
226
 
 
227
 
 
228
        /**
 
229
         * Simple helper method to fire a game event.  This method creates the game event
 
230
         * message and sends it on to the Game Manager for processing.
 
231
         * @param event The game event to fire.
 
232
         */
 
233
        //         void sendMessageToAll(dtDAL::GameEvent &event);
 
234
        //         void sendMessageToRobot(dtDAL::GameEvent &event, unsigned int robotID);
 
235
 
 
236
        // message stuff:
 
237
        void sendGlobalMessage(const std::string& msg);
 
238
 
 
239
        /**
 
240
         * Sends a local message, local in the sense that it will only be directed towards the
 
241
         * robot that is attached at the current robot with the given connectorID
 
242
         * @param msg                   The message to be sent
 
243
         * @param connectorID   The identifier of the connector on this side
 
244
         */
 
245
        void sendLocalMessage(const std::string& msg, unsigned int connectorID);
 
246
 
 
247
        /// @see sendLocalMessage(msg, connectorID)
 
248
        void sendLocalMessage(const std::string& msg, const std::string& connectorName);
 
249
 
 
250
        /**
 
251
         * Sends a "kind of" local message, namely to all the robot modules that are somehow
 
252
         * attached to the organism.
 
253
         */
 
254
        void sendOrganismMessage(const std::string& msg);
 
255
 
 
256
        bool receivedNewMsg() const {return !lastMessages.empty();};
 
257
        std::vector<SimMessage> getLastMessages() const {return lastMessages;};
 
258
        inline void clearMsgList() { lastMessages.clear(); };
 
259
 
 
260
//      //! Destructor
 
261
        ~Agent();
 
262
 
 
263
        void OnRemovedFromGM();
 
264
 
 
265
 
 
266
        void setRobotTranslation(const osg::Vec3& pos);
 
267
        void setRobotRotation(const osg::Vec3& rot);
 
268
 
 
269
        inline dtCore::UniqueId* getBusID() const {return robot->getBusID();}
 
270
 
 
271
 
 
272
        std::map<std::string, std::string> paramMap;
 
273
 
 
274
private:
 
275
 
 
276
        dtCore::RefPtr<RobotActorBase> robot;
 
277
 
 
278
        void sendMessage(const std::string& msg, SimulationMessageType::commType type, dtCore::UniqueId* commID);
 
279
 
 
280
        float deltaSimTime;
 
281
 
 
282
        std::vector<SimMessage> lastMessages;
 
283
        //bool newMsgReceived;
 
284
 
 
285
 
 
286
        bool manuallySetAngularVel;
 
287
 
 
288
 
 
289
};
 
290
}
 
291
 
 
292
#endif
 
293