【超小型 Linux 开发套件Quantum Tiny Linux】基于tinygl+mpu6050的3D角度计Demo
<div class='showpostmsg'><p>源码见附件</p><div></div>
<p> </p>
<p>视频</p>
<p>8a7af6ceb374849fac8ec8c30f65f96c<br />
b站视频</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1YR8Te5EFi&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> </p>
<p>前面我们移植了tinygl可以进行3D显示了,结合之前获取的mpu6050的角度信息,我们就可以3D动态显示角度的变化过程。实现水准(角度计)的Demo。</p>
<p > </p>
<p > </p>
<p >角度显示</p>
<p > char text;</p>
<p > /* 获取角度 */</p>
<p > int16_t accel;</p>
<p > mpu6050_itf_get_accel(accel);</p>
<p > accel /= 4096;</p>
<p > accel /= 4096;</p>
<p > accel /= 4096;</p>
<p > /* 加速度计算角度</p>
<p > θx=α1180/π=180/π</p>
<p > θy=β1180/π=180/π</p>
<p > θz=γ1180/π=*180/π</p>
<p > */</p>
<p > float th;</p>
<p > th = atan2(sqrt(accel*accel+accel*accel),accel) * 180/PI;</p>
<p > th = atan2(sqrt(accel*accel+accel*accel),accel) * 180/PI;</p>
<p > th = atan2(sqrt(accel*accel+accel*accel),accel) * 180/PI;</p>
<p > </p>
<p > glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);</p>
<p > </p>
<p > /* 显示角度 */</p>
<p > snprintf(text,sizeof(text),"x:%f",th);</p>
<p > glDrawText((GLubyte *)text, 0, 0, 0xFF0000);</p>
<p > snprintf(text,sizeof(text),"y:%f",th);</p>
<p > glDrawText((GLubyte *)text, 0, 12, 0x00FF00);</p>
<p > snprintf(text,sizeof(text),"z:%f",th);</p>
<p > glDrawText((GLubyte *)text, 0, 24, 0x0000FF);</p>
<p > </p>
<p > </p>
<p > </p>
<p >显示立方体,立方体绕三轴旋转来动态展示,三轴的角度</p>
<p > </p>
<p > /* 画立方体,并旋转角度 */</p>
<p > draw_cube(th, th, th, 2, winSizeX, winSizeY);</p>
<p > </p>
<p >void draw_cube(float x, float y, float z, float size, float cx, float cy)</p>
<p >{ </p>
<p > glRotatef(x, 1.0f, 0.0f, 0.0f);</p>
<p > glRotatef(y, 0.0f, 1.0f, 0.0f);</p>
<p > glRotatef(z, 0.0f, 0.0f, 1.0f);</p>
<p > </p>
<p > glBegin(GL_QUADS);</p>
<p > </p>
<p > // Front Face</p>
<p > glColor3f(1.0f, 0.0f, 0.0f); //红</p>
<p > glVertex3f(-size, -size, size);</p>
<p > glVertex3f(size, -size, size);</p>
<p > glVertex3f(size, size, size);</p>
<p > glVertex3f(-size, size, size);</p>
<p > </p>
<p > // Back Face</p>
<p > glColor3f(1.0f, 1.0f, 0.0f); //黄</p>
<p > glVertex3f(-size, -size, -size);</p>
<p > glVertex3f(size, -size, -size);</p>
<p > glVertex3f(size, size, -size);</p>
<p > glVertex3f(-size, size, -size);</p>
<p > </p>
<p > // Top Face</p>
<p > glColor3f(0.0f, 0.0f, 1.0f); //蓝</p>
<p > glVertex3f(-size, size, size);</p>
<p > glVertex3f(size, size, size);</p>
<p > glVertex3f(size, size, -size);</p>
<p > glVertex3f(-size, size, -size);</p>
<p > </p>
<p > // Bottom Face</p>
<p > glColor3f(1.0f, 0.0f, 1.0f); //紫</p>
<p > glVertex3f(-size, -size, size);</p>
<p > glVertex3f(size, -size, size);</p>
<p > glVertex3f(size, -size, -size);</p>
<p > glVertex3f(-size, -size, -size);</p>
<p > </p>
<p > // Left Face</p>
<p > glColor3f(0.0f, 1.0f, 0.0f); //绿</p>
<p > glVertex3f(-size, -size, -size);</p>
<p > glVertex3f(-size, -size, size);</p>
<p > glVertex3f(-size, size, size);</p>
<p > glVertex3f(-size, size, -size);</p>
<p > </p>
<p > // Right face</p>
<p > glColor3f(0.0f, 1.0f, 1.0f); //青</p>
<p > glVertex3f(size, -size, size);</p>
<p > glVertex3f(size, -size, -size);</p>
<p > glVertex3f(size, size, -size);</p>
<p > glVertex3f(size, size, size);</p>
<p > glEnd();</p>
<p >}</p>
<p > </p>
<p >完整的代码见附件。</p>
<p > </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 > </p>
<p >运行</p>
<p >./cube</p>
<p > </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]