1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5NanoC6、アナログジョイスティックの値を最小二乗法(線形単回帰)して遊ぶ(実装編

Last updated at Posted at 2025-02-16

x 過去ログを見よ!!!

x 3.1.2 (ESP32 Arduino 3.1.2)
x 片チャンネル
x 3秒ごと
x 送信直前の値をサンプリングする予定
x M5NanoC6の場合は、電圧が直接に返つてくる(mV(ミリボルト))
x nVで計算 (mVを1000倍する)
x nが10個とか15個とかが多いのは計算しやすいから。10の時は、合計は、55,平均は、5.5
15の時は、合計は、120,平均は、8,小数点以下がないのは、15,n=15のx偏差×y偏差の合計は、280

結果

Screenshot from 2025-02-17 05-33-05.jpg

イメージ

image_original (35).jpg

配線

o_coq782.jpg

こんなのが、たくさん出てきて、分からん。

o_coq816.jpg

プログラム



//ser_analog_joystick_min2j_M5NanoC6

//nは、15固定、1,2,3,4...12,13,14,15
//xの平均は、8 (計算済み)
//xの偏差の二乗の積は、280 (計算済み)


#define G_X 2
#define G_Y 1


void setup() {
  // put your setup code here, to run once:

  //gpio inte
  pinMode(G_X, ANALOG);  //gpio init
  pinMode(G_Y, ANALOG);  //gpio init

  //シリアルの初期化
  Serial.begin(9600);
  Serial.println();
  //シリアルの待ちが0.5*9
  for (int i = 0; i < 9; i++) {
    Serial.print('.');delay(500);  //接続待ち
  } //for
  Serial.println();

} //setup

void loop() {
  // put your main code here, to run repeatedly:

  float c_yl = 0;
  //float c_yr = 0;

  int y[20];
  int s = 0;  //input
  for (int i = 1; i <= 15; i++) {
    //センサー入力する
    s = s + (y[i] = (analogRead(G_X)*1000));             //センサーの値
  } //for
  s = s / 15;                            // s / 15

  int xydie = 0;
  for(int i = 1;i<=15;i++){    
    xydie = xydie + (   (i - 8) *  (y[i]-s)  );
    Serial.printf("%d,",       (i - 8) *  (y[i]-s)  );
  } //for

  int b = xydie / 280;
  //int a = s -(xydie / 280)*8;
  int a = s -((xydie*8) / 280);

  Serial.printf("\n");
  Serial.printf("xydie = %d\n",xydie);
  Serial.printf("\n");
  Serial.printf("b = %d\n",b);
  Serial.printf("a = %d\n",a);

  //c_yl = ((float)(a+b*15)) * (3.3f / 4096.0f);  //  電圧こと
  //c_yl = ((float)(a+((xydie*15) / 280))) * (3.3f / 4096.0f);  //  電圧こと
  c_yl = ((float)(a+((xydie*15) / 280))) * 0.000001f; //  電圧こと  x0.000001 = /1000000
  

  //表示
  //Serial.println(c_yl);
  Serial.printf("%f\n",c_yl);
  Serial.println();
  delay(3000);  //ダミー 0.3秒待つ

} //loop



1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?