libkovan  1
The kovan standard library
create.hpp
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright 2012 KISS Institute for Practical Robotics *
3  * *
4  * This file is part of libkovan. *
5  * *
6  * libkovan is free software: you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation, either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * libkovan is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with libkovan. Check the LICENSE file in the project root. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  **************************************************************************/
20 
34 #ifndef _CREATE_HPP_
35 #define _CREATE_HPP_
36 
37 #include <cstdio>
38 #include <cstdlib>
39 #include <vector>
40 
41 #ifndef WIN32
42 #include <sys/time.h>
43 #include <termios.h>
44 #include <pthread.h>
45 #else
46 #include <time.h>
47 #include <winsock2.h>
48 typedef unsigned int speed_t;
49 #endif
50 
51 #include "sensor.hpp"
52 #include "button.hpp"
53 #include "export.h"
54 
55 #define PI 3.14159f
56 
62 {
63 public:
64  CreateScript();
65  CreateScript(const CreateScript& rhs);
66 
67  void append(const unsigned char& byte);
68  void append(const unsigned char *data, const size_t& size);
69  void append(const CreateScript& script);
70 
71  void remove(const size_t& i, const size_t& len = 1);
72 
73  const unsigned char *data() const;
74  size_t size() const;
75 
76  unsigned char byte(const size_t& i);
77 
78  CreateScript& operator=(const CreateScript& rhs);
79 
80 private:
81  std::vector<unsigned char> m_script;
82 };
83 
88 {
89  timeval timestamp;
90 
91  int distance;
92  int angle;
93  short radius;
95  short leftVelocity;
96 };
97 
101 namespace CreatePackets
102 {
106  struct _1
107  {
108  unsigned char bumpsAndWheelDrops;
109  unsigned char wall;
110  unsigned char cliffLeft;
111  unsigned char cliffFrontLeft;
112  unsigned char cliffFrontRight;
113  unsigned char cliffRight;
114  unsigned char virtualWall;
115  unsigned char cargoBayDigitalInputs;
117  };
118 
122  struct _2
123  {
124  unsigned char ir;
125  unsigned char buttons;
126  unsigned char distance[2];
127  unsigned char angle[2];
128  };
129 
133  struct _3
134  {
135  unsigned char chargingState;
136  unsigned char voltage[2];
137  char current[2];
139  unsigned char batteryCharge[2];
140  unsigned char batteryCapacity[2];
141  };
142 
146  struct _4
147  {
148  unsigned char wallSignal[2];
149  unsigned char cliffLeftSignal[2];
150  unsigned char cliffFrontLeftSignal[2];
151  unsigned char cliffFrontRightSignal[2];
152  unsigned char cliffRightSignal[2];
153  unsigned char userDigitalInputs;
154  unsigned char userAnalogInput[2];
156  };
157 
161  struct _5
162  {
163  unsigned char mode;
164  unsigned char songNumber;
165  unsigned char songPlaying;
166  unsigned char numberOfStreamPackets;
167  char velocity[2];
168  char radius[2];
169  char rightVelocity[2];
170  char leftVelocity[2];
171  };
172 }
173 
174 namespace CreateSensors
175 {
176  class PlayButton;
177  class AdvanceButton;
178  class Wall;
179  class CliffLeft;
180  class CliffFrontLeft;
181  class CliffFrontRight;
182  class CliffRight;
183  class VirtualWall;
184  class WallSignal;
185  class CliffLeftSignal;
186  class CliffFrontLeftSignal;
187  class CliffFrontRightSignal;
188  class CliffRightSignal;
189  class CargoBayAnalogSignal;
190  class CargoBayDigitalInputs;
191  class IR;
192  class ChargingState;
193  class BatteryTemperature;
194  class BatteryCharge;
195  class BatteryCapacity;
196  class Angle;
197  class Distance;
198  class BumpLeft;
199  class BumpRight;
200  class WheelDropRight;
201  class WheelDropLeft;
202  class WheelDropCaster;
203 }
204 
213 {
214 public:
221  enum Mode {
225  FullMode
226  };
227 
228  ~Create();
229 
236  bool connect();
237 
238 
239  enum BaudRate
240  {
243  };
244 
252  bool connect(const BaudRate baudRate);
253 
260  bool disconnect();
261 
267  bool isConnected() const;
268 
269  void setPassiveMode();
270  void setSafeMode();
271  void setFullMode();
272 
273  void setMode(const Create::Mode& mode);
274  Create::Mode mode();
275 
276  void send(const CreateScript& script);
277 
278  bool write(const unsigned char& c);
279 
280 #ifndef SWIG
281  bool write(const unsigned char *data, const size_t& len);
282 #endif
283 
284  void flush();
285 
290  short read();
291 
298  int read(unsigned char *data, const size_t& len);
299 
307  bool blockingRead(unsigned char *data, const size_t& size, unsigned timeout = 25);
308 
309 
310  template<typename T>
311  inline bool blockingRead(T& data, unsigned timeout = 0)
312  {
313  return blockingRead(reinterpret_cast<unsigned char *>(&data), sizeof(T),
314  timeout ? timeout : sizeof(T) * 7);
315  }
316 
317  void setLeds(const bool& advance, const bool& play, const unsigned char& color, const unsigned char& brightness);
318 
319  void drive(const short& velocity, const short& radius);
320  void driveDirect(const short& left, const short& right);
321  void driveStraight(const short& speed);
322  void stop();
323 
330  void turn(const short& angle, const unsigned short& speed);
331 
338  void move(const short& millimeters, const unsigned short& speed);
339 
344  void spin(const short& speed);
345 
351  short angularVelocity() const;
352 
353  void spinClockwise(const short& speed);
354  void spinCounterClockwise(const short& speed);
355 
356  bool setBaudRate(const unsigned char& baudCode);
357 
358  void setDistance(const int distance);
359  void setAngle(const int angle);
360 
361  // High level sensors
362  AbstractButton *playButton() const;
363  AbstractButton *advanceButton() const;
364 
365  Sensor<bool> *wall() const;
366  Sensor<bool> *cliffLeft() const;
367  Sensor<bool> *cliffFrontLeft() const;
368  Sensor<bool> *cliffFrontRight() const;
369  Sensor<bool> *cliffRight() const;
370  Sensor<bool> *virtualWall() const;
371 
372  Sensor<unsigned short> *wallSignal() const;
373  Sensor<unsigned short> *cliffLeftSignal() const;
374  Sensor<unsigned short> *cliffFrontLeftSignal() const;
375  Sensor<unsigned short> *cliffFrontRightSignal() const;
376  Sensor<unsigned short> *cliffRightSignal() const;
377  Sensor<unsigned short> *cargoBayAnalogSignal() const;
378  Sensor<unsigned char> *cargoBayDigitalInputs() const;
379 
380  Sensor<unsigned char> *ir() const;
381 
382  Sensor<unsigned char> *chargingState() const;
383  Sensor<char> *batteryTemperature() const;
384  Sensor<unsigned short> *batteryCharge() const;
385  Sensor<unsigned short> *batteryCapacity() const;
386 
387  Sensor<int> *angle() const;
388  Sensor<int> *distance() const;
389 
390  Sensor<bool> *bumpLeft() const;
391  Sensor<bool> *bumpRight() const;
392 
393  Sensor<bool> *wheelDropLeft() const;
394  Sensor<bool> *wheelDropRight() const;
395  Sensor<bool> *wheelDropCaster() const;
396 
397  void setRefreshRate(const unsigned short& refreshRate);
398  const unsigned short& refreshRate() const;
399 
405  static Create *instance();
406 
407  const CreateState *state();
408  const CreatePackets::_1 *sensorPacket1();
409  const CreatePackets::_2 *sensorPacket2();
410  const CreatePackets::_3 *sensorPacket3();
411  const CreatePackets::_4 *sensorPacket4();
412  const CreatePackets::_5 *sensorPacket5();
413 
414  inline void beginAtomicOperation()
415  {
416  #ifndef WIN32
417  pthread_mutex_lock(&m_mutex);
418  #endif
419  }
420 
421  inline void endAtomicOperation()
422  {
423  #ifndef WIN32
424  pthread_mutex_unlock(&m_mutex);
425  #endif
426  }
427 
428  inline void setDefaultBaudRate(const BaudRate defaultBaudRate) { m_defaultBaudRate = defaultBaudRate; }
429  inline BaudRate defaultBaudRate() const { return m_defaultBaudRate; }
430 
431 private:
432  Create();
433  Create(const Create& rhs);
434  Create& operator=(const Create& rhs);
435 
436  void setLocalBaudRate(const speed_t& baudRate);
437 
438  bool start();
439 
440  bool open();
441  void close();
442 
443  template<typename T>
444  inline void lazyDelete(T *&ptr)
445  {
446  if(ptr) delete ptr;
447  }
448 
449  inline timeval timeOfDay() const
450  {
451 #ifdef _WIN32
452  FILETIME ft;
453  GetSystemTimeAsFileTime(&ft);
454  unsigned long t = (ULONGLONG)ft.dwLowDateTime + ((ULONGLONG)(ft.dwHighDateTime) << 32);
455 
456  timeval tv;
457  tv.tv_usec = t % 1000L;
458  tv.tv_sec = (t - tv.tv_usec) / 1000L;
459  return tv;
460 #else
461  timeval ret;
462  gettimeofday(&ret, NULL);
463  return ret;
464 #endif
465  }
466 
467  inline bool hasRequiredTimePassed(const timeval& timestamp) const
468  {
469  #ifndef WIN32
470  timeval current = timeOfDay();
471  timeval result;
472  timersub(&current, &timestamp, &result);
473  const long msecs = result.tv_sec * 1000 + result.tv_usec / 1000;
474  return msecs > m_refreshRate;
475  #else
476  return false;
477  #endif
478  }
479 
480  inline double timevalToFloat(const timeval& tv)
481  {
482  return ((double)tv.tv_sec) + ((double)tv.tv_usec / 1000000.0);
483  }
484 
485  inline float deg2rad(const float& deg)
486  {
487  return deg / 180.0 * PI;
488  }
489 
490  void updateState();
491 
492  void updateSensorPacket1();
493  void updateSensorPacket2(bool forceUpdate = false);
494  void updateSensorPacket3();
495  void updateSensorPacket4();
496  void updateSensorPacket5();
497 
498  CreateState m_state;
499 
500  unsigned short m_refreshRate;
501 
502  CreatePackets::_1 m_1;
503  CreatePackets::_2 m_2;
504  CreatePackets::_3 m_3;
505  CreatePackets::_4 m_4;
506  CreatePackets::_5 m_5;
507  timeval timestamps[5];
508 
509 
510  // These are all marked mutable because they
511  // are initialized lazily in a const accessor.
512  // I have yet to decide if this is the "correct"
513  // way to go about it.
514  mutable CreateSensors::PlayButton *m_playButton;
515  mutable CreateSensors::AdvanceButton *m_advanceButton;
516 
517  mutable CreateSensors::Wall *m_wall;
518  mutable CreateSensors::CliffLeft *m_cliffLeft;
519  mutable CreateSensors::CliffFrontLeft *m_cliffFrontLeft;
520  mutable CreateSensors::CliffFrontRight *m_cliffFrontRight;
521  mutable CreateSensors::CliffRight *m_cliffRight;
522  mutable CreateSensors::VirtualWall *m_virtualWall;
523 
524  mutable CreateSensors::WallSignal *m_wallSignal;
525  mutable CreateSensors::CliffLeftSignal *m_cliffLeftSignal;
526  mutable CreateSensors::CliffFrontLeftSignal *m_cliffFrontLeftSignal;
527  mutable CreateSensors::CliffFrontRightSignal *m_cliffFrontRightSignal;
528  mutable CreateSensors::CliffRightSignal *m_cliffRightSignal;
529  mutable CreateSensors::CargoBayAnalogSignal *m_cargoBayAnalogSignal;
530  mutable CreateSensors::CargoBayDigitalInputs *m_cargoBayDigitalInputs;
531 
532  mutable CreateSensors::IR *m_ir;
533 
534  mutable CreateSensors::ChargingState *m_chargingState;
535  mutable CreateSensors::BatteryTemperature *m_batteryTemperature;
536  mutable CreateSensors::BatteryCharge *m_batteryCharge;
537  mutable CreateSensors::BatteryCapacity *m_batteryCapacity;
538 
539  mutable CreateSensors::Angle *m_angle;
540  mutable CreateSensors::Distance *m_distance;
541 
542  mutable CreateSensors::BumpLeft *m_bumpLeft;
543  mutable CreateSensors::BumpRight *m_bumpRight;
544 
545  mutable CreateSensors::WheelDropLeft *m_wheelDropLeft;
546  mutable CreateSensors::WheelDropRight *m_wheelDropRight;
547  mutable CreateSensors::WheelDropCaster *m_wheelDropCaster;
548 
549  size_t m_i;
550  CreateScript m_script;
551  int m_tty;
552 #ifndef WIN32
553  pthread_mutex_t m_mutex;
554 #endif
555 
556  BaudRate m_defaultBaudRate;
557 };
558 
559 #endif
short rightVelocity
Definition: create.hpp:94
unsigned char numberOfStreamPackets
Definition: create.hpp:166
Definition: create.hpp:122
unsigned char batteryCapacity[2]
Definition: create.hpp:140
void endAtomicOperation()
Definition: create.hpp:421
unsigned char cliffFrontLeft
Definition: create.hpp:111
Definition: create.hpp:174
char leftVelocity[2]
Definition: create.hpp:170
unsigned char chargingSourcesAvailable
Definition: create.hpp:155
int distance
Definition: create.hpp:91
unsigned char userDigitalInputs
Definition: create.hpp:153
unsigned char cargoBayDigitalInputs
Definition: create.hpp:115
Definition: create.hpp:87
unsigned char buttons
Definition: create.hpp:125
short leftVelocity
Definition: create.hpp:95
Mode
Definition: create.hpp:221
Definition: create.hpp:161
Classes for manipulating and using buttons.
unsigned char lowSideDriverAndWheelOvercurrents
Definition: create.hpp:116
unsigned char cliffRight
Definition: create.hpp:113
unsigned char ir
Definition: create.hpp:124
unsigned char userAnalogInput[2]
Definition: create.hpp:154
Base sensor classes.
void beginAtomicOperation()
Definition: create.hpp:414
char rightVelocity[2]
Definition: create.hpp:169
unsigned char mode
Definition: create.hpp:163
unsigned char voltage[2]
Definition: create.hpp:136
char radius[2]
Definition: create.hpp:168
#define PI
Definition: create.hpp:55
Definition: create.hpp:61
unsigned char cliffFrontRightSignal[2]
Definition: create.hpp:151
short radius
Definition: create.hpp:93
unsigned char cliffLeftSignal[2]
Definition: create.hpp:149
Facilitates communication with the iRobot (R) Create (TM)
Definition: create.hpp:212
unsigned char cliffLeft
Definition: create.hpp:110
unsigned char bumpsAndWheelDrops
Definition: create.hpp:108
unsigned char cliffRightSignal[2]
Definition: create.hpp:152
unsigned char batteryCharge[2]
Definition: create.hpp:139
BaudRate
Definition: create.hpp:239
unsigned char cliffFrontLeftSignal[2]
Definition: create.hpp:150
Definition: create.hpp:133
timeval timestamp
Definition: create.hpp:89
int angle
Definition: create.hpp:92
Definition: create.hpp:222
Definition: create.h:411
unsigned char songPlaying
Definition: create.hpp:165
unsigned char chargingState
Definition: create.hpp:135
char velocity[2]
Definition: create.hpp:167
Definition: create.hpp:223
#define EXPORT_SYM
Definition: export.h:7
char batteryTemperature
Definition: create.hpp:138
void setDefaultBaudRate(const BaudRate defaultBaudRate)
Definition: create.hpp:428
Definition: create.hpp:106
Definition: create.h:410
unsigned char angle[2]
Definition: create.hpp:127
unsigned char virtualWall
Definition: create.hpp:114
unsigned char distance[2]
Definition: create.hpp:126
unsigned char cliffFrontRight
Definition: create.hpp:112
unsigned char wall
Definition: create.hpp:109
Definition: create.hpp:146
The base class for all buttons.
Definition: button.hpp:43
char current[2]
Definition: create.hpp:137
unsigned char songNumber
Definition: create.hpp:164
BaudRate defaultBaudRate() const
Definition: create.hpp:429
unsigned char wallSignal[2]
Definition: create.hpp:148
bool blockingRead(T &data, unsigned timeout=0)
Definition: create.hpp:311
Definition: create.hpp:101
BaudRate
Definition: create.h:408
Definition: create.hpp:224