This example illustrates communicating with a VectorNav sensor using the SPI protocol. Since access to the low-level SPI communication interface is specific to each platform, this example "mocks" the SPI module and serve as a starting point for replacing with the SPI access code specific to your platforms.
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/spi/main.c
to your project for compilation. You will also need to add <root>/c/include
to your include directories.
#include <stdio.h>
#include <string.h>
#include "vn/protocol/spi.h"
#include "vn/protocol/common.h"
int display_error(const char* msg);
void mockspi_initialize(void);
void mockspi_writeread(const char* dataOut, size_t dataOutSize, char* dataIn);
int main(void)
{
char txbuf[0x100];
char rxbuf[0x100];
size_t bufcmdsize;
size_t responseSize;
char strConversions[50];
size_t i;
mockspi_initialize();
bufcmdsize = sizeof(txbuf);
txbuf,
&bufcmdsize,
0,
&responseSize) != E_NONE)
return display_error("Error generating read yaw, pitch, roll command.\n");
mockspi_writeread(
txbuf,
responseSize,
rxbuf);
mockspi_writeread(
txbuf,
responseSize,
rxbuf);
if (VnSpi_parseYawPitchRoll(
rxbuf,
&ypr) != E_NONE)
return display_error("Error parsing yaw, pitch, roll.\n");
str_vec3f(strConversions, ypr);
printf("Current YPR: %s\n", strConversions);
for (i = 0; i < 25; i++)
{
mockspi_writeread(
txbuf,
responseSize,
rxbuf);
if (VnSpi_parseYawPitchRoll(
rxbuf,
&ypr) != E_NONE)
return display_error("Error parsing yaw, pitch, roll.\n");
str_vec3f(strConversions, ypr);
printf("Current YPR: %s\n", strConversions);
}
bufcmdsize = sizeof(txbuf);
txbuf,
&bufcmdsize,
0,
&responseSize,
VNYPR) != E_NONE)
return display_error("Error generating write async data output type command.\n");
mockspi_writeread(
txbuf,
responseSize,
rxbuf);
return 0;
}
int display_error(const char* msg)
{
printf("%s\n", msg);
return -1;
}
void mockspi_initialize(void)
{
}
void mockspi_writeread(const char* dataOut, size_t dataOutSize, char* dataIn)
{
char yprResponse[] = { 0x00, 0x01, 0x08, 0x00, 0xd8, 0x9c, 0xd4, 0x42, 0x44, 0xba, 0x9e, 0x40, 0x4e, 0xe4, 0x8b, 0x40 };
(dataOut);
(dataOutSize);
memcpy(dataIn, yprResponse, sizeof(yprResponse));
}