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

mindsensors-pfmate.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: mindsensors-pfmate.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 /** \file mindsensors-pfmate.h
00013  * \brief Mindsensors PFMate Sensor driver
00014  *
00015  * mindsensors-pfmate.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 3.59 AND HIGHER. 
00026 
00027  * \author Xander Soldaat (xander_at_botbench.com)
00028  * \date 22 July 2009
00029  * \version 0.1
00030  * \example mindsensors-pfmate-test1.c
00031  */
00032 
00033 #pragma systemFile
00034 
00035 #ifndef __MSPFM_H__
00036 #define __MSPFM_H__
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 #define MSPFM_I2C_ADDR    0x48      /*!< I2C address for sensor */
00043 #define MSPFM_CMD         0x41      /*!< Command register */
00044 #define MSPFM_IRCHAN      0x42      /*!< IR Channel selection register */
00045 #define MSPFM_MSELECT     0x43      /*!< Motor selection register */
00046 #define MSPFM_MOTOPA      0x44      /*!< Motor A operation register */
00047 #define MSPFM_MOTSPA      0x45      /*!< Motor A speed register */
00048 #define MSPFM_MOTOPB      0x46      /*!< Motor B operation register */
00049 #define MSPFM_MOTSPB      0x47      /*!< Motor B speed register */
00050 
00051 #define MSPFM_GOCMD       0x47      /*!< "Go" command to start sending IR signals to PF receiver */
00052 
00053 #define MSPFM_MOTORAB     0x00
00054 #define MSPFM_MOTORA      0x01
00055 #define MSPFM_MOTORB      0x02
00056 
00057 #define MSPFM_FLOAT       0x00
00058 #define MSPFM_FORWARD     0x01
00059 #define MSPFM_REVERSE     0x02
00060 #define MSPFM_BRAKE       0x03
00061 #define MSPFM_NOOP        0x0F
00062 
00063 
00064 bool MSPFMcontrolMotorA(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address = MSPFM_I2C_ADDR);
00065 bool MSPFMcontrolMotorB(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address = MSPFM_I2C_ADDR);
00066 bool MSPFMcontrolMotorAB(tSensors link, byte chan, byte motorA_op, byte motorA_speed, byte motorB_op, byte motorB_speed, ubyte address = MSPFM_I2C_ADDR);
00067 
00068 
00069 tByteArray MSPFM_I2CRequest;       /*!< Array to hold I2C command data */
00070 
00071 
00072 /**
00073  * Control motor A with the PFMate.
00074  *
00075  * @param link the PFMate port number
00076  * @param chan the channel of the IR receiver, value of 1-4
00077  * @param motor_op motor A operation, 0: float, 1: forward, 2: reverse, 3: brake
00078  * @param motor_speed the speed at which motor A should turn, value between 1-7
00079  * @param address the I2C address to use, optional, defaults to 0x48
00080  * @return true if no error occured, false if it did
00081  */
00082 bool MSPFMcontrolMotorA(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address) {
00083   return MSPFMcontrolMotorAB(link, chan, motor_op, motor_speed, MSPFM_NOOP, 0);
00084 }
00085 
00086 
00087 /**
00088  * Control motor B with the PFMate.
00089  *
00090  * @param link the PFMate port number
00091  * @param chan the channel of the IR receiver, value of 1-4
00092  * @param motor_op motor B operation, 0: float, 1: forward, 2: reverse, 3: brake
00093  * @param motor_speed the speed at which motor B should turn, value between 1-7
00094  * @param address the I2C address to use, optional, defaults to 0x48
00095  * @return true if no error occured, false if it did
00096  */
00097 bool MSPFMcontrolMotorB(tSensors link, byte chan, byte motor_op, byte motor_speed, ubyte address) {
00098   return MSPFMcontrolMotorAB(link, chan, MSPFM_NOOP, 0, motor_op, motor_speed);
00099 }
00100 
00101 
00102 /**
00103  * Control motors A and B with the PFMate.
00104  *
00105  * @param link the PFMate port number
00106  * @param chan the channel of the IR receiver, value of 1-4
00107  * @param motorA_op motor A operation, 0: float, 1: forward, 2: reverse, 3: brake
00108  * @param motorA_speed the speed at which motor A should turn, value between 1-7
00109  * @param motorB_op motor B operation, 0: float, 1: forward, 2: reverse, 3: brake
00110  * @param motorB_speed the speed at which motor B should turn, value between 1-7
00111  * @param address the I2C address to use, optional, defaults to 0x48
00112  * @return true if no error occured, false if it did
00113  */
00114 bool MSPFMcontrolMotorAB(tSensors link, byte chan, byte motorA_op, byte motorA_speed, byte motorB_op, byte motorB_speed, ubyte address) {
00115   byte mselect = MSPFM_MOTORAB;
00116 
00117   if (motorA_op == MSPFM_NOOP)
00118     mselect = MSPFM_MOTORB;
00119   else if (motorB_op == MSPFM_NOOP)
00120     mselect = MSPFM_MOTORA;
00121 
00122   memset(MSPFM_I2CRequest, 0, sizeof(tByteArray));
00123   MSPFM_I2CRequest[0] = 8;
00124   MSPFM_I2CRequest[1] = address;
00125   MSPFM_I2CRequest[2] = MSPFM_IRCHAN;
00126   MSPFM_I2CRequest[3] = chan;
00127   MSPFM_I2CRequest[4] = mselect;
00128   MSPFM_I2CRequest[5] = motorA_op;
00129   MSPFM_I2CRequest[6] = motorA_speed;
00130   MSPFM_I2CRequest[7] = motorB_op;
00131   MSPFM_I2CRequest[8] = motorB_speed;
00132 
00133   if (!writeI2C(link, MSPFM_I2CRequest))
00134     return false;
00135 
00136   MSPFM_I2CRequest[0] = 3;
00137   MSPFM_I2CRequest[1] = address;
00138   MSPFM_I2CRequest[2] = MSPFM_CMD;
00139   MSPFM_I2CRequest[3] = MSPFM_GOCMD;
00140 
00141   return writeI2C(link, MSPFM_I2CRequest);
00142 }
00143 
00144 
00145 #endif // __MSPFM_H__
00146 
00147 /*
00148  * $Id: mindsensors-pfmate.h 133 2013-03-10 15:15:38Z xander $
00149  */
00150 /* @} */
00151 /* @} */