MPU6050 6軸モーションセンサからMadgwickFilter・PID制御に加えてVL53レーザー測距センサモジュールを取付けテストを行いましたが、VL53の処理に45-12=33㎳かかりました。
回路図
プログラムの実行とスキップ(コメントアウト)
RemoteXY → 101_ //RemoteXY_Handler ();
VL53 → 102_ //dist = gVL530X.readRangeContinuousMillimeters();
シリアルモニター①
RemoteXY(実行)VL53(実行)
VL53の処理に時間がかかる
45-12=33㎳
②
RemoteXY(スキップ)
VL53(実行)
VL53の処理に時間がかかる
508-469=39㎳
③
RemoteXY(実行)VL53(スキップ)
MadgwickFilter PID処理に影響少ない
④
RemoteXY(スキップ)
VL53(スキップ)
MadgwickFilter PID処理に影響少ない
シリアルモニター確認用プログラム
- //////////////////////////////////////////////
- // RemoteXY include library //
- //////////////////////////////////////////////
- // you can enable debug logging to Serial at 115200
- //#define REMOTEXY__DEBUGLOG
- // RemoteXY select connection mode and include library
- #define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
- #include <ESP8266WiFi.h>
- // RemoteXY connection settings
- #define REMOTEXY_WIFI_SSID "RemoteXY"
- #define REMOTEXY_WIFI_PASSWORD "12345678"
- #define REMOTEXY_SERVER_PORT 6377
- #include <RemoteXY.h>
- // RemoteXY GUI configuration
- #pragma pack(push, 1)
- uint8_t RemoteXY_CONF[] = // 71 bytes
- { 255,6,0,0,0,64,0,17,0,0,0,139,1,106,200,1,1,5,0,5,
- 4,7,94,94,0,204,191,31,1,10,159,36,35,1,2,31,67,0,10,59,
- 159,38,35,49,4,26,31,85,80,0,31,79,70,70,0,4,3,109,100,16,
- 128,205,26,4,3,129,100,16,128,205,26 };
- // this structure defines all the variables and events of your control interface
- struct {
- // input variables
- int8_t joystick_01_x; // from -100 to 100
- int8_t joystick_01_y; // from -100 to 100
- uint8_t button_01; // =1 if button pressed, else =0
- uint8_t pushSwitch_01; // =1 if state is ON, else =0
- int8_t slider_01; // from 0 to 100
- int8_t slider_02; // from 0 to 100
- // other variable
- uint8_t connect_flag; // =1 if wire connected, else =0
- } RemoteXY;
- #pragma pack(pop)
- /////////////////////////////////////////////
- // END RemoteXY include //
- /////////////////////////////////////////////
- #include "MPU6050.h"
- MPU6050 accelgyro;
- #include <MadgwickAHRS.h>
- Madgwick MadgwickFilter;
- #include <VL53L0X.h>
- VL53L0X gVL530X;
- int16_t ax, ay, az;//加速度 int16_tは2バイトの符号付き整数
- int16_t gx, gy, gz;//角速度 同上
- float ROLL, PITCH, YAW;
- float ROLL_P, ROLL_I, ROLL_D, ROLL_M;
- float PITCH_P, PITCH_I, PITCH_D, PITCH_M;
- float R, P, Y;
- float Kp, Kd, Ki;
- int dist;
- int throttle;
- #define M1 12
- #define M2 13
- #define M3 14
- #define M4 15
- #define PWN_FREQ 1000 // PWM frequency: 1000Hz(1kHz)
- #define PWN_RANGE 100 // PWN range: 100
- void setup()
- {
- RemoteXY_Init ();
- pinMode(M1, OUTPUT);
- pinMode(M2, OUTPUT);
- pinMode(M3, OUTPUT);
- pinMode(M4, OUTPUT);
- Wire.begin();
- Serial.begin(115200);
- accelgyro.initialize();//I2Cデバイスの初期化
- delay(300);
- MadgwickFilter.begin(100);//フィルタのサンプリングを100Hzで
- analogWriteFreq(PWN_FREQ);
- analogWriteRange(PWN_RANGE);
- R = 0;
- P = 0;
- Y = 0;
- gVL530X.init();
- gVL530X.setTimeout(500);
- gVL530X.startContinuous();
- }
- void loop()
- {
- //RemoteXY_Handler ();
- //dist = gVL530X.readRangeContinuousMillimeters();
- accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
- MadgwickFilter.updateIMU(gx / 131.0, gy / 131.0, gz / 131.0, ax / 16384.0, ay / 16384.0, az / 16384.0);
- ROLL = MadgwickFilter.getRoll();
- PITCH = MadgwickFilter.getPitch();
- YAW = MadgwickFilter.getYaw();
- YAW = YAW - 180;
- YAW = 0 * YAW;
- // 角速度値を分解能で割って角速度に変換する
- float dpsX = gx / 131.0;
- float dpsY = gy / 131.0;
- float dpsZ = gz / 131.0;
- //定数
- Kp = 50;
- Ki = 0.1;
- Kd = 10;
- // PID制御
- ROLL_P = ROLL / 90; // P成分:傾き-90〜90度 → -1〜1
- ROLL_I += ROLL_P; // I成分:傾きの積算。
- if (10 < abs(ROLL_I)) ROLL_I = 0; //上限設定
- ROLL_D = dpsX / 250; // D成分:角速度-250〜250dps → -1〜1
- ROLL_M = ROLL_P * Kp +
- ROLL_I * Ki +
- ROLL_D * Kd;
- PITCH_P = PITCH / 90; // P成分:傾き-90〜90度 → -1〜1
- PITCH_I += PITCH_P; // I成分:傾きの積算。
- if (10 < abs(PITCH_I)) PITCH_I = 0; //上限設定
- PITCH_D = dpsY / 250; // D成分:角速度-250〜250dps → -1〜1
- PITCH_M = PITCH_P * Kp +
- PITCH_I * Ki +
- PITCH_D * Kd;
- // Serial.print("Kp:");Serial.print(Kp); Serial.print("| ");
- // Serial.print("Ki:");Serial.print(Ki); Serial.print("| ");
- // Serial.print("Kd:");Serial.print(Kd); Serial.print("| ");
- Serial.print("ROLL:");Serial.print(ROLL); Serial.print("| ");
- Serial.print("PITCH:");Serial.print(PITCH); Serial.print("| ");
- // Serial.print("YAW:");Serial.print(YAW); Serial.print("| ");
- // Serial.print("ROLL_P:");Serial.print(ROLL_P); Serial.print("| ");
- // Serial.print("ROLL_I:");Serial.print(ROLL_I); Serial.print("| ");
- // Serial.print("ROLL_D:");Serial.print(ROLL_D); Serial.print("| ");
- // Serial.print("ROLL_M:");Serial.print(ROLL_M); Serial.print("| ");
- // Serial.print("PITCH_P:");Serial.print(PITCH_P); Serial.print("| ");
- // Serial.print("PITCH_I:");Serial.print(PITCH_I); Serial.print("| ");
- // Serial.print("PITCH_D:");Serial.print(PITCH_D); Serial.print("| ");
- // Serial.print("PITCH_M:");Serial.print(PITCH_M); Serial.print("| ");
- Serial.print("dist:");Serial.print(dist); Serial.print("| ");
- Serial.print("\n");
- //シリアルプロッタ
- //Serial.print(ROLL); Serial.print(",");
- //Serial.print(ROLL_P); Serial.print(",");
- //Serial.print(ROLL_I); Serial.print(",");
- //Serial.print(ROLL_D); Serial.print(",");
- //Serial.println(ROLL_M);
- //Serial.print(PITCH); Serial.print(",");
- //Serial.print(PITCH_P); Serial.print(",");
- //Serial.print(PITCH_I); Serial.print(",");
- //Serial.print(PITCH_D); Serial.print(",");
- //Serial.println(PITCH_M);
- delay(5);
- }
0 件のコメント:
コメントを投稿