uyvli

Dependents:   theRobot3

Fork of PololuQik2 by Thomas Ashworth

Committer:
Fairy_Paolina
Date:
Sat Apr 12 20:25:58 2014 +0000
Revision:
4:33ca85c52dc0
Parent:
3:01272a06e922
modified;

Who changed what in which revision?

UserRevisionLine numberNew 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 */
Fairy_Paolina 4:33ca85c52dc0 51 #define MOTOR0CURRENTPACKET 0x90
Fairy_Paolina 4:33ca85c52dc0 52 #define MOTOR1CURRENTPACKET 0x91
Fairy_Paolina 4:33ca85c52dc0 53
srsmitherman 0:511d65ef1276 54
srsmitherman 0:511d65ef1276 55 #define DATAOVERRUNERRORBIT 3 /**< The bit which signifies a data over run error */
srsmitherman 0:511d65ef1276 56 #define FRAMEERRORBIT 4 /**< The bit which signifies a frame error */
srsmitherman 0:511d65ef1276 57 #define CRCERRORBIT 5 /**< The bit which signifies CRC error */
srsmitherman 0:511d65ef1276 58 #define FORMATERRORBIT 6 /**< The bit which signifies a format error */
srsmitherman 0:511d65ef1276 59 #define TIMEOUTERRORBIT 7 /**< The bit which signifies time out error */
srsmitherman 0:511d65ef1276 60
srsmitherman 0:511d65ef1276 61 /**
srsmitherman 0:511d65ef1276 62 * This is the driver for the motor controller. This is to control a Qik2s9v1.
srsmitherman 0:511d65ef1276 63 * It will not control multiple motor and it does not support daisy chaining motor controllers.
srsmitherman 0:511d65ef1276 64 *
srsmitherman 0:511d65ef1276 65 * The specification for the motor controller can be found http://www.pololu.com/catalog/product/1110/
srsmitherman 0:511d65ef1276 66 *
srsmitherman 0:511d65ef1276 67 * This motor controller is based on the work of Mr Fialkowski.
srsmitherman 0:511d65ef1276 68 * This motor controller requires the newSoftSerial library be installed on the build path.
srsmitherman 0:511d65ef1276 69 * This motor controller also requires the CRC7 class.
srsmitherman 0:511d65ef1276 70 *
srsmitherman 0:511d65ef1276 71 * @see CRC7
srsmitherman 0:511d65ef1276 72 */
srsmitherman 0:511d65ef1276 73 class PololuQik2 {
srsmitherman 0:511d65ef1276 74 public:
srsmitherman 0:511d65ef1276 75
srsmitherman 0:511d65ef1276 76 /**
srsmitherman 0:511d65ef1276 77 * The parameterised constructor for the pololuQik2 motor controller.
srsmitherman 0:511d65ef1276 78 *
srsmitherman 0:511d65ef1276 79 * @attention It is important to call the begin() method as this initialises the controller.
srsmitherman 0:511d65ef1276 80 * @see begin()
srsmitherman 0:511d65ef1276 81 *
srsmitherman 0:511d65ef1276 82 * @param txPin the pin on the controller to use for transmitting serial bytes.
srsmitherman 0:511d65ef1276 83 * @param rxPin the pin on the controller to use for recieving serial bytes.
srsmitherman 0:511d65ef1276 84 * @param errPin the digital output pin to use to reset the motor controller.
srsmitherman 0:511d65ef1276 85 * @param EnableCRC true if CRC should be used when communicating with the motor controller.
srsmitherman 0:511d65ef1276 86 * @param reset the pin on the controller to use to reset the motor controller.
srsmitherman 0:511d65ef1276 87 */
srsmitherman 0:511d65ef1276 88 PololuQik2::PololuQik2(PinName TxPin, PinName RxPin, PinName RSTPin, PinName errPin, void (*errorFunction)(void), bool enCRC);
srsmitherman 0:511d65ef1276 89
Fairy_Paolina 4:33ca85c52dc0 90 // get current
Fairy_Paolina 4:33ca85c52dc0 91 uint8_t PololuQik2::GetMotor0Current();
Fairy_Paolina 4:33ca85c52dc0 92 uint8_t PololuQik2::GetMotor1Current();
Fairy_Paolina 4:33ca85c52dc0 93 //
srsmitherman 0:511d65ef1276 94 /**
srsmitherman 0:511d65ef1276 95 * This method initialises and begins communication with the motor controller.
srsmitherman 0:511d65ef1276 96 * This method is also called when an error is exhibited.
srsmitherman 0:511d65ef1276 97 *
srsmitherman 0:511d65ef1276 98 * @see PololuQik2()
srsmitherman 0:511d65ef1276 99 */
srsmitherman 0:511d65ef1276 100 void begin();
srsmitherman 0:511d65ef1276 101
srsmitherman 0:511d65ef1276 102 /**
srsmitherman 0:511d65ef1276 103 * sets the speed of motor 0. This command has superseded motor0Forwards and motor0Reverse.
srsmitherman 0:511d65ef1276 104 * If the speed is less than 0 then the motor will reverse.
srsmitherman 0:511d65ef1276 105 * If the speed is greater than 0 then the motor will run forward
srsmitherman 0:511d65ef1276 106 * If the speed is 0 then the motor will be stopped.
srsmitherman 0:511d65ef1276 107 *
srsmitherman 0:511d65ef1276 108 * The bound for the speed are -127 to 127.
srsmitherman 0:511d65ef1276 109 * @since 1.2
srsmitherman 0:511d65ef1276 110 * @param speed the speed of the motor. If this value is negative the motor will reverse to the
srsmitherman 0:511d65ef1276 111 * appropriate speed.
srsmitherman 0:511d65ef1276 112 */
srsmitherman 0:511d65ef1276 113 void setMotor0Speed(int8_t speed);
srsmitherman 0:511d65ef1276 114
srsmitherman 0:511d65ef1276 115 /**
srsmitherman 0:511d65ef1276 116 * sets the speed of motor 1. This command has superseded motor1Forwards and motor1Reverse.
srsmitherman 0:511d65ef1276 117 * If the speed is less than 0 then the motor will reverse.
srsmitherman 0:511d65ef1276 118 * If the speed is greater than 0 then the motor will run forward
srsmitherman 0:511d65ef1276 119 * If the speed is 0 then the motor will be stopped.
srsmitherman 0:511d65ef1276 120 *
srsmitherman 0:511d65ef1276 121 * The bound for the speed are -127 to 127.
srsmitherman 0:511d65ef1276 122 * @since 1.2
srsmitherman 0:511d65ef1276 123 * @param speed the speed of the motor. If this value is negative the motor will reverse to the
srsmitherman 0:511d65ef1276 124 * appropriate speed.
srsmitherman 0:511d65ef1276 125 */
srsmitherman 0:511d65ef1276 126 void setMotor1Speed(int8_t speed);
srsmitherman 0:511d65ef1276 127
srsmitherman 0:511d65ef1276 128 /**
srsmitherman 0:511d65ef1276 129 * instructs the motor controller to halt both motors.
srsmitherman 0:511d65ef1276 130 * This is equivalent to called any of the forward or reverse methods
srsmitherman 0:511d65ef1276 131 * with the speed as zero (0).
srsmitherman 0:511d65ef1276 132 */
jjcarr2 2:25c41766d768 133 void stopBothMotors(int8_t speed);
srsmitherman 0:511d65ef1276 134
srsmitherman 0:511d65ef1276 135 /**
srsmitherman 0:511d65ef1276 136 * retrieves the firmware version from the motor controller. This will return one of two values.
srsmitherman 0:511d65ef1276 137 * Either the ASCII representation of 1 or the ASCII representation of 2.
srsmitherman 0:511d65ef1276 138 *
srsmitherman 0:511d65ef1276 139 * The return from this method will be either 49 or 50. If otherwise there is an error.
srsmitherman 0:511d65ef1276 140 *
srsmitherman 0:511d65ef1276 141 * @return the firmware version of the motor controller.
srsmitherman 0:511d65ef1276 142 */
srsmitherman 0:511d65ef1276 143 uint8_t getFirmwareVersion();
srsmitherman 0:511d65ef1276 144
srsmitherman 0:511d65ef1276 145 /**
srsmitherman 0:511d65ef1276 146 * Checks if the motor controller has a Data over run error
srsmitherman 0:511d65ef1276 147 *
srsmitherman 0:511d65ef1276 148 * returns true if it has a data over run error
srsmitherman 0:511d65ef1276 149 */
srsmitherman 0:511d65ef1276 150 bool hasDataOverrunError();
srsmitherman 0:511d65ef1276 151
srsmitherman 0:511d65ef1276 152 /**
srsmitherman 0:511d65ef1276 153 * Checks if the motor controller has a Frame error
srsmitherman 0:511d65ef1276 154 *
srsmitherman 0:511d65ef1276 155 * returns true if it has a Frame error
srsmitherman 0:511d65ef1276 156 */
srsmitherman 0:511d65ef1276 157 bool hasFrameError();
srsmitherman 0:511d65ef1276 158
srsmitherman 0:511d65ef1276 159 /**
srsmitherman 0:511d65ef1276 160 * Checks if the motor controller has a CRC error
srsmitherman 0:511d65ef1276 161 *
srsmitherman 0:511d65ef1276 162 * returns true if it has a CRC error
srsmitherman 0:511d65ef1276 163 */
srsmitherman 0:511d65ef1276 164 bool hasCRCError();
srsmitherman 0:511d65ef1276 165
srsmitherman 0:511d65ef1276 166 /**
srsmitherman 0:511d65ef1276 167 * Checks if the motor controller has a Format error
srsmitherman 0:511d65ef1276 168 *
srsmitherman 0:511d65ef1276 169 * returns true if it has a Format error
srsmitherman 0:511d65ef1276 170 */
srsmitherman 0:511d65ef1276 171 bool hasFormatError();
srsmitherman 0:511d65ef1276 172
srsmitherman 0:511d65ef1276 173 /**
srsmitherman 0:511d65ef1276 174 * Checks if the motor controller has a time out error
srsmitherman 0:511d65ef1276 175 *
srsmitherman 0:511d65ef1276 176 * returns true if it has a time out error
srsmitherman 0:511d65ef1276 177 */
srsmitherman 0:511d65ef1276 178 bool hasTimeoutError();
tashworth 3:01272a06e922 179
tashworth 3:01272a06e922 180 /**
tashworth 3:01272a06e922 181 * retrieves the error value from the motor controller and stores it in the
tashworth 3:01272a06e922 182 * class internal workings.
tashworth 3:01272a06e922 183 *
tashworth 3:01272a06e922 184 * @see errorBitSet()
tashworth 3:01272a06e922 185 * @see error()
tashworth 3:01272a06e922 186 */
tashworth 3:01272a06e922 187 uint8_t getError();
srsmitherman 0:511d65ef1276 188
srsmitherman 0:511d65ef1276 189 private:
srsmitherman 0:511d65ef1276 190
srsmitherman 0:511d65ef1276 191 void errorCall();
srsmitherman 0:511d65ef1276 192
srsmitherman 0:511d65ef1276 193 /**
srsmitherman 0:511d65ef1276 194 * This actually sends the data to the motor controller.
srsmitherman 0:511d65ef1276 195 *
srsmitherman 0:511d65ef1276 196 * @param message[] the message to be sent
srsmitherman 0:511d65ef1276 197 * @param length the length of the message to be sent
srsmitherman 0:511d65ef1276 198 */
srsmitherman 0:511d65ef1276 199 void sendMessage(unsigned char message[], uint8_t length);
srsmitherman 0:511d65ef1276 200
srsmitherman 0:511d65ef1276 201 /**
srsmitherman 0:511d65ef1276 202 * Checks if a specific bit in the error byte is set.
srsmitherman 0:511d65ef1276 203 * @param bitToCheck the bit number to check. Assume that the least significant bit is bit 1.
srsmitherman 0:511d65ef1276 204 * @return true if the bit is set.
srsmitherman 0:511d65ef1276 205 */
srsmitherman 0:511d65ef1276 206 bool errorBitSet(uint8_t bitToCheck);
srsmitherman 0:511d65ef1276 207
srsmitherman 0:511d65ef1276 208 /**
srsmitherman 0:511d65ef1276 209 * retrieves the error value from the motor controller and stores it in the
srsmitherman 0:511d65ef1276 210 * class internal workings.
srsmitherman 0:511d65ef1276 211 *
srsmitherman 0:511d65ef1276 212 * @see errorBitSet()
srsmitherman 0:511d65ef1276 213 * @see error()
srsmitherman 0:511d65ef1276 214 */
tashworth 3:01272a06e922 215 //uint8_t getError();
srsmitherman 0:511d65ef1276 216
srsmitherman 0:511d65ef1276 217 /**
srsmitherman 0:511d65ef1276 218 * sends a single byte to the motor controller
srsmitherman 0:511d65ef1276 219 *
srsmitherman 0:511d65ef1276 220 * @param byte the byte of data to send
srsmitherman 0:511d65ef1276 221 */
srsmitherman 0:511d65ef1276 222 void sendByte(uint8_t byte);
srsmitherman 0:511d65ef1276 223
srsmitherman 0:511d65ef1276 224 /**
srsmitherman 0:511d65ef1276 225 * Reads a byte from the motor controller. This is a blocking call.
srsmitherman 0:511d65ef1276 226 * If the motor controller is not sending anything then the program will
srsmitherman 0:511d65ef1276 227 * halt indefinitely.
srsmitherman 0:511d65ef1276 228 *
srsmitherman 0:511d65ef1276 229 * @return the byte from the motor controller.
srsmitherman 0:511d65ef1276 230 * @bug There is a suspected bug in this method.
srsmitherman 0:511d65ef1276 231 */
srsmitherman 0:511d65ef1276 232 uint8_t readByte();
srsmitherman 0:511d65ef1276 233
srsmitherman 0:511d65ef1276 234 Serial serialConnection; /**< Serial connection to the motor controller */
srsmitherman 0:511d65ef1276 235 DigitalOut resetPin; /**< The digital output on the controller which is connected to the motor controllers reset pin. */
srsmitherman 0:511d65ef1276 236 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 237 uint8_t errByte; /**< A temporary store for the error code received from the motor controller. */
srsmitherman 0:511d65ef1276 238 uint8_t firmwareVersion; /**< The motor controller firmware version */
srsmitherman 0:511d65ef1276 239 bool enableCRC; /**< this value will be true if CRCs are expected by the motor controller. */
srsmitherman 0:511d65ef1276 240 CRC7 CRC; /**< The CRC object used to generate the CRC checksums */
srsmitherman 0:511d65ef1276 241
srsmitherman 0:511d65ef1276 242 };
srsmitherman 0:511d65ef1276 243
srsmitherman 0:511d65ef1276 244 #endif /* POLOLUQIK2_H_ */