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.
Dependencies: SoftSerial MAX14690 Buffer
Fork of rtos_threading_with_callback by
i2cData.cpp
00001 #include "mbed.h" 00002 #include "MPU6050.h" 00003 #include "BMP180.h" 00004 #include "bmi160.h" 00005 #include "rtos.h" 00006 #include "global.h" 00007 00008 class AcquireI2cData 00009 { 00010 public: 00011 static void run() 00012 { 00013 BMI160::SensorData accData; 00014 BMI160::SensorData gyroData; 00015 BMI160::SensorTime sensorTime; 00016 for (;;) { 00017 imu.read(gyro1, gyro1+1, gyro1+2,acc1, acc1+1, acc1+2); 00018 bmp.ReadData(&temperature, &pressure); 00019 altitude=bmp.ReadAltitude(); 00020 imu2.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, BMI160::SENS_4G, BMI160::DPS_2000); 00021 acc2[0]=accData.xAxis.scaled; 00022 acc2[1]=accData.yAxis.scaled; 00023 acc2[2]=accData.zAxis.scaled; 00024 gyro2[0]=gyroData.xAxis.scaled; 00025 gyro2[1]=gyroData.yAxis.scaled; 00026 gyro2[2]=gyroData.zAxis.scaled; 00027 00028 { 00029 linkPacket *p = new linkPacket(); 00030 uint8_t *data = new uint8_t[24]; 00031 data[0]=((uint8_t*)&acc1[0])[0]; 00032 data[1]=((uint8_t*)&acc1[0])[1]; 00033 data[2]=((uint8_t*)&acc1[0])[2]; 00034 data[3]=((uint8_t*)&acc1[0])[3]; 00035 data[4]=((uint8_t*)&acc1[1])[0]; 00036 data[5]=((uint8_t*)&acc1[1])[1]; 00037 data[6]=((uint8_t*)&acc1[1])[2]; 00038 data[7]=((uint8_t*)&acc1[1])[3]; 00039 data[8]=((uint8_t*)&acc1[2])[0]; 00040 data[9]=((uint8_t*)&acc1[2])[1]; 00041 data[10]=((uint8_t*)&acc1[2])[2]; 00042 data[11]=((uint8_t*)&acc1[2])[3]; 00043 data[12]=((uint8_t*)&gyro1[0])[0]; 00044 data[13]=((uint8_t*)&gyro1[0])[1]; 00045 data[14]=((uint8_t*)&gyro1[0])[2]; 00046 data[15]=((uint8_t*)&gyro1[0])[3]; 00047 data[16]=((uint8_t*)&gyro1[1])[0]; 00048 data[17]=((uint8_t*)&gyro1[1])[1]; 00049 data[18]=((uint8_t*)&gyro1[1])[2]; 00050 data[19]=((uint8_t*)&gyro1[1])[3]; 00051 data[20]=((uint8_t*)&gyro1[2])[0]; 00052 data[21]=((uint8_t*)&gyro1[2])[1]; 00053 data[22]=((uint8_t*)&gyro1[2])[2]; 00054 data[23]=((uint8_t*)&gyro1[2])[3]; 00055 p->payload=data; 00056 p->payloadSz=24; 00057 p->priority =3; 00058 p->frameType = FRAMETYPE_IMU1; 00059 txQueue.addPacket(p); 00060 } 00061 { 00062 linkPacket *p = new linkPacket(); 00063 uint8_t *data = new uint8_t[24]; 00064 data[0]=((uint8_t*)&acc2[0])[0]; 00065 data[1]=((uint8_t*)&acc2[0])[1]; 00066 data[2]=((uint8_t*)&acc2[0])[2]; 00067 data[3]=((uint8_t*)&acc2[0])[3]; 00068 data[4]=((uint8_t*)&acc2[1])[0]; 00069 data[5]=((uint8_t*)&acc2[1])[1]; 00070 data[6]=((uint8_t*)&acc2[1])[2]; 00071 data[7]=((uint8_t*)&acc2[1])[3]; 00072 data[8]=((uint8_t*)&acc2[2])[0]; 00073 data[9]=((uint8_t*)&acc2[2])[1]; 00074 data[10]=((uint8_t*)&acc2[2])[2]; 00075 data[11]=((uint8_t*)&acc2[2])[3]; 00076 data[12]=((uint8_t*)&gyro2[0])[0]; 00077 data[13]=((uint8_t*)&gyro2[0])[1]; 00078 data[14]=((uint8_t*)&gyro2[0])[2]; 00079 data[15]=((uint8_t*)&gyro2[0])[3]; 00080 data[16]=((uint8_t*)&gyro2[1])[0]; 00081 data[17]=((uint8_t*)&gyro2[1])[1]; 00082 data[18]=((uint8_t*)&gyro2[1])[2]; 00083 data[19]=((uint8_t*)&gyro2[1])[3]; 00084 data[20]=((uint8_t*)&gyro2[2])[0]; 00085 data[21]=((uint8_t*)&gyro2[2])[1]; 00086 data[22]=((uint8_t*)&gyro2[2])[2]; 00087 data[23]=((uint8_t*)&gyro2[2])[3]; 00088 p->payload=data; 00089 p->payloadSz=24; 00090 p->priority =3; 00091 p->frameType = FRAMETYPE_IMU2; 00092 txQueue.addPacket(p); 00093 } 00094 { 00095 linkPacket *p = new linkPacket(); 00096 uint8_t *data = new uint8_t[4]; 00097 data[0]=((uint8_t*)&altitude)[0]; 00098 data[1]=((uint8_t*)&altitude)[1]; 00099 data[2]=((uint8_t*)&altitude)[2]; 00100 data[3]=((uint8_t*)&altitude)[3]; 00101 p->payload=data; 00102 p->payloadSz=4; 00103 p->priority =3; 00104 p->frameType = FRAMETYPE_ALTITUDE; 00105 txQueue.addPacket(p); 00106 } 00107 { 00108 linkPacket *p = new linkPacket(); 00109 uint8_t *data = new uint8_t[4]; 00110 data[0]=((uint8_t*)&pressure)[0]; 00111 data[1]=((uint8_t*)&pressure)[1]; 00112 data[2]=((uint8_t*)&pressure)[2]; 00113 data[3]=((uint8_t*)&pressure)[3]; 00114 p->payload=data; 00115 p->payloadSz=4; 00116 p->priority =3; 00117 p->frameType = FRAMETYPE_PRESSURE; 00118 txQueue.addPacket(p); 00119 } 00120 { 00121 linkPacket *p = new linkPacket(); 00122 uint8_t *data = new uint8_t[4]; 00123 data[0]=((uint8_t*)&temperature)[0]; 00124 data[1]=((uint8_t*)&temperature)[1]; 00125 data[2]=((uint8_t*)&temperature)[2]; 00126 data[3]=((uint8_t*)&temperature)[3]; 00127 p->payload=data; 00128 p->payloadSz=4; 00129 p->priority =3; 00130 p->frameType = FRAMETYPE_TEMPERATURE; 00131 txQueue.addPacket(p); 00132 } 00133 rtos::Thread::wait(1000); 00134 } 00135 } 00136 AcquireI2cData() 00137 { 00138 setupBMI160(); 00139 setupBMP(); 00140 setupMPU(); 00141 registerThread(AcquireI2cData::run); 00142 } 00143 static void setupBMI160() 00144 { 00145 //Power up sensors in normal mode 00146 imu2.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL); 00147 wait(0.1); 00148 imu2.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL); 00149 wait(0.1); 00150 BMI160::AccConfig accConfig; 00151 accConfig.range = BMI160::SENS_4G; 00152 accConfig.us = BMI160::ACC_US_OFF; 00153 accConfig.bwp = BMI160::ACC_BWP_2; 00154 accConfig.odr = BMI160::ACC_ODR_11; 00155 imu2.setSensorConfig(accConfig); //== BMI160::RTN_NO_ERROR 00156 BMI160::GyroConfig gyroConfig; 00157 BMI160::GyroConfig gyroConfigRead; 00158 gyroConfig.range = BMI160::DPS_2000; 00159 gyroConfig.bwp = BMI160::GYRO_BWP_2; 00160 gyroConfig.odr = BMI160::GYRO_ODR_11; 00161 imu2.setSensorConfig(gyroConfig); 00162 00163 00164 BMI160::SensorData accData; 00165 BMI160::SensorData gyroData; 00166 BMI160::SensorTime sensorTime; 00167 //Read feedback sensors 00168 imu2.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range); 00169 printf("BMI160\r\ngyro: %8.2f, %8.2f, %8.2f\r\nacc: %8.2f, %8.2f, %8.2f\r\n",gyroData.xAxis.scaled,gyroData.yAxis.scaled,gyroData.zAxis.scaled, accData.xAxis.scaled, accData.yAxis.scaled, accData.zAxis.scaled); 00170 00171 } 00172 static void setupMPU() 00173 { 00174 if(imu.getID()==0x68) { 00175 printf("MPU6050 OK"); 00176 wait(1); 00177 } else { 00178 printf("MPU6050 error ID=0x%x\r\n",imu.getID()); 00179 while(1) { 00180 } 00181 } 00182 imu.start(); 00183 } 00184 static void setupBMP() 00185 { 00186 // bmp180.Initialize(); // no altitude compensation and normal oversampling 00187 bmp.Initialize(64, BMP180_OSS_ULTRA_LOW_POWER); // 64m altitude compensation and low power oversampling 00188 bmp.ReadData(&temperature, &pressure); 00189 altitude=bmp.ReadAltitude(); 00190 printf("Pressure(hPa): %8.2f \t Temperature(C): %8.2f\t Altitude: %8.2f\r\n", pressure, temperature, altitude); 00191 } 00192 static MPU6050 imu; 00193 static BMI160_I2C imu2; 00194 static BMP180 bmp; 00195 }; 00196 00197 MPU6050 AcquireI2cData::imu(&i2c2); 00198 BMI160_I2C AcquireI2cData::imu2(&i2c, BMI160_I2C::I2C_ADRS_SDO_LO); 00199 BMP180 AcquireI2cData::bmp(&i2c2); 00200 // some witchcraft to register run function without touching anything outside our library 00201 static AcquireI2cData _dummy;
Generated on Tue Sep 13 2022 05:19:36 by
1.7.2
