Hard brake rather than coast
Fork of PololuQik2 by
PololuQik2.h@0:511d65ef1276, 2014-03-03 (annotated)
- Committer:
- srsmitherman
- Date:
- Mon Mar 03 19:34:13 2014 +0000
- Revision:
- 0:511d65ef1276
- Child:
- 2:25c41766d768
Pololu Qik Motor Controller;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
srsmitherman | 0:511d65ef1276 | 1 | /** |
srsmitherman | 0:511d65ef1276 | 2 | * @file PololuQik2.h |
srsmitherman | 0:511d65ef1276 | 3 | * @author Edward Wilson (edwilson1989@gmail.com) |
srsmitherman | 0:511d65ef1276 | 4 | * @author Eric Fialkowski (eric.fialkowski@gmail.com) |
srsmitherman | 0:511d65ef1276 | 5 | * @date Aug 6, 2010 |
srsmitherman | 0:511d65ef1276 | 6 | * @brief PololuQik2 motor drivers. |
srsmitherman | 0:511d65ef1276 | 7 | * |
srsmitherman | 0:511d65ef1276 | 8 | * @details This class is a derivative work based on work of Mr Fialkowski. |
srsmitherman | 0:511d65ef1276 | 9 | * |
srsmitherman | 0:511d65ef1276 | 10 | * PololuQik2 - basic class to control Pololu's Qik2s9v1 |
srsmitherman | 0:511d65ef1276 | 11 | * motor controller (http://www.pololu.com/catalog/product/1110) |
srsmitherman | 0:511d65ef1276 | 12 | * |
srsmitherman | 0:511d65ef1276 | 13 | * This uses the default settings for the motor controller and the |
srsmitherman | 0:511d65ef1276 | 14 | * Compact Protocol to communicate to it. |
srsmitherman | 0:511d65ef1276 | 15 | * |
srsmitherman | 0:511d65ef1276 | 16 | * This library is free software; you can redistribute it and/or |
srsmitherman | 0:511d65ef1276 | 17 | * modify it under the terms of the GNU Lesser General Public |
srsmitherman | 0:511d65ef1276 | 18 | * License as published by the Free Software Foundation; either |
srsmitherman | 0:511d65ef1276 | 19 | * version 2.1 of the License, or (at your option) any later version. |
srsmitherman | 0:511d65ef1276 | 20 | * |
srsmitherman | 0:511d65ef1276 | 21 | * This library is distributed in the hope that it will be useful, |
srsmitherman | 0:511d65ef1276 | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
srsmitherman | 0:511d65ef1276 | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
srsmitherman | 0:511d65ef1276 | 24 | * Lesser General Public License for more details. |
srsmitherman | 0:511d65ef1276 | 25 | * |
srsmitherman | 0:511d65ef1276 | 26 | * You should have received a copy of the GNU Lesser General Public |
srsmitherman | 0:511d65ef1276 | 27 | * License along with this library; if not, write to the Free Software |
srsmitherman | 0:511d65ef1276 | 28 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
srsmitherman | 0:511d65ef1276 | 29 | * |
srsmitherman | 0:511d65ef1276 | 30 | * @version 1.0 August 6, 2010 Initial code |
srsmitherman | 0:511d65ef1276 | 31 | * @version 1.2 December 28, 2010 Composition constructor added |
srsmitherman | 0:511d65ef1276 | 32 | * @version 1.3 April 22, 2011 Ported for MBED |
srsmitherman | 0:511d65ef1276 | 33 | */ |
srsmitherman | 0:511d65ef1276 | 34 | |
srsmitherman | 0:511d65ef1276 | 35 | #ifndef POLOLUQIK2_H_ |
srsmitherman | 0:511d65ef1276 | 36 | #define POLOLUQIK2_H_ |
srsmitherman | 0:511d65ef1276 | 37 | |
srsmitherman | 0:511d65ef1276 | 38 | #include "CRC7.h" |
srsmitherman | 0:511d65ef1276 | 39 | #include "mbed.h" |
srsmitherman | 0:511d65ef1276 | 40 | #include <inttypes.h> |
srsmitherman | 0:511d65ef1276 | 41 | |
srsmitherman | 0:511d65ef1276 | 42 | #define INITIALPACKET 0xAA /**< The packet used to initialise the motor controller*/ |
srsmitherman | 0:511d65ef1276 | 43 | #define MOTOR0FORWARDPACKET 0x88 /**< The packet used to set motor 0 forward */ |
srsmitherman | 0:511d65ef1276 | 44 | #define MOTOR1FORWARDPACKET 0x8C /**< The packet used to set motor 1 forward */ |
srsmitherman | 0:511d65ef1276 | 45 | #define MOTOR0REVERSEPACKET 0x8A /**< The packet used to set motor 0 in reverse */ |
srsmitherman | 0:511d65ef1276 | 46 | #define MOTOR1REVERSEPACKET 0x8E /**< The packet used to set motor 1 in reverse */ |
srsmitherman | 0:511d65ef1276 | 47 | #define MOTOR0COASTPACKET 0x86 /**< The packet used to all motor 0 to coast */ |
srsmitherman | 0:511d65ef1276 | 48 | #define MOTOR1COASTPACKET 0x87 /**< The packet used to all motor 1 to coast */ |
srsmitherman | 0:511d65ef1276 | 49 | #define FWVERSIONPACKET 0x81 /**< The packet to query the firmware version of the motor controller */ |
srsmitherman | 0:511d65ef1276 | 50 | #define ERRORPACKET 0x82 /**< The packet to request the error state from the motor controller */ |
srsmitherman | 0:511d65ef1276 | 51 | |
srsmitherman | 0:511d65ef1276 | 52 | #define DATAOVERRUNERRORBIT 3 /**< The bit which signifies a data over run error */ |
srsmitherman | 0:511d65ef1276 | 53 | #define FRAMEERRORBIT 4 /**< The bit which signifies a frame error */ |
srsmitherman | 0:511d65ef1276 | 54 | #define CRCERRORBIT 5 /**< The bit which signifies CRC error */ |
srsmitherman | 0:511d65ef1276 | 55 | #define FORMATERRORBIT 6 /**< The bit which signifies a format error */ |
srsmitherman | 0:511d65ef1276 | 56 | #define TIMEOUTERRORBIT 7 /**< The bit which signifies time out error */ |
srsmitherman | 0:511d65ef1276 | 57 | |
srsmitherman | 0:511d65ef1276 | 58 | /** |
srsmitherman | 0:511d65ef1276 | 59 | * This is the driver for the motor controller. This is to control a Qik2s9v1. |
srsmitherman | 0:511d65ef1276 | 60 | * It will not control multiple motor and it does not support daisy chaining motor controllers. |
srsmitherman | 0:511d65ef1276 | 61 | * |
srsmitherman | 0:511d65ef1276 | 62 | * The specification for the motor controller can be found http://www.pololu.com/catalog/product/1110/ |
srsmitherman | 0:511d65ef1276 | 63 | * |
srsmitherman | 0:511d65ef1276 | 64 | * This motor controller is based on the work of Mr Fialkowski. |
srsmitherman | 0:511d65ef1276 | 65 | * This motor controller requires the newSoftSerial library be installed on the build path. |
srsmitherman | 0:511d65ef1276 | 66 | * This motor controller also requires the CRC7 class. |
srsmitherman | 0:511d65ef1276 | 67 | * |
srsmitherman | 0:511d65ef1276 | 68 | * @see CRC7 |
srsmitherman | 0:511d65ef1276 | 69 | */ |
srsmitherman | 0:511d65ef1276 | 70 | class PololuQik2 { |
srsmitherman | 0:511d65ef1276 | 71 | public: |
srsmitherman | 0:511d65ef1276 | 72 | |
srsmitherman | 0:511d65ef1276 | 73 | /** |
srsmitherman | 0:511d65ef1276 | 74 | * The parameterised constructor for the pololuQik2 motor controller. |
srsmitherman | 0:511d65ef1276 | 75 | * |
srsmitherman | 0:511d65ef1276 | 76 | * @attention It is important to call the begin() method as this initialises the controller. |
srsmitherman | 0:511d65ef1276 | 77 | * @see begin() |
srsmitherman | 0:511d65ef1276 | 78 | * |
srsmitherman | 0:511d65ef1276 | 79 | * @param txPin the pin on the controller to use for transmitting serial bytes. |
srsmitherman | 0:511d65ef1276 | 80 | * @param rxPin the pin on the controller to use for recieving serial bytes. |
srsmitherman | 0:511d65ef1276 | 81 | * @param errPin the digital output pin to use to reset the motor controller. |
srsmitherman | 0:511d65ef1276 | 82 | * @param EnableCRC true if CRC should be used when communicating with the motor controller. |
srsmitherman | 0:511d65ef1276 | 83 | * @param reset the pin on the controller to use to reset the motor controller. |
srsmitherman | 0:511d65ef1276 | 84 | */ |
srsmitherman | 0:511d65ef1276 | 85 | PololuQik2::PololuQik2(PinName TxPin, PinName RxPin, PinName RSTPin, PinName errPin, void (*errorFunction)(void), bool enCRC); |
srsmitherman | 0:511d65ef1276 | 86 | |
srsmitherman | 0:511d65ef1276 | 87 | /** |
srsmitherman | 0:511d65ef1276 | 88 | * This method initialises and begins communication with the motor controller. |
srsmitherman | 0:511d65ef1276 | 89 | * This method is also called when an error is exhibited. |
srsmitherman | 0:511d65ef1276 | 90 | * |
srsmitherman | 0:511d65ef1276 | 91 | * @see PololuQik2() |
srsmitherman | 0:511d65ef1276 | 92 | */ |
srsmitherman | 0:511d65ef1276 | 93 | void begin(); |
srsmitherman | 0:511d65ef1276 | 94 | |
srsmitherman | 0:511d65ef1276 | 95 | /** |
srsmitherman | 0:511d65ef1276 | 96 | * sets the speed of motor 0. This command has superseded motor0Forwards and motor0Reverse. |
srsmitherman | 0:511d65ef1276 | 97 | * If the speed is less than 0 then the motor will reverse. |
srsmitherman | 0:511d65ef1276 | 98 | * If the speed is greater than 0 then the motor will run forward |
srsmitherman | 0:511d65ef1276 | 99 | * If the speed is 0 then the motor will be stopped. |
srsmitherman | 0:511d65ef1276 | 100 | * |
srsmitherman | 0:511d65ef1276 | 101 | * The bound for the speed are -127 to 127. |
srsmitherman | 0:511d65ef1276 | 102 | * @since 1.2 |
srsmitherman | 0:511d65ef1276 | 103 | * @param speed the speed of the motor. If this value is negative the motor will reverse to the |
srsmitherman | 0:511d65ef1276 | 104 | * appropriate speed. |
srsmitherman | 0:511d65ef1276 | 105 | */ |
srsmitherman | 0:511d65ef1276 | 106 | void setMotor0Speed(int8_t speed); |
srsmitherman | 0:511d65ef1276 | 107 | |
srsmitherman | 0:511d65ef1276 | 108 | /** |
srsmitherman | 0:511d65ef1276 | 109 | * sets the speed of motor 1. This command has superseded motor1Forwards and motor1Reverse. |
srsmitherman | 0:511d65ef1276 | 110 | * If the speed is less than 0 then the motor will reverse. |
srsmitherman | 0:511d65ef1276 | 111 | * If the speed is greater than 0 then the motor will run forward |
srsmitherman | 0:511d65ef1276 | 112 | * If the speed is 0 then the motor will be stopped. |
srsmitherman | 0:511d65ef1276 | 113 | * |
srsmitherman | 0:511d65ef1276 | 114 | * The bound for the speed are -127 to 127. |
srsmitherman | 0:511d65ef1276 | 115 | * @since 1.2 |
srsmitherman | 0:511d65ef1276 | 116 | * @param speed the speed of the motor. If this value is negative the motor will reverse to the |
srsmitherman | 0:511d65ef1276 | 117 | * appropriate speed. |
srsmitherman | 0:511d65ef1276 | 118 | */ |
srsmitherman | 0:511d65ef1276 | 119 | void setMotor1Speed(int8_t speed); |
srsmitherman | 0:511d65ef1276 | 120 | |
srsmitherman | 0:511d65ef1276 | 121 | /** |
srsmitherman | 0:511d65ef1276 | 122 | * instructs the motor controller to halt both motors. |
srsmitherman | 0:511d65ef1276 | 123 | * This is equivalent to called any of the forward or reverse methods |
srsmitherman | 0:511d65ef1276 | 124 | * with the speed as zero (0). |
srsmitherman | 0:511d65ef1276 | 125 | */ |
srsmitherman | 0:511d65ef1276 | 126 | void stopBothMotors(); |
srsmitherman | 0:511d65ef1276 | 127 | |
srsmitherman | 0:511d65ef1276 | 128 | /** |
srsmitherman | 0:511d65ef1276 | 129 | * retrieves the firmware version from the motor controller. This will return one of two values. |
srsmitherman | 0:511d65ef1276 | 130 | * Either the ASCII representation of 1 or the ASCII representation of 2. |
srsmitherman | 0:511d65ef1276 | 131 | * |
srsmitherman | 0:511d65ef1276 | 132 | * The return from this method will be either 49 or 50. If otherwise there is an error. |
srsmitherman | 0:511d65ef1276 | 133 | * |
srsmitherman | 0:511d65ef1276 | 134 | * @return the firmware version of the motor controller. |
srsmitherman | 0:511d65ef1276 | 135 | */ |
srsmitherman | 0:511d65ef1276 | 136 | uint8_t getFirmwareVersion(); |
srsmitherman | 0:511d65ef1276 | 137 | |
srsmitherman | 0:511d65ef1276 | 138 | /** |
srsmitherman | 0:511d65ef1276 | 139 | * Checks if the motor controller has a Data over run error |
srsmitherman | 0:511d65ef1276 | 140 | * |
srsmitherman | 0:511d65ef1276 | 141 | * returns true if it has a data over run error |
srsmitherman | 0:511d65ef1276 | 142 | */ |
srsmitherman | 0:511d65ef1276 | 143 | bool hasDataOverrunError(); |
srsmitherman | 0:511d65ef1276 | 144 | |
srsmitherman | 0:511d65ef1276 | 145 | /** |
srsmitherman | 0:511d65ef1276 | 146 | * Checks if the motor controller has a Frame error |
srsmitherman | 0:511d65ef1276 | 147 | * |
srsmitherman | 0:511d65ef1276 | 148 | * returns true if it has a Frame error |
srsmitherman | 0:511d65ef1276 | 149 | */ |
srsmitherman | 0:511d65ef1276 | 150 | bool hasFrameError(); |
srsmitherman | 0:511d65ef1276 | 151 | |
srsmitherman | 0:511d65ef1276 | 152 | /** |
srsmitherman | 0:511d65ef1276 | 153 | * Checks if the motor controller has a CRC error |
srsmitherman | 0:511d65ef1276 | 154 | * |
srsmitherman | 0:511d65ef1276 | 155 | * returns true if it has a CRC error |
srsmitherman | 0:511d65ef1276 | 156 | */ |
srsmitherman | 0:511d65ef1276 | 157 | bool hasCRCError(); |
srsmitherman | 0:511d65ef1276 | 158 | |
srsmitherman | 0:511d65ef1276 | 159 | /** |
srsmitherman | 0:511d65ef1276 | 160 | * Checks if the motor controller has a Format error |
srsmitherman | 0:511d65ef1276 | 161 | * |
srsmitherman | 0:511d65ef1276 | 162 | * returns true if it has a Format error |
srsmitherman | 0:511d65ef1276 | 163 | */ |
srsmitherman | 0:511d65ef1276 | 164 | bool hasFormatError(); |
srsmitherman | 0:511d65ef1276 | 165 | |
srsmitherman | 0:511d65ef1276 | 166 | /** |
srsmitherman | 0:511d65ef1276 | 167 | * Checks if the motor controller has a time out error |
srsmitherman | 0:511d65ef1276 | 168 | * |
srsmitherman | 0:511d65ef1276 | 169 | * returns true if it has a time out error |
srsmitherman | 0:511d65ef1276 | 170 | */ |
srsmitherman | 0:511d65ef1276 | 171 | bool hasTimeoutError(); |
srsmitherman | 0:511d65ef1276 | 172 | |
srsmitherman | 0:511d65ef1276 | 173 | private: |
srsmitherman | 0:511d65ef1276 | 174 | |
srsmitherman | 0:511d65ef1276 | 175 | void errorCall(); |
srsmitherman | 0:511d65ef1276 | 176 | |
srsmitherman | 0:511d65ef1276 | 177 | /** |
srsmitherman | 0:511d65ef1276 | 178 | * This actually sends the data to the motor controller. |
srsmitherman | 0:511d65ef1276 | 179 | * |
srsmitherman | 0:511d65ef1276 | 180 | * @param message[] the message to be sent |
srsmitherman | 0:511d65ef1276 | 181 | * @param length the length of the message to be sent |
srsmitherman | 0:511d65ef1276 | 182 | */ |
srsmitherman | 0:511d65ef1276 | 183 | void sendMessage(unsigned char message[], uint8_t length); |
srsmitherman | 0:511d65ef1276 | 184 | |
srsmitherman | 0:511d65ef1276 | 185 | /** |
srsmitherman | 0:511d65ef1276 | 186 | * Checks if a specific bit in the error byte is set. |
srsmitherman | 0:511d65ef1276 | 187 | * @param bitToCheck the bit number to check. Assume that the least significant bit is bit 1. |
srsmitherman | 0:511d65ef1276 | 188 | * @return true if the bit is set. |
srsmitherman | 0:511d65ef1276 | 189 | */ |
srsmitherman | 0:511d65ef1276 | 190 | bool errorBitSet(uint8_t bitToCheck); |
srsmitherman | 0:511d65ef1276 | 191 | |
srsmitherman | 0:511d65ef1276 | 192 | /** |
srsmitherman | 0:511d65ef1276 | 193 | * retrieves the error value from the motor controller and stores it in the |
srsmitherman | 0:511d65ef1276 | 194 | * class internal workings. |
srsmitherman | 0:511d65ef1276 | 195 | * |
srsmitherman | 0:511d65ef1276 | 196 | * @see errorBitSet() |
srsmitherman | 0:511d65ef1276 | 197 | * @see error() |
srsmitherman | 0:511d65ef1276 | 198 | */ |
srsmitherman | 0:511d65ef1276 | 199 | uint8_t getError(); |
srsmitherman | 0:511d65ef1276 | 200 | |
srsmitherman | 0:511d65ef1276 | 201 | /** |
srsmitherman | 0:511d65ef1276 | 202 | * sends a single byte to the motor controller |
srsmitherman | 0:511d65ef1276 | 203 | * |
srsmitherman | 0:511d65ef1276 | 204 | * @param byte the byte of data to send |
srsmitherman | 0:511d65ef1276 | 205 | */ |
srsmitherman | 0:511d65ef1276 | 206 | void sendByte(uint8_t byte); |
srsmitherman | 0:511d65ef1276 | 207 | |
srsmitherman | 0:511d65ef1276 | 208 | /** |
srsmitherman | 0:511d65ef1276 | 209 | * Reads a byte from the motor controller. This is a blocking call. |
srsmitherman | 0:511d65ef1276 | 210 | * If the motor controller is not sending anything then the program will |
srsmitherman | 0:511d65ef1276 | 211 | * halt indefinitely. |
srsmitherman | 0:511d65ef1276 | 212 | * |
srsmitherman | 0:511d65ef1276 | 213 | * @return the byte from the motor controller. |
srsmitherman | 0:511d65ef1276 | 214 | * @bug There is a suspected bug in this method. |
srsmitherman | 0:511d65ef1276 | 215 | */ |
srsmitherman | 0:511d65ef1276 | 216 | uint8_t readByte(); |
srsmitherman | 0:511d65ef1276 | 217 | |
srsmitherman | 0:511d65ef1276 | 218 | Serial serialConnection; /**< Serial connection to the motor controller */ |
srsmitherman | 0:511d65ef1276 | 219 | DigitalOut resetPin; /**< The digital output on the controller which is connected to the motor controllers reset pin. */ |
srsmitherman | 0:511d65ef1276 | 220 | InterruptIn errorPin; /**< The digital output on the controller which is connected to the motor controllers error pin. This must be an interrrupt pin on the controller */ |
srsmitherman | 0:511d65ef1276 | 221 | uint8_t errByte; /**< A temporary store for the error code received from the motor controller. */ |
srsmitherman | 0:511d65ef1276 | 222 | uint8_t firmwareVersion; /**< The motor controller firmware version */ |
srsmitherman | 0:511d65ef1276 | 223 | bool enableCRC; /**< this value will be true if CRCs are expected by the motor controller. */ |
srsmitherman | 0:511d65ef1276 | 224 | CRC7 CRC; /**< The CRC object used to generate the CRC checksums */ |
srsmitherman | 0:511d65ef1276 | 225 | |
srsmitherman | 0:511d65ef1276 | 226 | }; |
srsmitherman | 0:511d65ef1276 | 227 | |
srsmitherman | 0:511d65ef1276 | 228 | #endif /* POLOLUQIK2_H_ */ |