qinyunti 发表于 2024-7-20 16:25

【超小型 Linux 开发套件Quantum Tiny Linux】基于tinygl+mpu6050的3D角度计Demo

<div class='showpostmsg'><p>源码见附件</p>

<div></div>

<p>&nbsp;</p>

<p>视频</p>

<p>8a7af6ceb374849fac8ec8c30f65f96c<br />
b站视频</p>

<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1YR8Te5EFi&amp;page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
视频号</p>

<p><a href="https://mp.weixin.qq.com/s/AWBqpGxUXcHKvcQ7Vc7kig">基于tinygl+mpu6050的3D角度计Demo (qq.com)</a></p>

<p>&nbsp;</p>

<p>前面我们移植了tinygl可以进行3D显示了,结合之前获取的mpu6050的角度信息,我们就可以3D动态显示角度的变化过程。实现水准(角度计)的Demo。</p>

<p >&nbsp;</p>

<p >&nbsp;</p>

<p >角度显示</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; char&nbsp;text;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; /* 获取角度 */</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; int16_t&nbsp;accel;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; mpu6050_itf_get_accel(accel);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; accel /= 4096;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; accel /= 4096;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; accel /= 4096;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; /* 加速度计算角度</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &theta;x=&alpha;1180/&pi;=180/&pi;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &theta;y=&beta;1180/&pi;=180/&pi;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &theta;z=&gamma;1180/&pi;=*180/&pi;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; */</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; float&nbsp;th;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; th = atan2(sqrt(accel*accel+accel*accel),accel) * 180/PI;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; th = atan2(sqrt(accel*accel+accel*accel),accel) * 180/PI;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; th = atan2(sqrt(accel*accel+accel*accel),accel) * 180/PI;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; glClear(GL_COLOR_BUFFER_BIT&nbsp;| GL_DEPTH_BUFFER_BIT);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; /* 显示角度 */</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; snprintf(text,sizeof(text),&quot;x:%f&quot;,th);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; glDrawText((GLubyte&nbsp;*)text, 0, 0, 0xFF0000);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; snprintf(text,sizeof(text),&quot;y:%f&quot;,th);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; glDrawText((GLubyte&nbsp;*)text, 0, 12, 0x00FF00);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; snprintf(text,sizeof(text),&quot;z:%f&quot;,th);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; glDrawText((GLubyte&nbsp;*)text, 0, 24, 0x0000FF);</p>

<p >&nbsp;</p>

<p >&nbsp;</p>

<p >&nbsp;</p>

<p >显示立方体,立方体绕三轴旋转来动态展示,三轴的角度</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; /* 画立方体,并旋转角度 */</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; draw_cube(th, th, th, 2, winSizeX, winSizeY);</p>

<p >&nbsp;</p>

<p >void&nbsp;draw_cube(float&nbsp;x, float&nbsp;y, float&nbsp;z, float&nbsp;size, float&nbsp;cx, float&nbsp;cy)</p>

<p >{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>

<p >&nbsp; &nbsp; glRotatef(x, 1.0f, 0.0f, 0.0f);</p>

<p >&nbsp; &nbsp; glRotatef(y, 0.0f, 1.0f, 0.0f);</p>

<p >&nbsp; &nbsp; glRotatef(z, 0.0f, 0.0f, 1.0f);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; glBegin(GL_QUADS);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; // Front Face</p>

<p >&nbsp; &nbsp; glColor3f(1.0f, 0.0f, 0.0f);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//红</p>

<p >&nbsp; &nbsp; glVertex3f(-size, -size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, -size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, size, size);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; // Back Face</p>

<p >&nbsp; &nbsp; glColor3f(1.0f, 1.0f, 0.0f);&nbsp;&nbsp; &nbsp;//黄</p>

<p >&nbsp; &nbsp; glVertex3f(-size, -size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, -size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, size, -size);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; // Top Face</p>

<p >&nbsp; &nbsp; glColor3f(0.0f, 0.0f, 1.0f);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//蓝</p>

<p >&nbsp; &nbsp; glVertex3f(-size, size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, size, -size);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; // Bottom Face</p>

<p >&nbsp; &nbsp; glColor3f(1.0f, 0.0f, 1.0f);&nbsp;&nbsp; &nbsp;//紫</p>

<p >&nbsp; &nbsp; glVertex3f(-size, -size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, -size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, -size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, -size, -size);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; // Left Face</p>

<p >&nbsp; &nbsp; glColor3f(0.0f, 1.0f, 0.0f);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//绿</p>

<p >&nbsp; &nbsp; glVertex3f(-size, -size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, -size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(-size, size, -size);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; // Right face</p>

<p >&nbsp; &nbsp; glColor3f(0.0f, 1.0f, 1.0f);&nbsp;&nbsp; &nbsp;//青</p>

<p >&nbsp; &nbsp; glVertex3f(size, -size, size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, -size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, size, -size);</p>

<p >&nbsp; &nbsp; glVertex3f(size, size, size);</p>

<p >&nbsp; &nbsp; glEnd();</p>

<p >}</p>

<p >&nbsp;</p>

<p >完整的代码见附件。</p>

<p >&nbsp;</p>

<p >编译</p>

<p >gcc tinygl/src/*.c cube.c lcd.c mpu6050.c mpu6050_itf.c -I tinygl/include/ -o cube -lm</p>

<p >&nbsp;</p>

<p >运行</p>

<p >./cube</p>

<p >&nbsp;</p>

<p >测试效果见视频</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【超小型 Linux 开发套件Quantum Tiny Linux】基于tinygl+mpu6050的3D角度计Demo