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

NXTServo-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup mindsensors
00002  * @{
00003  * @defgroup nxtservo Servo Controller
00004  * Servo Controller
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: NXTServo-driver.h 48 2011-02-13 20:35:38Z xander $
00010  */
00011 
00012 #ifndef __NXTSERVO_H__
00013 #define __NXTSERVO_H__
00014 /** \file NXTServo-driver.h
00015  * \brief Mindsensors NXTServo Sensor Driver
00016  *
00017  * NXTServo-driver.h provides an API for the Mindsensors NXTServo Sensor
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00028  * \author Xander Soldaat (mightor_at_gmail.com)
00029  * \date 30 September 2009
00030  * \version 0.1
00031  * \example NXTServo-test1.c
00032  */
00033 
00034 #pragma systemFile
00035 
00036 #ifndef __COMMON_H__
00037 #include "common.h"
00038 #endif
00039 
00040 #define NXTSERVO_I2C_ADDR     0xB0      /*!< NXT Servo I2C device address */
00041 
00042 // Command register, can be written for commands and read for battery voltage level
00043 #define NXTSERVO_CMD          0x41      /*!< Servo command register
00044 */
00045 // Position registers, can be read and written
00046 #define NXTSERVO_POS_CHAN1    0x42      /*!< Servo channel 1 position (low byte) */
00047 #define NXTSERVO_POS_CHAN2    0x44      /*!< Servo channel 2 position (low byte) */
00048 #define NXTSERVO_POS_CHAN3    0x46      /*!< Servo channel 3 position (low byte) */
00049 #define NXTSERVO_POS_CHAN4    0x48      /*!< Servo channel 4 position (low byte) */
00050 #define NXTSERVO_POS_CHAN5    0x4A      /*!< Servo channel 5 position (low byte) */
00051 #define NXTSERVO_POS_CHAN6    0x4C      /*!< Servo channel 6 position (low byte) */
00052 #define NXTSERVO_POS_CHAN7    0x4E      /*!< Servo channel 7 position (low byte) */
00053 #define NXTSERVO_POS_CHAN8    0x50      /*!< Servo channel 8 position (low byte) */
00054 
00055 // Speed registers, can be read and written
00056 #define NXTSERVO_SPEED_CHAN1  0x52      /*!< Servo channel 1 speed */
00057 #define NXTSERVO_SPEED_CHAN2  0x53      /*!< Servo channel 2 speed */
00058 #define NXTSERVO_SPEED_CHAN3  0x54      /*!< Servo channel 3 speed */
00059 #define NXTSERVO_SPEED_CHAN4  0x55      /*!< Servo channel 4 speed */
00060 #define NXTSERVO_SPEED_CHAN5  0x56      /*!< Servo channel 5 speed */
00061 #define NXTSERVO_SPEED_CHAN6  0x57      /*!< Servo channel 6 speed */
00062 #define NXTSERVO_SPEED_CHAN7  0x58      /*!< Servo channel 7 speed */
00063 #define NXTSERVO_SPEED_CHAN8  0x59      /*!< Servo channel 8 speed */
00064 
00065 // Quick position register, can only be written
00066 #define NXTSERVO_QPOS_CHAN1   0x42      /*!< Servo channel 1 quick position */
00067 #define NXTSERVO_QPOS_CHAN2   0x44      /*!< Servo channel 2 quick position */
00068 #define NXTSERVO_QPOS_CHAN3   0x46      /*!< Servo channel 3 quick position */
00069 #define NXTSERVO_QPOS_CHAN4   0x48      /*!< Servo channel 4 quick position */
00070 #define NXTSERVO_QPOS_CHAN5   0x4A      /*!< Servo channel 5 quick position */
00071 #define NXTSERVO_QPOS_CHAN6   0x4C      /*!< Servo channel 6 quick position */
00072 #define NXTSERVO_QPOS_CHAN7   0x4E      /*!< Servo channel 7 quick position */
00073 #define NXTSERVO_QPOS_CHAN8   0x50      /*!< Servo channel 8 quick position */
00074 
00075 /*! Some other defines. */
00076 #define NXTSERVO_MIN_POS      500       /*!< Servo minimum pulse width in uS */
00077 #define NXTSERVO_MAX_POS     2500       /*!< Servo maximum pulse width in uS */
00078 #define NXTSERVO_MID_POS     1500       /*!< Servo centered pulse width in uS */
00079 
00080 /*
00081 <function prototypes>
00082 */
00083 bool NXTServoSetSpeed(tSensors link, ubyte servochan, ubyte speed, ubyte address);
00084 bool NXTServoSetPos(tSensors link, ubyte servochan, int position, ubyte speed, ubyte address = NXTSERVO_I2C_ADDR);
00085 bool NXTServoQSetPos(tSensors link, ubyte servochan, ubyte position, byte speed, ubyte address = NXTSERVO_I2C_ADDR);
00086 int NXTServoReadVoltage(tSensors link, ubyte address = NXTSERVO_I2C_ADDR);
00087 
00088 tByteArray NXTSERVO_I2CRequest;         /*!< Array to hold I2C command data */
00089 tByteArray NXTSERVO_I2CReply;           /*!< Array to hold I2C reply data */
00090 
00091 
00092 /**
00093  * Set the speed register for the specified servo.  This is the amount to increase
00094  * the current position by every 24ms when the servo position is changed.
00095  *
00096  * Note: this is an internal function and should not be called directly.
00097  * @param link the NXTServo port number
00098  * @param servochan the servo channel to use
00099  * @param speed the amount to increase the position by every 24ms
00100  * @param address the I2C address to use, optional, defaults to 0xB0
00101  * @return true if no error occured, false if it did
00102  */
00103 bool NXTServoSetSpeed(tSensors link, ubyte servochan, ubyte speed, ubyte address) {
00104   memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00105   NXTSERVO_I2CRequest[0] = 3;
00106   NXTSERVO_I2CRequest[1] = address;
00107   NXTSERVO_I2CRequest[2] = NXTSERVO_SPEED_CHAN1 + (servochan - 1);
00108   NXTSERVO_I2CRequest[3] = speed;
00109 
00110   return writeI2C(link, NXTSERVO_I2CRequest, 0);
00111 }
00112 
00113 
00114 /**
00115  * Tell the servo to move to the specified position using the specified speed.
00116  * @param link the NXTServo port number
00117  * @param servochan the servo channel to use
00118  * @param position the position to move the servo to
00119  * @param speed the amount to increase the position by every 24ms
00120  * @param address the I2C address to use, optional, defaults to 0xB0
00121  * @return true if no error occured, false if it did
00122  */
00123 bool NXTServoSetPos(tSensors link, ubyte servochan, int position, ubyte speed, ubyte address) {
00124   memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00125   if (!NXTServoSetSpeed(link, servochan, speed, address))
00126     return false;
00127 
00128   position = clip(position, 500, 2500);
00129 
00130   // set the position register and tell NXTServo to move the servo
00131   memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00132   NXTSERVO_I2CRequest[0] = 4;
00133   NXTSERVO_I2CRequest[1] = address;
00134   NXTSERVO_I2CRequest[2] = NXTSERVO_POS_CHAN1 + ((servochan - 1) * 2);
00135   NXTSERVO_I2CRequest[3] = position & 0x00FF;
00136   NXTSERVO_I2CRequest[4] = (position >> 8) & 0x00FF;
00137 
00138   return writeI2C(link, NXTSERVO_I2CRequest, 0);
00139 }
00140 
00141 
00142 
00143 /**
00144  * Tell the servo to move to the specified position using the specified speed.
00145  * This uses the Quick Position register which is less accurate.  It only accepts
00146  * values from 50 to 250
00147  * @param link the NXTServo port number
00148  * @param servochan the servo channel to use
00149  * @param position the position to move the servo to
00150  * @param speed the amount to increase the position by every 24ms
00151  * @param address the I2C address to use, optional, defaults to 0xB0
00152  * @return true if no error occured, false if it did
00153  */
00154 bool NXTServoQSetPos(tSensors link, ubyte servochan, ubyte position, byte speed, ubyte address) {
00155   memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00156 
00157   position = clip(position, 50, 250);
00158 
00159   if (!NXTServoSetSpeed(link, servochan, speed, address))
00160     return false;
00161 
00162   // set the position register and tell NXTServo to move the servo
00163 
00164   NXTSERVO_I2CRequest[0] = 3;
00165   NXTSERVO_I2CRequest[1] = address;
00166   NXTSERVO_I2CRequest[2] = NXTSERVO_QPOS_CHAN1 + (servochan - 1);
00167   NXTSERVO_I2CRequest[3] = position;
00168 
00169   return writeI2C(link, NXTSERVO_I2CRequest, 0);
00170 }
00171 
00172 
00173 /**
00174  * Get the voltage level of the battery pack.
00175  * @param link the NXTServo port number
00176  * @param address the I2C address to use, optional, defaults to 0xB0
00177  * @return the voltage of the battery pack or -1 if an error occurred
00178  */
00179 int NXTServoReadVoltage(tSensors link, ubyte address) {
00180   long mvs = 0;
00181 
00182   memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00183 
00184   NXTSERVO_I2CRequest[0] = 2;                 // Message size
00185   NXTSERVO_I2CRequest[1] = address;           // I2C Address
00186   NXTSERVO_I2CRequest[2] = NXTSERVO_CMD;      // Start red sensor value
00187 
00188   if (!writeI2C(link, NXTSERVO_I2CRequest, 1))
00189     return -1;
00190 
00191   if (!readI2C(link, NXTSERVO_I2CReply, 1))
00192     return -1;
00193 
00194   mvs = ((long)NXTSERVO_I2CReply[0] * 3886) / 100;
00195 
00196   return mvs;
00197 }
00198 
00199 #endif // __NXTSERVO_H__
00200 /*
00201  * $Id: NXTServo-driver.h 48 2011-02-13 20:35:38Z xander $
00202  */
00203 /* @} */
00204 /* @} */