![]() |
VectorNav MATLAB Library
|
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.
C:\vnproglib.>> NET.addAssembly('C:\vnproglib\net\bin\win32\VectorNav.dll')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.*
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)EzAsyncData object and made available through its CurrentData property. To display the current yaw, pitch, roll data, execute the command below.>> ez.CurrentData.YawPitchRollCurrentData property as below.>> ez.CurrentDataSuppose 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
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
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
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())
Once you are done using the sensor, it is a good idea to disconnect the sensor.
>> ez.Disconnect()
1.8.10