VectorNav MATLAB Library
getting_started

This example walks through using Mathworks MATLAB to connect to a VectorNav sensor using the compiled VectorNav .NET Library, configure the sensor to output various data types, and then display this data in the MATLAB environment. Once you are familiar with the basics, you will probably be interested in composing scripts that make use of the VectorNav .NET Library in MATLAB. Please see the Scripting example for some pointers on making scripts.

Note
This example was constructed using Mathworks MATLAB R2014b and may need to be adjusted for your specific version.

Steps

  1. Open MATLAB. Now to connect to a VectorNav sensor requires that we load the compiled VectorNav .NET Library assembly. Once loaded, we will use the functionality within the assembly for configuration and access to the sensor. To load the assembly, you will need to specify the full path to the assembly. The sample command below works with the library extracted in the location C:\vnproglib.
    >> NET.addAssembly('C:\vnproglib\net\bin\win32\VectorNav.dll')
  2. The .NET environment uses namespaces to organize its classes and structures. So that we don't have to type the full namespace of a class when we access it, we will import these namespaces with the commands below.

    >> import VectorNav.Sensor.*
    >> import VectorNav.Protocol.Uart.*

  3. Now create an EzAsyncData object connected to our sensor by executing the command below. Note that you will need to modify the COM port and baudrate appropriately for your system.
    >> ez = EzAsyncData.Connect('COM1', 115200)
  4. Now all VectorNav sensors in their factory default state will be outputting some type of ASCII asynchronous data message that contains yaw, pitch, roll data. All of the different types of ASCII and binary asynchronous data messages will be automatically processed by the EzAsyncData object and made available through its CurrentData property. To display the current yaw, pitch, roll data, execute the command below.
    >> ez.CurrentData.YawPitchRoll
    In fact, you can get all of the current data by just calling the CurrentData property as below.
    >> ez.CurrentData
  5. Suppose that your application requires you to have quaternion attitude data. If you request quaternion data from the CurrentData property as illustrated below, there is a good change you will get the displayed "Null object must have a value" error message.

    >> ez.CurrentData.Quaternion
    Message: Nullable object must have a value.
    Source: mscorlib
    HelpLink:

    This message indicates that the EzAsyncData has not received any quaternion data and is indicate by accessing the HasQuaternion property.

    >> ez.CurrentData.HasQuaternion
    ans = 0

    Now even though the sensor is outputting yaw, pitch, roll data, it is possible to access this orientation data as a quaternion through the AnyAttitude property. This property has the latest attitude information regardless of the type received. Hence, to access quaternion data, execute the command below.
    >> ez.CurrentData.AnyAttitude.Quat

  6. You may also wish to configure the sensor from within MATLAB, for example, we can configure the sensor to output ASCII asynchronous data type VNQTN, which will contain attitude in quaternion format. The EzAsyncData actually wraps a VectorNav.Sensor.VnSensor object that provides methods for changing the configuration and is available through the Sensor property. Now configure the sensor to output VNQTN data by executing the command below.

    >> ez.Sensor.WriteAsyncDataOutputType(AsciiAsync.VNQTN)

    You should now be able to directly access quaternion data directly.

    >> ez.CurrentData.Quaternion

  7. Let's now look at configuring the sensor to output binary message types since this allows much more flexibility in what type of data we receive. The easiest way to do this is to first read the current binary output configuration and then modify it for our needs.

    >> br = ez.Sensor.ReadBinaryOutput1()

    Execute the commands below to configure the br structure for outputting binary data.

    >> br.AsyncMode = AsyncMode.Port1
    >> br.RateDivisor = 100
    >> br.CommonField = bitor(bitor(CommonGroup.TimeStartup, CommonGroup.YawPitchRoll), CommonGroup.Imu)

    Now send the update br structure to the sensor.

    >> ez.Sensor.WriteBinaryOutput1(br)

    The sensor should now be outputting new data types are these can be seen by executing the commands below.

    >> ez.CurrentData.TimeStartup
    >> ez.CurrentData.YawPitchRoll
    >> ez.CurrentData.AccelerationUncompensated
    >> ez.CurrentData.AngularRateUncompensated

  8. Often you will want to get vector data from the VectorNav .NET Library into matrix format so that further computations can be done within the MATLAB environment. This is accomplished by first converting the vector to an array and then calling the appropriate conversion function from MATLAB. The snippet below shows how to get the yaw, pitch, roll into a native matrix format of MATLAB.

    >> native = single(ez.CurrentData.YawPitchRoll.ToArray())

  9. Once you are done using the sensor, it is a good idea to disconnect the sensor.

    >> ez.Disconnect()