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

277 lines
23 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: ez_async_data/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">ez_async_data/main.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>This example illustrates using the high-level EzAsyncData class to access a easily connect to a VectorNav sensor on a system with an operating system and display current data from the VectorNav sensor.</p>
<h1><a class="anchor" id="vsCppEzAsyncData"></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/ez_async_data/projects/vs20XX/ez_async_data.sln</code>.</li>
<li>Open the project file <code>main.cpp</code> and edit the <code>SensorPort</code> and <code>SensorBaudrate</code> constants at the top of the <code>main()</code> function to the settings used by your attached VectorNav sensor.</li>
<li>Build the entire solution by going to the menu <code>BUILD -&gt; Build Solution</code>.</li>
<li>Right-click the project <code>ez_async_data</code> and select <code>Debug -&gt; Start new instance</code>.</li>
</ol>
<h1><a class="anchor" id="makeCppEzAsyncData"></a>
Make (Linux/Mac OS X)</h1>
<ol type="1">
<li>You will first need to open the file <code>&lt;root&gt;/cpp/examples/ez_async_data/main.cpp</code> and edit the <code>SensorPort</code> and <code>SensorBaudrate</code> constants at the top of the <code>main()</code> function to the settings used by your attached VectorNav sensor.</li>
<li>Open a terminal and change to the directory <code>&lt;root&gt;/cpp/examples/ez_async_data</code> .</li>
<li>To build the example, run the command <code>make</code> .</li>
<li>Run the example by executing the command <code>sudo ./ez_async_data</code> . Note that it is required to run the command using <code>sudo</code> since administrator privileges are required to access the serial ports on Linux.</li>
</ol>
<h1><a class="anchor" id="clionCppEzAsyncData"></a>
CLion (Windows/Linux/Mac OS X)</h1>
<ol type="1">
<li>Open the project file located at <code>&lt;root&gt;/cpp/examples/ez_async_data</code> in CLion.</li>
<li>Open the project file <code>main.cpp</code> and edit the <code>SensorPort</code> and <code>SensorBaudrate</code> constants at the top of the <code>main()</code> function to the settings used by your attached VectorNav sensor.</li>
<li>Make sure the <code>ez_async_data</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>ez_async_data</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="otherCppEzAsyncData"></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/ez_async_data/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. Finally, before compiling, open the file <code>main.cpp</code> and edit the <code>SensorPort</code> and <code>SensorBaudrate</code> constants at the top of the <code>main()</code> function to the settings used by your attached VectorNav sensor.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// Include this header file to get access to the EzAsyncData class.</span></div>
<div class="line"><span class="preprocessor">#include &quot;vn/ezasyncdata.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// We need this file for our sleep function.</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="thread_8h.html">vn/thread.h</a>&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_1sensors.html">vn::sensors</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"><span class="keyword">using namespace </span><a class="code" href="namespacevn_1_1xplat.html">vn::xplat</a>;</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="comment">// This example walks through using the EzAsyncData class to easily access</span></div>
<div class="line"> <span class="comment">// asynchronous data from a VectorNav sensor at a slight performance hit which is</span></div>
<div class="line"> <span class="comment">// acceptable for many applications, especially simple data logging.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// First determine which COM port your sensor is attached to and update the</span></div>
<div class="line"> <span class="comment">// constant below. Also, if you have changed your sensor from the factory</span></div>
<div class="line"> <span class="comment">// default baudrate of 115200, you will need to update the baudrate</span></div>
<div class="line"> <span class="comment">// constant below as well.</span></div>
<div class="line"> <span class="keyword">const</span> <span class="keywordtype">string</span> SensorPort = <span class="stringliteral">&quot;COM1&quot;</span>; <span class="comment">// Windows format for physical and virtual (USB) serial port.</span></div>
<div class="line"> <span class="comment">// const string SensorPort = &quot;/dev/ttyS1&quot;; // Linux format for physical serial port.</span></div>
<div class="line"> <span class="comment">// const string SensorPort = &quot;/dev/ttyUSB0&quot;; // Linux format for virtual (USB) serial port.</span></div>
<div class="line"> <span class="comment">// const string SensorPort = &quot;/dev/tty.usbserial-FTXXXXXX&quot;; // Mac OS X format for virtual (USB) serial port.</span></div>
<div class="line"> <span class="comment">// const string SensorPort = &quot;/dev/ttyS0&quot;; // CYGWIN format. Usually the Windows COM port number minus 1. This would connect to COM1.</span></div>
<div class="line"> <span class="keyword">const</span> uint32_t SensorBaudrate = 115200;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// We create and connect to a sensor by the call below.</span></div>
<div class="line"> <a name="_a0"></a><a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html">EzAsyncData</a>* ez = EzAsyncData::connect(SensorPort, SensorBaudrate);</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Now let&#39;s display the latest yaw, pitch, roll data at 5 Hz for 5 seconds.</span></div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 25; i++)</div>
<div class="line"> {</div>
<div class="line"> Thread::sleepMs(200);</div>
<div class="line"></div>
<div class="line"> <span class="comment">// This reads the latest data that has been processed by the EzAsyncData class.</span></div>
<div class="line"> <a name="_a1"></a><a class="code" href="classvn_1_1sensors_1_1_composite_data.html">CompositeData</a> cd = ez-&gt;<a name="a2"></a><a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a66a57c778c54d65bf94c3414ac6207bf">currentData</a>();</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Make sure that we have some yaw, pitch, roll data.</span></div>
<div class="line"> <span class="keywordflow">if</span> (!cd.<a name="a3"></a><a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ab5e9e3b13c2099ee6be59f922cb4230b">hasYawPitchRoll</a>())</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;YPR Unavailable.&quot;</span> &lt;&lt; endl;</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Current YPR: &quot;</span> &lt;&lt; cd.<a name="a4"></a><a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ad4e4fa3680adc9fb799918731eb46ebb">yawPitchRoll</a>() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Most of the asynchronous data handling is done by EzAsyncData but there are times</span></div>
<div class="line"> <span class="comment">// when we wish to configure the sensor directly while still having EzAsyncData do</span></div>
<div class="line"> <span class="comment">// most of the grunt work.This is easily accomplished and we show changing the ASCII</span></div>
<div class="line"> <span class="comment">// asynchronous data output type here.</span></div>
<div class="line"> <span class="keywordflow">try</span></div>
<div class="line"> {</div>
<div class="line"> ez-&gt;<a name="a5"></a><a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#aee584ba8f61d315997d3a345f5ec9d7d">sensor</a>()-&gt;<a name="a6"></a><a class="code" href="group__register_access_methods.html#gac63e50c05591e083bbd6c6e20ca04482">writeAsyncDataOutputType</a>(VNYPR);</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">catch</span> (...)</div>
<div class="line"> {</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Error setting async data output type.&quot;</span> &lt;&lt; endl;</div>
<div class="line"> <span class="keywordflow">return</span> -1;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[New ASCII Async Output]&quot;</span> &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// We can now display yaw, pitch, roll data from the new ASCII asynchronous data type.</span></div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 25; i++)</div>
<div class="line"> {</div>
<div class="line"> Thread::sleepMs(200);</div>
<div class="line"></div>
<div class="line"> <a class="code" href="classvn_1_1sensors_1_1_composite_data.html">CompositeData</a> cd = ez-&gt;<a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a66a57c778c54d65bf94c3414ac6207bf">currentData</a>();</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span> (!cd.<a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ab5e9e3b13c2099ee6be59f922cb4230b">hasYawPitchRoll</a>())</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;YPR Unavailable.&quot;</span> &lt;&lt; endl;</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Current YPR: &quot;</span> &lt;&lt; cd.<a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ad4e4fa3680adc9fb799918731eb46ebb">yawPitchRoll</a>() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// The CompositeData structure contains some helper methods for getting data</span></div>
<div class="line"> <span class="comment">// into various formats. For example, although the sensor is configured to</span></div>
<div class="line"> <span class="comment">// output yaw, pitch, roll, our application might need it as a quaternion</span></div>
<div class="line"> <span class="comment">// value. However, if we query the quaternion field, we see that we don&#39;t</span></div>
<div class="line"> <span class="comment">// have any data.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;HasQuaternion: &quot;</span> &lt;&lt; ez-&gt;<a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a66a57c778c54d65bf94c3414ac6207bf">currentData</a>().<a name="a7"></a><a class="code" href="classvn_1_1sensors_1_1_composite_data.html#a9685d21de20a6a1db06d957a46fa2513">hasQuaternion</a>() &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Uncommenting the line below will cause an exception to be thrown since</span></div>
<div class="line"> <span class="comment">// quaternion data is not available.</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// cout &lt;&lt; &quot;Current Quaternion: &quot; &lt;&lt; ez-&gt;currentData().quaternion() &lt;&lt; endl;</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// However, the CompositeData structure provides the anyAttitude field</span></div>
<div class="line"> <span class="comment">// which will perform the necessary conversions automatically.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[Quaternion from AnyAttitude]&quot;</span> &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 25; i++)</div>
<div class="line"> {</div>
<div class="line"> Thread::sleepMs(200);</div>
<div class="line"></div>
<div class="line"> <span class="comment">// This reads the latest data that has been processed by the EzAsyncData class.</span></div>
<div class="line"> <a class="code" href="classvn_1_1sensors_1_1_composite_data.html">CompositeData</a> cd = ez-&gt;<a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a66a57c778c54d65bf94c3414ac6207bf">currentData</a>();</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Make sure that we have some attitude data.</span></div>
<div class="line"> <span class="keywordflow">if</span> (!cd.<a name="a8"></a><a class="code" href="classvn_1_1sensors_1_1_composite_data.html#a12bb907c93f26e7a557e97769997bcbc">hasAnyAttitude</a>())</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Attitude Unavailable.&quot;</span> &lt;&lt; endl;</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Current Quaternion: &quot;</span> &lt;&lt; cd.<a name="a9"></a><a class="code" href="classvn_1_1sensors_1_1_composite_data.html#a43926a064cf3e84194a21f7368487a4f">anyAttitude</a>().<a name="a10"></a><a class="code" href="classvn_1_1math_1_1_attitude_f.html#a325a3c5834cb2862656121e1aaa1e1cf">quat</a>() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Throughout this example, we have been using the ez-&gt;currentData() to get the most</span></div>
<div class="line"> <span class="comment">// up-to-date readings from the sensor that have been processed. When called, this</span></div>
<div class="line"> <span class="comment">// method returns immediately with the current values, thus the reason we have to</span></div>
<div class="line"> <span class="comment">// put the Thread::sleepMs(200) in the for loop. Otherwise, we would blaze through</span></div>
<div class="line"> <span class="comment">// the for loop and just print out the same values. The for loop below illustrates</span></div>
<div class="line"> <span class="comment">// this.</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[For Loop Without Sleep]&quot;</span> &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 25; i++)</div>
<div class="line"> {</div>
<div class="line"> <a class="code" href="classvn_1_1sensors_1_1_composite_data.html">CompositeData</a> cd = ez-&gt;<a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a66a57c778c54d65bf94c3414ac6207bf">currentData</a>();</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span> (!cd.<a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ab5e9e3b13c2099ee6be59f922cb4230b">hasYawPitchRoll</a>())</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;YPR Unavailable.&quot;</span> &lt;&lt; endl;</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Current YPR: &quot;</span> &lt;&lt; cd.<a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ad4e4fa3680adc9fb799918731eb46ebb">yawPitchRoll</a>() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// Often, we would like to get and process each packet received from the sensor.</span></div>
<div class="line"> <span class="comment">// This is not realistic with ez-&gt;currentData() since it is non-blocking and we</span></div>
<div class="line"> <span class="comment">// would also have to compare each CompositeData struture for changes in the data.</span></div>
<div class="line"> <span class="comment">// However, EzAsyncData also provides the getNextData() method which blocks until</span></div>
<div class="line"> <span class="comment">// a new data packet is available. The for loop below shows how to output each</span></div>
<div class="line"> <span class="comment">// data packet received from the sensor using getNextData().</span></div>
<div class="line"></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[getNextData Method]&quot;</span> &lt;&lt; endl;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 25; i++)</div>
<div class="line"> {</div>
<div class="line"> <a class="code" href="classvn_1_1sensors_1_1_composite_data.html">CompositeData</a> cd = ez-&gt;<a name="a11"></a><a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a14f8238e75dda346f03c4076be420163">getNextData</a>();</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">if</span> (!cd.<a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ab5e9e3b13c2099ee6be59f922cb4230b">hasYawPitchRoll</a>())</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;YPR Unavailable.&quot;</span> &lt;&lt; endl;</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Current YPR: &quot;</span> &lt;&lt; cd.<a class="code" href="classvn_1_1sensors_1_1_composite_data.html#ad4e4fa3680adc9fb799918731eb46ebb">yawPitchRoll</a>() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> ez-&gt;<a name="a12"></a><a class="code" href="classvn_1_1sensors_1_1_ez_async_data.html#a37f8ebbec42aa7e5cdfb6a75977f4285">disconnect</a>();</div>
<div class="line"></div>
<div class="line"> <span class="keyword">delete</span> ez;</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">return</span> 0;</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>