Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:d4c2464a3868, committed 2015-06-09
- Comitter:
- JackyZhangFromSeeed
- Date:
- Tue Jun 09 10:18:45 2015 +0000
- Commit message:
- grove_gyroscope
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_gyroscope.cpp Tue Jun 09 10:18:45 2015 +0000
@@ -0,0 +1,107 @@
+
+
+#include "suli2.h"
+#include "grove_gyroscope.h"
+
+
+
+//local functions
+static char _read_char(I2C_T *i2c, unsigned char addr);
+static double grove_gyro_gettemperature(I2C_T *i2c);
+static void grove_gyro_getxyz(I2C_T *i2c, int16_t *x,int16_t *y,int16_t *z);
+
+//local variables
+static unsigned char cmdbuf[2];
+static unsigned char databuf[2];
+static int16_t x_offset;
+static int16_t y_offset;
+static int16_t z_offset;
+
+// Read 1 byte from I2C
+static char _read_char(I2C_T *i2c, unsigned char addr)
+{
+ suli_i2c_write(i2c, GYRO_ADDRESS, &addr, 1);
+ suli_i2c_read(i2c, GYRO_ADDRESS, databuf, 1);
+ return databuf[0];
+}
+
+void grove_gyroscope_init(I2C_T *i2c, int pinsda, int pinscl)
+{
+ suli_i2c_init(i2c, pinsda, pinscl);
+}
+
+bool grove_gyro_write_setup(I2C_T *i2c)
+{
+ cmdbuf[0] = ITG3200_PWR_M;
+ cmdbuf[1] = 0x80;
+ suli_i2c_write(i2c, GYRO_ADDRESS, cmdbuf, 2);//send a reset to the device
+ cmdbuf[0] = ITG3200_SMPL;
+ cmdbuf[1] = 0x00;
+ suli_i2c_write(i2c, GYRO_ADDRESS, cmdbuf, 2);//sample rate divider
+ cmdbuf[0] = ITG3200_DLPF;
+ cmdbuf[1] = 0x18;
+ suli_i2c_write(i2c, GYRO_ADDRESS, cmdbuf, 2);//+/-2000 degrees/s (default value)
+
+ return true;
+}
+
+/*Function: Get the temperature from ITG3200 that with a on-chip*/
+/* temperature sensor. */
+static double grove_gyro_gettemperature(I2C_T *i2c)
+{
+ int temp;
+ double temperature;
+ //temp = read(ITG3200_TMP_H, ITG3200_TMP_L);
+ temp = (_read_char(i2c, ITG3200_TMP_H) << 8) + _read_char(i2c, ITG3200_TMP_L);
+ temperature = 35+ ((double) (temp + 13200)) / 280;
+ return(temperature);
+}
+
+/*Function: Get the contents of the registers in the ITG3200*/
+/* so as to calculate the angular velocity. */
+static void grove_gyro_getxyz(I2C_T *i2c, int16_t *x,int16_t *y,int16_t *z)
+{
+ *x = (_read_char(i2c, ITG3200_GX_H) << 8) + _read_char(i2c, ITG3200_GX_L) + x_offset;
+ *y = (_read_char(i2c, ITG3200_GY_H) << 8) + _read_char(i2c, ITG3200_GY_L) + y_offset;
+ *z = (_read_char(i2c, ITG3200_GZ_H) << 8) + _read_char(i2c, ITG3200_GZ_L) + z_offset;
+}
+
+/*Function: Get the angular velocity and its unit is degree per second.*/
+bool grove_gyro_getangularvelocity(I2C_T *i2c, float *ax,float *ay,float *az)
+{
+ int16_t x,y,z;
+ grove_gyro_getxyz(i2c, &x,&y,&z);
+ *ax = x/14.375;
+ *ay = y/14.375;
+ *az = z/14.375;
+
+ return true;
+}
+
+bool grove_gyro_zerocalibrate(I2C_T *i2c)
+{
+ int16_t x_offset_temp;
+ int16_t y_offset_temp;
+ int16_t z_offset_temp;
+ int16_t x,y,z;
+ x_offset = 0;
+ y_offset = 0;
+ z_offset = 0;
+
+ for (int i = 0;i < 200;i++){
+ suli_delay_ms(10);
+ grove_gyro_getxyz(i2c, &x,&y,&z);
+ x_offset_temp += x;
+ y_offset_temp += y;
+ z_offset_temp += z;
+ }
+
+ x_offset = abs(x_offset_temp)/200;
+ y_offset = abs(y_offset_temp)/200;
+ z_offset = abs(z_offset_temp)/200;
+ if(x_offset_temp > 0)x_offset = -x_offset;
+ if(y_offset_temp > 0)y_offset = -y_offset;
+ if(z_offset_temp > 0)z_offset = -z_offset;
+
+ return true;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/grove_gyroscope.h Tue Jun 09 10:18:45 2015 +0000 @@ -0,0 +1,33 @@ + + + +#ifndef __GROVE_GYROSCOPE_LIGHT_H__ +#define __GROVE_GYROSCOPE_LIGHT_H__ + +#include "suli2.h" + +#define GYRO_ADDRESS (0x68<<1) + +// ITG3200 Register Defines +#define ITG3200_WHO 0x00 +#define ITG3200_SMPL 0x15 +#define ITG3200_DLPF 0x16 +#define ITG3200_INT_C 0x17 +#define ITG3200_INT_S 0x1A +#define ITG3200_TMP_H 0x1B +#define ITG3200_TMP_L 0x1C +#define ITG3200_GX_H 0x1D +#define ITG3200_GX_L 0x1E +#define ITG3200_GY_H 0x1F +#define ITG3200_GY_L 0x20 +#define ITG3200_GZ_H 0x21 +#define ITG3200_GZ_L 0x22 +#define ITG3200_PWR_M 0x3E + + +void grove_gyroscope_init(I2C_T *i2c, int pinsda, int pinscl); +bool grove_gyro_write_setup(I2C_T *i2c); +bool grove_gyro_getangularvelocity(I2C_T *i2c, float *ax,float *ay,float *az); +bool grove_gyro_zerocalibrate(I2C_T *i2c); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_gyroscope_class.cpp Tue Jun 09 10:18:45 2015 +0000
@@ -0,0 +1,24 @@
+
+
+#include "grove_gyroscope_class.h"
+
+GroveGyroscope::GroveGyroscope(int pinsda, int pinscl)
+{
+ this->i2c = (I2C_T *)malloc(sizeof(I2C_T));
+ grove_gyroscope_init(this->i2c, pinsda, pinscl);
+}
+
+bool GroveGyroscope::write_setup(void)
+{
+ return grove_gyro_write_setup(this->i2c);
+}
+
+bool GroveGyroscope::read_gyroscope(float *gx, float *gy, float *gz)
+{
+ return grove_gyro_getangularvelocity(this->i2c, gx, gy, gz);
+}
+
+bool GroveGyroscope::write_zerocalibrate(void)
+{
+ return grove_gyro_zerocalibrate(this->i2c);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_gyroscope_class.h Tue Jun 09 10:18:45 2015 +0000
@@ -0,0 +1,25 @@
+
+
+
+#ifndef __GROVE_GYROSCOPE_CLASS_H__
+#define __GROVE_GYROSCOPE_CLASS_H__
+
+#include "grove_gyroscope.h"
+
+//GROVE_NAME "Grove_Gyroscope"
+//IF_TYPE I2C
+//IMAGE_URL http://www.seeedstudio.com/wiki/File:Gbgr.jpg
+
+class GroveGyroscope
+{
+public:
+ GroveGyroscope(int pinsda, int pinscl);
+ bool write_setup(void);
+ bool read_gyroscope(float *gx, float *gy, float *gz);
+ bool write_zerocalibrate(void);
+
+private:
+ I2C_T *i2c;
+};
+
+#endif