libkipr  1.0.0
create.hpp
Go to the documentation of this file.
1 
8 #ifndef _KIPR_CREATE_CREATE_HPP_
9 #define _KIPR_CREATE_CREATE_HPP_
10 
11 #include <cstdio>
12 #include <cstdlib>
13 #include <vector>
14 
15 #ifndef WIN32
16 #include <sys/time.h>
17 #include <termios.h>
18 #include <pthread.h>
19 #else
20 #include <time.h>
21 #include <winsock2.h>
22 typedef unsigned int speed_t;
23 #endif
24 
25 #include "kipr/sensor/sensor.hpp"
26 #include "kipr/button/button.hpp"
27 #include "kipr/export/export.h"
28 
29 #define PI 3.14159f
30 
31 namespace kipr
32 {
33  namespace create
34  {
40  {
41  public:
44 
45  void append(const unsigned char &byte);
46  void append(const unsigned char *data, const size_t &size);
47  void append(const CreateScript &script);
48 
49  void remove(const size_t &i, const size_t &len = 1);
50 
51  const unsigned char *data() const;
52  size_t size() const;
53 
54  unsigned char byte(const size_t &i);
55 
57 
58  private:
59  std::vector<unsigned char> m_script;
60  };
61 
65  struct CreateState
66  {
67  timeval timestamp;
68 
69  int distance;
70  int angle;
71  short radius;
73  short leftVelocity;
74  };
75 
79  namespace CreatePackets
80  {
84  struct _1
85  {
86  unsigned char bumpsAndWheelDrops;
87  unsigned char wall;
88  unsigned char cliffLeft;
89  unsigned char cliffFrontLeft;
90  unsigned char cliffFrontRight;
91  unsigned char cliffRight;
92  unsigned char virtualWall;
93  unsigned char cargoBayDigitalInputs;
95  };
96 
100  struct _2
101  {
102  unsigned char ir;
103  unsigned char buttons;
104  unsigned char distance[2];
105  unsigned char angle[2];
106  };
107 
111  struct _3
112  {
113  unsigned char chargingState;
114  unsigned char voltage[2];
115  char current[2];
117  unsigned char batteryCharge[2];
118  unsigned char batteryCapacity[2];
119  };
120 
124  struct _4
125  {
126  unsigned char wallSignal[2];
127  unsigned char cliffLeftSignal[2];
128  unsigned char cliffFrontLeftSignal[2];
129  unsigned char cliffFrontRightSignal[2];
130  unsigned char cliffRightSignal[2];
131  unsigned char userDigitalInputs;
132  unsigned char userAnalogInput[2];
134  };
135 
139  struct _5
140  {
141  unsigned char mode;
142  unsigned char songNumber;
143  unsigned char songPlaying;
144  unsigned char numberOfStreamPackets;
145  char velocity[2];
146  char radius[2];
147  char rightVelocity[2];
148  char leftVelocity[2];
149  };
150 
154  struct _101
155  {
156  unsigned char leftEncoderCounts[2]; // 43
157  unsigned char rightEncoderCounts[2]; // 44
158 
159  unsigned char lightBumpBits; // 45
160  unsigned char lightBumpLeftSignal[2]; // 46
161  unsigned char lightBumpFrontLeftSignal[2]; // 47
162  unsigned char lightBumpCenterLeftSignal[2]; // 48
163  unsigned char lightBumpCenterRightSignal[2]; // 49
164  unsigned char lightBumpFrontRightSignal[2]; // 50
165  unsigned char lightBumpRightSignal[2]; // 51
166 
167  char leftMotorCurrent[2]; // 54
168  char rightMotorCurrent[2]; // 55
169  char mainBrushMotorCurrent[2]; // 56
170  char sideBrushMotorCurrent[2]; // 57
171  char stasis; // 58
172  };
173  }
174 
175  namespace sensors
176  {
177  class PlayButton;
178  class AdvanceButton;
179  class Wall;
180  class CliffLeft;
181  class CliffFrontLeft;
182  class CliffFrontRight;
183  class CliffRight;
184  class VirtualWall;
185  class WallSignal;
186  class CliffLeftSignal;
187  class CliffFrontLeftSignal;
188  class CliffFrontRightSignal;
189  class CliffRightSignal;
190  class CargoBayAnalogSignal;
191  class CargoBayDigitalInputs;
192  class IR;
193  class ChargingState;
194  class BatteryTemperature;
195  class BatteryCharge;
196  class BatteryCapacity;
197  class Angle;
198  class Distance;
199  class BumpLeft;
200  class BumpRight;
201  class WheelDropRight;
202  class WheelDropLeft;
203  class WheelDropCaster;
204 
205  class LightBumpLeft;
206  class LightBumpFrontLeft;
207  class LightBumpFrontRight;
208  class LightBumpRight;
209  class LightBumpCenterLeft;
210  class LightBumpCenterRight;
211 
212  class LightBumpLeftSignal;
213  class LightBumpFrontLeftSignal;
214  class LightBumpFrontRightSignal;
215  class LightBumpRightSignal;
216  class LightBumpCenterLeftSignal;
217  class LightBumpCenterRightSignal;
218 
219  class SongPlaying;
220  class SongNumber;
221  }
222 
231  {
232  public:
239  enum Mode
240  {
244  FullMode
245  };
246 
248 
255  bool connect();
256 
257  enum BaudRate
258  {
260  Baud115200 = 1
261  };
262 
270  bool connect(const BaudRate baudRate);
271 
278  bool disconnect();
279 
285  bool isConnected() const;
286 
287  void setOffMode();
289  void setSafeMode();
290  void setFullMode();
291 
292  void setMode(const Create::Mode &mode);
294 
295  void send(const CreateScript &script);
296 
297  bool write(const unsigned char &c);
298 
299 #ifndef SWIG
300  bool write(const unsigned char *data, const size_t &len);
301 #endif
302 
303  void flush();
304 
309  short read();
310 
317  int read(unsigned char *data, const size_t &len);
318 
326  bool blockingRead(unsigned char *data, const size_t &size, unsigned timeout = 25);
327 
328  template <typename T>
329  inline bool blockingRead(T &data, unsigned timeout = 0)
330  {
331  return blockingRead(reinterpret_cast<unsigned char *>(&data), sizeof(T),
332  timeout ? timeout : sizeof(T) * 7);
333  }
334 
335  void setLeds(const bool &advance, const bool &play, const unsigned char &color, const unsigned char &brightness);
336 
337  void drive(const short &velocity, const short &radius);
338  void driveDirect(const short &left, const short &right);
339  void driveStraight(const short &speed);
340  void stop();
341 
348  void turn(const short &angle, const unsigned short &speed);
349 
356  void move(const short &millimeters, const unsigned short &speed);
357 
362  void spin(const short &speed);
363 
369  short angularVelocity() const;
370 
371  void spinClockwise(const short &speed);
372  void spinCounterClockwise(const short &speed);
373 
374  bool setBaudRate(const unsigned char &baudCode);
375 
376  void setDistance(const int distance);
377  void setAngle(const int angle);
378 
379  // High level sensors
382 
389 
397 
399 
404 
407 
410 
414 
421 
428 
431 
432  void setRefreshRate(const unsigned short &refreshRate);
433  const unsigned short &refreshRate() const;
434 
452  bool loadSong(const unsigned char *song, const unsigned char length, const unsigned char songNum);
453 
461  bool playSong(const unsigned char songNum);
462 
468  static Create *instance();
469 
470  const CreateState *state();
477 
478  inline void beginAtomicOperation()
479  {
480 #ifndef WIN32
481  pthread_mutex_lock(&m_mutex);
482 #endif
483  }
484 
485  inline void endAtomicOperation()
486  {
487 #ifndef WIN32
488  pthread_mutex_unlock(&m_mutex);
489 #endif
490  }
491 
492  inline void setDefaultBaudRate(const BaudRate defaultBaudRate) { m_defaultBaudRate = defaultBaudRate; }
493  inline BaudRate defaultBaudRate() const { return m_defaultBaudRate; }
494 
495  private:
496  Create();
497  Create(const Create &rhs);
498  Create &operator=(const Create &rhs);
499 
500  void setLocalBaudRate(const speed_t &baudRate);
501 
502  bool start();
503 
504  bool open();
505  void close();
506 
507  template <typename T>
508  inline void lazyDelete(T *&ptr)
509  {
510  if (ptr)
511  delete ptr;
512  }
513 
514  inline timeval timeOfDay() const
515  {
516 #ifdef _WIN32
517  FILETIME ft;
518  GetSystemTimeAsFileTime(&ft);
519  unsigned long t = (ULONGLONG)ft.dwLowDateTime + ((ULONGLONG)(ft.dwHighDateTime) << 32);
520 
521  timeval tv;
522  tv.tv_usec = t % 1000L;
523  tv.tv_sec = (t - tv.tv_usec) / 1000L;
524  return tv;
525 #else
526  timeval ret;
527  gettimeofday(&ret, NULL);
528  return ret;
529 #endif
530  }
531 
532  inline bool hasRequiredTimePassed(const timeval &timestamp) const
533  {
534 #ifndef WIN32
535  timeval current = timeOfDay();
536  timeval result;
537  timersub(&current, &timestamp, &result);
538  const long msecs = result.tv_sec * 1000 + result.tv_usec / 1000;
539  return msecs > m_refreshRate;
540 #else
541  return false;
542 #endif
543  }
544 
545  inline double timevalToFloat(const timeval &tv)
546  {
547  return ((double)tv.tv_sec) + ((double)tv.tv_usec / 1000000.0);
548  }
549 
550  inline float deg2rad(const float &deg)
551  {
552  return deg / 180.0 * PI;
553  }
554 
555  void updateState();
556 
557  void updateSensorPacket1();
558  void updateSensorPacket2(bool forceUpdate = false);
559  void updateSensorPacket3();
560  void updateSensorPacket4();
561  void updateSensorPacket5();
562  void updateSensorPacket101();
563 
564  CreateState m_state;
565 
566  unsigned short m_refreshRate;
567 
568  CreatePackets::_1 m_1;
569  CreatePackets::_2 m_2;
570  CreatePackets::_3 m_3;
571  CreatePackets::_4 m_4;
572  CreatePackets::_5 m_5;
573  CreatePackets::_101 m_101;
574  timeval timestamps[6];
575  bool have_packet[6];
576 
577  // These are all marked mutable because they
578  // are initialized lazily in a const accessor.
579  mutable sensors::PlayButton *m_playButton;
580  mutable sensors::AdvanceButton *m_advanceButton;
581 
582  mutable sensors::Wall *m_wall;
583  mutable sensors::CliffLeft *m_cliffLeft;
584  mutable sensors::CliffFrontLeft *m_cliffFrontLeft;
585  mutable sensors::CliffFrontRight *m_cliffFrontRight;
586  mutable sensors::CliffRight *m_cliffRight;
587  mutable sensors::VirtualWall *m_virtualWall;
588 
589  mutable sensors::WallSignal *m_wallSignal;
590  mutable sensors::CliffLeftSignal *m_cliffLeftSignal;
591  mutable sensors::CliffFrontLeftSignal *m_cliffFrontLeftSignal;
592  mutable sensors::CliffFrontRightSignal *m_cliffFrontRightSignal;
593  mutable sensors::CliffRightSignal *m_cliffRightSignal;
594  mutable sensors::CargoBayAnalogSignal *m_cargoBayAnalogSignal;
595  mutable sensors::CargoBayDigitalInputs *m_cargoBayDigitalInputs;
596 
597  mutable sensors::LightBumpLeft *m_lightBumpLeft;
598  mutable sensors::LightBumpFrontLeft *m_lightBumpFrontLeft;
599  mutable sensors::LightBumpCenterLeft *m_lightBumpCenterLeft;
600  mutable sensors::LightBumpCenterRight *m_lightBumpCenterRight;
601  mutable sensors::LightBumpFrontRight *m_lightBumpFrontRight;
602  mutable sensors::LightBumpRight *m_lightBumpRight;
603 
604  mutable sensors::LightBumpLeftSignal *m_lightBumpLeftSignal;
605  mutable sensors::LightBumpFrontLeftSignal *m_lightBumpFrontLeftSignal;
606  mutable sensors::LightBumpCenterLeftSignal *m_lightBumpCenterLeftSignal;
607  mutable sensors::LightBumpCenterRightSignal *m_lightBumpCenterRightSignal;
608  mutable sensors::LightBumpFrontRightSignal *m_lightBumpFrontRightSignal;
609  mutable sensors::LightBumpRightSignal *m_lightBumpRightSignal;
610 
611  mutable sensors::IR *m_ir;
612 
613  mutable sensors::ChargingState *m_chargingState;
614  mutable sensors::BatteryTemperature *m_batteryTemperature;
615  mutable sensors::BatteryCharge *m_batteryCharge;
616  mutable sensors::BatteryCapacity *m_batteryCapacity;
617 
618  mutable sensors::Angle *m_angle;
619  mutable sensors::Distance *m_distance;
620 
621  mutable sensors::BumpLeft *m_bumpLeft;
622  mutable sensors::BumpRight *m_bumpRight;
623 
624  mutable sensors::WheelDropLeft *m_wheelDropLeft;
625  mutable sensors::WheelDropRight *m_wheelDropRight;
626  mutable sensors::WheelDropCaster *m_wheelDropCaster;
627 
628  mutable sensors::SongPlaying *m_songPlaying;
629  mutable sensors::SongNumber *m_songNumber;
630 
631  size_t m_i;
632  CreateScript m_script;
633  int m_tty;
634 #ifndef WIN32
635  pthread_mutex_t m_mutex;
636 #endif
637 
638  BaudRate m_defaultBaudRate;
639  };
640  }
641 }
642 
643 #endif /* INCLUDE_WALLABY_CREATE_HPP_ */
Definition: button.hpp:13
Definition: create.hpp:40
const unsigned char * data() const
void append(const unsigned char &byte)
void append(const CreateScript &script)
void remove(const size_t &i, const size_t &len=1)
CreateScript(const CreateScript &rhs)
CreateScript & operator=(const CreateScript &rhs)
void append(const unsigned char *data, const size_t &size)
unsigned char byte(const size_t &i)
Facilitates communication with the iRobot (R) Create (TM)
Definition: create.hpp:231
sensor::Sensor< bool > * cliffFrontRight() const
sensor::Sensor< bool > * wall() const
void send(const CreateScript &script)
sensor::Sensor< bool > * cliffRight() const
sensor::Sensor< unsigned short > * cliffRightSignal() const
sensor::Sensor< bool > * bumpLeft() const
void setLeds(const bool &advance, const bool &play, const unsigned char &color, const unsigned char &brightness)
void spinCounterClockwise(const short &speed)
bool write(const unsigned char *data, const size_t &len)
void move(const short &millimeters, const unsigned short &speed)
void turn(const short &angle, const unsigned short &speed)
void setDistance(const int distance)
sensor::Sensor< unsigned char > * songNumber() const
sensor::Sensor< bool > * lightBumpRight() const
sensor::Sensor< bool > * lightBumpCenterLeft() const
sensor::Sensor< unsigned char > * cargoBayDigitalInputs() const
sensor::Sensor< unsigned short > * lightBumpCenterRightSignal() const
BaudRate
Definition: create.hpp:258
sensor::Sensor< bool > * lightBumpCenterRight() const
bool setBaudRate(const unsigned char &baudCode)
void beginAtomicOperation()
Definition: create.hpp:478
BaudRate defaultBaudRate() const
Definition: create.hpp:493
int read(unsigned char *data, const size_t &len)
void endAtomicOperation()
Definition: create.hpp:485
sensor::Sensor< unsigned char > * songPlaying() const
sensor::Sensor< bool > * cliffFrontLeft() const
sensor::Sensor< unsigned short > * cargoBayAnalogSignal() const
bool connect(const BaudRate baudRate)
const CreatePackets::_4 * sensorPacket4()
sensor::Sensor< int > * distance() const
bool blockingRead(T &data, unsigned timeout=0)
Definition: create.hpp:329
button::AbstractButton * playButton() const
const unsigned short & refreshRate() const
sensor::Sensor< char > * batteryTemperature() const
sensor::Sensor< unsigned short > * batteryCapacity() const
void drive(const short &velocity, const short &radius)
void spinClockwise(const short &speed)
short angularVelocity() const
const CreatePackets::_2 * sensorPacket2()
sensor::Sensor< unsigned short > * lightBumpRightSignal() const
sensor::Sensor< unsigned short > * lightBumpCenterLeftSignal() const
sensor::Sensor< unsigned short > * wallSignal() const
sensor::Sensor< bool > * lightBumpLeft() const
button::AbstractButton * advanceButton() const
void driveDirect(const short &left, const short &right)
const CreatePackets::_101 * sensorPacket101()
const CreateState * state()
sensor::Sensor< unsigned char > * chargingState() const
sensor::Sensor< unsigned short > * lightBumpFrontRightSignal() const
Create::Mode mode()
void setRefreshRate(const unsigned short &refreshRate)
void setMode(const Create::Mode &mode)
void spin(const short &speed)
sensor::Sensor< bool > * wheelDropLeft() const
sensor::Sensor< bool > * lightBumpFrontLeft() const
Mode
Definition: create.hpp:240
@ OffMode
Definition: create.hpp:241
@ SafeMode
Definition: create.hpp:243
@ PassiveMode
Definition: create.hpp:242
sensor::Sensor< unsigned short > * cliffFrontRightSignal() const
sensor::Sensor< unsigned short > * cliffLeftSignal() const
bool write(const unsigned char &c)
sensor::Sensor< bool > * virtualWall() const
const CreatePackets::_5 * sensorPacket5()
sensor::Sensor< int > * angle() const
sensor::Sensor< bool > * wheelDropCaster() const
sensor::Sensor< unsigned short > * batteryCharge() const
sensor::Sensor< unsigned short > * lightBumpLeftSignal() const
const CreatePackets::_3 * sensorPacket3()
bool blockingRead(unsigned char *data, const size_t &size, unsigned timeout=25)
void setDefaultBaudRate(const BaudRate defaultBaudRate)
Definition: create.hpp:492
bool isConnected() const
sensor::Sensor< unsigned short > * cliffFrontLeftSignal() const
sensor::Sensor< bool > * cliffLeft() const
sensor::Sensor< bool > * wheelDropRight() const
sensor::Sensor< unsigned char > * ir() const
void driveStraight(const short &speed)
void setAngle(const int angle)
static Create * instance()
sensor::Sensor< bool > * bumpRight() const
const CreatePackets::_1 * sensorPacket1()
sensor::Sensor< unsigned short > * lightBumpFrontLeftSignal() const
sensor::Sensor< bool > * lightBumpFrontRight() const
BaudRate
The baudrate to use with the create.
Definition: create.h:659
@ Baud115200
Baudrate of 115200 times per second.
Definition: create.h:661
@ Baud57600
Baudrate of 57600 times per second.
Definition: create.h:660
#define PI
Definition: create.hpp:29
#define EXPORT_SYM
Definition: export.h:7
bool loadSong(const unsigned char *song, const unsigned char length, const unsigned char songNum)
bool playSong(const unsigned char songNum)
Definition: accel.hpp:7
Definition: create.hpp:155
unsigned char lightBumpRightSignal[2]
Definition: create.hpp:165
unsigned char rightEncoderCounts[2]
Definition: create.hpp:157
unsigned char lightBumpCenterRightSignal[2]
Definition: create.hpp:163
char mainBrushMotorCurrent[2]
Definition: create.hpp:169
char rightMotorCurrent[2]
Definition: create.hpp:168
unsigned char lightBumpFrontLeftSignal[2]
Definition: create.hpp:161
char sideBrushMotorCurrent[2]
Definition: create.hpp:170
unsigned char lightBumpLeftSignal[2]
Definition: create.hpp:160
char stasis
Definition: create.hpp:171
unsigned char leftEncoderCounts[2]
Definition: create.hpp:156
unsigned char lightBumpBits
Definition: create.hpp:159
unsigned char lightBumpFrontRightSignal[2]
Definition: create.hpp:164
char leftMotorCurrent[2]
Definition: create.hpp:167
unsigned char lightBumpCenterLeftSignal[2]
Definition: create.hpp:162
Definition: create.hpp:85
unsigned char wall
Definition: create.hpp:87
unsigned char virtualWall
Definition: create.hpp:92
unsigned char bumpsAndWheelDrops
Definition: create.hpp:86
unsigned char cliffLeft
Definition: create.hpp:88
unsigned char cliffFrontRight
Definition: create.hpp:90
unsigned char cliffRight
Definition: create.hpp:91
unsigned char cargoBayDigitalInputs
Definition: create.hpp:93
unsigned char lowSideDriverAndWheelOvercurrents
Definition: create.hpp:94
unsigned char cliffFrontLeft
Definition: create.hpp:89
Definition: create.hpp:101
unsigned char ir
Definition: create.hpp:102
unsigned char buttons
Definition: create.hpp:103
unsigned char angle[2]
Definition: create.hpp:105
unsigned char distance[2]
Definition: create.hpp:104
Definition: create.hpp:112
char batteryTemperature
Definition: create.hpp:116
unsigned char chargingState
Definition: create.hpp:113
unsigned char batteryCharge[2]
Definition: create.hpp:117
unsigned char batteryCapacity[2]
Definition: create.hpp:118
char current[2]
Definition: create.hpp:115
unsigned char voltage[2]
Definition: create.hpp:114
Definition: create.hpp:125
unsigned char cliffLeftSignal[2]
Definition: create.hpp:127
unsigned char wallSignal[2]
Definition: create.hpp:126
unsigned char chargingSourcesAvailable
Definition: create.hpp:133
unsigned char cliffRightSignal[2]
Definition: create.hpp:130
unsigned char cliffFrontLeftSignal[2]
Definition: create.hpp:128
unsigned char cliffFrontRightSignal[2]
Definition: create.hpp:129
unsigned char userAnalogInput[2]
Definition: create.hpp:132
unsigned char userDigitalInputs
Definition: create.hpp:131
Definition: create.hpp:140
char radius[2]
Definition: create.hpp:146
char leftVelocity[2]
Definition: create.hpp:148
unsigned char numberOfStreamPackets
Definition: create.hpp:144
unsigned char songNumber
Definition: create.hpp:142
char velocity[2]
Definition: create.hpp:145
unsigned char mode
Definition: create.hpp:141
char rightVelocity[2]
Definition: create.hpp:147
unsigned char songPlaying
Definition: create.hpp:143
Definition: create.hpp:66
short rightVelocity
Definition: create.hpp:72
int angle
Definition: create.hpp:70
int distance
Definition: create.hpp:69
short leftVelocity
Definition: create.hpp:73
short radius
Definition: create.hpp:71
timeval timestamp
Definition: create.hpp:67