This example illustrates basic connectivity and interaction with a VectorNav sensor over a serial port.
Visual Studio (Windows)
- Open the solution file for your specific Visual Studio version located at
<root>/cpp/examples/getting_started/projects/vs20XX/getting_started.sln
.
- Open the project file
main.cpp
and edit the SensorPort
and SensorBaudrate
constants at the top of the main()
function to the settings used by your attached VectorNav sensor.
- Build the entire solution by going to the menu
BUILD -> Build Solution
.
- Right-click the project
getting_started
and select Debug -> Start new instance
.
Make (Linux/Mac OS X)
- You will first need to open the file
<root>/cpp/examples/getting_started/main.cpp
and edit the SensorPort
and SensorBaudrate
constants at the top of the main()
function to the settings used by your attached VectorNav sensor.
- Open a terminal and change to the directory
<root>/cpp/examples/getting_started
.
- To build the example, run the command
make
.
- Run the example by executing the command
sudo ./getting_started
. Note that it is required to run the command using sudo
since administrator privileges are required to access the serial ports on Linux.
CLion (Windows/Linux/Mac OS X)
- Open the project file located at
<root>/cpp/examples/getting_started
in CLion.
- Open the project file
main.cpp
and edit the SensorPort
and SensorBaudrate
constants at the top of the main()
function to the settings used by your attached VectorNav sensor.
- Make sure the
getting_started
configuration is active. You can set this by clicking the small drop-down list in the upper-right corner and selecting the option getting_started
.
- Build the solution by going to the menu
Run -> Build
.
- Run the example by going to the menu
Run -> Run 'gettings_started'
.
Other
To compile and run for an environment not listed here, you will need to add all of the *.c
files in the directory <root>/c/src
along with the file located at <root>/c/examples/getting_started/main.c
to your project for compilation. You will also need to add <root>/c/include
to your include directories. Finally, before compiling, open the file main.cpp
and edit the SensorPort
and SensorBaudrate
constants at the top of the main()
function to the settings used by your attached VectorNav sensor.
#include <iostream>
#include "vn/sensors.h"
using namespace std;
void asciiAsyncMessageReceived(
void* userData,
Packet& p,
size_t index);
void asciiOrBinaryAsyncMessageReceived(
void* userData,
Packet& p,
size_t index);
int main(int argc, char *argv[])
{
const string SensorPort = "COM1";
const uint32_t SensorBaudrate = 115200;
vs.
connect(SensorPort, SensorBaudrate);
cout << "Model Number: " << mn << endl;
cout << "Current YPR: " << ypr << endl;
cout <<
"Current Magnetic: " << reg.
mag << endl;
cout <<
"Current Acceleration: " << reg.
accel << endl;
cout <<
"Current Angular Rates: " << reg.
gyro << endl;
cout << "Old Async Frequency: " << oldHz << " Hz" << endl;
cout << "New Async Frequency: " << newHz << " Hz" << endl;
cout <<
"Old Heading Mode: " << vpeReg.
headingMode << endl;
cout <<
"New Heading Mode: " << vpeReg.
headingMode << endl;
cout << "ASCII Async Type: " << asyncType << endl;
cout << "Starting sleep..." << endl;
Thread::sleepSec(5);
ASYNCMODE_PORT1,
200,
COMMONGROUP_TIMESTARTUP | COMMONGROUP_YAWPITCHROLL,
TIMEGROUP_NONE,
IMUGROUP_NONE,
GPSGROUP_NONE,
ATTITUDEGROUP_NONE,
INSGROUP_NONE);
cout << "Starting sleep..." << endl;
Thread::sleepSec(5);
return 0;
}
void asciiAsyncMessageReceived(
void* userData,
Packet& p,
size_t index)
{
if (p.
type() != Packet::TYPE_ASCII)
return;
return;
cout << "ASCII Async YPR: " << ypr << endl;
}
void asciiOrBinaryAsyncMessageReceived(
void* userData,
Packet& p,
size_t index)
{
{
cout << "ASCII Async YPR: " << ypr << endl;
return;
}
if (p.
type() == Packet::TYPE_BINARY)
{
COMMONGROUP_TIMESTARTUP | COMMONGROUP_YAWPITCHROLL,
TIMEGROUP_NONE,
IMUGROUP_NONE,
GPSGROUP_NONE,
ATTITUDEGROUP_NONE,
INSGROUP_NONE))
return;
cout << "Binary Async TimeStartup: " << timeStartup << endl;
cout << "Binary Async YPR: " << ypr << endl;
}
}