Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

mindsensors-rcxmotormux.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup msmtrmx RCX Motor MUX
00004  * RCX Motor MUX
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: mindsensors-rcxmotormux.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 /** \file mindsensors-rcxmotormux.h
00013  * \brief RobotC Mindsensors RCX Motor MUX Driver
00014  *
00015  * mindsensors-rcxmotormux.h provides an API for the Mindsensors RCX Motor MUX Driver.
00016  * License: You may use this code as you wish, provided you give credit where its due.
00017  *
00018  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00019 
00020  * \author Daniel Playfair Cal (daniel.playfair.cal_at_gmail.com)
00021  * \date 2011-09-20
00022  * \version 2
00023  * \example mindsensors-rcxmotormux-test1.c
00024  */
00025 
00026 #pragma systemFile
00027 
00028 #ifndef __COMMON_H__
00029 #include "common.h"
00030 #endif
00031 
00032 /*! i2c address/registers */
00033 #define MSMTRMX_I2C_ADDR       0xB4      /*!< MSMTRMX I2C device address */
00034 #define MSMTRMX_MOTOR_REG     0x42      /*!< Motor address */
00035 
00036 /*! motor settings struct, each motor can be set to one of these modes */
00037 typedef enum {
00038   MSMTRMX_MODE_FLOAT = 0,
00039   MSMTRMX_MODE_FORWARD = 1,
00040   MSMTRMX_MODE_REVERSE = 2,
00041   MSMTRMX_MODE_BRAKE = 3
00042 } tMSMTRMXSettings;
00043 
00044 /*! motors struct, these are the four motor channels on the MUX */
00045 typedef enum {
00046   MSMTRMX_M1 = 0,
00047   MSMTRMX_M2 = 1,
00048   MSMTRMX_M3 = 2,
00049   MSMTRMX_M4 = 3
00050 } tMSMTRMXMotors;
00051 
00052 tByteArray MSMTRMX_I2CMessage;       /*!< Array to hold I2C command data */
00053 
00054 /*! Function prototypes */
00055 bool MSMTRMX_Control(tSensors link, tMSMTRMXMotors channel, int power, ubyte address = MSMTRMX_I2C_ADDR);
00056 bool MSMTRMX_Brake(tSensors link, tMSMTRMXMotors channel, unsigned byte brakeForce, ubyte address = MSMTRMX_I2C_ADDR);
00057 
00058 
00059 /**
00060  * This function sets the specified motor to the given power level,
00061  * with the motor floated at speed zero. The controller floats during
00062  * inactive PWM periods.
00063  * @param link port number
00064  * @param channel motor number
00065  * @param power speed to set the motor to (-255 to +255)
00066  * @param address the I2C address to use, optional, defaults to 0xB4
00067  * @return true if message is sent successfully
00068  */
00069 bool MSMTRMX_Control(tSensors link, tMSMTRMXMotors channel, int power, ubyte address) {
00070   tMSMTRMXSettings dir;
00071 
00072   if (power == 0) {
00073     dir = MSMTRMX_MODE_BRAKE;
00074     power = 0;
00075   }
00076   else if (power < 0) {
00077     dir = MSMTRMX_MODE_REVERSE;
00078     power = -power;
00079   }
00080   else if (power > 0) {
00081     dir = MSMTRMX_MODE_FORWARD;
00082   }
00083 
00084   memset(MSMTRMX_I2CMessage, 0, sizeof(tByteArray));
00085 
00086   MSMTRMX_I2CMessage[0] = 4;
00087   MSMTRMX_I2CMessage[1] = address;
00088   MSMTRMX_I2CMessage[2] = MSMTRMX_MOTOR_REG + channel * 2;
00089   MSMTRMX_I2CMessage[3] = (ubyte) dir;
00090   MSMTRMX_I2CMessage[4] = power;
00091 
00092   if (!writeI2C(link, MSMTRMX_I2CMessage)) {
00093     return false;
00094   }
00095 
00096   return true;
00097 }
00098 
00099 
00100 /**
00101  * This function applies the specified braking power to the specified motor
00102  * @param link port number
00103  * @param channel motor number
00104  * @param brakeForce brake power to set the motor to (0-255, 0 == float)
00105  * @param address the I2C address to use, optional, defaults to 0xB4
00106  * @return true if message is sent successfully
00107  */
00108 bool MSMTRMX_Brake(tSensors link, tMSMTRMXMotors channel, unsigned byte brakeForce, ubyte address) {
00109 
00110   memset(MSMTRMX_I2CMessage, 0, sizeof(tByteArray));
00111 
00112   MSMTRMX_I2CMessage[0] = 4;
00113   MSMTRMX_I2CMessage[1] = address;
00114   MSMTRMX_I2CMessage[2] = MSMTRMX_MOTOR_REG + channel * 2;
00115   MSMTRMX_I2CMessage[3] = (ubyte) MSMTRMX_MODE_BRAKE;
00116   MSMTRMX_I2CMessage[4] = brakeForce;
00117 
00118   if (!writeI2C(link, MSMTRMX_I2CMessage)) {
00119     return false;
00120   }
00121 
00122   return true;
00123 }
00124 
00125 /*
00126  * $Id: mindsensors-rcxmotormux.h 133 2013-03-10 15:15:38Z xander $
00127  */
00128 
00129 /* @} */
00130 /* @} */