aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Sanson <jamie.sanson@outlook.com>2016-06-07 17:05:50 +1200
committerJamie Sanson <jamie.sanson@outlook.com>2016-06-07 17:05:50 +1200
commitc2ee9af63d07e1a846e99121101e8026e754bb7f (patch)
treec6ae59f480ac88490bce327dc7f0cb454970f635
parentb4c7039db64e6b7685d2b4b9cd350f7e7a9c27b0 (diff)
downloadmetis-1-onboard-c2ee9af63d07e1a846e99121101e8026e754bb7f.tar.gz
metis-1-onboard-c2ee9af63d07e1a846e99121101e8026e754bb7f.tar.bz2
metis-1-onboard-c2ee9af63d07e1a846e99121101e8026e754bb7f.zip
Remove redundant method from library and initialise properly
-rw-r--r--test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.cpp48
-rw-r--r--test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.h18
-rw-r--r--test_flight_basic_logging/test_flight_basic_logging.ino35
3 files changed, 43 insertions, 58 deletions
diff --git a/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.cpp b/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.cpp
index ba8b1a3..f3e5b54 100644
--- a/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.cpp
+++ b/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.cpp
@@ -2,34 +2,11 @@
#include "MPU9250_helper.h"
#include "i2c_t3.h"
-// Set initial input parameters
-enum Ascale {
- AFS_2G = 0,
- AFS_4G,
- AFS_8G,
- AFS_16G
-};
-
-enum Gscale {
- GFS_250DPS = 0,
- GFS_500DPS,
- GFS_1000DPS,
- GFS_2000DPS
-};
-
-enum Mscale {
- MFS_14BITS = 0, // 0.6 mG per LSB
- MFS_16BITS // 0.15 mG per LSB
-};
-
-uint8_t Gscale = GFS_250DPS;
-uint8_t Ascale = AFS_8G;
-uint8_t Mscale = MFS_16BITS; // Choose either 14-bit or 16-bit magnetometer resolution
-uint8_t Mmode = 0x02; // 2 for 8 Hz, 6 for 100 Hz continuous magnetometer data read
-
-
-MPU9250_helper::MPU9250_helper(){
-
+MPU9250_helper::MPU9250_helper(uint8_t AScale, uint8_t GScale, uint8_t MScale, uint8_t Mmode){
+ _gscale = GScale;
+ _ascale = AScale;
+ _mscale = MScale;
+ _mmode = Mmode;
}
void MPU9250_helper::initMPU9250()
@@ -61,7 +38,7 @@ void MPU9250_helper::initMPU9250()
// c = c & ~0xE0; // Clear self-test bits [7:5]
c = c & ~0x02; // Clear Fchoice bits [1:0]
c = c & ~0x18; // Clear AFS bits [4:3]
- c = c | Gscale << 3; // Set full scale range for the gyro
+ c = c | _gscale << 3; // Set full scale range for the gyro
// c =| 0x00; // Set Fchoice for the gyro to 11 by writing its inverse to bits 1:0 of GYRO_CONFIG
writeByte(MPU9250_ADDRESS, GYRO_CONFIG, c ); // Write new GYRO_CONFIG value to register
@@ -69,7 +46,7 @@ void MPU9250_helper::initMPU9250()
c = readByte(MPU9250_ADDRESS, ACCEL_CONFIG); // get current ACCEL_CONFIG register value
// c = c & ~0xE0; // Clear self-test bits [7:5]
c = c & ~0x18; // Clear AFS bits [4:3]
- c = c | Ascale << 3; // Set full scale range for the accelerometer
+ c = c | _ascale << 3; // Set full scale range for the accelerometer
writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, c); // Write new ACCEL_CONFIG register value
// Set accelerometer sample rate configuration
@@ -92,15 +69,6 @@ void MPU9250_helper::initMPU9250()
delay(100);
}
-void MPU9250_helper::waitForInput()
-{
- while(!Serial.available()){};
-
- while(Serial.available()){
- Serial.read();
- };
-}
-
void MPU9250_helper::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
Wire1.beginTransmission(address); // Initialize the Tx buffer
@@ -144,7 +112,7 @@ void MPU9250_helper::readAccelData(int16_t * destination)
float MPU9250_helper::getAccelRes()
{
- switch (Ascale)
+ switch (_ascale)
{
// Possible accelerometer scales (and their register bit settings) are:
// 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11).
diff --git a/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.h b/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.h
index 86463df..729777e 100644
--- a/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.h
+++ b/test_flight_basic_logging/libraries/MPU9250_helper/MPU9250_helper.h
@@ -129,15 +129,27 @@
#define ZA_OFFSET_H 0x7D
#define ZA_OFFSET_L 0x7E
+#define AFS_2G 0
+#define AFS_4G 1
+#define AFS_8G 2
+#define AFS_16G 3
+
+#define GFS_250DPS 0
+#define GFS_500DPS 1
+#define GFS_1000DPS 2
+#define GFS_2000DPS 3
+
+#define MFS_14BITS 0
+#define MFS_16BITS 1
+
// Library function prototypes
class MPU9250_helper
{
public:
- MPU9250_helper();
+ MPU9250_helper(uint8_t AScale, uint8_t GScale, uint8_t MScale, uint8_t Mmode);
// init and basic helpers
void initMPU9250();
- void waitForInput();
// utility functions
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
@@ -147,6 +159,8 @@ class MPU9250_helper
// data specific functions
void readAccelData(int16_t * destination);
float getAccelRes();
+ private:
+ uint8_t _ascale, _gscale, _mscale, _mmode;
};
#endif
diff --git a/test_flight_basic_logging/test_flight_basic_logging.ino b/test_flight_basic_logging/test_flight_basic_logging.ino
index 2d71754..af9b48e 100644
--- a/test_flight_basic_logging/test_flight_basic_logging.ino
+++ b/test_flight_basic_logging/test_flight_basic_logging.ino
@@ -28,41 +28,44 @@
#include <SPI.h>
#include <MPU9250_helper.h>
-int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output
+// Defining the scaling of sensor values
+int8_t Ascale = AFS_16G;
+int8_t Gscale = GFS_250DPS;
+int8_t Mscale = MFS_16BITS; // Choose either 14-bit or 16-bit magnetometer resolution
+int8_t Mmode = 0x02; // 2 for 8Hz, 6 for 100Hz continuous
+
+// Stores the 16-bit signed accelerometer sensor output
+int16_t accelCount[3];
float aRes;
float ax,ay,az;
-MPU9250_helper sensor_helper;
+MPU9250_helper helper(Ascale, Gscale, Mscale, Mmode);
void setup() {
Wire1.begin(I2C_MASTER, 0x00, I2C_PINS_29_30, I2C_PULLUP_EXT, I2C_RATE_400);
- Serial.begin(38400);
-
- sensor_helper.waitForInput();
+ Serial.begin(115200);
+ while(!Serial.available()){}
+
Serial.println("Reading whoami byte (using wire1)");
- byte c = sensor_helper.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250
+ byte c = helper.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); Serial.print(" I should be "); Serial.println(0x71, HEX);
- delay(1000);
- sensor_helper.initMPU9250();
+ helper.initMPU9250();
}
void loop() {
- sensor_helper.readAccelData(accelCount); // Read the x/y/z adc values
- aRes = sensor_helper.getAccelRes();
+ helper.readAccelData(accelCount); // Read the x/y/z adc values
+ aRes = helper.getAccelRes();
- // Now we'll calculate the accleration value into actual g's
- ax = (float)accelCount[0]*aRes; // - accelBias[0]; // get actual g value, this depends on scale being set
- ay = (float)accelCount[1]*aRes; // - accelBias[1];
- az = (float)accelCount[2]*aRes; // - accelBias[2];
+ ax = (float)accelCount[0]*aRes;
+ ay = (float)accelCount[1]*aRes;
+ az = (float)accelCount[2]*aRes;
Serial.print("X: "); Serial.print(ax, DEC);
Serial.print(" Y: "); Serial.print(ay, DEC);
Serial.print(" Z: "); Serial.println(az, DEC);
-
- delay(100);
}