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

MSPFM-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup pfmate PFMate Sensor
00004  * PFMate Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: MSPFM-driver.h 48 2011-02-13 20:35:38Z xander $
00010  */
00011 
00012 /** \file MSPFM-driver.h
00013  * \brief Mindsensors PFMate Sensor driver
00014  *
00015  * MSPFM-driver.h provides an API for the Mindsensors PFMate Sensor driver
00016  *
00017  * Changelog:
00018  * - 0.1: Initial release
00019  *
00020  * Credits:
00021  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00022  *
00023  * License: You may use this code as you wish, provided you give credit where its due.
00024  *
00025  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00026  * \author Xander Soldaat (mightor_at_gmail.com)
00027  * \date 22 July 2009
00028  * \version 0.1
00029  * \example MSPFM-test1.c
00030  */
00031 
00032 #pragma systemFile
00033 
00034 #ifndef __MSPFM_H__
00035 #define __MSPFM_H__
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define MSPFM_I2C_ADDR    0x48      /*!< I2C address for sensor */
00042 #define MSPFM_CMD         0x41      /*!< Command register */
00043 #define MSPFM_IRCHAN      0x42      /*!< IR Channel selection register */
00044 #define MSPFM_MSELECT     0x43      /*!< Motor selection register */
00045 #define MSPFM_MOTOPA      0x44      /*!< Motor A operation register */
00046 #define MSPFM_MOTSPA      0x45      /*!< Motor A speed register */
00047 #define MSPFM_MOTOPB      0x46      /*!< Motor B operation register */
00048 #define MSPFM_MOTSPB      0x47      /*!< Motor B speed register */
00049 
00050 #define MSPFM_GOCMD       0x47      /*!< "Go" command to start sending IR signals to PF receiver */
00051 
00052 #define MSPFM_MOTORAB     0x00
00053 #define MSPFM_MOTORA      0x01
00054 #define MSPFM_MOTORB      0x02
00055 
00056 #define MSPFM_FLOAT       0x00
00057 #define MSPFM_FORWARD     0x01
00058 #define MSPFM_REVERSE     0x02
00059 #define MSPFM_BRAKE       0x03
00060 #define MSPFM_NOOP        0x0F
00061 
00062 
00063 bool MSPFMcontrolMotorA(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address = MSPFM_I2C_ADDR);
00064 bool MSPFMcontrolMotorB(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address = MSPFM_I2C_ADDR);
00065 bool MSPFMcontrolMotorAB(tSensors link, byte chan, byte motorA_op, byte motorA_speed, byte motorB_op, byte motorB_speed, ubyte address = MSPFM_I2C_ADDR);
00066 
00067 
00068 tByteArray MSPFM_I2CRequest;       /*!< Array to hold I2C command data */
00069 
00070 
00071 /**
00072  * Control motor A with the PFMate.
00073  *
00074  * @param link the PFMate port number
00075  * @param chan the channel of the IR receiver, value of 1-4
00076  * @param motor_op motor A operation, 0: float, 1: forward, 2: reverse, 3: brake
00077  * @param motor_speed the speed at which motor A should turn, value between 1-7
00078  * @param address the I2C address to use, optional, defaults to 0x48
00079  * @return true if no error occured, false if it did
00080  */
00081 bool MSPFMcontrolMotorA(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address) {
00082   return MSPFMcontrolMotorAB(link, chan, motor_op, motor_speed, MSPFM_NOOP, 0);
00083 }
00084 
00085 
00086 /**
00087  * Control motor B with the PFMate.
00088  *
00089  * @param link the PFMate port number
00090  * @param chan the channel of the IR receiver, value of 1-4
00091  * @param motor_op motor B operation, 0: float, 1: forward, 2: reverse, 3: brake
00092  * @param motor_speed the speed at which motor B should turn, value between 1-7
00093  * @param address the I2C address to use, optional, defaults to 0x48
00094  * @return true if no error occured, false if it did
00095  */
00096 bool MSPFMcontrolMotorB(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address) {
00097   return MSPFMcontrolMotorAB(link, chan, MSPFM_NOOP, 0, motor_op, motor_speed);
00098 }
00099 
00100 
00101 /**
00102  * Control motors A and B with the PFMate.
00103  *
00104  * @param link the PFMate port number
00105  * @param chan the channel of the IR receiver, value of 1-4
00106  * @param motorA_op motor A operation, 0: float, 1: forward, 2: reverse, 3: brake
00107  * @param motorA_speed the speed at which motor A should turn, value between 1-7
00108  * @param motorB_op motor B operation, 0: float, 1: forward, 2: reverse, 3: brake
00109  * @param motorB_speed the speed at which motor B should turn, value between 1-7
00110  * @param address the I2C address to use, optional, defaults to 0x48
00111  * @return true if no error occured, false if it did
00112  */
00113 bool MSPFMcontrolMotorAB(tSensors link, byte chan, byte motorA_op, byte motorA_speed, byte motorB_op, byte motorB_speed, ubyte address) {
00114   byte mselect = MSPFM_MOTORAB;
00115 
00116   if (motorA_op == MSPFM_NOOP)
00117     mselect = MSPFM_MOTORB;
00118   else if (motorB_op == MSPFM_NOOP)
00119     mselect = MSPFM_MOTORA;
00120 
00121   memset(MSPFM_I2CRequest, 0, sizeof(tByteArray));
00122   MSPFM_I2CRequest[0] = 8;
00123   MSPFM_I2CRequest[1] = address;
00124   MSPFM_I2CRequest[2] = MSPFM_IRCHAN;
00125   MSPFM_I2CRequest[3] = chan;
00126   MSPFM_I2CRequest[4] = mselect;
00127   MSPFM_I2CRequest[5] = motorA_op;
00128   MSPFM_I2CRequest[6] = motorA_speed;
00129   MSPFM_I2CRequest[7] = motorB_op;
00130   MSPFM_I2CRequest[8] = motorB_speed;
00131 
00132   if (!writeI2C(link, MSPFM_I2CRequest, 0))
00133     return false;
00134 
00135   MSPFM_I2CRequest[0] = 3;
00136   MSPFM_I2CRequest[1] = address;
00137   MSPFM_I2CRequest[2] = MSPFM_CMD;
00138   MSPFM_I2CRequest[3] = MSPFM_GOCMD;
00139 
00140   return writeI2C(link, MSPFM_I2CRequest, 0);
00141 }
00142 
00143 
00144 #endif // __MSPFM_H__
00145 
00146 /*
00147  * $Id: MSPFM-driver.h 48 2011-02-13 20:35:38Z xander $
00148  */
00149 /* @} */
00150 /* @} */