Files
INS-VN-200/vnproglib/cpp/help/uart_protocol_2main_8cpp-example.html
r.koeppe 2d22ccd2d6 initial
2024-05-14 02:14:13 +02:00

499 lines
43 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: uart_protocol/main.cpp</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>Classes</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">uart_protocol/main.cpp</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.</p>
<h1><a class="anchor" id="vsCppUartProtocol"></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;/cpp/examples/uart_protocol/projects/vs20XX/uart_protocol.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>uart_protocol</code> and select <code>Debug -&gt; Start new instance</code> .</li>
</ol>
<h1><a class="anchor" id="makeCppUartProtocol"></a>
Make (Linux/Mac OS X)</h1>
<ol type="1">
<li>Open a terminal and change to the directory <code>&lt;root&gt;/cpp/examples/uart_protocol</code> .</li>
<li>To build the example, run the command <code>make</code> .</li>
<li>Run the example by executing the command <code>./uart_protocol</code> .</li>
</ol>
<h1><a class="anchor" id="cpplionCUartProtocol"></a>
CLion (Windows/Linux/Mac OS X)</h1>
<ol type="1">
<li>Open the project file located at <code>&lt;root&gt;/cpp/examples/uart_protocol</code> in CLion.</li>
<li>Make sure the <code>uart_protocol</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>uart_protocol</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 'uart_protocol'</code> .</li>
</ol>
<h1><a class="anchor" id="otherCppUartProtocol"></a>
Other</h1>
<p>To compile and run for an environment not listed here, you will need to add all of the <code>*.cpp</code> files in the directory <code>&lt;root&gt;/cpp/src</code> along with the file located at <code>&lt;root&gt;/c/examples/uart_protocol/main.c</code> to your project for compilation. You will also need to add <code>&lt;root&gt;/cpp/include</code> to your include directories.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// Include this header file to get access to VectorNav UART protocol methods.</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/packet.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/sensors.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacevn_1_1math.html">vn::math</a>;</div>
<div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacevn_1_1protocol_1_1uart.html">vn::protocol::uart</a>;</div>
<div class="line"></div>
<div class="line"><span class="comment">// Method declarations for future use.</span></div>
<div class="line"><span class="keywordtype">void</span> validPacketFoundHandler(<span class="keywordtype">void</span>* userData, <a name="_a0"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html">Packet</a> &amp;packet, <span class="keywordtype">size_t</span> runningIndexOfPacketStart);</div>
<div class="line"><span class="keywordtype">void</span> UserUart_initialize();</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_mockReceivedData(<span class="keywordtype">string</span> data);</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_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"></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">int</span> argc, <span class="keywordtype">char</span> *argv[])</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;</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"></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"> </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">// just the UART Protocol features without the VnSensor object requires you</span></div>
<div class="line"> <span class="comment">// to do all of the grunt work of initializing and managing the UART port</span></div>
<div class="line"> <span class="comment">// for your development environment. Once this is setup, you will</span></div>
<div class="line"> <span class="comment">// initialize the UART protocol and then simply pass arrays of data between</span></div>
<div class="line"> <span class="comment">// your UART code and the VectorNav C++ Library&#39;s protocol code. To keep</span></div>
<div class="line"> <span class="comment">// this example generic, we will mock the necessary UART initialization and</span></div>
<div class="line"> <span class="comment">// management functions that would need to be replaced by code specific for</span></div>
<div class="line"> <span class="comment">// your environment to tie into a real VectorNav sensor. For now we just</span></div>
<div class="line"> <span class="comment">// use some fake data to illustrate the process.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// The Protocol structure encapsulates the data used for buffering and</span></div>
<div class="line"> <span class="comment">// handling incoming data. It is not associated with sending commands to</span></div>
<div class="line"> <span class="comment">// the sensor. This will be illustrated further in the example.</span></div>
<div class="line"> <a name="_a1"></a><a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html">PacketFinder</a> pf;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Register our callback method for when the PacketFinder finds an ASCII</span></div>
<div class="line"> <span class="comment">// asynchronous packet.</span></div>
<div class="line"> pf.<a name="a2"></a><a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a9e776aea3fee7838d59b2681914a2804">registerPossiblePacketFoundHandler</a>(NULL, <a name="a3"></a><a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#ad2ecc4731a2ede8f263c88a03029fe4a">vn::protocol::uart::PacketFinder::ValidPacketFoundHandler</a>(validPacketFoundHandler));</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 PacketFinder 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(<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"></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 PacketFinder 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, sizeof(buffer), &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"> 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 PacketFinder, 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"> pf.<a name="a4"></a><a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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 may</span></div>
<div class="line"> <span class="comment">// serve as a reference for adding this extra code.</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"> numOfBytes = Packet::genReadModelNumber(ERRORDETECTIONMODE_CHECKSUM, buffer, <span class="keyword">sizeof</span>(buffer));</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(buffer, numOfBytes);</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="stringliteral">&quot;$VNRRG,01,VN-200T-CR*31\r\n&quot;</span>);</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"> pf.<a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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"> numOfBytes = Packet::genWriteAsyncDataOutputFrequency(ERRORDETECTIONMODE_CHECKSUM, buffer, <span class="keyword">sizeof</span>(buffer), 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(buffer, numOfBytes);</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="stringliteral">&quot;$VNWRG,07,2*6F\r\n&quot;</span>);</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"> pf.<a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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 registers 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"> cout &lt;&lt; <span class="stringliteral">&quot;Reading current values of the VPE Basic Control register.&quot;</span> &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// First generate a read register command.</span></div>
<div class="line"> numOfBytes = Packet::genReadVpeBasicControl(ERRORDETECTIONMODE_CHECKSUM, buffer, <span class="keyword">sizeof</span>(buffer));</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(buffer, numOfBytes);</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="stringliteral">&quot;$VNRRG,35,1,1,1,1*75\r\n&quot;</span>);</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"> pf.<a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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"> cout &lt;&lt; <span class="stringliteral">&quot;Writing new values to the VPE Basic Control register.&quot;</span> &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Generate the write register command.</span></div>
<div class="line"> numOfBytes = Packet::genWriteVpeBasicControl(</div>
<div class="line"> ERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> buffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(buffer),</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(buffer, numOfBytes);</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="stringliteral">&quot;$VNWRG,35,1,0,1,1*71\r\n&quot;</span>);</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"> pf.<a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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"> numOfBytes = Packet::genWriteBinaryOutput1(</div>
<div class="line"> ERRORDETECTIONMODE_CHECKSUM,</div>
<div class="line"> buffer,</div>
<div class="line"> <span class="keyword">sizeof</span>(buffer),</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(buffer, numOfBytes);</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Mock that the sensor responded to our request by outputting async binary data.</span></div>
<div class="line"> UserUart_mockReceivedData(reinterpret_cast&lt;char*&gt;(mockBinaryAsyncData), <span class="keyword">sizeof</span>(mockBinaryAsyncData));</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"> pf.<a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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(<span class="stringliteral">&quot;$VNERR,12*72\r\n&quot;</span>);</div>
<div class="line"> UserUart_checkForReceivedData(buffer, <span class="keyword">sizeof</span>(buffer), &amp;numOfBytes);</div>
<div class="line"> pf.<a class="code" href="classvn_1_1protocol_1_1uart_1_1_packet_finder.html#a433d1dda70ec1fdcfece9d0d80c4994e">processReceivedData</a>(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> validPacketFoundHandler(<span class="keywordtype">void</span>* userData, <a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html">Packet</a> &amp;packet, <span class="keywordtype">size_t</span> runningIndexOfPacketStart)</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> (packet.<a name="a5"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#a673e63cf13227931bc6c1309b47bbc7c">type</a>() == Packet::TYPE_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> (packet.<a name="a6"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#a86c26ef2939346c3c1ccdb70176bbe42">isAsciiAsync</a>())</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"> AsciiAsync asyncType = packet.<a name="a7"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#aac35580a613fcb4677b31bde124787f5">determineAsciiAsyncType</a>();</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="_a8"></a><a class="code" href="structvn_1_1math_1_1vec.html">vec3f</a> ypr, mag, accel, angularRate;</div>
<div class="line"> packet.<a name="a9"></a><a class="code" href="group__uart_packet_ascii_async_parsers.html#ga3b96b423fc20c6d8175d037b92df0ac3">parseVNYMR</a>(&amp;ypr, &amp;mag, &amp;accel, &amp;angularRate);</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[Found VNYMR Packet]&quot;</span> &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; YawPitchRoll: &quot;</span> &lt;&lt; ypr &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Magnetic: &quot;</span> &lt;&lt; mag &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Acceleration: &quot;</span> &lt;&lt; accel &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Angular Rate: &quot;</span> &lt;&lt; angularRate &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (packet.<a name="a10"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#a7d5948d6bec2ce409f533e1e89bd46d4">isResponse</a>())</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"> packet.<a name="a11"></a><a class="code" href="group__uart_ascii_response_parsers.html#ga8b6517ab04af71c8b71ac2133219f35b">parseModelNumber</a>(modelNumber);</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Model Number: &quot;</span> &lt;&lt; modelNumber &lt;&lt; endl;</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"> packet.<a name="a12"></a><a class="code" href="group__uart_ascii_response_parsers.html#gabf6b21b86d05b6b6c59f3a1fd7b245a7">parseAsyncDataOutputFrequency</a>(&amp;asyncOutputFreq);</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Asynchronous Output Frequency: &quot;</span> &lt;&lt; asyncOutputFreq &lt;&lt; <span class="stringliteral">&quot; Hz&quot;</span> &lt;&lt; endl;</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"> packet.<a name="a13"></a><a class="code" href="group__uart_ascii_response_parsers.html#gaac03e6d64655a22820c05bded7f66deb">parseVpeBasicControl</a>(&amp;gEnable, &amp;gHeadingMode, &amp;gFilteringMode, &amp;gTuningMode);</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[VPE Basic Control]&quot;</span> &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Enable: &quot;</span> &lt;&lt; (gEnable ? <span class="stringliteral">&quot;true&quot;</span> : <span class="stringliteral">&quot;false&quot;</span>) &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Heading Mode: &quot;</span> &lt;&lt; <span class="keyword">static_cast&lt;</span>vn::protocol::uart::HeadingMode<span class="keyword">&gt;</span>(gHeadingMode) &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Filtering Mode: &quot;</span> &lt;&lt; <span class="keyword">static_cast&lt;</span>vn::protocol::uart::FilterMode<span class="keyword">&gt;</span>(gFilteringMode) &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Tuning Mode: &quot;</span> &lt;&lt; <span class="keyword">static_cast&lt;</span>vn::protocol::uart::FilterMode<span class="keyword">&gt;</span>(gTuningMode) &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (packet.<a name="a14"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#a526fbedc50f063405118cc5890fbcb0b">isError</a>())</div>
<div class="line"> {</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Sensor Error: &quot;</span> &lt;&lt; packet.<a name="a15"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#a018e534f5007b9313264ee7f270ce177">parseError</a>() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (packet.<a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#a673e63cf13227931bc6c1309b47bbc7c">type</a>() == Packet::TYPE_BINARY)</div>
<div class="line"> {</div>
<div class="line"> uint64_t timeStartup;</div>
<div class="line"> <a class="code" href="structvn_1_1math_1_1vec.html">vec3f</a> ypr;</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> (!packet.<a name="a16"></a><a class="code" href="structvn_1_1protocol_1_1uart_1_1_packet.html#afa1df5ba1214f4359a3b3b0d02446f0a">isCompatible</a>(</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 = packet.<a name="a17"></a><a class="code" href="group__uart_packet_binary_extractors.html#ga4792b2e7cd05e47792c8eda1402f521a">extractUint64</a>();</div>
<div class="line"> ypr = packet.<a name="a18"></a><a class="code" href="group__uart_packet_binary_extractors.html#ga9d966dba94a885948aa1c734a76f12f6">extractVec3f</a>();</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[Binary Packet Received]&quot;</span> &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; TimeStartup: &quot;</span> &lt;&lt; timeStartup &lt;&lt; endl;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Yaw Pitch Roll: &quot;</span> &lt;&lt; ypr &lt;&lt; endl;</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()</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 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">// Convenient override of above method.</span></div>
<div class="line"><span class="keywordtype">void</span> UserUart_mockReceivedData(<span class="keywordtype">string</span> data)</div>
<div class="line">{</div>
<div class="line"> memcpy(mockUartReceivedDataBuffer, data.c_str(), data.length());</div>
<div class="line"> mockUartReceivedDataSize = data.length();</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="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 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">// 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:27:03 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>