いつもお世話になっています。
前提・実現したいこと
ジャイロセンサは温度で誤差が変動するので、
センサに内蔵の温度センサで値を取得したいです。
※達成例:センサのヘッダファイルをインクルードしてMain文の中に温度を格納している変数を記述する。
何故、各センサのソースコードでのtemperatureは型の宣言無しに記述できているのでしょうか。
発生している問題
MPU9250(319行目)やLSM9DS1(116行目)のtempetatureの型がわからないので、
どのようにMain文に温度の値の取得するためのコードを
書けば良いのかわからないです。
MPU9250
void MPU9250::update() { uint8_t response[21]; int16_t bit_data[3]; int i; //Send I2C command at first WriteReg(MPUREG_I2C_SLV0_ADDR, AK8963_I2C_ADDR | READ_FLAG); //Set the I2C slave addres of AK8963 and set for read. WriteReg(MPUREG_I2C_SLV0_REG, AK8963_HXL); //I2C slave 0 register address from where to begin data transfer WriteReg(MPUREG_I2C_SLV0_CTRL, 0x87); //Read 7 bytes from the magnetometer //must start your read from AK8963A register 0x03 and read seven bytes so that upon read of ST2 register 0x09 the AK8963A will unlatch the data registers for the next measurement. ReadRegs(MPUREG_ACCEL_XOUT_H, response, 21); //Get accelerometer value for(i=0; i<3; i++) { bit_data[i] = ((int16_t)response[i*2] << 8) | response[i*2+1]; } _ax = G_SI * bit_data[0] / acc_divider; _ay = G_SI * bit_data[1] / acc_divider; _az = G_SI * bit_data[2] / acc_divider; //Get temperature bit_data[0] = ((int16_t)response[i*2] << 8) | response[i*2+1]; temperature = ((bit_data[0] - 21) / 333.87) + 21; //Get gyroscope value for(i=4; i<7; i++) { bit_data[i-4] = ((int16_t)response[i*2] << 8) | response[i*2+1]; } _gx = (PI / 180) * bit_data[0] / gyro_divider; _gy = (PI / 180) * bit_data[1] / gyro_divider; _gz = (PI / 180) * bit_data[2] / gyro_divider; //Get Magnetometer value for(i=7; i<10; i++) { bit_data[i-7] = ((int16_t)response[i*2+1] << 8) | response[i*2]; } _mx = bit_data[0] * magnetometer_ASA[0]; _my = bit_data[1] * magnetometer_ASA[1]; _mz = bit_data[2] * magnetometer_ASA[2]; }
LSM9DS1
void LSM9DS1::update() { uint8_t response[6]; int16_t bit_data[3]; // Read temperature ReadRegs(DEVICE_ACC_GYRO, LSM9DS1XG_OUT_TEMP_L, &response[0], 2); temperature = (float)(((int16_t)response[1] << 8) | response[0]) / 256. + 25.; // Read accelerometer ReadRegs(DEVICE_ACC_GYRO, LSM9DS1XG_OUT_X_L_XL, &response[0], 6); for (int i=0; i<3; i++) { bit_data[i] = ((int16_t)response[2*i+1] << 8) | response[2*i] ; } _ax = G_SI * ((float)bit_data[0] * acc_scale); _ay = G_SI * ((float)bit_data[1] * acc_scale); _az = G_SI * ((float)bit_data[2] * acc_scale); // Read gyroscope ReadRegs(DEVICE_ACC_GYRO, LSM9DS1XG_OUT_X_L_G, &response[0], 6); for (int i=0; i<3; i++) { bit_data[i] = ((int16_t)response[2*i+1] << 8) | response[2*i] ; } _gx = (PI / 180) * ((float)bit_data[0] * gyro_scale); _gy = (PI / 180) * ((float)bit_data[1] * gyro_scale); _gz = (PI / 180) * ((float)bit_data[2] * gyro_scale); // Read magnetometer ReadRegs(DEVICE_MAGNETOMETER, LSM9DS1M_OUT_X_L_M, &response[0], 6); for (int i=0; i<3; i++) { bit_data[i] = ((int16_t)response[2*i+1] << 8) | response[2*i] ; } _mx = 100.0 * ((float)bit_data[0] * mag_scale); _my = 100.0 * ((float)bit_data[1] * mag_scale); _mz = 100.0 * ((float)bit_data[2] * mag_scale); // Change rotation of LSM9DS1 like in MPU-9250 rotate(); }
実際にセンサの値を使っているコード
ここには、温度についての記述がありませんでした。
C++
1/* 2Provided to you by Emlid Ltd (c) 2015. 3twitter.com/emlidtech || www.emlid.com || info@emlid.com 4Example: Read accelerometer, gyroscope and magnetometer values from 5inertial measurement unit: MPU9250 or LSM9DS1 over SPI on Raspberry Pi + Navio. 6Navio's onboard sensors are connected to the SPI bus on Raspberry Pi 7and can be read through /dev/spidev0.1 (MPU9250), /dev/spidev0.3 (acc/gyro LSM9DS1) 8and /dev/spidev0.2 (mag LSM9DS1). 9To run this example navigate to the directory containing it and run following commands: 10make 11./AccelGyroMag -i [sensor name] 12Sensors names: mpu is MPU9250, lsm is LSM9DS1. 13For print help: 14./AccelGyroMag -h 15*/ 16 17#include "Common/MPU9250.h" 18#include "Navio2/LSM9DS1.h" 19#include "Common/Util.h" 20#include <unistd.h> 21#include <string> 22#include <memory> 23 24std::unique_ptr <InertialSensor> get_inertial_sensor( std::string sensor_name) 25{ 26 if (sensor_name == "mpu") { 27 printf("Selected: MPU9250\n"); 28 auto ptr = std::unique_ptr <InertialSensor>{ new MPU9250() }; 29 return ptr; 30 } 31 else if (sensor_name == "lsm") { 32 printf("Selected: LSM9DS1\n"); 33 auto ptr = std::unique_ptr <InertialSensor>{ new LSM9DS1() }; 34 return ptr; 35 } 36 else { 37 return NULL; 38 } 39} 40 41void print_help() 42{ 43 printf("Possible parameters:\nSensor selection: -i [sensor name]\n"); 44 printf("Sensors names: mpu is MPU9250, lsm is LSM9DS1\nFor help: -h\n"); 45} 46 47std::string get_sensor_name(int argc, char *argv[]) 48{ 49 if (get_navio_version() == NAVIO2) { 50 51 if (argc < 2) { 52 printf("Enter parameter\n"); 53 print_help(); 54 return std::string(); 55 } 56 57 // prevent the error message 58 opterr = 0; 59 int parameter; 60 61 while ((parameter = getopt(argc, argv, "i:h")) != -1) { 62 switch (parameter) { 63 case 'i': return optarg; 64 case 'h': print_help(); return "-1"; 65 case '?': printf("Wrong parameter.\n"); 66 print_help(); 67 return std::string(); 68 } 69 } 70 71 } else { //sensor on NAVIO+ 72 73 return "mpu"; 74 } 75 76} 77//============================================================================= 78int main(int argc, char *argv[]) 79{ 80 81 if (check_apm()) { 82 return 1; 83 } 84 85 auto sensor_name = get_sensor_name(argc, argv); 86 if (sensor_name.empty()) 87 return EXIT_FAILURE; 88 89 auto sensor = get_inertial_sensor(sensor_name); 90 91 if (!sensor) { 92 printf("Wrong sensor name. Select: mpu or lsm\n"); 93 return EXIT_FAILURE; 94 } 95 96 if (!sensor->probe()) { 97 printf("Sensor not enabled\n"); 98 return EXIT_FAILURE; 99 } 100 sensor->initialize(); 101 102 float ax, ay, az; 103 float gx, gy, gz; 104 float mx, my, mz; 105//------------------------------------------------------------------------- 106 107 while(1) { 108 sensor->update(); 109 sensor->read_accelerometer(&ax, &ay, &az); 110 sensor->read_gyroscope(&gx, &gy, &gz); 111 sensor->read_magnetometer(&mx, &my, &mz); 112 printf("Acc: %+7.3f %+7.3f %+7.3f ", ax, ay, az); 113 printf("Gyr: %+8.3f %+8.3f %+8.3f ", gx, gy, gz); 114 printf("Mag: %+7.3f %+7.3f %+7.3f\n", mx, my, mz); 115 116 usleep(500000); 117 } 118}
補足情報(FW/ツールのバージョンなど)
C++11
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/13 10:18 編集
2019/05/13 10:33
2019/05/13 10:34