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

HTIRR-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup HiTechnic
00002  * @{
00003  * @defgroup htirr IR Receiver Sensor
00004  * HiTechnic IR Receiver Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: HTIRR-driver.h 48 2011-02-13 20:35:38Z xander $
00010  */
00011 
00012 #ifndef __HTIRR_H__
00013 #define __HTIRR_H__
00014 /** \file HTIRR-driver.h
00015  * \brief HiTechnic IR Receiver Sensor driver
00016  *
00017  * HTIRR2-driver.h provides an API for the IR Receiver Sensor driver.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Changed HTIRRreadChannel() proto to use signed bytes like function.
00022  *
00023  * Credits:
00024  * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
00025  *
00026  * License: You may use this code as you wish, provided you give credit where its due.
00027  *
00028  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00029  * \author Xander Soldaat (mightor_at_gmail.com)
00030  * \date 03 November 2009
00031  * \version 0.2
00032  * \example HTIRR-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define HTIRR_I2C_ADDR        0x02      /*!< HTIRR I2C device address */
00042 #define HTIRR_OFFSET          0x42      /*!< Offset for data registers */
00043 
00044 // Values contained by registers in active mode
00045 #define HTIRR_MOTOR_1A        0x00      /*!< Color number */
00046 #define HTIRR_MOTOR_1B        0x01      /*!< Color number */
00047 #define HTIRR_MOTOR_2A        0x02      /*!< Color number */
00048 #define HTIRR_MOTOR_2B        0x03      /*!< Color number */
00049 #define HTIRR_MOTOR_3A        0x04      /*!< Color number */
00050 #define HTIRR_MOTOR_3B        0x05      /*!< Color number */
00051 #define HTIRR_MOTOR_4A        0x06      /*!< Color number */
00052 #define HTIRR_MOTOR_4B        0x07      /*!< Color number */
00053 
00054 /*! Some define. */
00055 #define MOTOR_BRAKE           -128      /*!< Motor brake */
00056 
00057 /*
00058 <function prototypes>
00059 */
00060 bool HTIRRreadChannel(tSensors link, byte channel, sbyte &motA, sbyte &motB);
00061 bool HTIRRreadAllChannels(tSensors link, tsByteArray &motorSpeeds);
00062 
00063 tByteArray HTIRR_I2CRequest;           /*!< Array to hold I2C command data */
00064 tByteArray HTIRR_I2CReply;             /*!< Array to hold I2C reply data */
00065 
00066 
00067 /**
00068  * Get the speeds of the motors for a given channel.
00069  * @param link the HTIRR port number
00070  * @param channel the channel number (1 to 4)
00071  * @param motA the speed for Motor A (-100 to +100, -128 = brake)
00072  * @param motB the speed for Motor B (-100 to +100, -128 = brake)
00073  * @return true if no error occured, false if it did
00074  */
00075 bool HTIRRreadChannel(tSensors link, byte channel, sbyte &motA, sbyte &motB) {
00076   memset(HTIRR_I2CRequest, 0, sizeof(tByteArray));
00077 
00078   HTIRR_I2CRequest[0] = 2;                                // Message size
00079   HTIRR_I2CRequest[1] = HTIRR_I2C_ADDR;                   // I2C Address
00080   HTIRR_I2CRequest[2] = HTIRR_OFFSET + ((channel - 1) * 2); // Start of speed registry
00081 
00082   if (!writeI2C(link, HTIRR_I2CRequest, 2))
00083     return false;
00084 
00085   if (!readI2C(link, HTIRR_I2CReply, 2))
00086     return false;
00087 
00088   memcpy(motA, HTIRR_I2CReply[0], 1);
00089   memcpy(motB, HTIRR_I2CReply[1], 1);
00090 
00091   return true;
00092 }
00093 
00094 
00095 /**
00096  * Get the speeds of the motors for all channels.
00097  * @param link the HTIRR port number
00098  * @param motorSpeeds the speeds for all the motors (-100 to +100, -128 = brake)
00099  * @return true if no error occured, false if it did
00100  */
00101 bool HTIRRreadAllChannels(tSensors link, tsByteArray &motorSpeeds){
00102   memset(motorSpeeds, 0, sizeof(tsByteArray));
00103   memset(HTIRR_I2CRequest, 0, sizeof(tByteArray));
00104 
00105   HTIRR_I2CRequest[0] = 2;                // Message size
00106   HTIRR_I2CRequest[1] = HTIRR_I2C_ADDR;   // I2C Address
00107   HTIRR_I2CRequest[2] = HTIRR_OFFSET;     // Start of speed registry
00108 
00109   if (!writeI2C(link, HTIRR_I2CRequest, 8))
00110     return false;
00111 
00112   if (!readI2C(link, HTIRR_I2CReply, 8))
00113     return false;
00114 
00115   memcpy(motorSpeeds, HTIRR_I2CReply, 8);
00116   return true;
00117 }
00118 
00119 #endif // __HTIRR_H__
00120 /*
00121  * $Id: HTIRR-driver.h 48 2011-02-13 20:35:38Z xander $
00122  */
00123 /* @} */
00124 /* @} */