forked from ZhuYanzhen1/miniFOC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_8c_source.html
134 lines (132 loc) · 17.8 KB
/
filter_8c_source.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>miniFOC: algorithm/filter.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="Acme_LOGO.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">miniFOC<span id="projectnumber"> 1.0.2</span>
</div>
<div id="projectbrief">This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="dir_99d3723365464dd656f512988bb8dbbd.html">algorithm</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle"><div class="title">filter.c</div></div>
</div><!--header-->
<div class="contents">
<a href="filter_8c.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span><span class="comment">/**************************************************************************/</span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno"> 11</span><span class="preprocessor">#include "<a class="code" href="filter_8h.html">filter.h</a>"</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno"> 12</span><span class="preprocessor">#include "<a class="code" href="system_8h.html">system.h</a>"</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"> 13</span><span class="preprocessor">#include "<a class="code" href="foc_8h.html">foc.h</a>"</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno"> 14</span><span class="preprocessor">#include "<a class="code" href="config_8h.html">config.h</a>"</span></div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno"> 15</span> </div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"><a class="line" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714"> 19</a></span><span class="keyword">volatile</span> <a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> <a class="code hl_variable" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a>;</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span> </div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno"><a class="line" href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e"> 29</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">filter_coefficient_config</a>(<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *param, <span class="keywordtype">float</span> cutoff_freq, <span class="keywordtype">float</span> sample_freq, <span class="keywordtype">float</span> coefficient) {</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno"> 30</span> <span class="comment">/* clear the value of the first order low pass filter parameter handler */</span></div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno"> 31</span> <a class="code hl_function" href="system_8c.html#a7e7cece1b30ef67968b244698f999b0e">user_memset</a>(param, 0x00, <span class="keyword">sizeof</span>(<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a>));</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"> 32</span> </div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno"> 33</span> <span class="comment">/* the RC time constant is calculated from the cut-off frequency */</span></div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno"> 34</span> <span class="keywordtype">float</span> time_const = 1.0f / (6.2831852f * cutoff_freq);</div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno"> 35</span> </div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno"> 36</span> <span class="comment">/* calculation of digital low-pass filter coefficients */</span></div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno"> 37</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a95c29040dec4c9e97cf2f64cf4212435">coefficient1</a> = (1.0f / (1.0f + time_const * sample_freq)) * coefficient;</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno"> 38</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a08c1824c5523caa58ec64b9fcfd48f92">coefficient2</a> = (time_const * sample_freq) / (1.0f + time_const * sample_freq);</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"> 39</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a> = 0;</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno"> 40</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">last_result</a> = 0;</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno"> 41</span>}</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span> </div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"><a class="line" href="filter_8c.html#aa1e7128fffcf2b56a6fb1c23f89afd75"> 47</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="filter_8c.html#aa1e7128fffcf2b56a6fb1c23f89afd75">filter_config</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> <a class="code hl_function" href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">filter_coefficient_config</a>((<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *) &<a class="code hl_variable" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a>,</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> <a class="code hl_define" href="config_8h.html#ae9289eb166344bb9d0d6560b6da41118">TIM13_FREQUENCY</a>, <a class="code hl_define" href="config_8h.html#adddd943909a2fac22c5fe8e3949d105f">SPEED_UP_FREQ</a>, <a class="code hl_define" href="foc_8h.html#a2ce50cab9f20bc664e4888ffc07e656e">SPEED_COEFFICIENT</a>);</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span>}</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> </div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"><a class="line" href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592"> 58</a></span><span class="keywordtype">float</span> <a class="code hl_function" href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">filter_update_value</a>(<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *param, <span class="keywordtype">short</span> value) {</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span> <span class="comment">/* judge whether the input parameters are abnormal,</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span><span class="comment"> * calculate the output value of the first-order low-pass filter */</span></div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> <span class="keywordflow">if</span> (value < 200 && value > -200)</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a> = param-><a class="code hl_variable" href="struct_filter___structure__t.html#a95c29040dec4c9e97cf2f64cf4212435">coefficient1</a> * (float) value + param-><a class="code hl_variable" href="struct_filter___structure__t.html#a08c1824c5523caa58ec64b9fcfd48f92">coefficient2</a> * param-><a class="code hl_variable" href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">last_result</a>;</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span> </div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> <span class="comment">/* update the output value of the previous time */</span></div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">last_result</a> = param-><a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a>;</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span> <span class="keywordflow">return</span> param-><a class="code hl_variable" href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">current_result</a>;</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span>}</div>
<div class="ttc" id="aconfig_8h_html"><div class="ttname"><a href="config_8h.html">config.h</a></div><div class="ttdoc">used to place important parameter configurations for users</div></div>
<div class="ttc" id="aconfig_8h_html_adddd943909a2fac22c5fe8e3949d105f"><div class="ttname"><a href="config_8h.html#adddd943909a2fac22c5fe8e3949d105f">SPEED_UP_FREQ</a></div><div class="ttdeci">#define SPEED_UP_FREQ</div><div class="ttdoc">set speed update frequency to 200Hz</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00030">config.h:30</a></div></div>
<div class="ttc" id="aconfig_8h_html_ae9289eb166344bb9d0d6560b6da41118"><div class="ttname"><a href="config_8h.html#ae9289eb166344bb9d0d6560b6da41118">TIM13_FREQUENCY</a></div><div class="ttdeci">#define TIM13_FREQUENCY</div><div class="ttdoc">set speed/angle PID loop frequency to 100Hz</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00028">config.h:28</a></div></div>
<div class="ttc" id="afilter_8c_html_a07532054f0695f8abd4f68cece7b2f7e"><div class="ttname"><a href="filter_8c.html#a07532054f0695f8abd4f68cece7b2f7e">filter_coefficient_config</a></div><div class="ttdeci">void filter_coefficient_config(Filter_Structure_t *param, float cutoff_freq, float sample_freq, float coefficient)</div><div class="ttdoc">configure low-pass filter parameters</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00029">filter.c:29</a></div></div>
<div class="ttc" id="afilter_8c_html_a687b2ec6984ce9fa9d08189a3e7e1592"><div class="ttname"><a href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">filter_update_value</a></div><div class="ttdeci">float filter_update_value(Filter_Structure_t *param, short value)</div><div class="ttdoc">update the output value of the first-order low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00058">filter.c:58</a></div></div>
<div class="ttc" id="afilter_8c_html_a70331d536c7d5e99a76bd8651fd6b714"><div class="ttname"><a href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a></div><div class="ttdeci">volatile Filter_Structure_t velocity_filter</div><div class="ttdoc">motor rotation speed low pass filter handle</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00019">filter.c:19</a></div></div>
<div class="ttc" id="afilter_8c_html_aa1e7128fffcf2b56a6fb1c23f89afd75"><div class="ttname"><a href="filter_8c.html#aa1e7128fffcf2b56a6fb1c23f89afd75">filter_config</a></div><div class="ttdeci">void filter_config(void)</div><div class="ttdoc">configure low-pass filter parameters</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00047">filter.c:47</a></div></div>
<div class="ttc" id="afilter_8h_html"><div class="ttname"><a href="filter_8h.html">filter.h</a></div><div class="ttdoc">this is the header file of filter.c, which defines the structure of filter algorithm.</div></div>
<div class="ttc" id="afoc_8h_html"><div class="ttname"><a href="foc_8h.html">foc.h</a></div><div class="ttdoc">this is the header file of foc.c, which defines the structure of FOC algorithm and angle conversion f...</div></div>
<div class="ttc" id="afoc_8h_html_a2ce50cab9f20bc664e4888ffc07e656e"><div class="ttname"><a href="foc_8h.html#a2ce50cab9f20bc664e4888ffc07e656e">SPEED_COEFFICIENT</a></div><div class="ttdeci">#define SPEED_COEFFICIENT</div><div class="ttdoc">mechanical rotate speed conversion factor</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00028">foc.h:28</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html"><div class="ttname"><a href="struct_filter___structure__t.html">Filter_Structure_t</a></div><div class="ttdoc">structure of low-pass filter algorithm</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00016">filter.h:16</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a08c1824c5523caa58ec64b9fcfd48f92"><div class="ttname"><a href="struct_filter___structure__t.html#a08c1824c5523caa58ec64b9fcfd48f92">Filter_Structure_t::coefficient2</a></div><div class="ttdeci">float coefficient2</div><div class="ttdoc">low-pass filter coefficient 2</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00018">filter.h:18</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a72864a941ae97fcfd272e9493d6cb6c7"><div class="ttname"><a href="struct_filter___structure__t.html#a72864a941ae97fcfd272e9493d6cb6c7">Filter_Structure_t::last_result</a></div><div class="ttdeci">float last_result</div><div class="ttdoc">previous calculation results of low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00019">filter.h:19</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a845b4ebfab6dc03b5babd7f9ff5e2b6c"><div class="ttname"><a href="struct_filter___structure__t.html#a845b4ebfab6dc03b5babd7f9ff5e2b6c">Filter_Structure_t::current_result</a></div><div class="ttdeci">float current_result</div><div class="ttdoc">current calculation results of low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00020">filter.h:20</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html_a95c29040dec4c9e97cf2f64cf4212435"><div class="ttname"><a href="struct_filter___structure__t.html#a95c29040dec4c9e97cf2f64cf4212435">Filter_Structure_t::coefficient1</a></div><div class="ttdeci">float coefficient1</div><div class="ttdoc">low-pass filter coefficient 1</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00017">filter.h:17</a></div></div>
<div class="ttc" id="asystem_8c_html_a7e7cece1b30ef67968b244698f999b0e"><div class="ttname"><a href="system_8c.html#a7e7cece1b30ef67968b244698f999b0e">user_memset</a></div><div class="ttdeci">void user_memset(void *buf, unsigned char data, unsigned char num)</div><div class="ttdoc">format buffer array to fixed value, replace memset function</div><div class="ttdef"><b>Definition:</b> <a href="system_8c_source.html#l00054">system.c:54</a></div></div>
<div class="ttc" id="asystem_8h_html"><div class="ttname"><a href="system_8h.html">system.h</a></div><div class="ttdoc">system basic function header file</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2
</small></address>
</body>
</html>