00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #ifndef IROBOT_CREATE_HH
00026 # define IROBOT_CREATE_HH
00027 # include <iostream>
00028 # include <queue>
00029 # include <stdexcept>
00030 # include <utility>
00031 # include <vector>
00032
00033 # include <irobot-create-fwd.hh>
00034
00035 namespace iRobot
00036 {
00037
00045 # define IROBOT_MAKE_EXCEPTION(NAME, DESCRIPTION) \
00046 struct NAME : public std::runtime_error \
00047 { \
00048 explicit NAME () \
00049 : std::runtime_error (DESCRIPTION) \
00050 {} \
00051 explicit NAME (const std::string& what) \
00052 : std::runtime_error (what) \
00053 {} \
00054 }
00055
00064 IROBOT_MAKE_EXCEPTION (CommandNotAvailable,
00065 "This command is not available in this mode.");
00066
00069 IROBOT_MAKE_EXCEPTION (InvalidArgument, "This argument is invalid.");
00070
00073 IROBOT_MAKE_EXCEPTION (LibSerialNotAvailable, "Serial port support has not been compiled.");
00074
00075 # undef IROBOT_MAKE_EXCEPTION
00076
00077
00079
00086 class Create
00087 {
00088 public:
00090 typedef std::pair<unsigned char, unsigned char> note_t;
00093 typedef std::vector<note_t> song_t;
00094
00099 enum Mode
00100 {
00103 IROBOT_CREATE_OFF,
00105 IROBOT_CREATE_PASSIVE,
00110 IROBOT_CREATE_SAFE,
00115 IROBOT_CREATE_FULL
00116 };
00117
00119 enum Baud
00120 {
00122 BAUD_300 = 0,
00124 BAUD_600 = 1,
00126 BAUD_1200 = 2,
00128 BAUD_2400 = 3,
00130 BAUD_4800 = 4,
00132 BAUD_9600 = 5,
00134 BAUD_14400 = 6,
00136 BAUD_19200 = 7,
00138 BAUD_28800 = 8,
00140 BAUD_38400 = 9,
00142 BAUD_57600 = 10,
00145 BAUD_115200 = 11
00146 };
00147
00149 enum Demo
00150 {
00154 DEMO_COVER = 0,
00155
00159 DEMO_COVER_AND_DOCK = 1,
00160
00163 DEMO_SPOT_COVER = 2,
00164
00167 DEMO_MOUSE = 3,
00168
00170 DEMO_DRIVE_FIGURE_EIGHT = 4,
00171
00175 DEMO_WIMP = 5,
00176
00181 DEMO_HOME = 6,
00182
00187 DEMO_TAG = 7,
00188
00191 DEMO_PACHELBEL = 8,
00192
00198 DEMO_BANJO = 9,
00199
00201 DEMO_ABORT = 255
00202 };
00203
00205 enum DriveCommand
00206 {
00208 DRIVE_STRAIGHT,
00210 DRIVE_INPLACE_CLOCKWISE,
00212 DRIVE_INPLACE_COUNTERCLOCKWISE
00213 };
00214
00218 enum Led
00219 {
00221 LED_NONE = 0,
00223 LED_PLAY = 2,
00225 LED_ADVANCE = 8,
00227 LED_ALL = LED_PLAY | LED_ADVANCE
00228 };
00229
00231 enum StreamState
00232 {
00234 STREAM_STATE_OFF = 0,
00236 STREAM_STATE_ON = 1
00237 };
00238
00240 enum ChargingState
00241 {
00242 CHARGING_STATE_NOT_CHARGING = 0,
00243 CHARGING_STATE_RECONDITIONING_CHARGING = 1,
00244 CHARGING_STATE_FULL_CHARGING = 2,
00245 CHARGING_STATE_TRICKLE_CHARGING = 3,
00246 CHARGING_STATE_WAITING = 4,
00247 CHARGING_STATE_CHARGING_FAULT_CONDITION = 5
00248
00249 };
00250
00252 enum Event
00253 {
00255 EVENT_WHEEL_DROP = 1,
00257 EVENT_FRONT_WHEEL_DROP = 2,
00259 EVENT_LEFT_WHEEL_DROP = 3,
00261 EVENT_RIGHT_WHEEL_DROP = 4,
00263 EVENT_BUMP = 5,
00265 EVENT_LEFT_BUMP = 6,
00267 EVENT_RIGHT_BUMP = 7,
00269 EVENT_VIRTUAL_WALL = 8,
00271 EVENT_WALL = 9,
00273 EVENT_CLIFF = 10,
00275 EVENT_LEFT_CLIFF = 11,
00277 EVENT_FRONT_LEFT_CLIFF = 12,
00279 EVENT_FRONT_RIGHT_CLIFF = 13,
00281 EVENT_RIGHT_CLIFF = 14,
00283 EVENT_HOME_BASE = 15,
00285 EVENT_ADVANCE_BUTTON = 16,
00287 EVENT_PLAY_BUTTON = 17,
00289 EVENT_DIGITAL_INPUT_0 = 18,
00291 EVENT_DIGITAL_INPUT_1 = 19,
00293 EVENT_DIGITAL_INPUT_2 = 20,
00295 EVENT_DIGITAL_INPUT_3 = 21,
00297 EVENT_OI_MODE_PASSIVE = 22
00298 };
00299
00301 enum EventState
00302 {
00304 EVENT_OCCURRING,
00306 EVENT_NOT_OCCURRING
00307 };
00308
00314 enum Opcode
00315 {
00317 OPCODE_START = 128,
00319 OPCODE_BAUD = 129,
00321 OPCODE_CONTROL = 130,
00323 OPCODE_SAFE = 131,
00325 OPCODE_FULL = 132,
00327 OPCODE_SPOT = 134,
00329 OPCODE_COVER = 135,
00331 OPCODE_DEMO = 136,
00333 OPCODE_DRIVE = 137,
00335 OPCODE_LOW_SIDE_DRIVERS = 138,
00337 OPCODE_LEDS = 139,
00339 OPCODE_SONG = 140,
00341 OPCODE_PLAY = 141,
00343 OPCODE_SENSORS = 142,
00345 OPCODE_COVER_AND_DOCK = 143,
00347 OPCODE_PWM_LOW_SIDE_DRIVERS = 144,
00349 OPCODE_DRIVE_DIRECT = 145,
00351 OPCODE_DIGITAL_OUTPUTS = 147,
00353 OPCODE_STREAM = 148,
00355 OPCODE_QUERY_LIST = 149,
00357 OPCODE_PAUSE_RESUME_STREAM = 150,
00359 OPCODE_SEND_IR = 151,
00361 OPCODE_SCRIPT = 152,
00363 OPCODE_PLAY_SCRIPT = 153,
00365 OPCODE_SHOW_SCRIPT = 154,
00367 OPCODE_WAIT_TIME = 155,
00369 OPCODE_WAIT_DISTANCE = 156,
00371 OPCODE_WAIT_ANGLE = 157,
00373 OPCODE_WAIT_EVENT = 158
00374 };
00375
00377 enum SensorPacket
00378 {
00380 SENSOR_GROUP_0 = 0,
00382 SENSOR_GROUP_1 = 1,
00384 SENSOR_GROUP_2 = 2,
00386 SENSOR_GROUP_3 = 3,
00388 SENSOR_GROUP_4 = 4,
00390 SENSOR_GROUP_5 = 5,
00392 SENSOR_GROUP_6 = 6,
00394 SENSOR_BUMPS_WHEELS_DROPS = 7,
00396 SENSOR_WALL = 8,
00398 SENSOR_CLIFF_LEFT = 9,
00400 SENSOR_CLIFF_FRONT_LEFT = 10,
00402 SENSOR_CLIFF_FRONT_RIGHT = 11,
00404 SENSOR_CLIFF_RIGHT = 12,
00406 SENSOR_VIRTUAL_WALL = 13,
00408 SENSOR_OVERCURRENTS = 14,
00410 SENSOR_IR = 17,
00412 SENSOR_BUTTONS = 18,
00414 SENSOR_DISTANCE = 19,
00416 SENSOR_ANGLE = 20,
00418 SENSOR_CHARGING_STATE = 21,
00420 SENSOR_VOLTAGE = 22,
00422 SENSOR_CURRENT = 23,
00424 SENSOR_BATTERY_TEMPERATURE = 24,
00426 SENSOR_BATTERY_CHARGE = 25,
00428 SENSOR_BATTERY_CAPACITY = 26,
00430 SENSOR_WALL_SIGNAL = 27,
00432 SENSOR_CLIFF_LEFT_SIGNAL = 28,
00434 SENSOR_CLIFF_FRONT_LEFT_SIGNAL = 29,
00436 SENSOR_CLIFF_FRONT_RIGHT_SIGNAL = 30,
00438 SENSOR_CLIFF_RIGHT_SIGNAL = 31,
00440 SENSOR_CARGO_BAY_DIGITAL_INPUT = 32,
00442 SENSOR_CARGO_BAY_ANALOG_SIGNAL = 33,
00444 SENSOR_CHARGING_SOURCES_AVAILABLE = 34,
00446 SENSOR_OI_MODE = 35,
00448 SENSOR_SONG_NUMBER = 36,
00450 SENSOR_SONG_PLAYING = 37,
00452 SENSOR_NUMBER_STREAM_PACKETS = 38,
00454 SENSOR_REQUESTED_VELOCITY = 39,
00456 SENSOR_REQUESTED_RADIUS = 40,
00458 SENSOR_REQUESTED_RIGHT_VELOCITY = 41,
00460 SENSOR_REQUESTED_LEFT_VELOCITY = 42
00461 };
00462
00465 typedef std::vector<unsigned char> opcodes_t;
00466
00470 typedef std::vector<SensorPacket> sensorPackets_t;
00471
00472 typedef std::queue<SensorPacket> queriedPackets_t;
00473
00475 static const unsigned char LED_COLOR_GREEN = 0;
00477 static const unsigned char LED_COLOR_RED = 255;
00478
00480 static const unsigned char LED_INTENSITY_OFF = 0;
00482 static const unsigned char LED_INTENSITY_FULL = 255;
00483
00485 static const unsigned char LOW_SIDE_VELOCITY_MIN = 0;
00486
00488 static const unsigned char LOW_SIDE_VELOCITY_MAX = 128;
00489
00491 static const int VELOCITY_MIN = -500;
00493 static const int VELOCITY_MAX = 500;
00494
00496 static const int RADIUS_MIN = -2000;
00498 static const int RADIUS_MAX = 2000;
00499
00501 static const unsigned char SONG_MIN = 0;
00503 static const unsigned char SONG_MAX = 15;
00504
00506 static const unsigned char SONG_MAX_SIZE = 16;
00507
00509 static const unsigned char NO_NOTE = 30;
00511 static const unsigned char NOTE_MIN = 31;
00513 static const unsigned char NOTE_MAX = 127;
00514
00516 static const unsigned char SCRIPT_MAX_SIZE = 100;
00517
00519 static const unsigned char STREAM_HEADER = 19;
00520
00521
00523 static const unsigned char SENSOR_BIT_WHEELDROP_CASTER = 4;
00525 static const unsigned char SENSOR_BIT_WHEELDROP_LEFT = 3;
00527 static const unsigned char SENSOR_BIT_WHEELDROP_RIGHT = 2;
00529 static const unsigned char SENSOR_BIT_BUMP_LEFT = 1;
00531 static const unsigned char SENSOR_BIT_BUMP_RIGHT = 0;
00532
00534 static const unsigned char SENSOR_BIT_LEFTWHEELOVERCURRENT = 4;
00536 static const unsigned char SENSOR_BIT_RIGHTWHEELOVERCURRENT = 3;
00538 static const unsigned char SENSOR_BIT_LD2OVERCURRENT = 2;
00540 static const unsigned char SENSOR_BIT_LD1OVERCURRENT = 1;
00542 static const unsigned char SENSOR_BIT_LD0OVERCURRENT = 0;
00543
00545 static const unsigned char SENSOR_BIT_ADVANCEBUTTON = 2;
00547 static const unsigned char SENSOR_BIT_PLAYBUTTON = 0;
00548
00550 static const unsigned char SENSOR_BIT_DEVICEDETECT = 4;
00552 static const unsigned char SENSOR_BIT_DIGITALINPUT3 = 3;
00554 static const unsigned char SENSOR_BIT_DIGITALINPUT2 = 2;
00556 static const unsigned char SENSOR_BIT_DIGITALINPUT1 = 1;
00558 static const unsigned char SENSOR_BIT_DIGITALINPUT0 = 0;
00559
00561 static const unsigned char SENSOR_BIT_HOMEBASECHARGERAVAILABLE = 1;
00563 static const unsigned char SENSOR_BIT_INTERNALCHARGERAVAILABLE = 0;
00564
00566
00569 explicit Create (std::iostream& stream) throw (InvalidArgument);
00570
00573 explicit Create (LibSerial::SerialStream& stream)
00574 throw (InvalidArgument, LibSerialNotAvailable);
00575
00577 ~Create () throw ();
00578
00580
00582
00587 void sendBaudCommand (Baud speed)
00588 throw (CommandNotAvailable, InvalidArgument);
00589
00592 void sendStartCommand ();
00593
00595 void sendSafeCommand ()
00596 throw (CommandNotAvailable);
00597
00599 void sendFullCommand ()
00600 throw (CommandNotAvailable);
00601
00604 void sendDemoCommand (Demo demo)
00605 throw (CommandNotAvailable, InvalidArgument);
00606
00611 void sendDriveCommand (short v, short r)
00612 throw (CommandNotAvailable, InvalidArgument);
00613
00617 void sendDriveCommand (short v, DriveCommand dc)
00618 throw (CommandNotAvailable, InvalidArgument);
00619
00623 void sendDriveDirectCommand (short vr, short vl)
00624 throw (CommandNotAvailable, InvalidArgument);
00625
00630 void sendLedCommand (Led l, unsigned char c, unsigned char i)
00631 throw (CommandNotAvailable, InvalidArgument);
00632
00637 void sendDigitalOutputsCommand (bool d1, bool d2, bool d3)
00638 throw (CommandNotAvailable);
00639
00644 void sendPwmLowSideDriversCommand (unsigned char lsd1,
00645 unsigned char lsd2,
00646 unsigned char lsd3)
00647 throw (CommandNotAvailable, InvalidArgument);
00648
00654 void sendLowSideDriversCommand (bool lsd1, bool lsd2, bool lsd3)
00655 throw (CommandNotAvailable);
00656
00660 void sendIrCommand (unsigned char v)
00661 throw (CommandNotAvailable);
00662
00666 void sendSongCommand (unsigned char sid, const song_t& song)
00667 throw (CommandNotAvailable, InvalidArgument);
00668
00671 void sendPlaySongCommand (unsigned char sid)
00672 throw (CommandNotAvailable, InvalidArgument);
00673
00676 void sendSensorsCommand (SensorPacket sp)
00677 throw (CommandNotAvailable, InvalidArgument);
00678
00681 void sendQueryListCommand (const sensorPackets_t& lsp)
00682 throw (CommandNotAvailable, InvalidArgument);
00683
00688 void sendStreamCommand (const sensorPackets_t& lsp)
00689 throw (CommandNotAvailable, InvalidArgument);
00690
00693 void sendPauseStreamCommand (StreamState st)
00694 throw (CommandNotAvailable, InvalidArgument);
00695
00698 void sendScriptCommand (const opcodes_t& script)
00699 throw (CommandNotAvailable, InvalidArgument);
00700
00702 void sendPlayScriptCommand ()
00703 throw (CommandNotAvailable);
00704
00706 void sendShowScriptCommand ()
00707 throw (CommandNotAvailable);
00708
00711 void sendWaitTimeCommand (unsigned char t)
00712 throw (CommandNotAvailable);
00713
00716 void sendWaitDistanceCommand (short d)
00717 throw (CommandNotAvailable);
00718
00721 void sendWaitAngleCommand (short a)
00722 throw (CommandNotAvailable);
00723
00727 void sendWaitEventCommand (Event e, EventState es = EVENT_OCCURRING)
00728 throw (CommandNotAvailable, InvalidArgument);
00730
00732
00736 void updateSensors ();
00737
00739 void readStream ();
00740
00742
00744
00745 Mode mode () throw ();
00746
00747 bool wheeldropCaster ();
00748 bool wheeldropLeft ();
00749 bool wheeldropRight ();
00750 bool bumpLeft ();
00751 bool bumpRight ();
00752
00754 bool wall ();
00755
00756 bool cliffLeft ();
00757 bool cliffFrontLeft ();
00758 bool cliffFrontRight ();
00759 bool cliffRight ();
00760 bool deviceDetect ();
00761 bool digitalInput3 ();
00762 bool digitalInput2 ();
00763 bool digitalInput1 ();
00764 bool digitalInput0 ();
00765 short analogSignal ();
00766 bool homeBaseChargerAvailable ();
00767 bool internalChargerAvailable ();
00768 bool virtualWall ();
00769 bool leftWheelOvercurrent ();
00770 bool rightWheelOvercurrent ();
00771 bool ld2Overcurrent ();
00772 bool ld1Overcurrent ();
00773 bool ld0Overcurrent ();
00774 unsigned char ir ();
00775 bool advanceButton ();
00776 bool playButton ();
00777 short distance ();
00778 short angle ();
00779 ChargingState chargingState ();
00780 short batteryVoltage ();
00781 short batteryCurrent ();
00782 short batteryTemperature ();
00783 short batteryCharge ();
00784 short batteryCapacity ();
00785 short wallSignal ();
00786 short cliffLeftSignal ();
00787 short cliffFrontLeftSignal ();
00788 short cliffFrontRightSignal ();
00789 short cliffRightSignal ();
00790 unsigned char songNumber ();
00791 bool songPlaying ();
00792 unsigned char streamPackets ();
00793 short requestedVelocity ();
00794 short requestedRadius ();
00795 short requestedLeftVelocity ();
00796 short requestedRightVelocity ();
00797
00799
00800 protected:
00803 bool readSensorPacket ();
00804
00806 bool readSensorPacket (SensorPacket, std::istream&);
00807
00808 private:
00810 void init () throw (InvalidArgument);
00811
00813 Mode currentMode_;
00814
00816 std::iostream& stream_;
00817
00819
00821 sensorPackets_t streamedSensors_;
00822
00825 queriedPackets_t queriedSensors_;
00826
00828 bool wheeldropCaster_;
00830 bool wheeldropLeft_;
00832 bool wheeldropRight_;
00834 bool bumpLeft_;
00836 bool bumpRight_;
00838 bool wall_;
00840 bool cliffLeft_;
00842 bool cliffFrontLeft_;
00844 bool cliffFrontRight_;
00846 bool cliffRight_;
00848 bool deviceDetect_;
00850 bool digitalInput3_;
00852 bool digitalInput2_;
00854 bool digitalInput1_;
00856 bool digitalInput0_;
00858 short analogSignal_;
00860 bool homeBaseChargerAvailable_;
00862 bool internalChargerAvailable_;
00864 bool virtualWall_;
00866 bool leftWheelOvercurrent_;
00868 bool rightWheelOvercurrent_;
00870 bool ld2Overcurrent_;
00872 bool ld1Overcurrent_;
00874 bool ld0Overcurrent_;
00876 unsigned char ir_;
00878 bool advanceButton_;
00880 bool playButton_;
00882 short distance_;
00884 short angle_;
00886 ChargingState chargingState_;
00888 short batteryVoltage_;
00890 short batteryCurrent_;
00892 short batteryTemperature_;
00894 short batteryCharge_;
00896 short batteryCapacity_;
00898 short wallSignal_;
00900 short cliffLeftSignal_;
00902 short cliffFrontLeftSignal_;
00904 short cliffFrontRightSignal_;
00906 short cliffRightSignal_;
00908 unsigned char songNumber_;
00910 bool songPlaying_;
00912 unsigned char streamPackets_;
00914 short requestedVelocity_;
00916 short requestedRadius_;
00918 short requestedLeftVelocity_;
00920 short requestedRightVelocity_;
00921
00923 };
00924
00925 }
00926
00927 #endif