微信小程序如何将三轴传感器的数据转换为角度

//接收消息监听

    client.on('message', (topic, message) => { 

        

      let msg=message.toString();

      const data=JSON.parse(msg);

      const timestamp = new Date().toISOString().substr(0, 19).replace('T', ' '); 

      // 假设lon和lat是浮点数,我们将它们乘以100并保留两位小数  

    const lonFormatted = (data.lon/100).toFixed(6);  

    const latFormatted = (data.lat/100).toFixed(6);

    console.log('角度:',accelDisplay,'角度:',accelDisplay1); 

     

      // 假设 data.accel 是您的定位数据  

      let accelDisplay = "";

      let accelDisplay1='';

      // 遍历并处理加速度数据  

    let accel = data.accel;  

    let pitch = 0,roll = 0; // 假设roll是绕y轴的旋转(横滚角),pitch是绕x轴的旋转(俯仰角)  

  

    // 注意:在实际应用中,你可能需要使用Math.atan2而不是Math.atan,以获取正确的象限信息  

    // 这里为了简化示例,我们仅使用Math.atan并假设设备静止且没有旋转到负值区域  

    if (accel.x !== 0 && accel.y !== 0) {  

        // 俯仰角pitch(绕x轴旋转,与y轴和z轴构成的平面之间的角度)  

        pitch = Math.atan(accel.y / Math.sqrt(Math.pow(accel.x, 2) + Math.pow(accel.z, 2))) * (180 / Math.PI); 

        roll = Math.atan(accel.x / Math.sqrt(Math.pow(accel.y, 2) + Math.pow(accel.z, 2))) * (180 / Math.PI);

        // 根据你的需求调整角度的正负号和范围(0-360或-180到180)  

        // 这里我们假设pitch和roll的范围是-90到90度  

        pitch = pitch > 90 ? 90 : pitch < -90 ? -90 : pitch;  

        roll = roll > 90 ? 90 : roll < -90 ? -90 : roll;

    }  

  

    // 将加速度数据添加到accelDisplay中  

    accelDisplay += `${pitch.toFixed(2)}°`;

    accelDisplay1+=`${roll.toFixed(2)}°`

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值