Dependents:   Lab6_Gyro

Files at this revision

API Documentation at this revision

Comitter:
brunostgr
Date:
Tue Dec 17 17:49:17 2019 +0000
Commit message:
Asdf

Changed in this revision

ITG3200.cpp Show annotated file Show diff for this revision Revisions of this file
ITG3200.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ITG3200.cpp	Tue Dec 17 17:49:17 2019 +0000
@@ -0,0 +1,114 @@
+
+/**
+ * Includes
+ */
+#include "ITG3200.h"
+
+ITG3200::ITG3200(PinName sda, PinName scl, int i2cAddressW, int i2cAddressR) {
+
+    i2c = new I2C(sda, scl);
+    // Gyro designed to work at 400KHz. See datasheet for details.
+    i2c->frequency(100000);
+    i2cAddW = i2cAddressW;
+    i2cAddR = i2cAddressR;   
+
+}
+
+void ITG3200::Init(void){
+    wait_ms(50);    // Temps necessaire pour le demarrage du gyro     
+    char registerNumber[2] = {0x16,0x18};
+    char test = 0x3E;
+    char registerContents[1] = {};
+    i2c->write(i2cAddW, registerNumber, 2);
+    
+    i2c->write(i2cAddW, &test, 1);
+    i2c->read(i2cAddR, registerContents, 1);
+    registerContents[0] = registerContents[0] | 0x02;
+    i2c->write(i2cAddW, &registerContents[0], 1);
+}
+
+int ITG3200::GetX(void){
+
+    char registerNumber = GYRO_XOUT_H;
+    char registerContents[2] = {0x00, 0x00};
+    
+    //First, send the number of register we wish to read,
+    //in this case, register numbers 2, 3, which hold the
+    //compass bearing as a 16-bit word.
+    i2c->write(i2cAddW, &registerNumber, 1);
+    
+    //Now read two bytes which will be the contents of
+    //these registers.
+    i2c->read(i2cAddR, registerContents, 2);
+    
+    //Register 2 [read first], was the high byte, followed by
+    //register 3 [read second], which was the low byte.
+
+    return (int16_t)((registerContents[0] << 8) | registerContents[1]);
+    
+}
+
+int ITG3200::GetY(void){
+    
+    char registerNumber = GYRO_YOUT_H;
+    char registerContents[2] = {0x00, 0x00};
+    
+    //First, send the number of register we wish to read,
+    //in this case, register numbers 2, 3, which hold the
+    //compass bearing as a 16-bit word.
+    i2c->write(i2cAddW, &registerNumber, 1);
+    
+    //Now read two bytes which will be the contents of
+    //these registers.
+    i2c->read(i2cAddR, registerContents, 2);
+    
+    //Register 2 [read first], was the high byte, followed by
+    //register 3 [read second], which was the low byte.
+
+    return (int16_t)((registerContents[0] << 8) | registerContents[1]);
+
+}
+    
+int ITG3200::GetZ(void){
+    
+    char registerNumber = GYRO_ZOUT_H;
+    char registerContents[2] = {0x00, 0x00};
+    
+    //First, send the number of register we wish to read,
+    //in this case, register numbers 2, 3, which hold the
+    //compass bearing as a 16-bit word.
+    i2c->write(i2cAddW, &registerNumber, 1);
+    
+    //Now read two bytes which will be the contents of
+    //these registers.
+    i2c->read(i2cAddR, registerContents, 2);
+    
+    //Register 2 [read first], was the high byte, followed by
+    //register 3 [read second], which was the low byte.
+    
+    return (int16_t)((registerContents[0] << 8) | registerContents[1]);
+
+}
+
+int ITG3200::GetTemp(void){
+    
+    char registerNumber = GYRO_TEMP_H;
+    char registerContents[2] = {0x00, 0x00};
+    uint16_t temp;
+    //First, send the number of register we wish to read,
+    //in this case, register numbers 2, 3, which hold the
+    //compass bearing as a 16-bit word.
+    i2c->write(i2cAddW, &registerNumber, 1);
+    
+    //Now read two bytes which will be the contents of
+    //these registers.
+    i2c->read(i2cAddR, registerContents, 2);
+    
+    //Register 2 [read first], was the high byte, followed by
+    //register 3 [read second], which was the low byte.
+
+    temp = (int16_t)((registerContents[0] << 8) | registerContents[1]);
+    temp = (temp / 280) - 150;      // 280LSB / °C - offset du capteur
+    return temp;
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ITG3200.h	Tue Dec 17 17:49:17 2019 +0000
@@ -0,0 +1,80 @@
+
+
+#ifndef ITG3200_H
+#define ITG3200_H
+
+/**
+ * Includes
+ */
+#include "mbed.h"
+
+/**
+ * Defines
+ */
+#define ITG3200_DEFAULT_I2C_ADDRESS 0b11010000
+
+//-----------
+// Registers
+//-----------
+#define GYRO_XOUT_H 0x1D
+#define GYRO_YOUT_H 0x1F
+#define GYRO_ZOUT_H 0x21
+#define GYRO_TEMP_H 0x1B
+
+/**
+ * ITG3200 digital compass module.
+ */
+class ITG3200 {
+
+    I2C* i2c;
+    int  i2cAddW,i2cAddR;
+
+public:
+
+    /**
+     * Constructor.
+     *
+     * @param sda mbed pin to use for I2C SDA
+     * @param scl mbed pin to use for I2C SCL
+     * @param address I2C address of this device.
+     */
+    ITG3200(PinName sda, PinName scl, int i2cAddressW, int i2cAddressR);
+
+    void Init(void);
+
+
+    /** 
+     * Reads the current X-axis rotational rate(roll) of the gyroscope.
+     *
+     * @return roll integer.
+     *         
+     */
+    int GetX(void);
+    
+    /**
+     * Reads the current Y-axis rotational rate(pitch) of the gyroscope.
+     *
+     * @return pitch integer.
+     *         
+     */
+    int GetY(void);
+    
+    /**
+     * Reads the current Z-axis rotational rate(yaw) of the gyroscope.
+     *
+     * @return yaw integer.
+     *         
+     */
+    int GetZ(void);
+    
+    /**
+     * Reads the current temperature of the gyroscope's die.
+     *
+     * @return temperature integer.
+     *         
+     */
+    int GetTemp(void);
+
+};
+
+#endif /* ITG3200_H */