【微雪RP2040双核开发板】加速度,角速度计实现
<div class='showpostmsg'><h1>95c875fa4573e6a4bea7f6e29781ecb2<br /> </h1>
<h1><b>前言</b></h1>
<p >前一篇我们移植了LVGL,有GUI之后我们就可以利用这块圆形的240x240的屏幕做一些有意思的东西了。</p>
<p >我们结合六轴加速度计,使用chart控件将,六轴数据通过曲线显示出来,实现一个加速度计,角速度计算的Demo。</p>
<p > </p>
<h1 ><b>过程</b></h1>
<p >QMI8658数据的获取可以参考样例程序</p>
<p >chart的使用可以参考lvgl官网。</p>
<p >添加代码</p>
<p >/home/lhj/RP2040-LCD-1.28/c/examples/LCD_1in28_test.c</p>
<p ><br />
编译</p>
<p >cd build</p>
<p >export PICO_SDK_PATH="/home/lhj/pico-setup/pico/pico-sdk" && cmake -DLV_CONF_BUILD_DISABLE_EXAMPLES=1 -DLV_CONF_BUILD_DISABLE_DEMOS=1 ..</p>
<p >make</p>
<p > </p>
<div class="parsedown-markdown">
<p>/*****************************************************************************</p>
<p >* | File : LCD_1in28_test.c</p>
<p >* | Author : Waveshare team</p>
<p >* | Function : 1.3inch LCD test demo</p>
<p >* | Info :</p>
<p >*----------------</p>
<p >* | This version: V1.0</p>
<p >* | Date : 2021-08-20</p>
<p >* | Info :</p>
<p >#</p>
<p ># Permission is hereby granted, free of charge, to any person obtaining a copy</p>
<p ># of this software and associated documnetation files (the "Software"), to deal</p>
<p ># in the Software without restriction, including without limitation the rights</p>
<p ># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</p>
<p ># copies of the Software, and to permit persons to whom the Software is</p>
<p ># furished to do so, subject to the following conditions:</p>
<p >#</p>
<p ># The above copyright notice and this permission notice shall be included in</p>
<p ># all copies or substantial portions of the Software.</p>
<p >#</p>
<p ># THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</p>
<p ># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</p>
<p ># FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</p>
<p ># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</p>
<p ># LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</p>
<p ># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</p>
<p ># THE SOFTWARE.</p>
<p >#</p>
<p >******************************************************************************/</p>
<p >#include "LCD_Test.h"</p>
<p >#include "LCD_1in28.h"</p>
<p >#include "QMI8658.h"</p>
<p >#include <stdio.h></p>
<p >#include "pico/stdlib.h"</p>
<p >#include "hardware/adc.h"</p>
<p >#include "draw.h"</p>
<p >#include "lvgl.h"</p>
<p >#include "lv_port_disp.h"</p>
<p > </p>
<p >bool timer_callback(repeating_timer_t *rt)</p>
<p >{</p>
<p > lv_tick_inc(5);</p>
<p > ///printf("%lld\r\n",time_us_64());</p>
<p >}</p>
<p > </p>
<p >static void btn_event_cb(lv_event_t * e)</p>
<p >{</p>
<p > lv_event_code_t code = lv_event_get_code(e);</p>
<p > lv_obj_t * btn = lv_event_get_target(e);</p>
<p > if(code == LV_EVENT_CLICKED) {</p>
<p > static uint8_t cnt = 0;</p>
<p > cnt++;</p>
<p > </p>
<p > /*Get the first child of the button which is the label and change its text*/</p>
<p > lv_obj_t * label = lv_obj_get_child(btn, 0);</p>
<p > lv_label_set_text_fmt(label, "Button: %d", cnt);</p>
<p > }</p>
<p >}</p>
<p > </p>
<p >/**</p>
<p > * Create a button with a label and react on click event.</p>
<p > */</p>
<p >void lv_example_get_started_1(void)</p>
<p >{</p>
<p > lv_color_t color;</p>
<p > color.full=(uint16_t)0xF800;</p>
<p > lv_obj_t * btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/</p>
<p > lv_obj_set_style_text_font(btn, &lv_font_montserrat_24 ,0);</p>
<p > lv_obj_set_style_text_color(btn,color,0);</p>
<p > lv_obj_set_pos(btn, 120-100, 120-50); /*Set its position*/</p>
<p > lv_obj_set_size(btn, 200, 100); /*Set its size*/</p>
<p > lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/</p>
<p > </p>
<p > lv_obj_t * label = lv_label_create(btn); /*Add a label to the button*/</p>
<p > lv_label_set_text(label, "Hello LVGL"); /*Set the labels text*/</p>
<p > lv_obj_center(label);</p>
<p >}</p>
<p > </p>
<p >lv_obj_t * chart;</p>
<p >lv_chart_series_t * ser1;</p>
<p >lv_chart_series_t * ser2;</p>
<p >lv_chart_series_t * ser3;</p>
<p >lv_chart_series_t * ser4;</p>
<p >lv_chart_series_t * ser5;</p>
<p >lv_chart_series_t * ser6;</p>
<p >int adc_chart_init(void)</p>
<p >{</p>
<p > lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0x343247), 0);</p>
<p > </p>
<p > /*Create a chart*/</p>
<p > chart = lv_chart_create(lv_scr_act());</p>
<p > lv_chart_set_type(chart, LV_CHART_TYPE_LINE);</p>
<p > lv_chart_set_point_count(chart, 20u);</p>
<p > lv_chart_set_range(chart,LV_CHART_AXIS_PRIMARY_Y, 0, 240);</p>
<p > lv_chart_set_range(chart,LV_CHART_AXIS_SECONDARY_Y, 0, 240);</p>
<p > lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);</p>
<p > lv_chart_set_div_line_count(chart, 4, 8);</p>
<p > //lv_chart_set_zoom_x(lv_obj_t * obj, uint16_t zoom_x);</p>
<p > //lv_chart_set_zoom_y(lv_obj_t * obj, uint16_t zoom_y);</p>
<p > lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 0, 5, 1, true, 80);</p>
<p > lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_X, 0, 0, 12, 1, true, 50);</p>
<p > lv_obj_set_size(chart, 240, 240);</p>
<p > lv_obj_align(chart, LV_ALIGN_CENTER, 0, 0);</p>
<p > </p>
<p > /*Add two data series*/</p>
<p > ser1 = lv_chart_add_series(chart,lv_color_hex(0xFF0000),LV_CHART_AXIS_PRIMARY_Y);</p>
<p > ser2 = lv_chart_add_series(chart,lv_color_hex(0x00FF00),LV_CHART_AXIS_PRIMARY_Y);</p>
<p > ser3 = lv_chart_add_series(chart,lv_color_hex(0x0000FF),LV_CHART_AXIS_PRIMARY_Y);</p>
<p > ser4 = lv_chart_add_series(chart,lv_color_hex(0xFFFF00),LV_CHART_AXIS_PRIMARY_Y);</p>
<p > ser5 = lv_chart_add_series(chart,lv_color_hex(0x00FFFF),LV_CHART_AXIS_PRIMARY_Y);</p>
<p > ser6 = lv_chart_add_series(chart,lv_color_hex(0xFF00FF),LV_CHART_AXIS_PRIMARY_Y);</p>
<p > return 0;</p>
<p >}</p>
<p > </p>
<p >int adc_chart_newdata(int index,int32_t newdata,int refresh)</p>
<p >{</p>
<p > if(index == 0)</p>
<p > {</p>
<p > lv_chart_set_next_value(chart, ser1, newdata);</p>
<p > }</p>
<p > if(index == 1)</p>
<p > {</p>
<p > lv_chart_set_next_value(chart, ser2, newdata);</p>
<p > }</p>
<p > if(index == 2)</p>
<p > {</p>
<p > lv_chart_set_next_value(chart, ser3, newdata); </p>
<p > }</p>
<p > if(index == 3)</p>
<p > {</p>
<p > lv_chart_set_next_value(chart, ser4, newdata); </p>
<p > }</p>
<p > if(index == 4)</p>
<p > {</p>
<p > lv_chart_set_next_value(chart, ser5, newdata); </p>
<p > }</p>
<p > else</p>
<p > {</p>
<p > lv_chart_set_next_value(chart, ser6, newdata); </p>
<p > }</p>
<p > if(refresh)</p>
<p > {</p>
<p > lv_chart_refresh(chart); /*Required after direct set*/</p>
<p > }</p>
<p > return 0;</p>
<p >}</p>
<p > </p>
<p >struct repeating_timer timer;</p>
<p > </p>
<p >int LCD_1in28_test(void)</p>
<p >{</p>
<p > if (DEV_Module_Init() != 0)</p>
<p > {</p>
<p > return -1;</p>
<p > }</p>
<p > adc_init();</p>
<p > adc_gpio_init(29);</p>
<p > adc_select_input(3);</p>
<p > LCD_1IN28_Init(VERTICAL);</p>
<p > LCD_1IN28_Clear(WHITE);</p>
<p > DEV_SET_PWM(60);</p>
<p > add_repeating_timer_ms(5,timer_callback,0,&timer);</p>
<p > ///sleep_ms(5000);</p>
<p > lv_init();</p>
<p > lv_port_disp_init();</p>
<p > //lv_example_get_started_1();</p>
<p > </p>
<p > float acc, gyro;</p>
<p > static int num = 0;</p>
<p > unsigned int tim_count=0;</p>
<p > adc_chart_init();</p>
<p > QMI8658_init();</p>
<p > </p>
<p > while(1)</p>
<p > {</p>
<p > /* 读取采样值 */</p>
<p > num++;</p>
<p > QMI8658_read_xyz(acc, gyro, &tim_count);</p>
<p > adc_chart_newdata(0,acc*240/1024+120,0);</p>
<p > adc_chart_newdata(1,acc*240/1024+120,0);</p>
<p > adc_chart_newdata(2,acc*240/1024+120,0);</p>
<p > adc_chart_newdata(3,gyro*240/1024+120,0);</p>
<p > adc_chart_newdata(4,gyro*240/1024+120,0);</p>
<p > </p>
<p > if(num>=5)</p>
<p > {</p>
<p > num = 0;</p>
<p > adc_chart_newdata(5,gyro*240/1024+120,1);</p>
<p > printf("acc_x = %4.3fmg , acc_y = %4.3fmg , acc_z = %4.3fmg\r\n", acc, acc, acc);</p>
<p > printf("gyro_x = %4.3fdps, gyro_y = %4.3fdps, gyro_z = %4.3fdps\r\n", gyro, gyro, gyro);</p>
<p > }</p>
<p > else</p>
<p > {</p>
<p > adc_chart_newdata(5,gyro,0);</p>
<p > }</p>
<p > </p>
<p > sleep_us(1000); /* 必须又sleep 否则定时器不会回调 */</p>
<p > lv_task_handler(); </p>
<p > }</p>
<p > </p>
<p > DEV_Module_Exit();</p>
<p > return 0;</p>
<p >}</p>
</div>
<p > </p>
<h1 ><b>测试效果</b></h1>
<p > </p>
<p > </p>
<h1 ><b>总结</b></h1>
<p >以上实现了简单的六轴加速度计的曲线显示,还可以优化下显示界面,比如显示数值等。</p>
<p >或者可以做成切换到罗盘等各种形式,有GUI后就可以自由发挥了。</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> <p>六轴加速度计的曲线显示效果还是很不错的</p>
感谢分享,历害了。 <p>感谢分享,历害了。</p>
<p>不错~厉害厉害~</p> <p>六轴加速度计是板载的吗?</p>
秦天qintian0303 发表于 2022-12-8 09:07
六轴加速度计是板载的吗?
<p>是的</p>
页:
[1]