Roomba robot class

Dependencies:   mbed

Committer:
AjK
Date:
Wed Dec 01 19:20:43 2010 +0000
Revision:
0:b26b94a6a065
0.1 Untested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:b26b94a6a065 1 /*
AjK 0:b26b94a6a065 2 Copyright (c) 2010 Andy Kirkham
AjK 0:b26b94a6a065 3
AjK 0:b26b94a6a065 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:b26b94a6a065 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:b26b94a6a065 6 in the Software without restriction, including without limitation the rights
AjK 0:b26b94a6a065 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:b26b94a6a065 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:b26b94a6a065 9 furnished to do so, subject to the following conditions:
AjK 0:b26b94a6a065 10
AjK 0:b26b94a6a065 11 The above copyright notice and this permission notice shall be included in
AjK 0:b26b94a6a065 12 all copies or substantial portions of the Software.
AjK 0:b26b94a6a065 13
AjK 0:b26b94a6a065 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:b26b94a6a065 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:b26b94a6a065 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:b26b94a6a065 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:b26b94a6a065 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:b26b94a6a065 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:b26b94a6a065 20 THE SOFTWARE.
AjK 0:b26b94a6a065 21
AjK 0:b26b94a6a065 22 @file Roomba.h
AjK 0:b26b94a6a065 23 @purpose http://mbed.org/forum/mbed/topic/1476
AjK 0:b26b94a6a065 24 @version 0.1 (untested)
AjK 0:b26b94a6a065 25 @date Dec 2010
AjK 0:b26b94a6a065 26 @author Andy Kirkham
AjK 0:b26b94a6a065 27 @see http://mbed.org/forum/mbed/topic/1476
AjK 0:b26b94a6a065 28 @require http://mbed.org/cookbook/MODSERIAL
AjK 0:b26b94a6a065 29 */
AjK 0:b26b94a6a065 30
AjK 0:b26b94a6a065 31 #ifndef ROOMBA_H
AjK 0:b26b94a6a065 32 #define ROOMBA_H
AjK 0:b26b94a6a065 33
AjK 0:b26b94a6a065 34 #include "mbed.h"
AjK 0:b26b94a6a065 35 #include "MODSERIAL.h"
AjK 0:b26b94a6a065 36
AjK 0:b26b94a6a065 37 /**
AjK 0:b26b94a6a065 38 * @brief Class efinition of a Roomba robot.
AjK 0:b26b94a6a065 39 * @author Andy Kirkham
AjK 0:b26b94a6a065 40 * @see http://mbed.org/forum/mbed/topic/1476
AjK 0:b26b94a6a065 41 * @see http://mbed.org/cookbook/MODSERIAL
AjK 0:b26b94a6a065 42 *
AjK 0:b26b94a6a065 43 * <b>Roomba</b> manages communications with teh robot.
AjK 0:b26b94a6a065 44 * It exports a simple API to send commands or read
AjK 0:b26b94a6a065 45 * sensor data.
AjK 0:b26b94a6a065 46 *
AjK 0:b26b94a6a065 47 * This module requires MODSERIAL
AjK 0:b26b94a6a065 48 */
AjK 0:b26b94a6a065 49 class Roomba {
AjK 0:b26b94a6a065 50 public:
AjK 0:b26b94a6a065 51
AjK 0:b26b94a6a065 52 // States (and commands)
AjK 0:b26b94a6a065 53 enum RoombaState {
AjK 0:b26b94a6a065 54 Idle = 0
AjK 0:b26b94a6a065 55 , GetSensors
AjK 0:b26b94a6a065 56 , CmdStart = 128
AjK 0:b26b94a6a065 57 , CmdBaud
AjK 0:b26b94a6a065 58 , CmdControl
AjK 0:b26b94a6a065 59 , CmdSafe
AjK 0:b26b94a6a065 60 , CmdFull
AjK 0:b26b94a6a065 61 , CmdPower
AjK 0:b26b94a6a065 62 , CmdSpot
AjK 0:b26b94a6a065 63 , CmdClean
AjK 0:b26b94a6a065 64 , CmdMax
AjK 0:b26b94a6a065 65 , CmdDrive
AjK 0:b26b94a6a065 66 , CmdMotors
AjK 0:b26b94a6a065 67 , CmdLeds
AjK 0:b26b94a6a065 68 , CmdSong
AjK 0:b26b94a6a065 69 , CmdPlay
AjK 0:b26b94a6a065 70 , CmdSensors
AjK 0:b26b94a6a065 71 , CmdForceSeekingDock
AjK 0:b26b94a6a065 72 };
AjK 0:b26b94a6a065 73
AjK 0:b26b94a6a065 74 // Calltypes and return types
AjK 0:b26b94a6a065 75 enum CallType {
AjK 0:b26b94a6a065 76 NotIdle = -1
AjK 0:b26b94a6a065 77 , Ok = 0
AjK 0:b26b94a6a065 78 , Invoke = 1
AjK 0:b26b94a6a065 79 , SerialCallback
AjK 0:b26b94a6a065 80 };
AjK 0:b26b94a6a065 81
AjK 0:b26b94a6a065 82
AjK 0:b26b94a6a065 83 // Indexes into array to select what var
AjK 0:b26b94a6a065 84 enum Sensor8bit {
AjK 0:b26b94a6a065 85 bumpsWheeldrops = 0
AjK 0:b26b94a6a065 86 , wall
AjK 0:b26b94a6a065 87 , cliffLeft
AjK 0:b26b94a6a065 88 , cliffFrontLeft
AjK 0:b26b94a6a065 89 , cliffFrontRight
AjK 0:b26b94a6a065 90 , cliffRight
AjK 0:b26b94a6a065 91 , virtualWall
AjK 0:b26b94a6a065 92 , motorOvercurrents
AjK 0:b26b94a6a065 93 , dirtDetectorLeft
AjK 0:b26b94a6a065 94 , dirtDetectorRight
AjK 0:b26b94a6a065 95 , remoteControlCommand
AjK 0:b26b94a6a065 96 , buttons
AjK 0:b26b94a6a065 97 , chargingState
AjK 0:b26b94a6a065 98 , temperature
AjK 0:b26b94a6a065 99 , NumOf8bitSensors
AjK 0:b26b94a6a065 100 };
AjK 0:b26b94a6a065 101
AjK 0:b26b94a6a065 102 // Indexes into array to select what var
AjK 0:b26b94a6a065 103 enum Sensor16bit {
AjK 0:b26b94a6a065 104 distance
AjK 0:b26b94a6a065 105 , angle
AjK 0:b26b94a6a065 106 , voltage
AjK 0:b26b94a6a065 107 , charge
AjK 0:b26b94a6a065 108 , current
AjK 0:b26b94a6a065 109 , capacity
AjK 0:b26b94a6a065 110 , NumOf16bitSensors
AjK 0:b26b94a6a065 111 };
AjK 0:b26b94a6a065 112
AjK 0:b26b94a6a065 113 Roomba(PinName tx, PinName rx) { init(tx, rx); }
AjK 0:b26b94a6a065 114 ~Roomba() { delete uart; delete tick; }
AjK 0:b26b94a6a065 115
AjK 0:b26b94a6a065 116 int command(RoombaState cmd, int opts = 0, char *opt = (char *)NULL, bool block = true);
AjK 0:b26b94a6a065 117
AjK 0:b26b94a6a065 118 uint8_t sensor(Sensor8bit idx) { return sensors8bit[idx]; }
AjK 0:b26b94a6a065 119 uint16_t sensor(Sensor16bit idx) { return sensors16bit[idx]; }
AjK 0:b26b94a6a065 120
AjK 0:b26b94a6a065 121 // Interrupt callback functions.
AjK 0:b26b94a6a065 122 void cbSerial(void) { cmdSensors(SerialCallback); }
AjK 0:b26b94a6a065 123 void cbTicker(void) { cmdSensors(Invoke); } ;
AjK 0:b26b94a6a065 124
AjK 0:b26b94a6a065 125 protected:
AjK 0:b26b94a6a065 126 int state;
AjK 0:b26b94a6a065 127 MODSERIAL *uart;
AjK 0:b26b94a6a065 128 Ticker *tick;
AjK 0:b26b94a6a065 129
AjK 0:b26b94a6a065 130 // Internal methods
AjK 0:b26b94a6a065 131 void init(PinName tx, PinName rx);
AjK 0:b26b94a6a065 132 int cmdSensors(CallType type);
AjK 0:b26b94a6a065 133
AjK 0:b26b94a6a065 134 // Sensor values. Populated with data
AjK 0:b26b94a6a065 135 // via ticker/serial interrupts. Available
AjK 0:b26b94a6a065 136 // to the user program via the .sensor() API
AjK 0:b26b94a6a065 137 // function.
AjK 0:b26b94a6a065 138 uint8_t sensors8bit[NumOf8bitSensors];
AjK 0:b26b94a6a065 139 uint16_t sensors16bit[NumOf16bitSensors];
AjK 0:b26b94a6a065 140 };
AjK 0:b26b94a6a065 141
AjK 0:b26b94a6a065 142 #endif