美文网首页
esp32s计算上报ADXL335 倾斜度

esp32s计算上报ADXL335 倾斜度

作者: 大道至简非简 | 来源:发表于2019-04-13 10:32 被阅读0次

    1、总体思路

    采用esp32s arduino编程,采集adxl335的模拟量数据,可以根据三轴的变化即可计算角度变化;

    2、相关配件

    esp32s开发板


    image.png

    esp32s引脚图,标有ad开头的都是可以通讯模拟量数据;


    image.png image.png

    gy-61 adxl335 提供xyz三轴变化数字;


    image.png

    3、原理参考

    官方理论
    https://www.analog.com/media/cn/technical-documentation/application-notes/AN-1057_cn.pdf

    理论内容参考
    https://www.electronicwings.com/sensors-modules/adxl335-accelerometer-module
    https://www.electronicwings.com/arduino/adxl335-accelerometer-interfacing-with-arduino-uno

    image.png
    image.png
    image.png
    image.png

    <看到数学题瞬间头疼>

    esp32 adc相关参考
    https://lastminuteengineers.com/adxl335-accelerometer-arduino-tutorial/
    https://microcontrollerslab.com/adc-esp32-measuring-voltage-example/
    https://microcontrollerslab.com/esp32-pinout-use-gpio-pins/
    esp32的adc输出值等于接入电压*输出值除以4095

    voltage_value = (ADC_VALUE * 3.3 ) / (4095);
    Serial.print("Voltage = ");
    Serial.print(voltage_value);
    Serial.print("volts");
    delay(1000);
    
    

    3、esp32s 和adxl335接线图

    image.png

    示意图采用arduino板子。原理一样。
    esp32s按照下列接口接起来;
    const int x_out = 36;
    const int y_out = 39;
    const int z_out = 34;

    注:VCC要接5V电源,跟算法有关。

    采用杜邦线连起来;

    4、代码思路

    esp32读取模拟量电压值,
    计算ADXL335重力偏离值;
    再计算便宜角度和旋转角度,算法参考理论说明;

    //引入数学计算库
    #include <math.h>
    
    //变量定义----------------------------bigen
    
    // x-axis x轴数据 对应gpio36
    const int x_out = 36;
    // y-axis y轴数据 对应gpio36
    const int y_out = 39;
    // z-axis z轴数据 对应gpio36
    const int z_out = 34;
    
    
    // 采集10次样本以降低噪音
    const int sampleSize = 10;
    
    
    
    //变量定义--------------------------------end
    
    //函数定义----------------------------bigen
    
    // 采样获取均值
    int ReadAxis(int axisPin)
    {
      long reading = 0;
      //  读取adc值
      analogRead(axisPin);
      delay(1);
      for (int i = 0; i < sampleSize; i++)
      {
        reading += analogRead(axisPin);
      }
      //  返回adc平均值
      return reading / sampleSize;
    }
    
    //初始化串口
    void setupSerial() {
      //  初始化默认串口
      Serial.begin(115200);
      //  因为采用默认串口和nb_iot模块通讯,可以另外用软串口输出调试信息;
    }
    
    
    
    //函数定义--------------------------------end
    
    
    //主程序----------------------------bigen
    void setup() {
      //  初始化串口
      setupSerial();
      delay(1000);
    }
    
    void loop() {
      //定义x/y/z模拟量值
      int ADC_X_VALUE, ADC_Y_VALUE, ADC_Z_VALUE;
      //  定义x/y/z重力单元值
      double Axout, Ayout, Azout;
      //定义倾斜度
      double theta, psy, phi;
      //  定义偏转度
      double  roll, pitch, yaw;
      Serial.println();
      //读取x/y/z轴模拟量数据,等于电压值
      ADC_X_VALUE = ReadAxis(x_out);
      ADC_Y_VALUE = ReadAxis(y_out);
      ADC_Z_VALUE = ReadAxis(z_out);
    
      //计算x/y/z重力单元值,esp32的adc需要以4095参数,0.333来源于adxl说明,大部分采用0.330
      Axout = ( ( ( (double)(ADC_X_VALUE * 5) / 4095) - 1.65 ) / 0.333 );
      Ayout = ( ( ( (double)(ADC_Y_VALUE * 5) / 4095) - 1.65 ) / 0.333 );
      Azout = ( ( ( (double)(ADC_Z_VALUE * 5) / 4095) - 1.65 ) / 0.333 );
    
      Serial.print("Axout = ");
      Serial.print(Axout);
      Serial.print("\t\t");
      Serial.print("Ayout = ");
      Serial.print(Ayout);
      Serial.print("\t\t");
      Serial.print("Azout = ");
      Serial.print(Azout);
      Serial.print("\t\t");
      Serial.println();
    
    
      //计算偏离角度,公式参考原理说明
      theta = atan(Axout / (sqrt((pow (Ayout, 2.0)) + (pow (Azout, 2.0))))) * 57.29577951;
      psy =  atan(Ayout / (sqrt((pow (Axout, 2.0)) + (pow (Azout, 2.0))))) * 57.29577951;
      phi =  atan((sqrt((pow (Ayout, 2.0)) + (pow (Axout, 2.0)))) / Azout) * 57.29577951;
    
      Serial.print("theta = ");
      Serial.print(theta);
      Serial.print("\t\t");
      Serial.print("psy = ");
      Serial.print(psy);
      Serial.print("\t\t");
      Serial.print("phi = ");
      Serial.print(phi);
      Serial.print("\t\t");
      Serial.println();
    
      //计算旋转角度,公式参考原理说明
      roll = (atan2(Ayout, Azout)) * 57.29577951 + 180;
      pitch =  (atan2(Azout, Axout)) * 57.29577951 + 180;
      yaw =  (atan2(Axout, Ayout)) * 57.29577951 + 180;
    
      Serial.print("roll = ");
      Serial.print(roll);
      Serial.print("\t\t");
      Serial.print("pitch = ");
      Serial.print(pitch);
      Serial.print("\t\t");
      Serial.print("pitch = ");
      Serial.print(pitch);
      Serial.print("\t\t");
      Serial.println();
    
    //间隔3秒采集一次
      delay(3000);
    }
    
    //主程序--------------------------------end
    
    
    

    5、查看效果

    移动模块数字变化显示;

    roll = 211.53       pitch = 211.57      pitch = 211.57      
    [16:27:06.486] 
    Axout = 1.44        Ayout = 0.54        Azout = 0.89        
    theta = 54.25       psy = 17.64     phi = 60.04     
    roll = 211.25       pitch = 211.61      pitch = 211.61      
    [16:27:09.487] 
    [16:27:09.496] Axout = 0.47     Ayout = 1.10        Azout = 2.26        
    theta = 10.65       psy = 25.40     phi = 27.84     
    roll = 205.88       pitch = 258.20      pitch = 258.20      
    [16:27:12.497] 
    Axout = 0.46        Ayout = 1.03        Azout = 2.23        
    theta = 10.54       psy = 24.23     phi = 26.70     
    roll = 204.68       pitch = 258.43      pitch = 258.43      
    [16:27:15.496] 
    Axout = 0.49        Ayout = 1.01        Azout = 2.23        
    theta = 11.40       psy = 23.78     phi = 26.68     
    roll = 204.29       pitch = 257.53      pitch = 257.53      
    [16:27:18.495] 
    [16:27:18.505] Axout = 0.46     Ayout = 1.01        Azout = 2.25        
    theta = 10.56       psy = 23.80     phi = 26.31     
    roll = 204.23       pitch = 258.45      pitch = 258.45      
    [16:27:21.507] 
    Axout = 1.67        Ayout = 1.21        Azout = 0.43        
    theta = 52.51       psy = 35.00     phi = 78.25     
    roll = 250.46       pitch = 194.39      pitch = 194.39      
    [16:27:24.508] 
    Axout = 2.23        Ayout = 2.14        Azout = 0.42        
    theta = 45.65       psy = 43.29     phi = 82.19     
    roll = 258.79       pitch = 190.76      pitch = 190.76
    

    6、场景应用

    理论和实际角度感受值感觉有差异,还未验证测通。
    真实的应用场景最好是建立自己的标准库,跟标准库比较做预警。
    还需要理论研究。

    7、上报思路

    文章可参考esp32-cam用nb_iot sim7020上报思路,代码可复用。不赘述。

    全文完。

    相关文章

      网友评论

          本文标题:esp32s计算上报ADXL335 倾斜度

          本文链接:https://www.haomeiwen.com/subject/nookwqtx.html