INS-VN-200/vnproglib/c/help/getting_started_2main_8c-example.html
2024-05-14 02:14:13 +02:00

556 lines
47 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://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=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>VectorNav C Library: getting_started/main.c</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>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</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="proglib.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">VectorNav C Library
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div><!-- top -->
<!-- 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 class="header">
<div class="headertitle">
<div class="title">getting_started/main.c</div> </div>
</div><!--header-->
<div class="contents">
<p>This example illustrates basic connectivity and interaction with a VectorNav sensor using the low-level UART protocol code that is part of the <em>core</em> section of the library.</p>
<h1><a class="anchor" id="vsCGettingStarted"></a>
Visual Studio (Windows)</h1>
<ol type="1">
<li>Open the solution file for your specific Visual Studio version located at <code>&lt;root&gt;/c/examples/getting_started/projects/vs20XX/getting_started.sln</code>.</li>
<li>Build the entire solution by going to the menu <code>BUILD -&gt; Build Solution</code>.</li>
<li>Right-click the project <code>getting_started</code> and select <code>Debug -&gt; Start new instance</code>.</li>
</ol>
<h1><a class="anchor" id="makeCGettingStarted"></a>
Make (Linux/Mac OS X)</h1>
<ol type="1">
<li>Open a terminal and change to the directory <code>&lt;root&gt;/c/examples/getting_started</code> .</li>
<li>To build the example, run the command <code>make</code> .</li>
<li>Run the example by executing the command <code>./getting_started</code> .</li>
</ol>
<h1><a class="anchor" id="clionCGettingStarted"></a>
CLion (Windows/Linux/Mac OS X)</h1>
<ol type="1">
<li>Open the project file located at <code>&lt;root&gt;/c/examples/getting_started</code> in CLion.</li>
<li>Make sure the <code>getting_started</code> configuration is active. You can set this by clicking the small drop-down list in the upper-right corner and selecting the option <code>getting_started</code>.</li>
<li>Build the solution by going to the menu <code>Run -&gt; Build</code>.</li>
<li>Run the example by going to the menu <code>Run -&gt; Run 'gettings_started'</code>.</li>
</ol>
<h1><a class="anchor" id="otherCGettingStarted"></a>
Other</h1>
<p>To compile and run for an environment not listed here, you will need to add all of the <code>*.c</code> files in the directory <code>&lt;root&gt;/c/src</code> along with the file located at <code>&lt;root&gt;/c/examples/getting_started/main.c</code> to your project for compilation. You will also need to add <code>&lt;root&gt;/c/include</code> to your include directories.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/* Include files needed to use the UART protocol. */</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/util.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/protocol/upack.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/protocol/upackf.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/int.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/* This include file normally contains higher level abstractions for working</span></div>
<div class="line"><span class="comment"> * with VectorNav sensors. However, we include it for some string functions to</span></div>
<div class="line"><span class="comment"> * display named options for the sensor&#39;s register&#39;s fields. */</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="sensors_8h.html">vn/sensors.h</a>&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> packetFoundHandler(<span class="keywordtype">void</span> *userData, <a name="_a0"></a><a class="code" href="struct_vn_uart_packet.html">VnUartPacket</a> *packet, <span class="keywordtype">size_t</span> runningIndexOfPacketStart);</div>
<div class="line"><span class="keywordtype">void</span> UserUart_initialize(<span class="keywordtype">void</span>);</div>
<div class="line"><span class="keywordtype">bool</span> UserUart_checkForReceivedData(<span class="keywordtype">char</span>* buffer, <span class="keywordtype">size_t</span> bufferSize, <span class="keywordtype">size_t</span>* numOfBytesReceived);</div>
<div class="line"><span class="keywordtype">void</span> UserUart_mockReceivedData(<span class="keywordtype">char</span>* buffer, <span class="keywordtype">size_t</span> bufferSize);</div>
<div class="line"><span class="keywordtype">void</span> UserUart_sendData(<span class="keywordtype">char</span> *data, <span class="keywordtype">size_t</span> size);</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">bool</span> gIsCheckingForModelNumberResponse = <span class="keyword">false</span>;</div>
<div class="line"><span class="keywordtype">bool</span> gIsCheckingForAsyncOutputFreqResponse = <span class="keyword">false</span>;</div>
<div class="line"><span class="keywordtype">bool</span> gIsCheckingForVpeBasicControlResponse = <span class="keyword">false</span>;</div>
<div class="line">uint8_t gEnable, gHeadingMode, gFilteringMode, gTuningMode;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordtype">char</span> buffer[256];</div>
<div class="line"> <span class="keywordtype">size_t</span> numOfBytes, readModelNumberSize, writeAsyncOutputFreqSize, readVpeBasicControlSize, writeVpeBasicControlSize;</div>
<div class="line"> <span class="keywordtype">size_t</span> writeBinaryOutput1Size;</div>
<div class="line"> <span class="keywordtype">char</span> mockAsciiAsyncData[] = <span class="stringliteral">&quot;$VNYMR,+100.949,-007.330,+000.715,-00.0049,-00.2449,+00.5397,-01.258,-00.100,-09.701,-00.000018,+00.001122,-00.000551*69\r\n&quot;</span>;</div>
<div class="line"> <span class="keywordtype">char</span> mockReadModelNumberResponse[] = <span class="stringliteral">&quot;$VNRRG,01,VN-200T-CR*31\r\n&quot;</span>;</div>
<div class="line"> <span class="keywordtype">char</span> mockWriteAsyncOutputFrequencyResponse[] = <span class="stringliteral">&quot;$VNWRG,07,2*6F\r\n&quot;</span>;</div>
<div class="line"> <span class="keywordtype">char</span> mockFactoryDefaultReadVpeBasicControlResponse[] = <span class="stringliteral">&quot;$VNRRG,35,1,1,1,1*75\r\n&quot;</span>;</div>
<div class="line"> <span class="keywordtype">char</span> mockWriteVpeBasicControlResponse[] = <span class="stringliteral">&quot;$VNWRG,35,1,0,1,1*71\r\n&quot;</span>;</div>
<div class="line"> <span class="keywordtype">char</span> mockErrorMessage[] = <span class="stringliteral">&quot;$VNERR,12*72\r\n&quot;</span>;</div>
<div class="line"> uint8_t mockBinaryAsyncData[] = { 0xFA, 0x01, 0x09, 0x00, 0x70, 0x05, 0x00, 0x03, 0x0A, 0x00, 0x00, 0x00, 0x48, 0x0E, 0x2C, 0x42, 0x08, 0x4C, 0x37, 0xC1, 0x10, 0x38, 0x8B, 0xC2, 0xD4, 0xCB };</div>
<div class="line"> <span class="keywordtype">char</span> genReadModelNumberBuffer[256];</div>
<div class="line"> <span class="keywordtype">char</span> genWriteAsyncOutputFrequencyBuffer[256];</div>
<div class="line"> <span class="keywordtype">char</span> genReadVpeBasicControlBuffer[256];</div>
<div class="line"> <span class="keywordtype">char</span> genWriteVpeBasicControlBuffer[256];</div>
<div class="line"> <span class="keywordtype">char</span> genWriteBinaryOutput1Buffer[256];</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* This example provides an overview of the UART protocol functionality</span></div>
<div class="line"><span class="comment"> * of the VectorNav C Library.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Using the UART Protocol allows communicating with a VectorNav sensor</span></div>
<div class="line"><span class="comment"> * over a UART interface using both ASCII and binary protocols. Usage of</span></div>
<div class="line"><span class="comment"> * this &quot;core&quot; feature requires you to do all of the grunt work of</span></div>
<div class="line"><span class="comment"> * initializing and managing the UART port for your development</span></div>
<div class="line"><span class="comment"> * environment. Once this is setup, you will initialize the UART protocol</span></div>
<div class="line"><span class="comment"> * and then simply pass arrays of data between your UART code and the</span></div>
<div class="line"><span class="comment"> * VectorNav C Library&#39;s protocol code. To keep this example generic, we</span></div>
<div class="line"><span class="comment"> * will mock the necessary UART initialization and management functions</span></div>
<div class="line"><span class="comment"> * that would need to be replaced by code specific for your environment</span></div>
<div class="line"><span class="comment"> * to tie into a real VectorNav sensor. For now we just use some fake data</span></div>
<div class="line"><span class="comment"> * to illustrate the process. */</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/* The VnUartProtocol structure encapsulates the data used for buffering</span></div>
<div class="line"><span class="comment"> * and handling incoming data. It is not associated with sending commands</span></div>
<div class="line"><span class="comment"> * to the sensor. This will be illustrated further in the example. */</span></div>
<div class="line"> <a name="_a1"></a><a class="code" href="struct_vn_uart_packet_finder.html">VnUartPacketFinder</a> up;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* First thing you should do is initialize the data structure. */</span></div>
<div class="line"> VnUartPacketFinder_initialize(&amp;up);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Register our callback method for when the VnUartPacketFinder finds an</span></div>
<div class="line"><span class="comment"> ASCII asynchronous packet. */</span></div>
<div class="line"> VnUartPacketFinder_registerPacketFoundHandler(&amp;up, packetFoundHandler, NULL);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Initialize the UART port (this is mimicked in this example). */</span></div>
<div class="line"> UserUart_initialize();</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* With our VnUartProtocol and mock UART initialized, we will fake an</span></div>
<div class="line"><span class="comment"> * asynchronous message output by a VectorNav sensor and received by our</span></div>
<div class="line"><span class="comment"> * mock UART port. */</span></div>
<div class="line"> UserUart_mockReceivedData(mockAsciiAsyncData, strlen(mockAsciiAsyncData));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Normally you will be continually checking for new UART data and then</span></div>
<div class="line"><span class="comment"> * passing any received data to the VnUartProtocol to build, parse and</span></div>
<div class="line"><span class="comment"> * verify data packets. Since this is just for demonstration purposes, we</span></div>
<div class="line"><span class="comment"> * just poll the mock UART for its current data. In a real world</span></div>
<div class="line"><span class="comment"> * environment where you are servicing a real UART port, you would have</span></div>
<div class="line"><span class="comment"> * code similar to the code lines below.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * char buffer[256];</span></div>
<div class="line"><span class="comment"> * while (1)</span></div>
<div class="line"><span class="comment"> * {</span></div>
<div class="line"><span class="comment"> * size_t numOfBytes;</span></div>
<div class="line"><span class="comment"> * if (UserUart_checkForReceivedData(buffer, 256, &amp;numOfBytes))</span></div>
<div class="line"><span class="comment"> * VnUartPacketFinder_processReceivedData(buffer, numOfBytes);</span></div>
<div class="line"><span class="comment"> * }</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now when we pass the data to the VnUartPacketFinder, our function</span></div>
<div class="line"><span class="comment"> * validPacketFoundHandler will be called since we passed in a complete</span></div>
<div class="line"><span class="comment"> * and valid data packet. Scroll down to the function validPacketFoundHandler</span></div>
<div class="line"><span class="comment"> * to see how to process and extract the values from the packet. */</span></div>
<div class="line"> VnUartPacketFinder_processData(&amp;up,(uint8_t*) buffer, numOfBytes);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Processing received asynchronous data from the sensor is fairly straight</span></div>
<div class="line"><span class="comment"> * forward. However, you may wish to query or configure the sensor and in</span></div>
<div class="line"><span class="comment"> * theory this is still straight forward if you do not add code to handle</span></div>
<div class="line"><span class="comment"> * retransmits of commands or communication timeouts. We will show the</span></div>
<div class="line"><span class="comment"> * basic structure of querying and configuring and leave it to the</span></div>
<div class="line"><span class="comment"> * developer for adding edge case handling code. The file sensors.cpp in</span></div>
<div class="line"><span class="comment"> * the C++ may serve as a reference for adding this extra code.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/* We will first illustrate querying the sensor&#39;s model number. First we</span></div>
<div class="line"><span class="comment"> * generate a read register command. */</span></div>
<div class="line"> <a name="a2"></a><a class="code" href="group__uart__genread__functions.html#ga76e37c9c18f1b4b96d17e9e4d0a195ce">VnUartPacket_genReadModelNumber</a>(</div>
<div class="line"> genReadModelNumberBuffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(genReadModelNumberBuffer),</div>
<div class="line"> VNERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> &amp;readModelNumberSize);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now send the data to the sensor. */</span></div>
<div class="line"> gIsCheckingForModelNumberResponse = <span class="keyword">true</span>;</div>
<div class="line"> UserUart_sendData(genReadModelNumberBuffer, readModelNumberSize);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Mock that the sensor responded to our request. */</span></div>
<div class="line"> UserUart_mockReceivedData(mockReadModelNumberResponse, strlen(mockReadModelNumberResponse));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now process the mock data that our fake UART port received and hand it</span></div>
<div class="line"><span class="comment"> * over to our UART packet finder. */</span></div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> VnUartPacketFinder_processData(&amp;up, (uint8_t*)buffer, numOfBytes);</div>
<div class="line"> gIsCheckingForModelNumberResponse = <span class="keyword">false</span>;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Let&#39;s see how to perform a trivial configuration of the sensor. We will</span></div>
<div class="line"><span class="comment"> * change the asynchronous data output frequency to 2 Hz. */</span></div>
<div class="line"> <a name="a3"></a><a class="code" href="group__uart__genwrite__functions.html#gafe690f4740b28ac67955415708496354">VnUartPacket_genWriteAsyncDataOutputFrequency</a>(</div>
<div class="line"> genWriteAsyncOutputFrequencyBuffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(genWriteAsyncOutputFrequencyBuffer),</div>
<div class="line"> VNERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> &amp;writeAsyncOutputFreqSize,</div>
<div class="line"> 2);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now send the data to the sensor. */</span></div>
<div class="line"> gIsCheckingForAsyncOutputFreqResponse = <span class="keyword">true</span>;</div>
<div class="line"> UserUart_sendData(genWriteAsyncOutputFrequencyBuffer, writeAsyncOutputFreqSize);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Mock that the sensor responded to our request. */</span></div>
<div class="line"> UserUart_mockReceivedData(mockWriteAsyncOutputFrequencyResponse, strlen(mockWriteAsyncOutputFrequencyResponse));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now process the mock data that our fake UART port received and hand it</span></div>
<div class="line"><span class="comment"> * over to our UART packet finder. */</span></div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> VnUartPacketFinder_processData(&amp;up, (uint8_t*)buffer, numOfBytes);</div>
<div class="line"> gIsCheckingForAsyncOutputFreqResponse = <span class="keyword">false</span>;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Other configuration register on the VectorNav sensors have multiple</span></div>
<div class="line"><span class="comment"> * fields that need to be set when we write to them. If we only are</span></div>
<div class="line"><span class="comment"> * interested in one field, a safe and easy way to perform this is to first</span></div>
<div class="line"><span class="comment"> * read the current configuration, change the fields we are concerned with,</span></div>
<div class="line"><span class="comment"> * and then write the settings back to the sensor. We will illustrate this</span></div>
<div class="line"><span class="comment"> * now by changing the sensors heading mode of the VPE Basic Control</span></div>
<div class="line"><span class="comment"> * register. */</span></div>
<div class="line"></div>
<div class="line"> printf(<span class="stringliteral">&quot;Reading current values of the VPE Basic Control register.\n&quot;</span>);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* First generate a read register command. */</span></div>
<div class="line"> <a name="a4"></a><a class="code" href="group__uart__genread__functions.html#ga44d3ce815ddda62cee9461454f069f71">VnUartPacket_genReadVpeBasicControl</a>(</div>
<div class="line"> genReadVpeBasicControlBuffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(genReadVpeBasicControlBuffer),</div>
<div class="line"> VNERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> &amp;readVpeBasicControlSize);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now send the data to the sensor. */</span></div>
<div class="line"> gIsCheckingForVpeBasicControlResponse = <span class="keyword">true</span>;</div>
<div class="line"> UserUart_sendData(genReadVpeBasicControlBuffer, readVpeBasicControlSize);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Mock that the sensor responded to our request. */</span></div>
<div class="line"> UserUart_mockReceivedData(mockFactoryDefaultReadVpeBasicControlResponse, strlen(mockFactoryDefaultReadVpeBasicControlResponse));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Now process the mock data that our fake UART port received and hand it</span></div>
<div class="line"><span class="comment"> * over to our UART packet finder. */</span></div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> VnUartPacketFinder_processData(&amp;up, (uint8_t*)buffer, numOfBytes);</div>
<div class="line"> gIsCheckingForVpeBasicControlResponse = <span class="keyword">false</span>;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* The validPacketFoundHandler will have set the current values of the VPE</span></div>
<div class="line"><span class="comment"> * Basic Control register into our global variables. Let&#39;s now change the</span></div>
<div class="line"><span class="comment"> * heading mode field for this register while keeping the other fields in</span></div>
<div class="line"><span class="comment"> * their current state. */</span></div>
<div class="line"></div>
<div class="line"> printf(<span class="stringliteral">&quot;Writing new values to the VPE Basic Control register.\n&quot;</span>);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Generate the write register command. */</span></div>
<div class="line"> <a name="a5"></a><a class="code" href="group__uart__genwrite__functions.html#ga6d561442602beacf394680798d43d9f7">VnUartPacket_genWriteVpeBasicControl</a>(</div>
<div class="line"> genWriteVpeBasicControlBuffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(genWriteVpeBasicControlBuffer),</div>
<div class="line"> VNERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> &amp;writeVpeBasicControlSize,</div>
<div class="line"> gEnable,</div>
<div class="line"> 0, <span class="comment">/* Could possibly use a value from the enum HeadingMode in sensors.h. */</span></div>
<div class="line"> gFilteringMode,</div>
<div class="line"> gTuningMode);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Send the data to the sensor. */</span></div>
<div class="line"> gIsCheckingForVpeBasicControlResponse = <span class="keyword">true</span>;</div>
<div class="line"> UserUart_sendData(genWriteVpeBasicControlBuffer, writeVpeBasicControlSize);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Mock that the sensor responded to our request. */</span></div>
<div class="line"> UserUart_mockReceivedData(mockWriteVpeBasicControlResponse, strlen(mockWriteVpeBasicControlResponse));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Process the mock data that our fake UART port received and hand it</span></div>
<div class="line"><span class="comment"> * over to our UART packet finder. */</span></div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> VnUartPacketFinder_processData(&amp;up, (uint8_t*)buffer, numOfBytes);</div>
<div class="line"> gIsCheckingForVpeBasicControlResponse = <span class="keyword">false</span>;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* The VectorNav sensor also supports binary asynchronous data output,</span></div>
<div class="line"><span class="comment"> * which can be configured by the user to support flexible configuration of</span></div>
<div class="line"><span class="comment"> * data output types. In this example, we will show how to configure the</span></div>
<div class="line"><span class="comment"> * sensor&#39;s binary output configuration register, and then process a packet</span></div>
<div class="line"><span class="comment"> * received of this binary output data. */</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Generate our command to configure the Binary Output 1 register. Normally</span></div>
<div class="line"><span class="comment"> * when working with the functions from the file uart.h, the data types as</span></div>
<div class="line"><span class="comment"> * listed in the user manual are used, without any abstractions getting in</span></div>
<div class="line"><span class="comment"> * the way. However, here we use some enums defined in sensors.h for</span></div>
<div class="line"><span class="comment"> * specifying the flags of the register&#39;s fields since it is much easier to</span></div>
<div class="line"><span class="comment"> * understand. Here we configure the sensor to output yaw, pitch, roll and</span></div>
<div class="line"><span class="comment"> * timestart data at 4 Hz. Note that the sensor&#39;s user manual requires</span></div>
<div class="line"><span class="comment"> * specifying which groups are present; however, this function call will</span></div>
<div class="line"><span class="comment"> * take care of determining which fields are present. */</span></div>
<div class="line"> <a name="a6"></a><a class="code" href="group__uart__genwrite__functions.html#ga07afb3340e7843c1ecb9f55f1b5091b1">VnUartPacket_genWriteBinaryOutput1</a>(</div>
<div class="line"> (uint8_t*)genWriteBinaryOutput1Buffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(genWriteBinaryOutput1Buffer),</div>
<div class="line"> VNERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> &amp;writeBinaryOutput1Size,</div>
<div class="line"> ASYNCMODE_PORT1,</div>
<div class="line"> 200,</div>
<div class="line"> COMMONGROUP_TIMESTARTUP | COMMONGROUP_YAWPITCHROLL, <span class="comment">/* Note use of binary OR to configure flags. */</span></div>
<div class="line"> TIMEGROUP_NONE,</div>
<div class="line"> IMUGROUP_NONE,</div>
<div class="line"> GPSGROUP_NONE,</div>
<div class="line"> ATTITUDEGROUP_NONE,</div>
<div class="line"> INSGROUP_NONE);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Send the data to the sensor. */</span></div>
<div class="line"> UserUart_sendData(genWriteBinaryOutput1Buffer, writeBinaryOutput1Size);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Mock that the sensor responded to our request. */</span></div>
<div class="line"> UserUart_mockReceivedData((<span class="keywordtype">char</span>*)mockBinaryAsyncData, 26);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Process the mock data that our fake UART port received and hand it</span></div>
<div class="line"><span class="comment"> * over to our UART packet finder. */</span></div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> VnUartPacketFinder_processData(&amp;up, (uint8_t*)buffer, numOfBytes);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Lastly, you may want to include code that checks for error messages</span></div>
<div class="line"><span class="comment"> * output from the sensor. To demonstrate, we pass a fake error message to</span></div>
<div class="line"><span class="comment"> * be handled by our code. */</span></div>
<div class="line"> UserUart_mockReceivedData(mockErrorMessage, strlen(mockErrorMessage));</div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> VnUartPacketFinder_processData(&amp;up, (uint8_t*)buffer, numOfBytes);</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> packetFoundHandler(<span class="keywordtype">void</span> *userData, <a class="code" href="struct_vn_uart_packet.html">VnUartPacket</a> *packet, <span class="keywordtype">size_t</span> runningIndexOfPacketStart)</div>
<div class="line">{</div>
<div class="line"> <span class="comment">/* Silence &#39;unreferenced formal parameters&#39; warning in Visual Studio. */</span></div>
<div class="line"> (runningIndexOfPacketStart);</div>
<div class="line"> (userData);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* When this function is called, the packet will already have been</span></div>
<div class="line"><span class="comment"> * validated so no checksum/CRC check is required. */</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/* First see if this is an ASCII or binary packet. */</span></div>
<div class="line"> <span class="keywordflow">if</span> (VnUartPacket_type(packet) == PACKETTYPE_ASCII)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">/* Now that we know this is an ASCII packet, we can call the various</span></div>
<div class="line"><span class="comment"> * ASCII functions to further process this packet. */</span></div>
<div class="line"> <span class="keywordflow">if</span> (VnUartPacket_isAsciiAsync(packet))</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">/* We know we have an ASCII asynchronous data packet. Let&#39;s see if</span></div>
<div class="line"><span class="comment"> * this is a message type we are looking for. */</span></div>
<div class="line"> VnAsciiAsync asyncType = VnUartPacket_determineAsciiAsyncType(packet);</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span> (asyncType == VNYMR)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">/* Parse the VNYMR message. */</span></div>
<div class="line"> <a name="_a7"></a><a class="code" href="unionvec3f.html">vec3f</a> ypr, mag, accel, angularRate;</div>
<div class="line"> <span class="keywordtype">char</span> yprStr[100], magStr[100], accelStr[100], angularRateStr[100];</div>
<div class="line"></div>
<div class="line"> <a name="a8"></a><a class="code" href="group__uart_packet_ascii_async_parsers.html#ga8753a6d113cc5490df24ad5cb3d04309">VnUartPacket_parseVNYMR</a>(packet, &amp;ypr, &amp;mag, &amp;accel, &amp;angularRate);</div>
<div class="line"></div>
<div class="line"> str_vec3f(yprStr, ypr);</div>
<div class="line"> str_vec3f(magStr, mag);</div>
<div class="line"> str_vec3f(accelStr, accel);</div>
<div class="line"> str_vec3f(angularRateStr, angularRate);</div>
<div class="line"> printf(<span class="stringliteral">&quot;[Found VNYMR Packet]\n&quot;</span>);</div>
<div class="line"> printf(<span class="stringliteral">&quot; YawPitchRoll: %s\n&quot;</span>, yprStr);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Magnetic: %s\n&quot;</span>, magStr);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Acceleration: %s\n&quot;</span>, accelStr);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Angular Rate: %s\n&quot;</span>, angularRateStr);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (VnUartPacket_isResponse(packet))</div>
<div class="line"> {</div>
<div class="line"> <span class="keywordflow">if</span> (gIsCheckingForModelNumberResponse)</div>
<div class="line"> {</div>
<div class="line"> <span class="keywordtype">char</span> modelNumber[100];</div>
<div class="line"></div>
<div class="line"> <a name="a9"></a><a class="code" href="group__uart__genwrite__functions.html#gad811578908e41056b2b3a042a8e7f421">VnUartPacket_parseModelNumber</a>(packet, modelNumber);</div>
<div class="line"></div>
<div class="line"> printf(<span class="stringliteral">&quot;Model Number: %s\n&quot;</span>, modelNumber);</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (gIsCheckingForAsyncOutputFreqResponse)</div>
<div class="line"> {</div>
<div class="line"> uint32_t asyncOutputFreq;</div>
<div class="line"></div>
<div class="line"> <a name="a10"></a><a class="code" href="group__uart__genwrite__functions.html#gaa89c4d2c71f4e60cacb4f9da90367e12">VnUartPacket_parseAsyncDataOutputFrequency</a>(packet, &amp;asyncOutputFreq);</div>
<div class="line"></div>
<div class="line"> printf(<span class="stringliteral">&quot;Asynchronous Output Frequency: %u Hz\n&quot;</span>, asyncOutputFreq);</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (gIsCheckingForVpeBasicControlResponse)</div>
<div class="line"> {</div>
<div class="line"> <span class="keywordtype">char</span> enableStr[100], headingModeStr[100], filteringModeStr[100], tuningModeStr[100];</div>
<div class="line"></div>
<div class="line"> <a name="a11"></a><a class="code" href="group__uart__genwrite__functions.html#ga8c6e9480662677fe2ca207383b770b2d">VnUartPacket_parseVpeBasicControl</a>(packet, &amp;gEnable, &amp;gHeadingMode, &amp;gFilteringMode, &amp;gTuningMode);</div>
<div class="line"></div>
<div class="line"> strFromBool(enableStr, (<span class="keywordtype">bool</span>) gEnable);</div>
<div class="line"> <a name="a12"></a><a class="code" href="sensors_8h.html#a2930ef60abd21d0523be4da88b3e4446">strFromHeadingMode</a>(headingModeStr, gHeadingMode);</div>
<div class="line"> <a name="a13"></a><a class="code" href="sensors_8h.html#a8e0d2220e7a01304fee6395fd1bf82d4">strFromFilterMode</a>(filteringModeStr, gFilteringMode);</div>
<div class="line"> <a class="code" href="sensors_8h.html#a8e0d2220e7a01304fee6395fd1bf82d4">strFromFilterMode</a>(tuningModeStr, gTuningMode);</div>
<div class="line"> printf(<span class="stringliteral">&quot;[VPE Basic Control]\n&quot;</span>);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Enable: %s\n&quot;</span>, enableStr);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Heading Mode: %s\n&quot;</span>, headingModeStr);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Filtering Mode: %s\n&quot;</span>, filteringModeStr);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Tuning Mode: %s\n&quot;</span>, tuningModeStr);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (VnUartPacket_isError(packet))</div>
<div class="line"> {</div>
<div class="line"> uint8_t error;</div>
<div class="line"> <span class="keywordtype">char</span> errorStr[100];</div>
<div class="line"></div>
<div class="line"> <a name="a14"></a><a class="code" href="group__uart__genwrite__functions.html#ga4488412e853a2c1655fa1ebbb63d3efa">VnUartPacket_parseError</a>(packet, &amp;error);</div>
<div class="line"></div>
<div class="line"> <a name="a15"></a><a class="code" href="sensors_8h.html#a8c82ca99e6dc14d5f5a050c53420f788">strFromSensorError</a>(errorStr, (SensorError) error);</div>
<div class="line"></div>
<div class="line"> printf(<span class="stringliteral">&quot;Sensor Error: %s\n&quot;</span>, errorStr);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (VnUartPacket_type(packet) == PACKETTYPE_BINARY) {</div>
<div class="line"> uint64_t timeStartup;</div>
<div class="line"> <a class="code" href="unionvec3f.html">vec3f</a> ypr;</div>
<div class="line"> <span class="keywordtype">char</span> yprStr[100];</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* See if this is a binary packet type we are expecting. */</span></div>
<div class="line"> <span class="keywordflow">if</span> (!VnUartPacket_isCompatible(</div>
<div class="line"> packet,</div>
<div class="line"> COMMONGROUP_TIMESTARTUP | COMMONGROUP_YAWPITCHROLL,</div>
<div class="line"> TIMEGROUP_NONE,</div>
<div class="line"> IMUGROUP_NONE,</div>
<div class="line"> GPSGROUP_NONE,</div>
<div class="line"> ATTITUDEGROUP_NONE,</div>
<div class="line"> INSGROUP_NONE))</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">/* Not the type of binary packet we are expecting. */</span></div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Ok, we have our expected binary output packet. Since there are many</span></div>
<div class="line"><span class="comment"> * ways to configure the binary data output, the burden is on the user</span></div>
<div class="line"><span class="comment"> * to correctly parse the binary packet. However, we can make use of</span></div>
<div class="line"><span class="comment"> * the parsing convenience methods provided by the VnUartPacket structure.</span></div>
<div class="line"><span class="comment"> * When using these convenience methods, you have to extract them in</span></div>
<div class="line"><span class="comment"> * the order they are organized in the binary packet per the User Manual. */</span></div>
<div class="line"> timeStartup = <a name="a16"></a><a class="code" href="group__uart_packet_binary_extractors.html#ga6fe1746b5fec18983245f1bd020f39e9">VnUartPacket_extractUint64</a>(packet);</div>
<div class="line"> ypr = <a name="a17"></a><a class="code" href="group__uart_packet_binary_extractors.html#gab50c5ac1ff646d31e2648fe67c8977e4">VnUartPacket_extractVec3f</a>(packet);</div>
<div class="line"> str_vec3f(yprStr, ypr);</div>
<div class="line"> printf(<span class="stringliteral">&quot;[Binary Packet Received]\n&quot;</span>);</div>
<div class="line"> printf(<span class="stringliteral">&quot; TimeStartup: %u\n&quot;</span>, (uint32_t) timeStartup);</div>
<div class="line"> printf(<span class="stringliteral">&quot; Yaw Pitch Roll: %s\n&quot;</span>, yprStr);</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/* Some variables to enable our mock UART port. */</span></div>
<div class="line"><span class="keywordtype">char</span>* mockUartReceivedDataBuffer[256];</div>
<div class="line"><span class="keywordtype">size_t</span> mockUartReceivedDataSize;</div>
<div class="line"></div>
<div class="line"><span class="comment">/* This is a mock function which in a real development environment would</span></div>
<div class="line"><span class="comment"> * contain code to initialize the device&#39;s UART port. However, to keep this</span></div>
<div class="line"><span class="comment"> * example generic, we simply initialize our program to mimic a UART port</span></div>
<div class="line"><span class="comment"> * provided by the user. */</span></div>
<div class="line"><span class="keywordtype">void</span> UserUart_initialize(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line"> mockUartReceivedDataSize = 0;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/* This is another mock function which would be replaced in a real program to</span></div>
<div class="line"><span class="comment"> * actually query the environment&#39;s UART for any data received. We just mock</span></div>
<div class="line"><span class="comment"> * the data in this example. */</span></div>
<div class="line"><span class="keywordtype">bool</span> UserUart_checkForReceivedData(<span class="keywordtype">char</span>* buffer, <span class="keywordtype">size_t</span> bufferSize, <span class="keywordtype">size_t</span>* numOfBytesReceived)</div>
<div class="line">{</div>
<div class="line"> <span class="comment">/* Silence &#39;unreferenced formal parameters&#39; warning in Visual Studio. */</span></div>
<div class="line"> (bufferSize);</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span> (mockUartReceivedDataSize == 0)</div>
<div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"></div>
<div class="line"> memcpy(buffer, mockUartReceivedDataBuffer, mockUartReceivedDataSize);</div>
<div class="line"> *numOfBytesReceived = mockUartReceivedDataSize;</div>
<div class="line"></div>
<div class="line"> mockUartReceivedDataSize = 0;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/* This is a helper method for our mocked UART port that will mimic data that</span></div>
<div class="line"><span class="comment"> * has been received on a UART port. This function would not be implemented in</span></div>
<div class="line"><span class="comment"> * an environment that is using an actual UART port. */</span></div>
<div class="line"><span class="keywordtype">void</span> UserUart_mockReceivedData(<span class="keywordtype">char</span>* buffer, <span class="keywordtype">size_t</span> bufferSize)</div>
<div class="line">{</div>
<div class="line"> memcpy(mockUartReceivedDataBuffer, buffer, bufferSize);</div>
<div class="line"> mockUartReceivedDataSize = bufferSize;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/* This is a method for simulating sending data to a VectorNav sensor. This</span></div>
<div class="line"><span class="comment"> * will need to be implemented by the developer to actually send the data over</span></div>
<div class="line"><span class="comment"> * the system&#39;s UART. */</span></div>
<div class="line"><span class="keywordtype">void</span> UserUart_sendData(<span class="keywordtype">char</span> *data, <span class="keywordtype">size_t</span> size)</div>
<div class="line">{</div>
<div class="line"> <span class="comment">/* Silence &#39;unreferenced formal parameters&#39; warning in Visual Studio. */</span></div>
<div class="line"> (data);</div>
<div class="line"> (size);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* Do nothing since we are mocking a UART port. */</span></div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Tue Jan 3 2017 18:26:57 for VectorNav C Library by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.10
</small></address>
</body>
</html>