Shih-Ho Hsieh / Mbed 2 deprecated pololu5mag_with_platform

Dependencies:   mbed

Fork of Motor_XYZ_UI_SPI_I2C_5mag by Shih-Ho Hsieh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers altimu_10_v5.cpp Source File

altimu_10_v5.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    altimu_10_v5.cpp
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    08-October-2014
00007  * @brief   Implementation file for the AltIMU_10_v5 singleton class
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036 */ 
00037     
00038 /* Includes ------------------------------------------------------------------*/
00039 #include "mbed.h"
00040 #include "altimu_10_v5.h"
00041 
00042 /* Static variables ----------------------------------------------------------*/
00043 AltIMU_10_v5* AltIMU_10_v5::_instance0 = NULL;
00044 AltIMU_10_v5* AltIMU_10_v5::_instance1 = NULL;
00045 
00046 
00047 /* Methods -------------------------------------------------------------------*/
00048 /**
00049  * @brief  Constructor
00050  */
00051 AltIMU_10_v5::AltIMU_10_v5(DevI2C *ext_i2c, uint8_t SA0) : dev_i2c(ext_i2c),
00052     magnetometer(new LIS3MDL(*dev_i2c,SA0)),
00053     pt_sensor(new LPS25H(*dev_i2c,SA0)),
00054     gyro_lsm6ds33(new LSM6DS33(*dev_i2c,SA0))
00055 { 
00056 }
00057 
00058 /**
00059  * @brief     Get singleton instance
00060  * @return    a pointer to the initialized singleton instance of class AltIMU_10_v5.
00061  *            A return value of NULL indicates an out of memory situation.
00062  * @param[in] ext_i2c (optional) pointer to an instance of DevI2C to be used
00063  *            for communication on the expansion board. 
00064  *            Defaults to NULL.
00065  *            Taken into account only on the very first call of one of the 'Instance' functions.
00066  *            If not provided a new DevI2C will be created with standard
00067  *            configuration parameters.
00068  *            The used DevI2C object gets saved in instance variable dev_i2c.
00069  * @param[in] ff_irq_pin (optional) PinName of the pin associated to asynchronous 
00070  *            (i.e. interrupt based) free fall detection in case a LSM6DS33 3D 
00071  *            Acceleromenter and 3D Gyroscope is mounted on top of the DIL 24-pin socket.
00072  *            Defaults to F746ZG_PIN_FF.
00073  *            Taken into account only on the very first call of one of the 'Instance' functions.
00074  *            A value of 'NC' will avoid instantiation of the LSM6DS33 even if present.
00075  */
00076 AltIMU_10_v5* AltIMU_10_v5::Instance(DevI2C *ext_i2c, uint8_t SA0) {
00077     if(_instance0 == NULL && SA0 == 0) {
00078         if(ext_i2c == NULL)
00079             ext_i2c = new DevI2C(F746ZG_PIN_I2C_SDA, F746ZG_PIN_I2C_SCL);
00080 
00081         if(ext_i2c != NULL)
00082             _instance0 = new AltIMU_10_v5(ext_i2c, 0);
00083     
00084         if(_instance0 != NULL) {
00085             bool ret = _instance0->Init();
00086             if(!ret) {
00087                 error("Failed to init AltIMU_10_v5 expansion board for SA0=0!\n");
00088             }
00089         }
00090     }
00091     else if(_instance1 == NULL && SA0 == 1) {
00092         if(ext_i2c == NULL)
00093             ext_i2c = new DevI2C(F746ZG_PIN_I2C_SDA, F746ZG_PIN_I2C_SCL);
00094 
00095         if(ext_i2c != NULL)
00096             _instance1 = new AltIMU_10_v5(ext_i2c, 1);
00097     
00098         if(_instance1 != NULL) {
00099             bool ret = _instance1->Init();
00100             if(!ret) {
00101                 error("Failed to init AltIMU_10_v5 expansion board for SA0=1!\n");
00102             }
00103         }
00104     }
00105 
00106     return (SA0 == 0 ? _instance0 :
00107             (SA0 == 1 ? _instance1 : NULL));
00108 }
00109 
00110 /**
00111  * @brief     Get singleton instance
00112  * @return    a pointer to the initialized singleton instance of class AltIMU_10_v5.
00113  *            A return value of NULL indicates an out of memory situation.
00114  * @param[in] sda I2C data line pin.
00115  *            Taken into account only on the very first call of one of the 'Instance' functions.
00116  *            A new DevI2C will be created based on parameters 'sda' and 'scl'.
00117  *            The used DevI2C object gets saved in instance variable dev_i2c.
00118  * @param[in] scl I2C clock line pin.
00119  *            Taken into account only on the very first call of one of the 'Instance' functions.
00120  *            A new DevI2C will be created based on parameters 'sda' and 'scl'.
00121  *            The used DevI2C object gets saved in instance variable dev_i2c.
00122  * @param[in] ff_irq_pin (optional) PinName of the pin associated to asynchronous 
00123  *            (i.e. interrupt based) free fall detection in case a LSM6DS33 3D 
00124  *            Acceleromenter and 3D Gyroscope is mounted on top of the DIL 24-pin socket.
00125  *            Defaults to NC.
00126  *            Taken into account only on the very first call of one of the 'Instance' functions.
00127  *            A value of 'NC' will avoid instantiation of the LSM6DS33 even if present.
00128  */
00129 AltIMU_10_v5* AltIMU_10_v5::Instance(PinName sda, PinName scl, uint8_t SA0) {
00130     DevI2C *ext_i2c = new DevI2C(sda, scl);
00131 
00132     if(ext_i2c != NULL) return Instance(ext_i2c, SA0);
00133 
00134     return NULL;
00135 }
00136 
00137 
00138 /**
00139  * @brief  Initialize the singleton's magnetometer
00140  * @retval true if initialization successful, 
00141  * @retval false otherwise
00142  */
00143 bool AltIMU_10_v5::Init_LIS3MDL(void) {
00144     uint8_t m_id = 0;
00145     MAGNETO_InitTypeDef InitStructure;
00146 
00147     /* Check presence */
00148     if((magnetometer->read_id(&m_id) != MAGNETO_OK) ||
00149        (m_id != I_AM_LIS3MDL_M))
00150         {
00151             delete magnetometer;
00152             magnetometer = NULL;
00153             return true;
00154         }
00155       
00156     /* Configure sensor */
00157     InitStructure.M_FullScale = 4.0f;
00158     InitStructure.M_OperatingMode = LIS3MDL_M_MD_CONTINUOUS;
00159     InitStructure.M_XYOperativeMode = LIS3MDL_M_OM_HP;
00160     InitStructure.M_OutputDataRate = 1000.0f;
00161 
00162     if(magnetometer->init(&InitStructure) != MAGNETO_OK)
00163         {
00164             return false;
00165         }
00166       
00167     return true;
00168 }
00169 
00170 /**
00171  * @brief  Initialize the singleton's pressure sensor
00172  * @retval true if initialization successful, 
00173  * @retval false otherwise
00174  */
00175 bool AltIMU_10_v5::Init_LPS25H(void) {
00176     uint8_t p_id = 0;
00177     PRESSURE_InitTypeDef InitStructure;
00178     
00179     /* Check presence */
00180     if((pt_sensor->read_id(&p_id) != PRESSURE_OK) ||
00181        (p_id != I_AM_LPS25H))
00182         {
00183             delete pt_sensor;
00184             pt_sensor = NULL;
00185             return true;
00186         }
00187             
00188     /* Configure sensor */
00189     InitStructure.OutputDataRate = LPS25H_ODR_1Hz;
00190     InitStructure.BlockDataUpdate = LPS25H_BDU_CONT;
00191     InitStructure.DiffEnable = LPS25H_DIFF_ENABLE;
00192     InitStructure.SPIMode = LPS25H_SPI_SIM_3W;
00193     InitStructure.PressureResolution = LPS25H_P_RES_AVG_32;
00194     InitStructure.TemperatureResolution = LPS25H_T_RES_AVG_16;
00195         
00196     if(pt_sensor->init(&InitStructure) != PRESSURE_OK)
00197         {
00198             return false;
00199         }
00200             
00201     return true;
00202 }
00203 
00204 /**
00205  * @brief  Initialize the singleton's LSM6DS33 gyroscope
00206  * @retval true if initialization successful, 
00207  * @retval false otherwise
00208  */
00209 bool AltIMU_10_v5::Init_LSM6DS33(void) {
00210     IMU_6AXES_InitTypeDef InitStructure;
00211     uint8_t xg_id = 0;
00212     bool status=true;
00213 
00214     /* Check presence */
00215     if((status=(gyro_lsm6ds33->read_id(&xg_id) != IMU_6AXES_OK)) ||
00216        (xg_id != I_AM_LSM6DS33_XG))
00217         {
00218             delete gyro_lsm6ds33;
00219             gyro_lsm6ds33 = NULL;
00220             return true;
00221         }
00222             
00223     /* Configure sensor */
00224     InitStructure.G_FullScale       = 2000.0f; /* 2000DPS */
00225     InitStructure.G_OutputDataRate  = 119.0f;  /* 119HZ */
00226     InitStructure.G_X_Axis          = 1;       /* Enable */
00227     InitStructure.G_Y_Axis          = 1;       /* Enable */
00228     InitStructure.G_Z_Axis          = 1;       /* Enable */
00229 
00230     InitStructure.X_FullScale       = 2.0f;    /* 2G */
00231     InitStructure.X_OutputDataRate  = 119.0f;  /* 119HZ */
00232     InitStructure.X_X_Axis          = 1;       /* Enable */
00233     InitStructure.X_Y_Axis          = 1;       /* Enable */
00234     InitStructure.X_Z_Axis          = 1;       /* Enable */
00235               
00236     if(gyro_lsm6ds33->init(&InitStructure) != IMU_6AXES_OK)
00237         {
00238             return false; 
00239         }
00240             
00241     return true;
00242 }