libwallaby  v24
The wallaby standard library
create.hpp
Go to the documentation of this file.
1 /*
2  * create.hpp
3  *
4  * Created on: Nov 13, 2015
5  * Author: Joshua Southerland
6  */
7 
8 #ifndef INCLUDE_WALLABY_CREATE_HPP_
9 #define INCLUDE_WALLABY_CREATE_HPP_
10 
11 
12 
26 #ifndef _CREATE_HPP_
27 #define _CREATE_HPP_
28 
29 #include <cstdio>
30 #include <cstdlib>
31 #include <vector>
32 
33 #ifndef WIN32
34 #include <sys/time.h>
35 #include <termios.h>
36 #include <pthread.h>
37 #else
38 #include <time.h>
39 #include <winsock2.h>
40 typedef unsigned int speed_t;
41 #endif
42 
43 #include "sensor.hpp"
44 #include "button.hpp"
45 #include "export.h"
46 
47 #define PI 3.14159f
48 
54 {
55 public:
56  CreateScript();
57  CreateScript(const CreateScript& rhs);
58 
59  void append(const unsigned char& byte);
60  void append(const unsigned char *data, const size_t& size);
61  void append(const CreateScript& script);
62 
63  void remove(const size_t& i, const size_t& len = 1);
64 
65  const unsigned char *data() const;
66  size_t size() const;
67 
68  unsigned char byte(const size_t& i);
69 
70  CreateScript& operator=(const CreateScript& rhs);
71 
72 private:
73  std::vector<unsigned char> m_script;
74 };
75 
80 {
81  timeval timestamp;
82 
83  int distance;
84  int angle;
85  short radius;
87  short leftVelocity;
88 };
89 
93 namespace CreatePackets
94 {
98  struct _1
99  {
100  unsigned char bumpsAndWheelDrops;
101  unsigned char wall;
102  unsigned char cliffLeft;
103  unsigned char cliffFrontLeft;
104  unsigned char cliffFrontRight;
105  unsigned char cliffRight;
106  unsigned char virtualWall;
107  unsigned char cargoBayDigitalInputs;
109  };
110 
114  struct _2
115  {
116  unsigned char ir;
117  unsigned char buttons;
118  unsigned char distance[2];
119  unsigned char angle[2];
120  };
121 
125  struct _3
126  {
127  unsigned char chargingState;
128  unsigned char voltage[2];
129  char current[2];
131  unsigned char batteryCharge[2];
132  unsigned char batteryCapacity[2];
133  };
134 
138  struct _4
139  {
140  unsigned char wallSignal[2];
141  unsigned char cliffLeftSignal[2];
142  unsigned char cliffFrontLeftSignal[2];
143  unsigned char cliffFrontRightSignal[2];
144  unsigned char cliffRightSignal[2];
145  unsigned char userDigitalInputs;
146  unsigned char userAnalogInput[2];
148  };
149 
153  struct _5
154  {
155  unsigned char mode;
156  unsigned char songNumber;
157  unsigned char songPlaying;
158  unsigned char numberOfStreamPackets;
159  char velocity[2];
160  char radius[2];
161  char rightVelocity[2];
162  char leftVelocity[2];
163  };
164 
168  struct _101
169  {
170  unsigned char leftEncoderCounts[2];//43
171  unsigned char rightEncoderCounts[2];//44
172 
173  unsigned char lightBumpBits;//45
174  unsigned char lightBumpLeftSignal[2];//46
175  unsigned char lightBumpFrontLeftSignal[2];//47
176  unsigned char lightBumpCenterLeftSignal[2];//48
177  unsigned char lightBumpCenterRightSignal[2];//49
178  unsigned char lightBumpFrontRightSignal[2];//50
179  unsigned char lightBumpRightSignal[2];//51
180 
181  char leftMotorCurrent[2];//54
182  char rightMotorCurrent[2];//55
185  char stasis;//58
186  };
187 }
188 
189 namespace CreateSensors
190 {
191  class PlayButton;
192  class AdvanceButton;
193  class Wall;
194  class CliffLeft;
195  class CliffFrontLeft;
196  class CliffFrontRight;
197  class CliffRight;
198  class VirtualWall;
199  class WallSignal;
200  class CliffLeftSignal;
201  class CliffFrontLeftSignal;
202  class CliffFrontRightSignal;
203  class CliffRightSignal;
204  class CargoBayAnalogSignal;
205  class CargoBayDigitalInputs;
206  class IR;
207  class ChargingState;
208  class BatteryTemperature;
209  class BatteryCharge;
210  class BatteryCapacity;
211  class Angle;
212  class Distance;
213  class BumpLeft;
214  class BumpRight;
215  class WheelDropRight;
216  class WheelDropLeft;
217  class WheelDropCaster;
218 
219  class LightBumpLeft;
220  class LightBumpFrontLeft;
221  class LightBumpFrontRight;
222  class LightBumpRight;
223  class LightBumpCenterLeft;
224  class LightBumpCenterRight;
225 
226  class LightBumpLeftSignal;
227  class LightBumpFrontLeftSignal;
228  class LightBumpFrontRightSignal;
229  class LightBumpRightSignal;
230  class LightBumpCenterLeftSignal;
231  class LightBumpCenterRightSignal;
232 }
233 
242 {
243 public:
250  enum Mode {
254  FullMode
255  };
256 
257  ~Create();
258 
265  bool connect();
266 
267 
268  enum BaudRate
269  {
272  };
273 
281  bool connect(const BaudRate baudRate);
282 
289  bool disconnect();
290 
296  bool isConnected() const;
297 
298  void setOffMode();
299  void setPassiveMode();
300  void setSafeMode();
301  void setFullMode();
302 
303  void setMode(const Create::Mode& mode);
304  Create::Mode mode();
305 
306  void send(const CreateScript& script);
307 
308  bool write(const unsigned char& c);
309 
310 #ifndef SWIG
311  bool write(const unsigned char *data, const size_t& len);
312 #endif
313 
314  void flush();
315 
320  short read();
321 
328  int read(unsigned char *data, const size_t& len);
329 
337  bool blockingRead(unsigned char *data, const size_t& size, unsigned timeout = 25);
338 
339 
340  template<typename T>
341  inline bool blockingRead(T& data, unsigned timeout = 0)
342  {
343  return blockingRead(reinterpret_cast<unsigned char *>(&data), sizeof(T),
344  timeout ? timeout : sizeof(T) * 7);
345  }
346 
347  void setLeds(const bool& advance, const bool& play, const unsigned char& color, const unsigned char& brightness);
348 
349  void drive(const short& velocity, const short& radius);
350  void driveDirect(const short& left, const short& right);
351  void driveStraight(const short& speed);
352  void stop();
353 
360  void turn(const short& angle, const unsigned short& speed);
361 
368  void move(const short& millimeters, const unsigned short& speed);
369 
374  void spin(const short& speed);
375 
381  short angularVelocity() const;
382 
383  void spinClockwise(const short& speed);
384  void spinCounterClockwise(const short& speed);
385 
386  bool setBaudRate(const unsigned char& baudCode);
387 
388  void setDistance(const int distance);
389  void setAngle(const int angle);
390 
391  // High level sensors
392  AbstractButton *playButton() const;
393  AbstractButton *advanceButton() const;
394 
395  Sensor<bool> *wall() const;
396  Sensor<bool> *cliffLeft() const;
397  Sensor<bool> *cliffFrontLeft() const;
398  Sensor<bool> *cliffFrontRight() const;
399  Sensor<bool> *cliffRight() const;
400  Sensor<bool> *virtualWall() const;
401 
402  Sensor<unsigned short> *wallSignal() const;
403  Sensor<unsigned short> *cliffLeftSignal() const;
404  Sensor<unsigned short> *cliffFrontLeftSignal() const;
405  Sensor<unsigned short> *cliffFrontRightSignal() const;
406  Sensor<unsigned short> *cliffRightSignal() const;
407  Sensor<unsigned short> *cargoBayAnalogSignal() const;
408  Sensor<unsigned char> *cargoBayDigitalInputs() const;
409 
410  Sensor<unsigned char> *ir() const;
411 
412  Sensor<unsigned char> *chargingState() const;
413  Sensor<char> *batteryTemperature() const;
414  Sensor<unsigned short> *batteryCharge() const;
415  Sensor<unsigned short> *batteryCapacity() const;
416 
417  Sensor<int> *angle() const;
418  Sensor<int> *distance() const;
419 
420  Sensor<bool> *bumpLeft() const;
421  Sensor<bool> *bumpRight() const;
422 
423  Sensor<bool> *wheelDropLeft() const;
424  Sensor<bool> *wheelDropRight() const;
425  Sensor<bool> *wheelDropCaster() const;
426 
427  Sensor<bool> *lightBumpLeft() const;
428  Sensor<bool> *lightBumpFrontLeft() const;
429  Sensor<bool> *lightBumpCenterLeft() const;
430  Sensor<bool> *lightBumpCenterRight() const;
431  Sensor<bool> *lightBumpFrontRight() const;
432  Sensor<bool> *lightBumpRight() const;
433 
434  Sensor<unsigned short> *lightBumpLeftSignal() const;
435  Sensor<unsigned short> *lightBumpFrontLeftSignal() const;
436  Sensor<unsigned short> *lightBumpCenterLeftSignal() const;
437  Sensor<unsigned short> *lightBumpCenterRightSignal() const;
438  Sensor<unsigned short> *lightBumpFrontRightSignal() const;
439  Sensor<unsigned short> *lightBumpRightSignal() const;
440 
441  void setRefreshRate(const unsigned short& refreshRate);
442  const unsigned short& refreshRate() const;
443 
449  static Create *instance();
450 
451  const CreateState *state();
452  const CreatePackets::_1 *sensorPacket1();
453  const CreatePackets::_2 *sensorPacket2();
454  const CreatePackets::_3 *sensorPacket3();
455  const CreatePackets::_4 *sensorPacket4();
456  const CreatePackets::_5 *sensorPacket5();
457  const CreatePackets::_101 *sensorPacket101();
458 
459  inline void beginAtomicOperation()
460  {
461  #ifndef WIN32
462  pthread_mutex_lock(&m_mutex);
463  #endif
464  }
465 
466  inline void endAtomicOperation()
467  {
468  #ifndef WIN32
469  pthread_mutex_unlock(&m_mutex);
470  #endif
471  }
472 
473  inline void setDefaultBaudRate(const BaudRate defaultBaudRate) { m_defaultBaudRate = defaultBaudRate; }
474  inline BaudRate defaultBaudRate() const { return m_defaultBaudRate; }
475 
476 private:
477  Create();
478  Create(const Create& rhs);
479  Create& operator=(const Create& rhs);
480 
481  void setLocalBaudRate(const speed_t& baudRate);
482 
483  bool start();
484 
485  bool open();
486  void close();
487 
488  template<typename T>
489  inline void lazyDelete(T *&ptr)
490  {
491  if(ptr) delete ptr;
492  }
493 
494  inline timeval timeOfDay() const
495  {
496 #ifdef _WIN32
497  FILETIME ft;
498  GetSystemTimeAsFileTime(&ft);
499  unsigned long t = (ULONGLONG)ft.dwLowDateTime + ((ULONGLONG)(ft.dwHighDateTime) << 32);
500 
501  timeval tv;
502  tv.tv_usec = t % 1000L;
503  tv.tv_sec = (t - tv.tv_usec) / 1000L;
504  return tv;
505 #else
506  timeval ret;
507  gettimeofday(&ret, NULL);
508  return ret;
509 #endif
510  }
511 
512  inline bool hasRequiredTimePassed(const timeval& timestamp) const
513  {
514  #ifndef WIN32
515  timeval current = timeOfDay();
516  timeval result;
517  timersub(&current, &timestamp, &result);
518  const long msecs = result.tv_sec * 1000 + result.tv_usec / 1000;
519  return msecs > m_refreshRate;
520  #else
521  return false;
522  #endif
523  }
524 
525  inline double timevalToFloat(const timeval& tv)
526  {
527  return ((double)tv.tv_sec) + ((double)tv.tv_usec / 1000000.0);
528  }
529 
530  inline float deg2rad(const float& deg)
531  {
532  return deg / 180.0 * PI;
533  }
534 
535  void updateState();
536 
537  void updateSensorPacket1();
538  void updateSensorPacket2(bool forceUpdate = false);
539  void updateSensorPacket3();
540  void updateSensorPacket4();
541  void updateSensorPacket5();
542  void updateSensorPacket101();
543 
544  CreateState m_state;
545 
546  unsigned short m_refreshRate;
547 
548  CreatePackets::_1 m_1;
549  CreatePackets::_2 m_2;
550  CreatePackets::_3 m_3;
551  CreatePackets::_4 m_4;
552  CreatePackets::_5 m_5;
553  CreatePackets::_101 m_101;
554  timeval timestamps[6];
555  bool have_packet[6];
556 
557 
558  // These are all marked mutable because they
559  // are initialized lazily in a const accessor.
560  // I have yet to decide if this is the "correct"
561  // way to go about it.
562  mutable CreateSensors::PlayButton *m_playButton;
563  mutable CreateSensors::AdvanceButton *m_advanceButton;
564 
565  mutable CreateSensors::Wall *m_wall;
566  mutable CreateSensors::CliffLeft *m_cliffLeft;
567  mutable CreateSensors::CliffFrontLeft *m_cliffFrontLeft;
568  mutable CreateSensors::CliffFrontRight *m_cliffFrontRight;
569  mutable CreateSensors::CliffRight *m_cliffRight;
570  mutable CreateSensors::VirtualWall *m_virtualWall;
571 
572  mutable CreateSensors::WallSignal *m_wallSignal;
573  mutable CreateSensors::CliffLeftSignal *m_cliffLeftSignal;
574  mutable CreateSensors::CliffFrontLeftSignal *m_cliffFrontLeftSignal;
575  mutable CreateSensors::CliffFrontRightSignal *m_cliffFrontRightSignal;
576  mutable CreateSensors::CliffRightSignal *m_cliffRightSignal;
577  mutable CreateSensors::CargoBayAnalogSignal *m_cargoBayAnalogSignal;
578  mutable CreateSensors::CargoBayDigitalInputs *m_cargoBayDigitalInputs;
579 
580  mutable CreateSensors::LightBumpLeft *m_lightBumpLeft;
581  mutable CreateSensors::LightBumpFrontLeft *m_lightBumpFrontLeft;
582  mutable CreateSensors::LightBumpCenterLeft *m_lightBumpCenterLeft;
583  mutable CreateSensors::LightBumpCenterRight *m_lightBumpCenterRight;
584  mutable CreateSensors::LightBumpFrontRight *m_lightBumpFrontRight;
585  mutable CreateSensors::LightBumpRight *m_lightBumpRight;
586 
587  mutable CreateSensors::LightBumpLeftSignal *m_lightBumpLeftSignal;
588  mutable CreateSensors::LightBumpFrontLeftSignal *m_lightBumpFrontLeftSignal;
589  mutable CreateSensors::LightBumpCenterLeftSignal *m_lightBumpCenterLeftSignal;
590  mutable CreateSensors::LightBumpCenterRightSignal *m_lightBumpCenterRightSignal;
591  mutable CreateSensors::LightBumpFrontRightSignal *m_lightBumpFrontRightSignal;
592  mutable CreateSensors::LightBumpRightSignal *m_lightBumpRightSignal;
593 
594  mutable CreateSensors::IR *m_ir;
595 
596  mutable CreateSensors::ChargingState *m_chargingState;
597  mutable CreateSensors::BatteryTemperature *m_batteryTemperature;
598  mutable CreateSensors::BatteryCharge *m_batteryCharge;
599  mutable CreateSensors::BatteryCapacity *m_batteryCapacity;
600 
601  mutable CreateSensors::Angle *m_angle;
602  mutable CreateSensors::Distance *m_distance;
603 
604  mutable CreateSensors::BumpLeft *m_bumpLeft;
605  mutable CreateSensors::BumpRight *m_bumpRight;
606 
607  mutable CreateSensors::WheelDropLeft *m_wheelDropLeft;
608  mutable CreateSensors::WheelDropRight *m_wheelDropRight;
609  mutable CreateSensors::WheelDropCaster *m_wheelDropCaster;
610 
611  size_t m_i;
612  CreateScript m_script;
613  int m_tty;
614 #ifndef WIN32
615  pthread_mutex_t m_mutex;
616 #endif
617 
618  BaudRate m_defaultBaudRate;
619 };
620 
621 #endif
622 
623 
624 #endif /* INCLUDE_WALLABY_CREATE_HPP_ */
CreatePackets::_3::batteryCapacity
unsigned char batteryCapacity[2]
Definition: create.hpp:132
CreateSensors
Definition: create.hpp:189
CreatePackets::_101::leftEncoderCounts
unsigned char leftEncoderCounts[2]
Definition: create.hpp:170
button.hpp
PI
#define PI
Definition: create.hpp:47
CreateScript
Definition: create.hpp:53
CreatePackets::_4::chargingSourcesAvailable
unsigned char chargingSourcesAvailable
Definition: create.hpp:147
CreatePackets::_5::leftVelocity
char leftVelocity[2]
Definition: create.hpp:162
Create::OffMode
Definition: create.hpp:251
CreateState
Definition: create.hpp:79
Create::Mode
Mode
Definition: create.hpp:250
CreatePackets::_5::numberOfStreamPackets
unsigned char numberOfStreamPackets
Definition: create.hpp:158
CreatePackets::_5
Definition: create.hpp:153
CreatePackets::_1::cliffFrontLeft
unsigned char cliffFrontLeft
Definition: create.hpp:103
CreateState::rightVelocity
short rightVelocity
Definition: create.hpp:86
CreatePackets::_1::cargoBayDigitalInputs
unsigned char cargoBayDigitalInputs
Definition: create.hpp:107
CreatePackets::_101::lightBumpRightSignal
unsigned char lightBumpRightSignal[2]
Definition: create.hpp:179
CreateState::radius
short radius
Definition: create.hpp:85
CreatePackets::_101
Definition: create.hpp:168
CreatePackets::_4::userDigitalInputs
unsigned char userDigitalInputs
Definition: create.hpp:145
Create::endAtomicOperation
void endAtomicOperation()
Definition: create.hpp:466
CreatePackets::_101::lightBumpFrontRightSignal
unsigned char lightBumpFrontRightSignal[2]
Definition: create.hpp:178
CreatePackets::_1::lowSideDriverAndWheelOvercurrents
unsigned char lowSideDriverAndWheelOvercurrents
Definition: create.hpp:108
CreatePackets::_101::sideBrushMotorCurrent
char sideBrushMotorCurrent[2]
Definition: create.hpp:184
CreatePackets::_2::buttons
unsigned char buttons
Definition: create.hpp:117
CreatePackets::_1::cliffRight
unsigned char cliffRight
Definition: create.hpp:105
CreatePackets::_5::rightVelocity
char rightVelocity[2]
Definition: create.hpp:161
CreatePackets::_101::stasis
char stasis
Definition: create.hpp:185
CreateState::timestamp
timeval timestamp
Definition: create.hpp:81
CreatePackets::_101::mainBrushMotorCurrent
char mainBrushMotorCurrent[2]
Definition: create.hpp:183
CreatePackets::_5::mode
unsigned char mode
Definition: create.hpp:155
CreatePackets::_2::ir
unsigned char ir
Definition: create.hpp:116
CreatePackets::_3::voltage
unsigned char voltage[2]
Definition: create.hpp:128
CreatePackets::_4::userAnalogInput
unsigned char userAnalogInput[2]
Definition: create.hpp:146
CreatePackets::_101::lightBumpBits
unsigned char lightBumpBits
Definition: create.hpp:173
Create::PassiveMode
Definition: create.hpp:252
CreatePackets::_101::rightMotorCurrent
char rightMotorCurrent[2]
Definition: create.hpp:182
CreateState::leftVelocity
short leftVelocity
Definition: create.hpp:87
CreatePackets::_101::rightEncoderCounts
unsigned char rightEncoderCounts[2]
Definition: create.hpp:171
Create::blockingRead
bool blockingRead(T &data, unsigned timeout=0)
Definition: create.hpp:341
Create::BaudRate
BaudRate
Definition: create.hpp:268
CreatePackets::_5::radius
char radius[2]
Definition: create.hpp:160
CreatePackets::_1::bumpsAndWheelDrops
unsigned char bumpsAndWheelDrops
Definition: create.hpp:100
CreatePackets::_3
Definition: create.hpp:125
CreatePackets::_3::batteryCharge
unsigned char batteryCharge[2]
Definition: create.hpp:131
CreatePackets::_4::cliffFrontRightSignal
unsigned char cliffFrontRightSignal[2]
Definition: create.hpp:143
CreateState::angle
int angle
Definition: create.hpp:84
CreatePackets::_4::cliffLeftSignal
unsigned char cliffLeftSignal[2]
Definition: create.hpp:141
CreatePackets::_1::cliffLeft
unsigned char cliffLeft
Definition: create.hpp:102
Create::SafeMode
Definition: create.hpp:253
CreatePackets
Definition: create.hpp:93
CreatePackets::_4::cliffRightSignal
unsigned char cliffRightSignal[2]
Definition: create.hpp:144
Create::setDefaultBaudRate
void setDefaultBaudRate(const BaudRate defaultBaudRate)
Definition: create.hpp:473
CreatePackets::_4::cliffFrontLeftSignal
unsigned char cliffFrontLeftSignal[2]
Definition: create.hpp:142
CreatePackets::_101::lightBumpFrontLeftSignal
unsigned char lightBumpFrontLeftSignal[2]
Definition: create.hpp:175
sensor.hpp
CreatePackets::_101::lightBumpLeftSignal
unsigned char lightBumpLeftSignal[2]
Definition: create.hpp:174
CreatePackets::_1
Definition: create.hpp:98
AbstractButton
Definition: button.hpp:16
CreatePackets::_101::lightBumpCenterRightSignal
unsigned char lightBumpCenterRightSignal[2]
Definition: create.hpp:177
CreatePackets::_5::velocity
char velocity[2]
Definition: create.hpp:159
CreatePackets::_5::songPlaying
unsigned char songPlaying
Definition: create.hpp:157
CreatePackets::_3::chargingState
unsigned char chargingState
Definition: create.hpp:127
CreatePackets::_4
Definition: create.hpp:138
CreateState::distance
int distance
Definition: create.hpp:83
Create::defaultBaudRate
BaudRate defaultBaudRate() const
Definition: create.hpp:474
Baud57600
Definition: create.h:456
CreatePackets::_1::cliffFrontRight
unsigned char cliffFrontRight
Definition: create.hpp:104
CreatePackets::_2::angle
unsigned char angle[2]
Definition: create.hpp:119
EXPORT_SYM
#define EXPORT_SYM
Definition: export.h:14
CreatePackets::_101::lightBumpCenterLeftSignal
unsigned char lightBumpCenterLeftSignal[2]
Definition: create.hpp:176
CreatePackets::_3::batteryTemperature
char batteryTemperature
Definition: create.hpp:130
BaudRate
BaudRate
Definition: create.h:454
CreatePackets::_2::distance
unsigned char distance[2]
Definition: create.hpp:118
CreatePackets::_5::songNumber
unsigned char songNumber
Definition: create.hpp:156
CreatePackets::_101::leftMotorCurrent
char leftMotorCurrent[2]
Definition: create.hpp:181
Create
Facilitates communication with the iRobot (R) Create (TM)
Definition: create.hpp:241
Baud115200
Definition: create.h:457
CreatePackets::_4::wallSignal
unsigned char wallSignal[2]
Definition: create.hpp:140
Create::beginAtomicOperation
void beginAtomicOperation()
Definition: create.hpp:459
CreatePackets::_1::virtualWall
unsigned char virtualWall
Definition: create.hpp:106
CreatePackets::_2
Definition: create.hpp:114
CreatePackets::_1::wall
unsigned char wall
Definition: create.hpp:101
CreatePackets::_3::current
char current[2]
Definition: create.hpp:129
Sensor< bool >
export.h