A library for the MD25 motor controller from www.robot-electronics.co.uk. The MD25 supports both I2C and UART, but this library is designed for use with I2C only.

Committer:
UBAL
Date:
Mon Feb 20 10:37:14 2012 +0000
Revision:
0:a06e2ad45792
Working library for the MD25 motor controller.
Currently only I2C is supported.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UBAL 0:a06e2ad45792 1 /********************************************************************
UBAL 0:a06e2ad45792 2 * @filename MD25.h
UBAL 0:a06e2ad45792 3 * @author Thomas B. Joergensen (thomas.bal.jorgensen@gmail.com)
UBAL 0:a06e2ad45792 4 * @date 06 FEB 2012
UBAL 0:a06e2ad45792 5 * @version 1.0
UBAL 0:a06e2ad45792 6 * @target mbed (NXP LPC1768 - ARM Cortex M3 - 32-bit)
UBAL 0:a06e2ad45792 7 *
UBAL 0:a06e2ad45792 8 * @desciption A library for interacting with the MD25 DC motor
UBAL 0:a06e2ad45792 9 * controller. Find more at:
UBAL 0:a06e2ad45792 10 * http://www.robot-electronics.co.uk/htm/md25tech.htm
UBAL 0:a06e2ad45792 11 * http://www.robot-electronics.co.uk/htm/md25i2c.htm
UBAL 0:a06e2ad45792 12 *******************************************************************/
UBAL 0:a06e2ad45792 13
UBAL 0:a06e2ad45792 14 /* Includes */
UBAL 0:a06e2ad45792 15 #include "mbed.h"
UBAL 0:a06e2ad45792 16
UBAL 0:a06e2ad45792 17 /* REGISTERS */
UBAL 0:a06e2ad45792 18 #define REG_SPEED1 0x00 // R/W | Motor1 speed (mode 0,1) or speed (mode 2,3)
UBAL 0:a06e2ad45792 19 #define REG_SPEED2 0x01 // R/W | Motor2 speed (mode 0,1) or turn (mode 2,3)
UBAL 0:a06e2ad45792 20 #define REG_ENC1A 0x02 // R | Encoder 1 position, 1st byte (highest), capture count when read
UBAL 0:a06e2ad45792 21 #define REG_ENC1B 0x03 // R | Encoder 1 position, 2nd byte
UBAL 0:a06e2ad45792 22 #define REG_ENC1C 0x04 // R | Encoder 1 position, 3rd byte
UBAL 0:a06e2ad45792 23 #define REG_ENC1D 0x05 // R | Encoder 1 position, 4th (lowest byte)
UBAL 0:a06e2ad45792 24 #define REG_ENC2A 0x06 // R | Encoder 2 position, 1st byte (highest), capture count when read
UBAL 0:a06e2ad45792 25 #define REG_ENC2B 0x07 // R | Encoder 2 position, 2nd byte
UBAL 0:a06e2ad45792 26 #define REG_ENC2C 0x08 // R | Encoder 2 position, 3rd byte
UBAL 0:a06e2ad45792 27 #define REG_ENC2D 0x09 // R | Encoder 2 position, 4th byte (lowest byte)
UBAL 0:a06e2ad45792 28 #define REG_BATTERY_VOLTS 0x0A // R | The supply battery voltage
UBAL 0:a06e2ad45792 29 #define REG_MOTOR1_CURRENT 0x0B // R | The current through motor 1
UBAL 0:a06e2ad45792 30 #define REG_MOTOR2_CURRENT 0x0C // R | The current through motor 2
UBAL 0:a06e2ad45792 31 #define REG_SOFTWARE_REVISION 0x0D // R | Software Revision Number
UBAL 0:a06e2ad45792 32 #define REG_ACCELERATION_RATE 0x0E // R/W | Optional Acceleration register
UBAL 0:a06e2ad45792 33 #define REG_MODE 0x0F // R/W | Mode of operation (see below)
UBAL 0:a06e2ad45792 34 #define REG_COMMAND 0x10 // R/W | Used for reset of encoder counts and module address changes
UBAL 0:a06e2ad45792 35
UBAL 0:a06e2ad45792 36 /* MODES */
UBAL 0:a06e2ad45792 37 #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).
UBAL 0:a06e2ad45792 38 #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).
UBAL 0:a06e2ad45792 39 #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).
UBAL 0:a06e2ad45792 40 #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).
UBAL 0:a06e2ad45792 41
UBAL 0:a06e2ad45792 42 /* COMMANDS */
UBAL 0:a06e2ad45792 43 #define CMD_ENCODER_RESET 0x20 // Resets the encoder registers to zero
UBAL 0:a06e2ad45792 44 #define CMD_AUTO_SPEED_DISABLE 0x30 // Disables automatic speed regulation
UBAL 0:a06e2ad45792 45 #define CMD_AUTO_SPEED_ENABLE 0x31 // Enables automatic speed regulation (default)
UBAL 0:a06e2ad45792 46 #define CMD_TIMEOUT_DISABLE 0x32 // Disables 2 second timeout of motors (Version 2 onwards only)
UBAL 0:a06e2ad45792 47 #define CMD_TIMEOUT_ENABLE 0x33 // Enables 2 second timeout of motors when no I2C comms (default) (Version 2 onwards only)
UBAL 0:a06e2ad45792 48 #define CMD_CHANGE_I2C_ADDR_1 0xA0 // 1st in sequence to change I2C address
UBAL 0:a06e2ad45792 49 #define CMD_CHANGE_I2C_ADDR_2 0xAA // 2nd in sequence to change I2C address
UBAL 0:a06e2ad45792 50 #define CMD_CHANGE_I2C_ADDR_3 0xA5 // 3rd in sequence to change I2C address
UBAL 0:a06e2ad45792 51
UBAL 0:a06e2ad45792 52 /* I2C ADDRESS */
UBAL 0:a06e2ad45792 53 #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.
UBAL 0:a06e2ad45792 54 #define I2C_WRITE_BIT 0x00 // Add this to I2C address to perform a write.
UBAL 0:a06e2ad45792 55 #define I2C_READ_BIT 0x01 // Add this to I2C address to perform a read.
UBAL 0:a06e2ad45792 56
UBAL 0:a06e2ad45792 57 class MD25 {
UBAL 0:a06e2ad45792 58 private:
UBAL 0:a06e2ad45792 59 /* Communication */
UBAL 0:a06e2ad45792 60 I2C *i2c_interface;
UBAL 0:a06e2ad45792 61
UBAL 0:a06e2ad45792 62 /* Variables */
UBAL 0:a06e2ad45792 63 int mode;
UBAL 0:a06e2ad45792 64 char i2c_address;
UBAL 0:a06e2ad45792 65
UBAL 0:a06e2ad45792 66 /* Generic methods */
UBAL 0:a06e2ad45792 67 int write(char reg_addr, char *data, int bytes);
UBAL 0:a06e2ad45792 68 int read(char reg_addr, char *data, int bytes);
UBAL 0:a06e2ad45792 69 char doDiscover();
UBAL 0:a06e2ad45792 70 public:
UBAL 0:a06e2ad45792 71 /* Constructor and Destructor */
UBAL 0:a06e2ad45792 72 MD25(I2C *i2c_interface, char i2c_address);
UBAL 0:a06e2ad45792 73 ~MD25(void);
UBAL 0:a06e2ad45792 74
UBAL 0:a06e2ad45792 75 /* Control methods */
UBAL 0:a06e2ad45792 76 int mode_set(int mode); // Set the mode of operation (0 - 3) (default: 0).
UBAL 0:a06e2ad45792 77 int mode_get(); // Get the current mode of operation.
UBAL 0:a06e2ad45792 78 int encoder_reset(); // Resets the encoder registers.
UBAL 0:a06e2ad45792 79 int auto_speed_set(bool enabled); // Enable/disable automatic speed regulation (default: enabled).
UBAL 0:a06e2ad45792 80 int timeout_set(bool enabled); // Enable/disable 2 sec timeout of motors when no I2C comms (default: enabled).
UBAL 0:a06e2ad45792 81 int i2c_addr_set(char address); // Set a new I2C device address (Default: 0xB0).
UBAL 0:a06e2ad45792 82
UBAL 0:a06e2ad45792 83 /* Data methods */
UBAL 0:a06e2ad45792 84 int speed1_set(int speed); // Set the speed of motor 1. (Only mode 0 or 1).
UBAL 0:a06e2ad45792 85 int speed1_get(); // Get the set speed of motor 1. (Only mode 0 or 1).
UBAL 0:a06e2ad45792 86 int speed2_set(int speed); // Set the speed of motor 2. (Only mode 0 or 1).
UBAL 0:a06e2ad45792 87 int speed2_get(); // Get the set speed of motor 2. (Only mode 0 or 1).
UBAL 0:a06e2ad45792 88
UBAL 0:a06e2ad45792 89 int speed_set(int speed); // Set the speed. (Only mode 2 or 3).
UBAL 0:a06e2ad45792 90 int speed_get(); // Get the set speed. (Only mode 2 or 3).
UBAL 0:a06e2ad45792 91 int turn_set(int turn); // Set the turn speed. (Only mode 2 or 3).
UBAL 0:a06e2ad45792 92 int turn_get(); // Get the set turn speed. (Only mode 2 or 3).
UBAL 0:a06e2ad45792 93
UBAL 0:a06e2ad45792 94 int acceleration_set(int acceleration); // Set a desired acceleration rate.
UBAL 0:a06e2ad45792 95 int acceleration_get(); // Get the set desired acceleration rate.
UBAL 0:a06e2ad45792 96
UBAL 0:a06e2ad45792 97 int encoder1_get(); // Encoder 1 position.
UBAL 0:a06e2ad45792 98 int encoder2_get(); // Encoder 2 position.
UBAL 0:a06e2ad45792 99
UBAL 0:a06e2ad45792 100 float bat_voltage_get(); // Get battery voltage.
UBAL 0:a06e2ad45792 101 float motor1_current_get(); // Get current for motor 1.
UBAL 0:a06e2ad45792 102 float motor2_current_get(); // Get current for motor 2.
UBAL 0:a06e2ad45792 103
UBAL 0:a06e2ad45792 104 int software_rev_num_get(); // Returns the software revision in the PIC16F873 controller.
UBAL 0:a06e2ad45792 105 };