00001 #ifndef __ASEBA_ROS_H
00002 #define __ASEBA_ROS_H
00003
00004 #include <dashel/dashel.h>
00005 #include <boost/thread.hpp>
00006 #include <boost/thread/mutex.hpp>
00007
00008 #include <aseba/msg/msg.h>
00009 #include <aseba/msg/descriptions-manager.h>
00010
00011 #include "ros/ros.h"
00012 #include "std_msgs/String.h"
00013
00014 #include "asebaros/LoadScripts.h"
00015 #include "asebaros/GetNodeList.h"
00016 #include "asebaros/GetNodeId.h"
00017 #include "asebaros/GetNodeName.h"
00018 #include "asebaros/GetVariableList.h"
00019 #include "asebaros/SetVariable.h"
00020 #include "asebaros/GetVariable.h"
00021 #include "asebaros/GetEventId.h"
00022 #include "asebaros/GetEventName.h"
00023
00024 #include "asebaros/AsebaEvent.h"
00025 #include "asebaros/AsebaAnonymousEvent.h"
00026
00027 #include <vector>
00028
00029
00030 class AsebaROS;
00031
00032 class AsebaDashelHub: public Dashel::Hub
00033 {
00034 private:
00035 boost::thread* thread;
00036 AsebaROS* asebaROS;
00037 bool forward;
00038
00039 public:
00044 AsebaDashelHub(AsebaROS* asebaROS, unsigned port, bool forward);
00045
00051 void sendMessage(Aseba::Message *message, bool doLock, Dashel::Stream* sourceStream = 0);
00052
00054 void operator()();
00056 void startThread();
00058 void stopThread();
00059
00060 protected:
00061 virtual void connectionCreated(Dashel::Stream *stream);
00062 virtual void incomingData(Dashel::Stream *stream);
00063 virtual void connectionClosed(Dashel::Stream *stream, bool abnormal);
00064 };
00065
00066 using namespace asebaros;
00067
00068 typedef std::vector<ros::ServiceServer> ServiceServers;
00069 typedef std::vector<ros::Publisher> Publishers;
00070 typedef std::vector<ros::Subscriber> Subscribers;
00071
00072 class AsebaROS: public Aseba::DescriptionsManager
00073 {
00074 protected:
00075 typedef std::map<std::string, unsigned> NodesNamesMap;
00076 typedef std::map<std::string, Aseba::Compiler::VariablesMap> UserDefinedVariablesMap;
00077 class GetVariableQueryKey
00078 {
00079 public:
00080 GetVariableQueryKey(unsigned nodeId, unsigned pos) : nodeId(nodeId), pos(pos) { }
00081 bool operator<(const GetVariableQueryKey &that) const {
00082 return (nodeId < that.nodeId && pos < that.pos);
00083 }
00084 unsigned nodeId;
00085 unsigned pos;
00086
00087 };
00088 struct GetVariableQueryValue
00089 {
00090 typedef std::vector<sint16> DataVector;
00091 DataVector data;
00092 boost::condition_variable cond;
00093 };
00094 typedef std::map<GetVariableQueryKey, GetVariableQueryValue*> GetVariableQueryMap;
00095
00096 ros::NodeHandle n;
00097 ServiceServers s;
00098
00099 ros::Publisher anonPub;
00100 ros::Subscriber anonSub;
00101 Publishers pubs;
00102 Subscribers subs;
00103
00104 AsebaDashelHub hub;
00105 boost::mutex mutex;
00106
00107 Aseba::CommonDefinitions commonDefinitions;
00108 NodesNamesMap nodesNames;
00109 UserDefinedVariablesMap userDefinedVariablesMap;
00110 GetVariableQueryMap getVariableQueries;
00111
00112 protected:
00113 bool loadScript(LoadScripts::Request& req, LoadScripts::Response& res);
00114
00115 bool getNodeList(GetNodeList::Request& req, GetNodeList::Response& res);
00116 bool getNodeId(GetNodeId::Request& req, GetNodeId::Response& res);
00117 bool getNodeName(GetNodeName::Request& req, GetNodeName::Response& res);
00118
00119 bool getVariableList(GetVariableList::Request& req, GetVariableList::Response& res);
00120 bool setVariable(SetVariable::Request& req, SetVariable::Response& res);
00121 bool getVariable(GetVariable::Request& req, GetVariable::Response& res);
00122
00123 bool getEventId(GetEventId::Request& req, GetEventId::Response& res);
00124 bool getEventName(GetEventName::Request& req, GetEventName::Response& res);
00125
00126
00127 bool getNodePosFromNames(const std::string& nodeName, const std::string& variableName, unsigned& nodeId, unsigned& pos) const;
00128 void sendEventOnROS(const Aseba::UserMessage* asebaMessage);
00129
00130
00131 void nodeDescriptionReceived(unsigned nodeId);
00132 void eventReceived(const AsebaAnonymousEventConstPtr& event);
00133 void knownEventReceived(const uint16 id, const AsebaEventConstPtr& event);
00134
00135 public:
00136 AsebaROS(unsigned port, bool forward);
00137 ~AsebaROS();
00138
00139 void run();
00140
00141 void processAsebaMessage(Aseba::Message *message);
00142
00143 void connectTarget(const std::string& target) { hub.connect(target); }
00144 };
00145
00146
00147
00148 #endif // __ASEBA_ROS_H