What is great about the APM_BinComm library is this is how you add a new message definiton
Thanks go out to Michael Smith for this one.
#include "AsyncSerial.hpp"
#include "APM_BinComm/APM_BinComm.h"
#include <iostream>
#include <boost/bind.hpp>
These call back functions make life so much easier.
void h_attitude(void * arg, uint8_t messageId, uint8_t messageVersion, void * messageData)
{
int16_t roll=0,pitch=0,yaw=0;
BinComm * comm = (BinComm*)arg;
comm->unpack_msg_attitude(roll,pitch,yaw);
std::cout << "roll, pitch, yaw:\t" << roll << "\t" << pitch << "\t" << yaw << std::endl;
}
void h_location(void * arg, uint8_t messageId, uint8_t messageVersion, void * messageData)
{
BinComm * comm = (BinComm*)arg;
int32_t latitude = 0, longitude = 0;
int16_t altitude = 0, groundSpeed = 0, groundCourse = 0;
uint16_t timeOfWeek = 0;
comm->unpack_msg_location(latitude,longitude,altitude,groundSpeed,groundCourse,timeOfWeek);
std::cout << "latitude, longitude, altitude:\t" << latitude << "\t"
<< longitude << "\t" << altitude << std::endl;
std::cout << "ground speed, ground course, time of week:\t" << groundSpeed << "\t"
<< groundCourse << "\t" << timeOfWeek << std::endl;
}
class CommTest
{
private:
BufferedAsyncSerial serial;
BinComm::MessageHandler handlerTable[10];
Stream stream;
BinComm comm;
public:
CommTest(const std::string & device, long int baud) :
serial(device,baud),
stream(&serial),
comm(handlerTable,&stream)
{
int i=0;
handlerTable[i].messageID = BinComm::MSG_ATTITUDE;
handlerTable[i].handler = h_attitude;
handlerTable[i].arg = &comm;
i++;
handlerTable[i].messageID = BinComm::MSG_LOCATION;
handlerTable[i].handler = h_location;
handlerTable[i].arg = &comm;
i++;
// signals end of handle table
handlerTable[i].messageID = BinComm::MSG_NULL;
}
void update()
{
comm.update();
}
};
int main (int argc, char const* argv[])
{
if (argc != 3)
{
std::cout << "usage: " << argv[0] << " device baud" << std::endl;
return 0;
}
std::string device = argv[1];
long int baud = atol(argv[2]);
std::cout << "device: " << device << std::endl;
std::cout << "baud: " << baud << std::endl;
CommTest test(device,baud);
while(1)
{
test.update();
usleep(1000);
}
return 0;
}