MIRA
SCITOSManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 MetraLabs GmbH (MLAB), GERMANY.
3  * All rights reserved.
4  * Contact: info@MetraLabs.com
5  *
6  * Commercial Usage:
7  * Licensees holding valid commercial licenses may use this file in
8  * accordance with the commercial license agreement provided with the
9  * software or, alternatively, in accordance with the terms contained in
10  * a written agreement between you and MetraLabs.
11  *
12  * GNU General Public License Usage:
13  * Alternatively, this file may be used under the terms of the GNU
14  * General Public License version 3.0 as published by the Free Software
15  * Foundation and appearing in the file LICENSE.GPL3 included in the
16  * packaging of this file. Please review the following information to
17  * ensure the GNU General Public License version 3.0 requirements will be
18  * met: http://www.gnu.org/copyleft/gpl.html.
19  * Alternatively you may (at your option) use any later version of the GNU
20  * General Public License if such license has been publicly approved by
21  * MetraLabs (or its successors, if any).
22  *
23  * IN NO EVENT SHALL "MLAB" BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
24  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
25  * OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLABS" HAS BEEN
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * "MLAB" SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
31  * "AS IS" BASIS, AND "MLAB" HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
32  * SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
33  */
34 
43 #ifndef _MIRA_SCITOSMANAGER_H_
44 #define _MIRA_SCITOSMANAGER_H_
45 
46 #include <utils/ToString.h>
47 #include <fw/Unit.h>
48 
50 
52 #include <can/CANOpenSDOClient.h>
53 
54 #include <SCITOSDefines.h>
55 #include <SCITOSModule.h>
56 
57 namespace mira { namespace robot {
58 
60 
65 {
67 public:
68 
71  {
73  ErrorDescription(const std::string& iText) :
74  category("HWError"), text(iText) {}
75  ErrorDescription(const std::string& iCategory, const std::string& iText) :
76  category(iCategory), text(iText) {}
77 
78  std::string category;
79  std::string text;
80  };
81 
82 protected:
83 
84  static std::string toHexString(uint32 id)
85  {
86  std::ostringstream ss;
87  ss << std::hex << std::showbase << id;
88  return ss.str();
89  }
90 
91  static uint32 fromPrefixedString(const std::string& s)
92  {
93  std::istringstream ss(s);
94  uint32 i;
95  ss >> std::setbase(0) >> i;
96  return i;
97  }
98 
99 public:
100 
101  struct ModuleID
102  {
103  template<typename Reflector>
104  void reflect(Reflector& r)
105  {
106  r.property("Node",
107  getter<std::string>([&](){return SCITOSManager::toHexString(node);}),
108  setter<std::string>([&](const std::string& s){node = fromPrefixedString(s);}),
109  "Node number (default = 0 = wildcard)", serialization::IgnoreMissing());
110 
111  r.property("VendorID",
112  getter<std::string>([&](){return SCITOSManager::toHexString(vendorID);}),
113  setter<std::string>([&](const std::string& s){vendorID = fromPrefixedString(s);}),
114  "VendorID (default = 0 = wildcard)", serialization::IgnoreMissing());
115 
116  r.property("ProductID",
117  getter<std::string>([&](){return SCITOSManager::toHexString(productID);}),
118  setter<std::string>([&](const std::string& s){productID = fromPrefixedString(s);}),
119  "ProductID (default = 0 = wildcard)", serialization::IgnoreMissing());
120  }
121 
122  bool match(uint8 n, uint32 vID, uint32 pID) const
123  {
124  return (((node == 0) || (node == n)) &&
125  ((vendorID == 0) || (vendorID == vID)) &&
126  ((productID == 0) || (productID == pID)));
127  }
128 
129  uint8 node = 0;
130  uint32 vendorID = 0;
131  uint32 productID = 0;
132  };
133 
135  mInitializationFinished(false) {}
136 
138  template<typename Reflector>
139  void reflect(Reflector& r)
140  {
141  r.property("DeadModuleTimeout", mRemoveNodeTimeout,
142  "Timeout for removing dead modules", Duration::seconds(10));
143  r.roproperty("IgnoreModules", mIgnoreModules,
144  "List of CAN modules to be ignored.\n"
145  "Each entry consists of Node+VendorID+ProductID. "
146  "A module is ignored if it matches ALL specified elements, default=0=wildcard.");
147  auto it = mModules.begin();
148  for(; it != mModules.end(); ++it)
149  r.property(it->second->getName().c_str(), it->second, "Module");
150  }
151 
153  void reflect(XMLDeserializer& r);
154 
157  {
158  return mCANInterface;
159  }
160 
163  {
164  return mSDOClient;
165  }
166 
169  {
170  return mNMTMaster;
171  }
172 
176  bool hasHWTime() const
177  {
178  return mHasHWTimeSync;
179  }
180 
186  uint32 getHWTime(Time time = Time::now())
187  {
188  Duration diff = time - mLastHWTime;
189  return mLastHWTimeValue + (uint32)diff.totalMilliseconds()/10;
190  }
191 
195  void addErrorDescription(uint32 errorNr, const ErrorDescription& ed)
196  {
197  boost::mutex::scoped_lock lock(mErrorMutex);
198  mErrorDescriptions[errorNr] = ed;
199  }
200 
207  {
208  boost::mutex::scoped_lock lock(mErrorMutex);
209  auto errorDesc = mErrorDescriptions.find(errorNr);
210  // if this type of error is known set correct text and category
211  if (errorDesc != mErrorDescriptions.end())
212  return errorDesc->second;
213  std::string category("Unknown error");
214  std::string text = MakeString() << "Error ID " << std::hex << errorNr;
215  return ErrorDescription(category, text);
216  }
217 
222  Time correctPCTime(Time pcTime, uint32 hwTime)
223  {
224  // obtain the difference of the given hardware time and the hardware
225  // time that corresponds with the given PC time
226  uint32 diff = hwTime-getHWTime(pcTime);
227  int64 signedDiff = *reinterpret_cast<int32*>(&diff);
228  // return the PC time corrected by the hardware time difference
229  return pcTime + Duration::milliseconds(signedDiff*10);
230  }
231 
236  Time correctPCTime(Time pcTime, uint16 hwTime)
237  {
238  // obtain the difference of the given hardware time and the hardware
239  // time that corresponds with the given PC time
240  uint16 diff = hwTime-getHWTime(pcTime);
241  int32 signedDiff = *reinterpret_cast<int16*>(&diff);
242  // return the PC time corrected by the hardware time difference
243  return pcTime + Duration::milliseconds(signedDiff*10);
244  }
245 
247  boost::shared_ptr<robot::RobotModel> getRobotModel()
248  {
249  return mRobotModelProvider->getRobotModel();
250  }
251 
252  void updateProperties();
253 
254 protected:
257  virtual void initialize();
258  virtual void finalize();
259 
260  virtual void resume();
261  virtual void pause();
263 
264 private:
269  void onHeartbeat(uint8 node, mira::can::CANOpenNodeStatus status);
270 
275  void onPDO(const mira::can::CANMessage& message, const Time& time);
276 
277 private:
278 
279  boost::mutex mErrorMutex;
280  boost::mutex mPropertyMutex;
281  mira::can::CANChannelInterfacePtr mCANInterface;
284  std::map<uint8, SCITOSModulePtr> mModules;
285  std::map<uint8, std::pair<mira::can::CANOpenNodeInfo, Time>> mNodeInfos;
286  Duration mRemoveNodeTimeout;
287  std::vector<ModuleID> mIgnoreModules; // to be ignored (configured)
288  std::set<uint8> mIgnoredNodes; // already ignored (runtime)
289  std::string mRobotType;
290  uint32 mLastHWTimeValue;
291  Time mLastHWTime;
292  bool mHasHWTimeSync;
293  bool mInitializationFinished;
294  XMLDom mState;
295  std::map<uint32, ErrorDescription> mErrorDescriptions;
296  boost::shared_ptr<robot::RobotModelProvider> mRobotModelProvider;
297 };
298 
300 
301 }}
302 
303 #endif
void reflect(Reflector &r)
Reflect method for serialization.
Definition: SCITOSManager.h:139
tick_type milliseconds() const
SCITOSManager()
Definition: SCITOSManager.h:134
ErrorDescription(const std::string &iCategory, const std::string &iText)
Definition: SCITOSManager.h:75
ErrorDescription getErrorDescription(uint32 errorNr)
Returns the error description of the given error number.
Definition: SCITOSManager.h:206
Time correctPCTime(Time pcTime, uint16 hwTime)
Correct the PC time relative to given hardware time.
Definition: SCITOSManager.h:236
boost::shared_ptr< robot::RobotModel > getRobotModel()
Returns the SCITOS robot model.
Definition: SCITOSManager.h:247
std::string text
Definition: SCITOSManager.h:79
void addErrorDescription(uint32 errorNr, const ErrorDescription &ed)
Adds an error description for the given error number.
Definition: SCITOSManager.h:195
std::string category
Definition: SCITOSManager.h:78
boost::shared_ptr< CANOpenNMTMaster > CANOpenNMTMasterPtr
bool hasHWTime() const
Returns true if a hardware timestamp exists.
Definition: SCITOSManager.h:176
mira::can::CANOpenNMTMasterPtr getNMTMaster()
Returns the can NMT master pointer.
Definition: SCITOSManager.h:168
void reflect(Reflector &r)
Definition: SCITOSManager.h:104
boost::shared_ptr< CANOpenSDOClient > CANOpenSDOClientPtr
tick_type totalMilliseconds() const
bool match(uint8 n, uint32 vID, uint32 pID) const
Definition: SCITOSManager.h:122
sec_type seconds() const
static std::string toHexString(uint32 id)
Definition: SCITOSManager.h:84
ErrorDescription(const std::string &iText)
Definition: SCITOSManager.h:73
void reflect(Reflector &r, LogRecord &record)
#define MLAB_SCITOS_EXPORT
Definition: SCITOSDefines.h:57
#define MIRA_OBJECT(classIdentifier)
Base class for a SCITOS driver module.
boost::shared_ptr< CANChannelInterface > CANChannelInterfacePtr
ErrorDescription()
Definition: SCITOSManager.h:72
static uint32 fromPrefixedString(const std::string &s)
Definition: SCITOSManager.h:91
Defines for the SCITOS robot drivers.
mira::can::CANChannelInterfacePtr getCANInterface()
Returns the can interface pointer.
Definition: SCITOSManager.h:156
mira::can::CANOpenSDOClientPtr getCANSDOClient()
Returns the can sdo client pointer.
Definition: SCITOSManager.h:162
static Time now() static Time eternity()
The SCITOSManager manages all driver modules of a SCITOS robot.
Definition: SCITOSManager.h:64
Time correctPCTime(Time pcTime, uint32 hwTime)
Correct the PC time relative to given hardware time.
Definition: SCITOSManager.h:222
A description of a hardware error. Used to map error ids to text.
Definition: SCITOSManager.h:70
uint32 getHWTime(Time time=Time::now())
Return the hardware time that corresponds with the given PC time.
Definition: SCITOSManager.h:186
Definition: SCITOSManager.h:101