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?

M5StampS3、アナログジョイスティックの値を最小二乗法して遊ぶ

Last updated at Posted at 2025-02-11

x 過去ログを見よ!!!
x 片チャンネル
x 3秒ごと
x bの値の10進固定小数点でどうするか、検討中(20250214)予定は未定
x 送信直前の値をサンプリングする予定
x M5NanoC6の場合は、電圧が直接に返つてくるので新規につくらなとダメだ
x nが10個とか15個とかが多いのは計算しやすいから。10の時は、合計は、55,平均は、5.5:15の時は、合計は、120,平均は、8,小数点以下がないのは、15,n=15のx偏差×y偏差の合計は、280

結果

o_coq809.jpg

プログラム



//ser_analog_joystick_min2j_M5StampS3

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


#define G_X 13
#define G_Y 15


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));             //センサーの値
  } //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);  //  電圧こと
  

  //表示
  Serial.println(c_yl);
  Serial.println();
  delay(3000);  //ダミー 0.3秒待つ

} //loop



おまけ



#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    //               1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
    int y[]   = {NULL, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18};
    //int x[] = {NULL,-7,-6,-5,-4,-3,-2,-1, 0, 1, 2, 3, 4, 5, 6, 7};

    //xの平均は、8 (計算済み)
    //xの偏差の二乗の積は、280 (計算済み)

    int sum = 0;
    for(int i = 1;i<=15;i++){
        
        sum = sum + y[i];
        //printf("%d\n",y[i]);
        
    }
    int y_ave = sum / 15;
    
    printf("y_av = %d\n",y_ave);
  
    
    sum = 0;
    for(int i = 1;i<=15;i++){
        
        sum = sum + (   (i - 8) *  (y[i]-y_ave)  );
        printf("%d,",   (i - 8) *  (y[i]-y_ave)  );
    
    }
    printf("\n");
    printf("sum = %d\n",sum);
    printf("\n");
    printf("b = %d\n",sum / 280);
    printf("a = %d\n",y_ave -(sum / 280)*8);
  
}








y_av = 11
49,36,25,16,9,4,1,0,1,4,9,16,25,36,49,
sum = 280

b = 1
a = 3




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?