Thomas Jørgensen / MD25
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MD25.h Source File

MD25.h

00001 /********************************************************************
00002  * @filename    MD25.h
00003  * @author      Thomas B. Joergensen (thomas.bal.jorgensen@gmail.com)
00004  * @date        06 FEB 2012
00005  * @version     1.0
00006  * @target      mbed (NXP LPC1768 - ARM Cortex M3 - 32-bit)
00007  *
00008  * @desciption  A library for interacting with the MD25 DC motor
00009  *              controller. Find more at:
00010  *              http://www.robot-electronics.co.uk/htm/md25tech.htm
00011  *              http://www.robot-electronics.co.uk/htm/md25i2c.htm
00012  *******************************************************************/
00013 
00014 /* Includes */
00015 #include "mbed.h"
00016 
00017 /* REGISTERS */
00018 #define REG_SPEED1                  0x00    // R/W  | Motor1 speed (mode 0,1) or speed (mode 2,3)
00019 #define REG_SPEED2                  0x01    // R/W  | Motor2 speed (mode 0,1) or turn (mode 2,3) 
00020 #define REG_ENC1A                   0x02    // R    | Encoder 1 position, 1st byte (highest), capture count when read
00021 #define REG_ENC1B                   0x03    // R    | Encoder 1 position, 2nd byte
00022 #define REG_ENC1C                   0x04    // R    | Encoder 1 position, 3rd byte
00023 #define REG_ENC1D                   0x05    // R    | Encoder 1 position, 4th (lowest byte)
00024 #define REG_ENC2A                   0x06    // R    | Encoder 2 position, 1st  byte (highest), capture count when read
00025 #define REG_ENC2B                   0x07    // R    | Encoder 2 position, 2nd byte
00026 #define REG_ENC2C                   0x08    // R    | Encoder 2 position, 3rd byte
00027 #define REG_ENC2D                   0x09    // R    | Encoder 2 position, 4th byte (lowest byte) 
00028 #define REG_BATTERY_VOLTS           0x0A    // R    | The supply battery voltage
00029 #define REG_MOTOR1_CURRENT          0x0B    // R    | The current through motor 1
00030 #define REG_MOTOR2_CURRENT          0x0C    // R    | The current through motor 2
00031 #define REG_SOFTWARE_REVISION       0x0D    // R    | Software Revision Number
00032 #define REG_ACCELERATION_RATE       0x0E    // R/W  | Optional Acceleration register 
00033 #define REG_MODE                    0x0F    // R/W  | Mode of operation (see below)
00034 #define REG_COMMAND                 0x10    // R/W  | Used for reset of encoder counts and module address changes
00035 
00036 /* MODES */
00037 #define MODE_0                      0x00    // The meaning of the speed registers is literal speeds in the range of 0 (Full Reverse), 128 (Stop), 255 (Full Forward) (Default Setting).
00038 #define MODE_1                      0x01    // The meaning of the speed registers is literal speeds in the range of -128 (Full Reverse), 0 (Stop), 127 (Full Forward).
00039 #define MODE_2                      0x02    // Speed1 control both motors speed, and speed2 becomes the turn value. Data is in the range of 0 (Full Reverse), 128 (Stop), 255 (Full  Forward).
00040 #define MODE_3                      0x03    // Speed1 control both motors speed, and speed2 becomes the turn value. Data is in the range of -128 (Full Reverse), 0 (Stop), 127 (Full Forward).
00041 
00042 /* COMMANDS */
00043 #define CMD_ENCODER_RESET           0x20    // Resets the encoder registers to zero
00044 #define CMD_AUTO_SPEED_DISABLE      0x30    // Disables automatic speed regulation
00045 #define CMD_AUTO_SPEED_ENABLE       0x31    // Enables automatic speed regulation (default)
00046 #define CMD_TIMEOUT_DISABLE         0x32    // Disables 2 second timeout of motors (Version 2 onwards only)
00047 #define CMD_TIMEOUT_ENABLE          0x33    // Enables 2 second timeout of motors when no I2C comms (default) (Version 2 onwards only)
00048 #define CMD_CHANGE_I2C_ADDR_1       0xA0    // 1st in sequence to change I2C address
00049 #define CMD_CHANGE_I2C_ADDR_2       0xAA    // 2nd in sequence to change I2C address
00050 #define CMD_CHANGE_I2C_ADDR_3       0xA5    // 3rd in sequence to change I2C address
00051 
00052 /* I2C ADDRESS */
00053 #define I2C_START_ADDR              0xB0    // The start address of valid I2C addresses. LSB indicates R/W, so add 2 to get next valid. Last valid is 0xBE.
00054 #define I2C_WRITE_BIT               0x00    // Add this to I2C address to perform a write.
00055 #define I2C_READ_BIT                0x01    // Add this to I2C address to perform a read.
00056 
00057 class MD25 {
00058 private:
00059     /* Communication */
00060     I2C *i2c_interface;
00061     
00062     /* Variables */
00063     int mode;
00064     char i2c_address;
00065     
00066     /* Generic methods */
00067     int write(char reg_addr, char *data, int bytes);
00068     int read(char reg_addr, char *data, int bytes);
00069     char doDiscover();
00070 public:
00071     /* Constructor and Destructor */
00072     MD25(I2C *i2c_interface, char i2c_address);
00073     ~MD25(void);
00074     
00075     /* Control methods */
00076     int mode_set(int mode);                 // Set the mode of operation (0 - 3) (default: 0).
00077     int mode_get();                         // Get the current mode of operation.
00078     int encoder_reset();                    // Resets the encoder registers.
00079     int auto_speed_set(bool enabled);       // Enable/disable automatic speed regulation (default: enabled).
00080     int timeout_set(bool enabled);          // Enable/disable 2 sec timeout of motors when no I2C comms (default: enabled).
00081     int i2c_addr_set(char address);         // Set a new I2C device address (Default: 0xB0).
00082     
00083     /* Data methods */
00084     int speed1_set(int speed);              // Set the speed of motor 1. (Only mode 0 or 1).
00085     int speed1_get();                       // Get the set speed of motor 1. (Only mode 0 or 1).
00086     int speed2_set(int speed);              // Set the speed of motor 2. (Only mode 0 or 1).
00087     int speed2_get();                       // Get the set speed of motor 2. (Only mode 0 or 1).
00088     
00089     int speed_set(int speed);               // Set the speed. (Only mode 2 or 3).
00090     int speed_get();                        // Get the set speed. (Only mode 2 or 3).
00091     int turn_set(int turn);                 // Set the turn speed. (Only mode 2 or 3).
00092     int turn_get();                         // Get the set turn speed. (Only mode 2 or 3).
00093     
00094     int acceleration_set(int acceleration); // Set a desired acceleration rate.
00095     int acceleration_get();                 // Get the set desired acceleration rate.
00096     
00097     int encoder1_get();                     // Encoder 1 position.
00098     int encoder2_get();                     // Encoder 2 position.
00099     
00100     float bat_voltage_get();                // Get battery voltage.
00101     float motor1_current_get();             // Get current for motor 1.
00102     float motor2_current_get();             // Get current for motor 2.
00103     
00104     int software_rev_num_get();             // Returns the software revision in the PIC16F873 controller.
00105 };