qinyunti 发表于 2022-12-6 18:13

【微雪RP2040双核开发板】加速度,角速度计实现

<div class='showpostmsg'><h1>95c875fa4573e6a4bea7f6e29781ecb2<br />
&nbsp;</h1>

<h1><b>前言</b></h1>

<p >前一篇我们移植了LVGL,有GUI之后我们就可以利用这块圆形的240x240的屏幕做一些有意思的东西了。</p>

<p >我们结合六轴加速度计,使用chart控件将,六轴数据通过曲线显示出来,实现一个加速度计,角速度计算的Demo。</p>

<p >&nbsp;</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=&quot;/home/lhj/pico-setup/pico/pico-sdk&quot; &amp;&amp; cmake -DLV_CONF_BUILD_DISABLE_EXAMPLES=1 -DLV_CONF_BUILD_DISABLE_DEMOS=1 ..</p>

<p >make</p>

<p >&nbsp;</p>

<div class="parsedown-markdown">
<p>/*****************************************************************************</p>

<p >* | File &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; LCD_1in28_test.c</p>

<p >* | Author &nbsp; &nbsp; &nbsp;: &nbsp; Waveshare team</p>

<p >* | Function &nbsp; &nbsp;: &nbsp; 1.3inch LCD &nbsp;test demo</p>

<p >* | Info &nbsp; &nbsp; &nbsp; &nbsp;:</p>

<p >*----------------</p>

<p >* | This version: &nbsp; V1.0</p>

<p >* | Date &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; 2021-08-20</p>

<p >* | Info &nbsp; &nbsp; &nbsp; &nbsp;:</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 &quot;Software&quot;), 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 &nbsp;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 &quot;AS IS&quot;, 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&nbsp;&quot;LCD_Test.h&quot;</p>

<p >#include&nbsp;&quot;LCD_1in28.h&quot;</p>

<p >#include&nbsp;&quot;QMI8658.h&quot;</p>

<p >#include&nbsp;&lt;stdio.h&gt;</p>

<p >#include&nbsp;&quot;pico/stdlib.h&quot;</p>

<p >#include&nbsp;&quot;hardware/adc.h&quot;</p>

<p >#include&nbsp;&quot;draw.h&quot;</p>

<p >#include&nbsp;&quot;lvgl.h&quot;</p>

<p >#include&nbsp;&quot;lv_port_disp.h&quot;</p>

<p >&nbsp;</p>

<p >bool&nbsp;timer_callback(repeating_timer_t&nbsp;*rt)</p>

<p >{</p>

<p >&nbsp; &nbsp; lv_tick_inc(5);</p>

<p >&nbsp; &nbsp; ///printf(&quot;%lld\r\n&quot;,time_us_64());</p>

<p >}</p>

<p >&nbsp;</p>

<p >static&nbsp;void&nbsp;btn_event_cb(lv_event_t&nbsp;* e)</p>

<p >{</p>

<p >&nbsp; &nbsp; lv_event_code_t&nbsp;code&nbsp;= lv_event_get_code(e);</p>

<p >&nbsp; &nbsp; lv_obj_t&nbsp;* btn&nbsp;= lv_event_get_target(e);</p>

<p >&nbsp; &nbsp; if(code&nbsp;== LV_EVENT_CLICKED) {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; static&nbsp;uint8_t&nbsp;cnt&nbsp;= 0;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; cnt++;</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; /*Get the first child of the button which is the label and change its text*/</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_obj_t&nbsp;* label&nbsp;= lv_obj_get_child(btn, 0);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_label_set_text_fmt(label, &quot;Button: %d&quot;, cnt);</p>

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

<p >}</p>

<p >&nbsp;</p>

<p >/**</p>

<p >&nbsp;* Create a button with a label and react on click event.</p>

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

<p >void&nbsp;lv_example_get_started_1(void)</p>

<p >{</p>

<p >&nbsp; &nbsp; lv_color_t&nbsp;color;</p>

<p >&nbsp; &nbsp; color.full=(uint16_t)0xF800;</p>

<p >&nbsp; &nbsp; lv_obj_t&nbsp;* btn&nbsp;= lv_btn_create(lv_scr_act());&nbsp;&nbsp; &nbsp; /*Add a button the current screen*/</p>

<p >&nbsp; &nbsp; lv_obj_set_style_text_font(btn, &amp;lv_font_montserrat_24&nbsp;,0);</p>

<p >&nbsp; &nbsp; lv_obj_set_style_text_color(btn,color,0);</p>

<p >&nbsp; &nbsp; lv_obj_set_pos(btn, 120-100, 120-50);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*Set its position*/</p>

<p >&nbsp; &nbsp; lv_obj_set_size(btn, 200, 100);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*Set its size*/</p>

<p >&nbsp; &nbsp; lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*Assign a callback to the button*/</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; lv_obj_t&nbsp;* label&nbsp;= lv_label_create(btn);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*Add a label to the button*/</p>

<p >&nbsp; &nbsp; lv_label_set_text(label, &quot;Hello LVGL&quot;);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*Set the labels text*/</p>

<p >&nbsp; &nbsp; lv_obj_center(label);</p>

<p >}</p>

<p >&nbsp;</p>

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

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

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

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

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

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

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

<p >int&nbsp;adc_chart_init(void)</p>

<p >{</p>

<p >&nbsp; &nbsp; lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0x343247), 0);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; /*Create a chart*/</p>

<p >&nbsp; &nbsp; chart&nbsp;= lv_chart_create(lv_scr_act());</p>

<p >&nbsp; &nbsp; lv_chart_set_type(chart, LV_CHART_TYPE_LINE);</p>

<p >&nbsp; &nbsp; lv_chart_set_point_count(chart, 20u);</p>

<p >&nbsp; &nbsp; lv_chart_set_range(chart,LV_CHART_AXIS_PRIMARY_Y, 0, 240);</p>

<p >&nbsp; &nbsp; lv_chart_set_range(chart,LV_CHART_AXIS_SECONDARY_Y, 0, 240);</p>

<p >&nbsp; &nbsp; lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);</p>

<p >&nbsp; &nbsp; lv_chart_set_div_line_count(chart, 4, 8);</p>

<p >&nbsp; &nbsp; //lv_chart_set_zoom_x(lv_obj_t * obj, uint16_t zoom_x);</p>

<p >&nbsp; &nbsp; //lv_chart_set_zoom_y(lv_obj_t * obj, uint16_t zoom_y);</p>

<p >&nbsp; &nbsp; lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 0, 5, 1, true, 80);</p>

<p >&nbsp; &nbsp; lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_X, 0, 0, 12, 1, true, 50);</p>

<p >&nbsp; &nbsp; lv_obj_set_size(chart, 240, 240);</p>

<p >&nbsp; &nbsp; lv_obj_align(chart, LV_ALIGN_CENTER, 0, 0);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; /*Add two data series*/</p>

<p >&nbsp; &nbsp; ser1&nbsp;= lv_chart_add_series(chart,lv_color_hex(0xFF0000),LV_CHART_AXIS_PRIMARY_Y);</p>

<p >&nbsp; &nbsp; ser2&nbsp;= lv_chart_add_series(chart,lv_color_hex(0x00FF00),LV_CHART_AXIS_PRIMARY_Y);</p>

<p >&nbsp; &nbsp; ser3&nbsp;= lv_chart_add_series(chart,lv_color_hex(0x0000FF),LV_CHART_AXIS_PRIMARY_Y);</p>

<p >&nbsp; &nbsp; ser4&nbsp;= lv_chart_add_series(chart,lv_color_hex(0xFFFF00),LV_CHART_AXIS_PRIMARY_Y);</p>

<p >&nbsp; &nbsp; ser5&nbsp;= lv_chart_add_series(chart,lv_color_hex(0x00FFFF),LV_CHART_AXIS_PRIMARY_Y);</p>

<p >&nbsp; &nbsp; ser6&nbsp;= lv_chart_add_series(chart,lv_color_hex(0xFF00FF),LV_CHART_AXIS_PRIMARY_Y);</p>

<p >&nbsp; &nbsp; return&nbsp;0;</p>

<p >}</p>

<p >&nbsp;</p>

<p >int&nbsp;adc_chart_newdata(int&nbsp;index,int32_t&nbsp;newdata,int&nbsp;refresh)</p>

<p >{</p>

<p >&nbsp; &nbsp; if(index&nbsp;== 0)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_set_next_value(chart, ser1, newdata);</p>

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

<p >&nbsp; &nbsp; if(index&nbsp;== 1)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_set_next_value(chart, ser2, newdata);</p>

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

<p >&nbsp; &nbsp; if(index&nbsp;== 2)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_set_next_value(chart, ser3, newdata); &nbsp;</p>

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

<p >&nbsp; &nbsp; if(index&nbsp;== 3)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_set_next_value(chart, ser4, newdata); &nbsp;</p>

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

<p >&nbsp; &nbsp; if(index&nbsp;== 4)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_set_next_value(chart, ser5, newdata); &nbsp;</p>

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

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

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_set_next_value(chart, ser6, newdata); &nbsp;</p>

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

<p >&nbsp; &nbsp; if(refresh)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; lv_chart_refresh(chart);&nbsp;/*Required after direct set*/</p>

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

<p >&nbsp; &nbsp; return&nbsp;0;</p>

<p >}</p>

<p >&nbsp;</p>

<p >struct&nbsp;repeating_timer&nbsp;timer;</p>

<p >&nbsp;</p>

<p >int&nbsp;LCD_1in28_test(void)</p>

<p >{</p>

<p >&nbsp; &nbsp; if&nbsp;(DEV_Module_Init() != 0)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;-1;</p>

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

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

<p >&nbsp; &nbsp; adc_gpio_init(29);</p>

<p >&nbsp; &nbsp; adc_select_input(3);</p>

<p >&nbsp; &nbsp; LCD_1IN28_Init(VERTICAL);</p>

<p >&nbsp; &nbsp; LCD_1IN28_Clear(WHITE);</p>

<p >&nbsp; &nbsp; DEV_SET_PWM(60);</p>

<p >&nbsp; &nbsp; add_repeating_timer_ms(5,timer_callback,0,&amp;timer);</p>

<p >&nbsp; &nbsp; ///sleep_ms(5000);</p>

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

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

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

<p >&nbsp;</p>

<p >&nbsp; &nbsp; float&nbsp;acc, gyro;</p>

<p >&nbsp; &nbsp; static&nbsp;int&nbsp;num&nbsp;= 0;</p>

<p >&nbsp; &nbsp; unsigned&nbsp;int&nbsp;tim_count=0;</p>

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

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

<p >&nbsp;</p>

<p >&nbsp; &nbsp; while(1)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; /* 读取采样值 */</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; num++;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; QMI8658_read_xyz(acc, gyro, &amp;tim_count);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(0,acc*240/1024+120,0);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(1,acc*240/1024+120,0);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(2,acc*240/1024+120,0);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(3,gyro*240/1024+120,0);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(4,gyro*240/1024+120,0);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; if(num&gt;=5)</p>

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num&nbsp;= 0;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(5,gyro*240/1024+120,1);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;acc_x &nbsp; = %4.3fmg , acc_y &nbsp;= %4.3fmg , acc_z &nbsp;= %4.3fmg\r\n&quot;, acc, acc, acc);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;gyro_x &nbsp;= %4.3fdps, gyro_y = %4.3fdps, gyro_z = %4.3fdps\r\n&quot;, gyro, gyro, gyro);</p>

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

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

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

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adc_chart_newdata(5,gyro,0);</p>

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

<p >&nbsp;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; sleep_us(1000);&nbsp;/* 必须又sleep 否则定时器不会回调 */</p>

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

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

<p >&nbsp;</p>

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

<p >&nbsp; &nbsp; return&nbsp;0;</p>

<p >}</p>
</div>

<p >&nbsp;</p>

<h1 ><b>测试效果</b></h1>

<p > &nbsp;</p>

<p > &nbsp;</p>

<h1 ><b>总结</b></h1>

<p >以上实现了简单的六轴加速度计的曲线显示,还可以优化下显示界面,比如显示数值等。</p>

<p >或者可以做成切换到罗盘等各种形式,有GUI后就可以自由发挥了。</p>

<p >&nbsp;</p>

<p >&nbsp;</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>

Jacktang 发表于 2022-12-6 20:44

<p>六轴加速度计的曲线显示效果还是很不错的</p>

lugl4313820 发表于 2022-12-7 07:41

感谢分享,历害了。

sipower 发表于 2022-12-7 09:52

<p>感谢分享,历害了。</p>

吾妻思萌 发表于 2022-12-7 13:04

<p>不错~厉害厉害~</p>

秦天qintian0303 发表于 2022-12-8 09:07

<p>六轴加速度计是板载的吗?</p>

qinyunti 发表于 2022-12-8 09:24

秦天qintian0303 发表于 2022-12-8 09:07
六轴加速度计是板载的吗?

<p>是的</p>
页: [1]
查看完整版本: 【微雪RP2040双核开发板】加速度,角速度计实现