SMORES Robot Platform Simulation
Modlab at Penn, ASL at Cornell
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
WorldServer.hh
1 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // Author: Edward Yunkai Cui
3 // Description: This is a world plugin template for this simulation that
4 // provides an interface to serve as a middle ware. The main
5 // functions of this plugin are managing magnetic connections,
6 // loading configurations, interpreting gait commands, managing
7 // and routing communications, providing APIs to manage the
8 // shared libraries, managing module configurations, etc.
9 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 #ifndef _GAZEBO_WORLD_SERVER_HH_
11 #define _GAZEBO_WORLD_SERVER_HH_
12 #ifndef _GAZEBO_CUTOMIZED_WORLD_PLUGIN
13 #define _GAZEBO_CUTOMIZED_WORLD_PLUGIN WorldServer
14 #endif
15 #include <stdlib.h>
16 
17 #include <string>
18 #include <iostream>
19 #include <cmath>
20 #include <algorithm>
21 #include <fstream>
22 #include <sstream>
23 #include <iterator>
24 #include <boost/bind.hpp>
25 #include <queue>
26 #include <unistd.h>
27 
28 #include <sdf/sdf.hh>
29 #include "gazebo/gazebo.hh"
30 #include "gazebo/common/common.hh"
31 #include "gazebo/transport/transport.hh"
32 #include "gazebo/physics/physics.hh"
33 #include "gazebo/msgs/msgs.hh"
34 // Libraries for messages needed to use to communicate between plugins
35 #include "collision_message.pb.h"
36 #include "command_message.pb.h"
37 #include "world_status_message.pb.h"
38 // XML paser libraries
39 #include "rapidxml.hpp"
40 #include "rapidxml_utils.hpp"
41 // Libraries for connectivity representation
42 #include "SmoresModule.hh"
43 // Other useful classes for storing information
44 #include "CollisionInformation.hh"
45 #include "Condition.hh"
46 // Library for colored log text
47 #include "ColorLog.hh"
48 // Libraries for configuration motion feedback control
49 #include "LibraryTemplate.hh"
50 
51 #define PI 3.141593 // 3.1411593
52 #define VALIDCONNECTIONDISUPPER 0.110
53 #define VALIDCONNECTIONDISLOWER 0.098
54 // TODO: tests needed to make sure it depende on current execution folder
55 #define MODULEPATH "SMORE.sdf"
56 // #define INTIALCONFIGURATION "InitialConfiguration"
57 
58 using std::string;
59 using std::vector;
60 
61 typedef const boost::shared_ptr
62  <const collision_message::msgs::CollisionMessage> CollisionMessagePtr;
63 typedef const boost::shared_ptr
64  <const command_message::msgs::CommandMessage> CommandMessagePtr;
65 typedef boost::shared_ptr<gazebo::Condition> ConditionPtr;
66 typedef const boost::shared_ptr
67  <const command_message::msgs::WorldStatusMessage> WorldStatusMessagePtr;
68 
69 // TODO: Considering
70 namespace {
71 inline string Int2String(int number)
72 {
73  stringstream ss; //create a stringstream
74  ss << number; //add number to the stream
75  return ss.str(); //return a string with the contents of the stream
76 } // Int2String
77 } // namespace
78 namespace gazebo
79 {
80 class WorldServer : public WorldPlugin
81 {
82  public:
83  WorldServer();
84  ~WorldServer();
86  virtual void ExtraInitializationInLoad(physics::WorldPtr _parent,
87  sdf::ElementPtr _sdf);
89  LibraryTemplate *DynamicallyLoadedLibrary(const char* library_path,
90  void *lib_handle);
92  void CloseLoadedLibrary(void **lib_handle);
93  virtual void OnSystemRunningExtra(const common::UpdateInfo & _info);
95 
98  virtual void InsertModel(string name, math::Pose position, string joint_angles);
100 
103  virtual void InsertModel(string name, math::Pose position, string joint_angles,
104  string model_path);
106  void AddInitialPosition(math::Pose position);
108  void AddInitialJoints(string joint_angles);
110 
113  void DeleteSmoresmodulePtr(string module_name);
115 
118  void DeleteModule(string module_name);
120  void DeleteAllModules(void);
122 
126  math::Vector3 GetCurrentConfigurationPose(void);
128  void BuildConfigurationFromXML(string file_name);
130  void BuildConfigurationFromXML(string file_name, math::Vector3 initial_pose);
132  void BuildConnectionFromXML(string file_name);
134  virtual void ExtraWorkWhenModelInserted(CommandMessagePtr &msg);
136  double ConversionForAngleOverPi(double angle);
140  void EnableAutoMagneticConnection(void);
141  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
142  // These functions are used to connect or disconnect modules
143  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
145  void PassiveConnect(SmoresModulePtr module_1, SmoresModulePtr module_2,
146  int node1_ID, int node2_ID, double node_angle, double node_distance);
148  void PassiveConnect(SmoresModulePtr module_1, SmoresModulePtr module_2,
149  int node1_ID, int node2_ID);
151  void ActiveConnect(SmoresModulePtr module_1, SmoresModulePtr module_2,
152  int node1_ID, int node2_ID, double node_angle, double node_distance);
154  void ActiveConnect(SmoresModulePtr module_1, SmoresModulePtr module_2,
155  int node1_ID, int node2_ID);
158  void Disconnect(SmoresEdgePtr aEdge);
160  void Disconnect(SmoresModulePtr aModule, int node_ID);
162  void Disconnect(string moduleName, int node_ID);
164  void Disconnect(string moduleName1, string moduleName2);
165 
166  // Theses functions are used to send 'gait table'
167  // TODO: Think about how to combine these functions
169 
180  void SendGaitTable(SmoresModulePtr module, const int *flag,
181  const double *gait_value, int msg_type, unsigned int time_stamp,
182  string condition_str, string dependency_str);
184 
193  void SendGaitTable(SmoresModulePtr module, const int *flag,
194  const double *gait_value, int msg_type, unsigned int time_stamp);
196 
206  void SendGaitTable(SmoresModulePtr module, const int *flag,
207  const double *gait_value, int msg_type,
208  string condition_str, string dependency_str);
210 
218  void SendGaitTable(SmoresModulePtr module, const int *flag,
219  const double *gait_value, int msg_type);
221 
231  void SendGaitTable(SmoresModulePtr module, int joint_ID,
232  double gait_value, int msg_type, unsigned int time_stamp,
233  string condition_str, string dependency_str);
235 
243  void SendGaitTable(SmoresModulePtr module, int joint_ID,
244  double gait_value, int msg_type, unsigned int time_stamp);
246 
255  void SendGaitTable(SmoresModulePtr module, int joint_ID,
256  double gait_value, int msg_type,
257  string condition_str, string dependency_str);
259 
266  void SendGaitTable(SmoresModulePtr module, int joint_ID,
267  double gait_value, int msg_type);
270 
281  void SendGaitTable(SmoresModulePtr module, string module1, string module2,
282  int node1, int node2, int commandtype, unsigned int time_stamp,
283  string condition_str, string dependency_str);
286 
295  void SendGaitTable(SmoresModulePtr module, string module1, string module2,
296  int node1, int node2, int commandtype, unsigned int time_stamp);
299 
309  void SendGaitTable(SmoresModulePtr module, string module1, string module2,
310  int node1, int node2, int commandtype,
311  string condition_str, string dependency_str);
314 
322  void SendGaitTable(SmoresModulePtr module, string module1, string module2,
323  int node1, int node2, int commandtype);
325 
333  void SendGaitTableInstance(SmoresModulePtr module, const int *flag,
334  const double *gait_value, int msg_type);
336 
343  void SendGaitTableInstance(SmoresModulePtr module, const int *flag,
344  const double *gait_value);
346 
352  void SendPositionInstance(SmoresModulePtr module, double x, double y,
353  double orientation_angle);
356  void EraseComaands(SmoresModulePtr module);
359  void DynamicJointDestroy(SmoresEdgePtr aEdge);
361  void ReBuildDynamicJoint(SmoresEdgePtr a_edge);
362 
363  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
364  // These functions are utility functions
365  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
366  int GetNodeIDByName(string node_name);
368  bool CheckModuleExistByName(string module_name);
370 
374  SmoresModulePtr GetModulePtrByName(string module_name);
376  SmoresModulePtr GetModulePtrByIDX(unsigned int idx);
378  unsigned int GetModuleListSize(void);
379  void EraseCommandPtrByModule(SmoresModulePtr module_ptr);
380  int GetModuleIndexByName(string module_name);
382  bool AlreadyConnected(SmoresModulePtr module_1, SmoresModulePtr module_2,
383  int node1_ID, int node2_ID);
385  bool AlreadyConnected(SmoresModulePtr module_1, SmoresModulePtr module_2);
387  bool AlreadyConnected(SmoresModulePtr module, int node_ID);
389  void ReadFileAndGenerateCommands(const char* fileName);
391 
396  void FigureInterpret(const vector<string> *joints_spec, int *type_flags,
397  double *joint_values);
399 
402  void InterpretCommonGaitString(string a_command_str);
404 
407  void InterpretSpecialString(string a_command_str);
409 
412  unsigned int CountModules(SmoresModulePtr module);
414  unsigned int GetInitialJointSequenceSize(void);
416  unsigned int GetEdgeCounts(void);
418  SmoresEdgePtr GetEdgePtrByIDX(unsigned int idx);
419 
420  private:
421  void Load(physics::WorldPtr _parent, sdf::ElementPtr _sdf);
423  void AddEntityToWorld(std::string & _info);
425  void OnSystemRunning(const common::UpdateInfo & /*_info*/);
427  void FeedBackMessageDecoding(CommandMessagePtr &msg);
429  void WorldStatusMessageDecoding(WorldStatusMessagePtr &msg);
433  void AutomaticMagneticConnectionManagement(CollisionMessagePtr &msg);
436  void ConnectAndDynamicJointGeneration(SmoresModulePtr module_1,
437  SmoresModulePtr module_2, int node1_ID, int node2_ID,
438  SmoresEdgePtr an_edge);
441  void RotationQuaternionCalculation(math::Vector3 normal_axis,
442  math::Vector3 z_axis_of_link1, math::Vector3 z_axis_of_link2,
443  math::Vector3 first_rotation, math::Vector3 second_rotation,
444  math::Quaternion *first_rotation_of_link2,
445  math::Quaternion *second_rotation_of_link2);
447  void NewPositionCalculation(SmoresEdgePtr an_edge,
448  math::Pose old_pose_of_module1, math::Pose old_pose_of_module2,
449  int node1_ID, int node2_ID,
450  math::Pose *new_pose_of_module1, math::Pose *new_pose_of_module2);
452  void CommandManager(void);
453  void CommandExecution(ModuleCommandsPtr current_command_container);
455  void AddCondition(string conditionid);
456  void FinishOneConditionCommand(string conditionid);
457  bool CheckCondition(string conditionid);
459  int StripOffTimerInCommandString(string &command_string);
461  string StripOffCondition(string &command_string);
463  string StripOffDependency(string &command_string);
464 
465  public:
466  physics::WorldPtr currentWorld;
467  private:
468  event::ConnectionPtr addEntityConnection;
469  transport::PublisherPtr welcomePub;
470 
471  transport::PublisherPtr smoreWorldPub;
472  transport::SubscriberPtr smoreWorldSub;
474  vector<SmoresModulePtr> moduleList;
476  vector<CollisionInformation> pendingRequest;
478  vector<physics::JointPtr> dynamicConnections;
480  event::ConnectionPtr updateConnection;
482  vector<SmoresEdgePtr> connectionEdges;
484  vector<string> waitingNameList;
486  vector<string> initalJointValue;
488  vector<math::Pose> initialPosition;
490  vector<ConditionPtr> commandConditions;
491  vector<ModuleCommandsPtr> moduleCommandContainer;
492 // private: vector<transport::PublisherPtr> WorldPublisher;
494  vector<transport::SubscriberPtr> WorldColSubscriber;
496  bool autoMagneticConnectionFlag;
498  string configurationFile;
499 //+++++++++ testing ++++++++++++++++++++++++++++
500 };
501 } // namespace gazebo
502 #endif
LibraryTemplate * DynamicallyLoadedLibrary(const char *library_path, void *lib_handle)
Load a shared library in linux.
Definition: WorldServer.cc:67
boost::shared_ptr< ModuleCommands > ModuleCommandsPtr
Smart pointer to the ModuleCommands object.
Definition: SmoresModule.hh:23
This file contains rapidxml parser and DOM implementation.
Definition: LibraryTemplate.hh:13
void DeleteModule(string module_name)
Delete a model that already in the world.
Definition: WorldServer.cc:643
gazebo name space
Definition: system_gui.cc:41
void DeleteSmoresmodulePtr(string module_name)
Delete the smoresmodule object in the world.
Definition: WorldServer.cc:600
void SendPositionInstance(SmoresModulePtr module, double x, double y, double orientation_angle)
Used in direct control, dirve a module to a specific position.
Definition: WorldServer.cc:1075
void InterpretSpecialString(string a_command_str)
Interpret special command.
Definition: WorldServer.cc:1294
virtual void InsertModel(string name, math::Pose position, string joint_angles)
Insert a model to the current world.
Definition: WorldServer.cc:542
void BuildConnectionFromXML(string file_name)
This function is used to build connection using a XML file.
Definition: WorldServer.cc:201
unsigned int GetModuleListSize(void)
Get the count of the modules that are in the list.
Definition: WorldServer.cc:1145
Definition: WorldServer.hh:80
void EnableAutoMagneticConnection(void)
Definition: WorldServer.cc:88
void SendGaitTable(SmoresModulePtr module, const int *flag, const double *gait_value, int msg_type, unsigned int time_stamp, string condition_str, string dependency_str)
Send time based common gait with condition and dependency.
Definition: WorldServer.cc:880
bool AlreadyConnected(SmoresModulePtr module_1, SmoresModulePtr module_2, int node1_ID, int node2_ID)
Check whether two nodes are connected together.
Definition: WorldServer.cc:1169
SmoresModulePtr GetModulePtrByName(string module_name)
Get SmoresModule object by specifying the name.
Definition: WorldServer.cc:1126
void ReBuildDynamicJoint(SmoresEdgePtr a_edge)
Rebuild the dynamic joint using the information in the edge object.
Definition: WorldServer.cc:1464
void DeleteAllModules(void)
Delete all models that already in the world.
Definition: WorldServer.cc:657
virtual void ExtraInitializationInLoad(physics::WorldPtr _parent, sdf::ElementPtr _sdf)
This function will be called in Load() to perform extra initialization.
Definition: WorldServer.cc:64
void Disconnect(SmoresEdgePtr aEdge)
Definition: WorldServer.cc:748
SmoresEdgePtr GetEdgePtrByIDX(unsigned int idx)
Get SmoresEdge object by specifying the index in the vector connectionEdges.
Definition: WorldServer.cc:1456
void PassiveConnect(SmoresModulePtr module_1, SmoresModulePtr module_2, int node1_ID, int node2_ID, double node_angle, double node_distance)
Connect two modules by pointers and node_ID.
Definition: WorldServer.cc:705
unsigned int CountModules(SmoresModulePtr module)
Count how many modules are there in a cluster with the current module.
Definition: WorldServer.cc:1207
string Int2String(int number)
Converts int to string.
Definition: util.hh:25
bool CheckModuleExistByName(string module_name)
Get return true if the smores module with given name exist in the world.
Definition: WorldServer.cc:1117
boost::shared_ptr< SmoresModule > SmoresModulePtr
Smart pointer of the SmoresModule object.
Definition: CommandManagement.hh:19
void SendGaitTableInstance(SmoresModulePtr module, const int *flag, const double *gait_value, int msg_type)
Used in the direct driving situation, ignore execution order.
Definition: WorldServer.cc:1059
void CloseLoadedLibrary(void **lib_handle)
Close the loaded libraries.
Definition: WorldServer.cc:84
void AddInitialPosition(math::Pose position)
Add a new position to set at the end of 'intialPosition' vector.
Definition: WorldServer.cc:592
virtual void ExtraWorkWhenModelInserted(CommandMessagePtr &msg)
This function will be called after set the model initial position.
Definition: WorldServer.cc:313
unsigned int GetEdgeCounts(void)
Get the total counts of the edges that stored in the program.
Definition: WorldServer.cc:1460
double ConversionForAngleOverPi(double angle)
Convert angles so that their absolutely value always smaller than Pi.
Definition: WorldServer.cc:522
SmoresModulePtr GetModulePtrByIDX(unsigned int idx)
Get SmoresModule object by specifying the index in the vector moduleList.
Definition: WorldServer.cc:1137
void EraseComaands(SmoresModulePtr module)
Definition: WorldServer.cc:1091
void FigureInterpret(const vector< string > *joints_spec, int *type_flags, double *joint_values)
Used to interpret the number in gait table.
Definition: WorldServer.cc:1355
void AddInitialJoints(string joint_angles)
Add new initial joint values to set at the end of 'initalJointValue' vector.
Definition: WorldServer.cc:596
math::Vector3 GetCurrentConfigurationPose(void)
Get the current position in world of the inserted configuration.
Definition: WorldServer.cc:682
void BuildConfigurationFromXML(string file_name)
Build a configuration at origin using a XML file.
Definition: WorldServer.cc:145
void ActiveConnect(SmoresModulePtr module_1, SmoresModulePtr module_2, int node1_ID, int node2_ID, double node_angle, double node_distance)
TODO: Need add 'active' feature to this function.
Definition: WorldServer.cc:726
void DynamicJointDestroy(SmoresEdgePtr aEdge)
Definition: WorldServer.cc:529
void InterpretCommonGaitString(string a_command_str)
Interpret normal command string.
Definition: WorldServer.cc:1263
void ReadFileAndGenerateCommands(const char *fileName)
Read a 'gait table' stored in a text file.
Definition: WorldServer.cc:1232
unsigned int GetInitialJointSequenceSize(void)
Get the length of the initial joint value setting sequence.
Definition: WorldServer.cc:1228