// GyroITG3200 I2C device class file
// Based on InvenSense ITG-3200 datasheet rev. 1.4, 3/30/2010 (PS-ITG-3200A-00-01.4)
// Original work by 7/31/2011 by Jeff Rowberg <jeff@rowberg.net>
-// Java implementation for First Robotics Competition Team 2521 using WPILibj
+// Java implementation for First Robotics Competition Team 2521 using WPILibj
// 1/27/2015 by Joe Bussell <joe dot bussell at gmail dot com>
// Updates should (hopefully) always be available at https://github.com/bussell
//
/**
* Default constructor, uses default I2C address.
- *
+ *
* @see ITG3200_DEFAULT_ADDRESS
*/
public GyroClass(I2C.Port port) {
/**
* Specific address constructor.
- *
+ *
* @param address
* I2C address
* @see ITG3200_DEFAULT_ADDRESS
/**
* Verify the I2C connection. Make sure the device is connected and responds
* as expected.
- *
+ *
* @return True if connection is valid, false otherwise
*/
public boolean testConnection() {
/**
* Get Device ID. This register is used to verify the identity of the device
* (0b110100).
- *
+ *
* @return Device ID (should be 0x34, 52 dec, 64 oct)
* @see ITG3200_RA_WHO_AM_I
* @see ITG3200_RA_DEVID_BIT
/**
* Set Device ID. Write a new ID into the WHO_AM_I register (no idea why this
* should ever be necessary though).
- *
+ *
* @param id
* New device ID to set.
* @see getDeviceID()
/**
* Set sample rate.
- *
+ *
* @param rate
* New sample rate
* @see getRate()
/**
* Set full-scale range setting.
- *
+ *
* @param range
* New full-scale range value
* @see getFullScaleRange()
/**
* Set digital low-pass filter bandwidth.
- *
+ *
* @param bandwidth
* New DLFP bandwidth setting
* @see getDLPFBandwidth()
/**
* Get interrupt logic level mode. Will be set 0 for active-high, 1 for
* active-low.
- *
+ *
* @return Current interrupt mode (0=active-high, 1=active-low)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_ACTL_BIT
/**
* Set interrupt logic level mode.
- *
+ *
* @param mode
* New interrupt mode (0=active-high, 1=active-low)
* @see getInterruptMode()
/**
* Get interrupt drive mode. Will be set 0 for push-pull, 1 for open-drain.
- *
+ *
* @return Current interrupt drive mode (0=push-pull, 1=open-drain)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_OPEN_BIT
/**
* Set interrupt drive mode.
- *
+ *
* @param drive
* New interrupt drive mode (0=push-pull, 1=open-drain)
* @see getInterruptDrive()
/**
* Get interrupt latch mode. Will be set 0 for 50us-pulse, 1 for
* latch-until-int-cleared.
- *
+ *
* @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_LATCH_INT_EN_BIT
/**
* Set interrupt latch mode.
- *
+ *
* @param latch
* New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
* @see getInterruptLatch()
/**
* Get interrupt latch clear mode. Will be set 0 for status-read-only, 1 for
* any-register-read.
- *
+ *
* @return Current latch clear mode (0=status-read-only, 1=any-register-read)
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_INT_ANYRD_2CLEAR_BIT
/**
* Set interrupt latch clear mode.
- *
+ *
* @param clear
* New latch clear mode (0=status-read-only, 1=any-register-read)
* @see getInterruptLatchClear()
/**
* Get "device ready" interrupt enabled setting. Will be set 0 for disabled, 1
* for enabled.
- *
+ *
* @return Current interrupt enabled setting
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_ITG_RDY_EN_BIT
/**
* Set "device ready" interrupt enabled setting.
- *
+ *
* @param enabled
* New interrupt enabled setting
* @see getIntDeviceReadyEnabled()
/**
* Get "data ready" interrupt enabled setting. Will be set 0 for disabled, 1
* for enabled.
- *
+ *
* @return Current interrupt enabled setting
* @see ITG3200_RA_INT_CFG
* @see ITG3200_INTCFG_RAW_RDY_EN_BIT
/**
* Set "data ready" interrupt enabled setting.
- *
+ *
* @param enabled
* New interrupt enabled setting
* @see getIntDataReadyEnabled()
/**
* Get Device Ready interrupt status. The ITG_RDY interrupt indicates that the
* PLL is ready and gyroscopic data can be read.
- *
+ *
* @return Device Ready interrupt status
* @see ITG3200_RA_INT_STATUS
* @see ITG3200_INTSTAT_RAW_DATA_READY_BIT
* Get Data Ready interrupt status. In normal use, the RAW_DATA_RDY interrupt
* is used to determine when new sensor data is available in and of the sensor
* registers (27 to 32).
- *
+ *
* @return Data Ready interrupt status
* @see ITG3200_RA_INT_STATUS
* @see ITG3200_INTSTAT_RAW_DATA_READY_BIT
// TEMP_OUT_* registers
/**
* Get current internal temperature.
- *
+ *
* @return Temperature reading in 16-bit 2's complement format
* @see ITG3200_RA_TEMP_OUT_H
*/
public short getTemperature() {
byte[] buf = new byte[2];
ReadI2CBuffer(ITG3200_RA_TEMP_OUT_H, 2, buf);
- return (short) (((buf[0]) << 8) | buf[1]);
+ return (short) (((short) (buf[0]) << 8) | (short) buf[1]);
}
// GYRO_*OUT_* registers
/**
* Get 3-axis gyroscope readings.
- *
+ *
* @param x
* 16-bit signed integer container for X-axis rotation
* @param y
AllAxes data = new AllAxes();
byte[] buffer = new byte[6];
ReadI2CBuffer(ITG3200_RA_GYRO_XOUT_H, 6, buffer);
- data.XAxis = (short) (((buffer[0]) << 8) | buffer[1]);
- data.YAxis = (short) (((buffer[2]) << 8) | buffer[3]);
- data.ZAxis = (short) (((buffer[4]) << 8) | buffer[5]);
+ data.XAxis = (short) ((((short) buffer[0]) << 8) | buffer[1]);
+ data.YAxis = (short) ((((short) buffer[2]) << 8) | buffer[3]);
+ data.ZAxis = (short) ((((short) buffer[4]) << 8) | buffer[5]);
return data;
}
public short ReadShortFromRegister(byte register, int count) {
byte[] buffer = new byte[count];
ReadI2CBuffer(register, count, buffer);
- return (short) (((buffer[0]) << 8) | buffer[1]);
+ return (short) ((((short) buffer[0]) << 8) | buffer[1]);
}
/**
* Get X-axis gyroscope reading.
- *
+ *
* @return X-axis rotation measurement in 16-bit 2's complement format
* @see ITG3200_RA_GYRO_XOUT_H
*/
/**
* Get Y-axis gyroscope reading.
- *
+ *
* @return Y-axis rotation measurement in 16-bit 2's complement format
* @see ITG3200_RA_GYRO_YOUT_H
*/
/**
* Get Z-axis gyroscope reading.
- *
+ *
* @return Z-axis rotation measurement in 16-bit 2's complement format
* @see ITG3200_RA_GYRO_ZOUT_H
*/
public short getRotationZ() {
- return ReadShortFromRegister(ITG3200_RA_GYRO_ZOUT_H, 0);
+ return ReadShortFromRegister(ITG3200_RA_GYRO_ZOUT_H, 2);
}
// PWR_MGM register
/**
* Trigger a full device reset. A small delay of ~50ms may be desirable after
* triggering a reset.
- *
+ *
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_H_RESET_BIT
*/
* standby current. Clearing this bit puts the device back into normal mode.
* To save power, the individual standby selections for each of the gyros
* should be used if any gyro axis is not used by the application.
- *
+ *
* @return Current sleep mode enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_SLEEP_BIT
/**
* Set sleep mode status.
- *
+ *
* @param enabled
* New sleep mode enabled status
* @see getSleepEnabled()
/**
* Get X-axis standby enabled status. If enabled, the X-axis will not gather
* or report data (or use power).
- *
+ *
* @return Current X-axis standby enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_XG_BIT
/**
* Set X-axis standby enabled status.
- *
+ *
* @param New
* X-axis standby enabled status
* @see getStandbyXEnabled()
/**
* Get Y-axis standby enabled status. If enabled, the Y-axis will not gather
* or report data (or use power).
- *
+ *
* @return Current Y-axis standby enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_YG_BIT
/**
* Set Y-axis standby enabled status.
- *
+ *
* @param New
* Y-axis standby enabled status
* @see getStandbyYEnabled()
/**
* Get Z-axis standby enabled status. If enabled, the Z-axis will not gather
* or report data (or use power).
- *
+ *
* @return Current Z-axis standby enabled status
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_STBY_ZG_BIT
/**
* Set Z-axis standby enabled status.
- *
+ *
* @param New
* Z-axis standby enabled status
* @see getStandbyZEnabled()
/**
* Get clock source setting.
- *
+ *
* @return Current clock source setting
* @see ITG3200_RA_PWR_MGM
* @see ITG3200_PWR_CLK_SEL_BIT
/*
* (non-Javadoc)
- *
+ *
* @see edu.wpi.first.wpilibj.Sendable#getSmartDashboardType()
*/
@Override
/*
* (non-Javadoc)
- *
+ *
* @see
* edu.wpi.first.wpilibj.livewindow.LiveWindowSendable#startLiveWindowMode()
*/
/*
* (non-Javadoc)
- *
+ *
* @see
* edu.wpi.first.wpilibj.livewindow.LiveWindowSendable#stopLiveWindowMode()
*/
/*
* (non-Javadoc)
- *
+ *
* @see edu.wpi.first.wpilibj.PIDSource#pidGet()
*/
@Override
// TODO Auto-generated method stub
return null;
}
-
}
--- /dev/null
+/**
+ * Copyright (c) 2015, www.techhounds.com
+ * All rights reserved.
+ *
+ * <p>
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * </p>
+ * <ul>
+ * <li>Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.</li>
+ * <li>Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.</li>
+ * <li>Neither the name of the www.techhounds.com nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.</li>
+ * </ul>
+ *
+ * <p>
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * </p>
+ */
+
+package org.usfirst.frc.team3501.robot;
+
+import edu.wpi.first.wpilibj.PIDSource;
+
+/**
+ * A rotation tracker allows you to keep track of how far the robot has rotated
+ * since the object was constructed or zeroed.
+ *
+ * <p>
+ * This object is most useful when you want to turn your robot a particular
+ * number of degrees, or when you want to detect whether your robot is tipping.
+ * </p>
+ *
+ * <p>
+ * You will typically use the {@link #getRotationTracker} method associated with
+ * the gyro class associated with your hardware. For example
+ * {@link GyroItg3200.getRotationTrackerZ()}.
+ * </p>
+ */
+public interface RotationTracker extends PIDSource {
+
+ /**
+ * Returns the angle in signed decimal degrees since construction or zeroing.
+ *
+ * <p>
+ * This value is used as the PID sensor value.
+ * </p>
+ *
+ * @return Number of degrees the robot has rotated in the range of [-INF,
+ * +INF]. Positive values indicate clockwise rotation (720 means it
+ * has spun around twice and is facing the same direction).
+ */
+ public double getAngle();
+
+ /**
+ * Zeros the rotation tracker so the current direction we are pointing now
+ * becomes the zero point.
+ */
+ public void zero();
+
+}