segwayrmp.h
Go to the documentation of this file.
1 
39 #ifndef SEGWAYRMP_H
40 #define SEGWAYRMP_H
41 
42 #include <exception>
43 #include <sstream>
44 #include <queue>
45 #include <typeinfo>
46 
47 #include <boost/function.hpp>
48 #include <boost/thread.hpp>
49 
50 /* Setup (u)int*_t types if not UNIX. */
51 #if defined(_WIN32) && !defined(__MINGW32__)
52  typedef unsigned int uint32_t;
53 #else
54 # include <stdint.h>
55 #endif
56 
57 // ClassName - The name of the exception class
58 // Prefix - The common prefix
59 // Message - The default message, if one is not specified at Throw
60 // The message takes the form of:
61 // ClassName occurred at line # of file `<file>`: Prefix+Message
62 #define DEFINE_EXCEPTION(ClassName, Prefix, Message) \
63  class ClassName : public std::exception { \
64  void operator=(const ClassName &); \
65  const ClassName & operator=( ClassName ); \
66  const char * what_; \
67  const int id_; \
68  public: \
69  ClassName(const char * file, const int ln, \
70  const char * msg = Message, const int id = -1) : id_(id) { \
71  std::stringstream ss; \
72  ss << #ClassName " occurred at line " << ln \
73  << " of `" << file << "`: " << Prefix << msg; \
74  what_ = ss.str().c_str(); \
75  } \
76  virtual const char* what () const throw () { return what_; } \
77  const int error_number() { return this->id_; } \
78  };
79 
80 #define RMP_THROW(ExceptionClass) \
81  throw ExceptionClass(__FILE__, __LINE__)
82 
83 #define RMP_THROW_MSG(ExceptionClass, Message) \
84  throw ExceptionClass(__FILE__, __LINE__, (Message) )
85 
86 #define RMP_THROW_MSG_AND_ID(ExceptionClass, Message, Id) \
87  throw ExceptionClass(__FILE__, __LINE__, (Message), (Id) )
88 
92 #define MAX_SEGWAYSTATUS_QUEUE_SIZE 100
93 
94 namespace segwayrmp {
95 
99 typedef enum {
103  can = 0,
107  usb = 1,
111  serial = 2,
116  ethernet = 3,
118 } InterfaceType;
119 
123 typedef enum {
127  rmp50 = 0,
131  rmp100 = 1,
135  rmp200 = 2,
140  rmp400 = 3,
145 } SegwayRMPType;
146 
150 typedef enum {
154  disabled = 0,
159  tractor = 1,
163  balanced = 2,
169 
173 typedef enum {
177  light = 0,
182  tall = 1,
186  heavy = 2
188 
193 {
194 public:
195  SegwayTime (uint32_t sec = 0, uint32_t nsec = 0) {
196  this->sec = sec;
197  this->nsec = nsec;
198  }
199 
200  uint32_t sec;
201  uint32_t nsec;
202 };
203 
204 template<typename T>
206  std::queue<boost::shared_ptr<T> > queue_;
207  boost::mutex mutex_;
208  boost::condition_variable condition_variable_;
209  size_t size_;
210  bool canceled_;
211 public:
212  FiniteConcurrentSharedQueue(size_t size = 1024)
213  : size_(size), canceled_(false) {}
215 
216  size_t size() {
217  boost::mutex::scoped_lock(mutex_);
218  return queue_.size();
219  }
220 
221  bool empty() {
222  return this->size() == 0;
223  }
224 
225  bool enqueue(boost::shared_ptr<T> element) {
226  bool dropped_element = false;
227  {
228  boost::lock_guard<boost::mutex> lock(mutex_);
229  if (queue_.size() == size_) {
230  queue_.pop();
231  dropped_element = true;
232  }
233  queue_.push(element);
234  }
235  condition_variable_.notify_one();
236  return dropped_element;
237  }
238 
239  boost::shared_ptr<T> dequeue() {
240  boost::unique_lock<boost::mutex> lock(mutex_);
241  while (queue_.empty()) {
242  condition_variable_.wait(lock);
243  if (this->canceled_) {
244  return boost::shared_ptr<T>();
245  }
246  }
247  boost::shared_ptr<T> element = queue_.front();
248  queue_.pop();
249  return element;
250  }
251 
252  void cancel() {
253  {
254  boost::lock_guard<boost::mutex> lock(mutex_);
255  this->canceled_ = true;
256  }
257  condition_variable_.notify_all();
258  }
259 
260  void reset() {
261  {
262  boost::lock_guard<boost::mutex> lock(mutex_);
263  this->canceled_ = false;
264  }
265  condition_variable_.notify_all();
266  }
267 };
268 
269 // Forward declarations
270 class RMPIO;
271 class Packet;
272 
277 public:
279  float pitch;
280  float pitch_rate;
281  float roll;
282  float roll_rate;
285  float yaw_rate;
286  float servo_frames;
319  bool touched;
320 
321  SegwayStatus();
322 
323  std::string str();
324 
325  typedef boost::shared_ptr<SegwayStatus> Ptr;
326 };
327 
328 typedef boost::function<void(SegwayStatus::Ptr)> SegwayStatusCallback;
329 typedef boost::function<SegwayTime(void)> GetTimeCallback;
330 typedef boost::function<void(const std::exception&)> ExceptionCallback;
331 typedef boost::function<void(const std::string&)> LogMsgCallback;
332 
336 class SegwayRMP {
337 public:
345  SegwayRMP(InterfaceType interface_type = serial,
346  SegwayRMPType segway_rmp_type = rmp200);
347  ~SegwayRMP();
348 
357  void
358  configureSerial(std::string port, int baudrate = 460800);
359 
369  void
370  configureUSBBySerial(std::string serial_number,
371  int baudrate = 460800);
372 
382  void
383  configureUSBByDescription(std::string description, int baudrate = 460800);
384 
395  void
396  configureUSBByIndex(int device_index, int baudrate = 460800);
397 
403  void
404  connect(bool reset_integrators = true);
405 
409  void
410  shutdown();
411 
420  void
421  moveCounts(short int linear_counts, short int angular_counts);
422 
432  void
433  move(float linear_velocity, float angular_velocity);
434 
435  /************ Getter and Setters ************/
436 
442  void
443  setOperationalMode(OperationalMode operational_mode);
444 
451  void
452  setControllerGainSchedule(ControllerGainSchedule controller_gain_schedule);
453 
461  void
462  setBalanceModeLocking(bool state = true);
463 
469  void
470  resetAllIntegrators();
471 
480  void
481  setMaxVelocityScaleFactor(double scalar = 1.0);
482 
491  void
492  setMaxAccelerationScaleFactor(double scalar = 1.0);
493 
502  void
503  setMaxTurnScaleFactor(double scalar = 1.0);
504 
513  void
514  setCurrentLimitScaleFactor(double scalar = 1.0);
515 
561  void
562  setStatusCallback(SegwayStatusCallback callback);
563 
613  void
614  setLogMsgCallback(std::string log_level, LogMsgCallback callback);
615 
675  void
676  setTimestampCallback(GetTimeCallback callback);
677 
728  void
729  setExceptionCallback(ExceptionCallback callback);
730 private:
731  // Disable Copy Constructor
732  void operator=(const SegwayRMP &);
733  const SegwayRMP & operator=(SegwayRMP);
734 
735  // Interface implementation (pimpl idiom)
737 
738  // Configuration Variables
741 
742  // Status Variables
745 
746  // Constants
747  void SetConstantsBySegwayType_(SegwayRMPType &rmp_type);
753 
754  // Callbacks
755  SegwayStatusCallback status_callback_;
756  GetTimeCallback get_time_;
757  LogMsgCallback debug_, info_, error_;
758  ExceptionCallback handle_exception_;
760 
761  // Continuous Read Functions and Variables
762  void ReadContinuously_();
763  void ExecuteCallbacks_();
764  void StartReadingContinuously_();
765  void StopReadingContinuously_();
767  boost::thread read_thread_;
769 
770  // Parsing Functions and Variables
771  void ProcessPacket_(Packet &packet);
772  bool ParsePacket_(Packet &packet, SegwayStatus::Ptr &ss_ptr);
773 };
774 
775 DEFINE_EXCEPTION(NoHighPerformanceTimersException, "", "This system does not "
776  "appear to have a High Precision Event Timer (HPET) device.");
777 
778 DEFINE_EXCEPTION(ConnectionFailedException, "Error connecting to the "
779  "SegwayRMP: ", "Unspecified");
780 
781 DEFINE_EXCEPTION(ReadFailedException, "Error reading from the SegwayRMP: ",
782  "Unspecified");
783 
784 DEFINE_EXCEPTION(WriteFailedException, "Error writing to the SegwayRMP: ",
785  "Unspecified");
786 
787 DEFINE_EXCEPTION(MoveFailedException, "Error moving the SegwayRMP: ",
788  "Unspecified");
789 
790 DEFINE_EXCEPTION(ConfigurationException, "Error configuring the SegwayRMP: ",
791  "Unspecified");
792 
793 } // Namespace segwayrmp
794 
795 #endif
SegwayRMPType segway_rmp_type_
Definition: segwayrmp.h:740
LogMsgCallback info_
Definition: segwayrmp.h:757
boost::function< void(SegwayStatus::Ptr)> SegwayStatusCallback
Definition: segwayrmp.h:328
boost::shared_ptr< SegwayStatus > Ptr
Definition: segwayrmp.h:325
ExceptionCallback handle_exception_
Definition: segwayrmp.h:758
ControllerGainSchedule
Definition: segwayrmp.h:173
boost::shared_ptr< T > dequeue()
Definition: segwayrmp.h:239
GetTimeCallback get_time_
Definition: segwayrmp.h:756
DEFINE_EXCEPTION(PacketRetrievalException,"Error retrieving a packet from the"" SegwayRMP: ","Unspecified")
SegwayStatusCallback status_callback_
Definition: segwayrmp.h:755
std::queue< boost::shared_ptr< T > > queue_
Definition: segwayrmp.h:206
bool enqueue(boost::shared_ptr< T > element)
Definition: segwayrmp.h:225
boost::function< SegwayTime(void)> GetTimeCallback
Definition: segwayrmp.h:329
ControllerGainSchedule controller_gain_schedule
Definition: segwayrmp.h:311
boost::thread read_thread_
Definition: segwayrmp.h:767
float integrated_left_wheel_position
Definition: segwayrmp.h:288
SegwayStatus::Ptr segway_status_
Definition: segwayrmp.h:744
boost::function< void(const std::exception &)> ExceptionCallback
Definition: segwayrmp.h:330
double meters_to_counts_
Definition: segwayrmp.h:750
InterfaceType interface_type_
Definition: segwayrmp.h:739
float integrated_right_wheel_position
Definition: segwayrmp.h:290
FiniteConcurrentSharedQueue(size_t size=1024)
Definition: segwayrmp.h:212
SegwayTime(uint32_t sec=0, uint32_t nsec=0)
Definition: segwayrmp.h:195
OperationalMode operational_mode
Definition: segwayrmp.h:306
boost::condition_variable condition_variable_
Definition: segwayrmp.h:208
float integrated_forward_position
Definition: segwayrmp.h:292
FiniteConcurrentSharedQueue< SegwayStatus > ss_queue_
Definition: segwayrmp.h:759
boost::thread callback_execution_thread_
Definition: segwayrmp.h:768
boost::function< void(const std::string &)> LogMsgCallback
Definition: segwayrmp.h:331
double torque_to_counts_
Definition: segwayrmp.h:752


libsegwayrmp
Author(s): William Woodall
autogenerated on Mon Jun 10 2019 13:46:49