質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

mbed

mbed(エンベッド)は、Webサイト上でC++を使って開発を行う、ワンボードマイコンのプロトタイピングツールです。PCに開発環境をインストールする必要がなく、Webにアクセスできればどこにいても開発を行うことができます。

Q&A

解決済

2回答

1230閲覧

mbedのオンラインコンパイラでenumをintに代入できない

_nb_eri_

総合スコア4

C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

mbed

mbed(エンベッド)は、Webサイト上でC++を使って開発を行う、ワンボードマイコンのプロトタイピングツールです。PCに開発環境をインストールする必要がなく、Webにアクセスできればどこにいても開発を行うことができます。

0グッド

0クリップ

投稿2021/07/18 06:10

前提・実現したいこと

mbedでmpu9250を使おうと思い調べたライブラリを使ったところ、ノイズがひどくとても使える値が出てきませんでした。M5Stackの方は安定してるので勉強がてらライブラリを移植しようと考えました。
M5Stackのライブラリ : https://github.com/m5stack/M5Stack/blob/master/src/utility/MPU9250.cpp
https://github.com/m5stack/M5Stack/blob/master/src/utility/MPU9250.h

ある程度はmbed LPC1768用になったのですが、uint8_tの変数 Gscale にenum Gscale{}; の定数を代入するところでエラーが出ました。

発生している問題・エラーメッセージ

Error: Data member initializer is not allowed in "MPU9250_ByM5Stack/mpu9250.h", Line: 202, Col: 17

該当のソースコード

C++

1/* 2 Note: The MPU9250 is an I2C sensor and uses the Arduino Wire library. 3 Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or 4 a 3.3 V Teensy 3.1. We have disabled the internal pull-ups used by the Wire 5 library in the Wire.h/twi.c utility file. We are also using the 400 kHz fast 6 I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file. 7 */ 8#ifndef _MPU9250_H_ 9#define _MPU9250_H_ 10 11 12// See also MPU-9250 Register Map and Descriptions, Revision 4.0, 13// RM-MPU-9250A-00, Rev. 1.4, 9/9/2013 for registers not listed in above 14// document; the MPU9250 and MPU9150 are virtually identical but the latter has 15// a different register map 16 17//Magnetometer Registers 18/* マクロ定義がすごくいっぱい*/ 19 20class MPU9250 21{ 22protected: 23 // Set initial input parameters 24 enum Ascale { 25 AFS_2G = 0, 26 AFS_4G, 27 AFS_8G, 28 AFS_16G 29 }; 30 31 enum Gscale { 32 GFS_250DPS = 0, 33 GFS_500DPS, 34 GFS_1000DPS, 35 GFS_2000DPS 36 }; 37 38 enum Mscale { 39 MFS_14BITS = 0, // 0.6 mG per LSB 40 MFS_16BITS // 0.15 mG per LSB 41 }; 42 43 // Specify sensor full scale 44 uint8_t Ascale = AFS_2G; // <- ここ 45 uint8_t Gscale = GFS_250DPS; 46 // Choose either 14-bit or 16-bit magnetometer resolution 47 uint8_t Mscale = static_cast<int>(Gscale::MFS_16BITS); 48 // 2 for 8 Hz, 6 for 100 Hz continuous magnetometer data read 49 uint8_t Mmode = 0x02; 50 51public: 52 float pitch, yaw, roll; 53 float temperature; // Stores the real internal chip temperature in Celsius 54 int16_t tempCount; // Temperature raw count output 55 uint32_t delt_t = 0; // Used to control display output rate 56 57 uint32_t count = 0, sumCount = 0; // used to control display output rate 58 float deltat = 0.0f, sum = 0.0f; // integration interval for both filter schemes 59 uint32_t lastUpdate = 0, firstUpdate = 0; // used to calculate integration interval 60 uint32_t Now = 0; // used to calculate integration interval 61 62 int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output 63 int16_t magCount[3]; // Stores the 16-bit signed magnetometer sensor output 64 // Scale resolutions per LSB for the sensors 65 float aRes, gRes, mRes; 66 // Variables to hold latest sensor data values 67 float ax, ay, az, gx, gy, gz, mx, my, mz; 68 // Factory mag calibration and mag bias 69 float magCalibration[3] = {0, 0, 0}, magbias[3] = {0, 0, 0}; 70 // Bias corrections for gyro and accelerometer 71 float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0}; 72 float SelfTest[6]; 73 // Stores the 16-bit signed accelerometer sensor output 74 int16_t accelCount[3]; 75 76public: 77 void getMres(); 78 void getGres(); 79 void getAres(); 80 void readAccelData(int16_t *); 81 void readGyroData(int16_t *); 82 void readMagData(int16_t *); 83 int16_t readTempData(); 84 void updateTime(); 85 void initAK8963(float *); 86 void initMPU9250(); 87 void calibrateMPU9250(float * gyroBias, float * accelBias); 88 void MPU9250SelfTest(float * destination); 89 void writeByte(uint8_t, uint8_t, uint8_t); 90 uint8_t readByte(uint8_t, uint8_t); 91 void readBytes(uint8_t, uint8_t, uint8_t, uint8_t *); 92 93private: 94 I2C i2c; 95 SPI spi; 96}; // class MPU9250 97 98#endif // _MPU9250_H_

試したこと

【列挙型⇔数値型】enumとint型を相互変換する方法と内部型への数値変換
などを参考に static_cast<int>(Ascale::AFS_2G) とかにしてみましたが同じエラーを吐かれました。
uint8_t Ascale を int Ascale にしてコンパイルしましたが同じエラー。
enum class Ascale : int{}; のようにちゃんと書いても同じエラー。(そりゃそうか)
英語の記事を翻訳して読みましたがよくわからず。。。

補足情報

mbedのオンラインコンパイラはC++11っぽそうです。
元の使ってたライブラリはローパスフィルタが実装されてないのでそれを頑張ればできるのかもしれません。(どうすればできるのか教えていただけると幸いです)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

う~ん、エラーが再現しない。

C++

1#include <stdint.h> 2 3class MPU9250 4{ 5protected: 6 // Set initial input parameters 7 enum Ascale { 8 AFS_2G = 0, 9 AFS_4G, 10 AFS_8G, 11 AFS_16G 12 }; 13 14 enum Gscale { 15 GFS_250DPS = 0, 16 GFS_500DPS, 17 GFS_1000DPS, 18 GFS_2000DPS 19 }; 20 21 enum Mscale { 22 MFS_14BITS = 0, // 0.6 mG per LSB 23 MFS_16BITS // 0.15 mG per LSB 24 }; 25 26 // Specify sensor full scale 27 uint8_t Ascale = AFS_2G; // <- ここ 28 uint8_t Gscale = GFS_250DPS; 29 // Choose either 14-bit or 16-bit magnetometer resolution 30 uint8_t Mscale = static_cast<int>(Mscale::MFS_16BITS); //ここは勝手に修正 31 // 2 for 8 Hz, 6 for 100 Hz continuous magnetometer data read 32 uint8_t Mmode = 0x02; 33public: 34 MPU9250(){};//デフォルト今ストラクタでいいんだろうけど一応いじれるように。 35}; 36 37int main(){ 38 MPU9250 mpu; 39 return 0; 40}

mbed online compiler LPC1768にて試行。
警告はなんか出ますけどとりあえずエラーにはなりません。

投稿2021/07/18 11:29

thkana

総合スコア7629

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

_nb_eri_

2021/07/19 02:23

ご回答ありがとうございます。再現しない?!( ゚Д゚)ファイル分けると起きるみたいな感じなんでしょうか...?
guest

0

ベストアンサー

コンストラクタで初期化してはいかがか。

C++

1class MPU9250 2{ 3protected: 4 // Set initial input parameters 5 enum Ascale { ... }; 6 enum Gscale { ... }; 7 enum Mscale { ... }; 8 9 // Specify sensor full scale 10 uint8_t Ascale; 11 uint8_t Gscale; 12 uint8_t Mscale; 13 uint8_t Mmode = 0x02; 14 ... 15public: 16 MPU9250() : Ascale(AFS_2G), Gscale(GFS_250DPS), Mscale(MFS_16BITS), Mmode(0x02) {} 17 ...

投稿2021/07/18 06:45

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

_nb_eri_

2021/07/19 02:22

ご回答ありがとうございます。MPU9250.hで初期化してるところを軒並みMPU9250.cppで初期化したらなんかできました。どういうことなんでしょうか??
episteme

2021/07/19 02:56

どーもこーもない、コンパイラのC++11準拠っぷりがイマイチってことでしょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問