2
* Delta3D Open Source Game and Simulation Engine
3
* Copyright (C) 2005, BMH Associates, Inc.
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)
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
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
19
* @author Curtiss Murphy
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>
35
#include <srCore/robot/robotActorBase.h>
36
#include <srCore/export.h>
37
#include <srCore/simulationEvents.h>
38
#include <srCore/simulationUtils.h>
40
using namespace srCore;
42
namespace srInterface {
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.
50
class ROBOT_EXPORT Agent : public dtGame::GMComponent {
52
friend class SimulationArenaComponent;
55
// Inner class for simulation messages
56
// Todo: Check if this is still needed
61
SimulationMessageType::commType type;
67
//! Constructor for a Controller that controls one specific robot
68
Agent(const std::string &name, RobotActorBase* robotActor);
70
//! colors the collisionMesh of the Robot. This function will not be available on the real robot!
71
void __colorRobot(osg::Vec4 color);
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
76
void move2D(osg::Vec2 velocity);
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.
81
void rotate2D(float angular_v);
83
//! returns the collision ID
84
int getRobotID() {return robot->getID(); };
86
virtual void ProcessMessage(const dtGame::Message &message);
88
inline void setParamMap(std::map<std::string, std::string> paramMap) { this->paramMap = paramMap; } ;
92
osg::Vec3f getRobotPosition() const {return robot->getTranslation();};
93
osg::Vec3f getRobotRotation() const {return robot->getRotation();};
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);
103
int getConnectorState(dtCore::RefPtr<ConnectorBase> connector);
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.
110
void connect(dtCore::RefPtr<ConnectorBase> connector);
113
* Disconnect a robot from another robot.
115
void disconnect(dtCore::RefPtr<ConnectorBase> connector);
117
// void setActuatorPosition(dtCore::RefPtr<ActuatorBase> actuator, float value);
119
void setActuatorVelocity(dtCore::RefPtr<ActuatorBase> actuator, float value);
120
// void setActuatorAcceleration(ActuatorBase* actuator, float value);
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];
126
std::ostringstream msg; msg.clear(); msg.str("");
127
msg << "Sensor with id " << id << " doesn't exist!";
128
LOG_WARNING(msg.str());
133
//! Returns the amount of sensors
134
inline size_t getSensorSize() const {return robot->getSensors().size();};
136
//! Returns the amount of actuators (excluding the connectors)
137
inline size_t getActuatorSize() const {return robot->getActuators().size();};
139
//! Returns the amount of connectors
140
inline size_t getConnectorSize() const {return robot->getConnectors().size();};
142
//! Returns the amount of objects
143
inline size_t getObjectSize() const {return robot->getObjects().size();};
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];
150
std::ostringstream msg; msg.clear(); msg.str("");
151
msg << "Sensor with name " << name << " doesn't exist";
152
LOG_WARNING(msg.str());
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];
160
std::ostringstream msg; msg.clear(); msg.str("");
161
msg << "Actuator with id " << id << " doesn't exist!";
162
LOG_WARNING(msg.str());
167
//! Returns actuator with the given name that is attached to the robot
168
inline dtCore::RefPtr<ActuatorBase> getActuator(const std::string name) const {
170
for (unsigned int i=0; i<robot->getActuators().size(); i++)
171
if (robot->getActuators()[i]->getName() == name) return robot->getActuators()[i];
173
std::ostringstream msg; msg.clear(); msg.str("");
174
msg << "Sensor with name " << name << " doesn't exist!";
175
LOG_WARNING(msg.str());
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];
183
std::ostringstream msg; msg.clear(); msg.str("");
184
msg << "Connector with id " << id << " doesn't exist!";
185
LOG_WARNING(msg.str());
190
//! Returns actuator with the given name that is attached to the robot
191
inline dtCore::RefPtr<ConnectorBase> getConnector(const std::string name) const {
193
for (unsigned int i=0; i<robot->getConnectors().size(); i++)
194
if (robot->getConnectors()[i]->getName() == name) return robot->getConnectors()[i];
196
std::ostringstream msg; msg.clear(); msg.str("");
197
msg << "Connector with name " << name << " doesn't exist!";
198
LOG_WARNING(msg.str());
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];
206
std::ostringstream msg; msg.clear(); msg.str("");
207
msg << "Object with id " << id << " doesn't exist!";
208
LOG_WARNING(msg.str());
213
//! Returns object with the given name that is attached to the robot
214
inline dtCore::RefPtr<ObjectBase> getObject(const std::string name) const {
216
for (unsigned int i=0; i<robot->getObjects().size(); i++)
217
if (robot->getObjects()[i]->getName() == name) return robot->getObjects()[i];
219
std::ostringstream msg; msg.clear(); msg.str("");
220
msg << "Object with name " << name << " doesn't exist!";
221
LOG_WARNING(msg.str());
225
inline float getDeltaSimTime() const {return deltaSimTime;};
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.
233
// void sendMessageToAll(dtDAL::GameEvent &event);
234
// void sendMessageToRobot(dtDAL::GameEvent &event, unsigned int robotID);
237
void sendGlobalMessage(const std::string& msg);
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
245
void sendLocalMessage(const std::string& msg, unsigned int connectorID);
247
/// @see sendLocalMessage(msg, connectorID)
248
void sendLocalMessage(const std::string& msg, const std::string& connectorName);
251
* Sends a "kind of" local message, namely to all the robot modules that are somehow
252
* attached to the organism.
254
void sendOrganismMessage(const std::string& msg);
256
bool receivedNewMsg() const {return !lastMessages.empty();};
257
std::vector<SimMessage> getLastMessages() const {return lastMessages;};
258
inline void clearMsgList() { lastMessages.clear(); };
263
void OnRemovedFromGM();
266
void setRobotTranslation(const osg::Vec3& pos);
267
void setRobotRotation(const osg::Vec3& rot);
269
inline dtCore::UniqueId* getBusID() const {return robot->getBusID();}
272
std::map<std::string, std::string> paramMap;
276
dtCore::RefPtr<RobotActorBase> robot;
278
void sendMessage(const std::string& msg, SimulationMessageType::commType type, dtCore::UniqueId* commID);
282
std::vector<SimMessage> lastMessages;
283
//bool newMsgReceived;
286
bool manuallySetAngularVel;