RasPi/CopterController/DSM2.cpp

46 lines
1.1 KiB
C++
Raw Normal View History

#include "DSM2.h"
2015-03-10 20:02:35 -06:00
#include <iostream>
#include "wiringSerial.h"
2015-03-10 20:02:35 -06:00
#define VALUE_MASK 0x3FF
2015-03-16 18:06:01 -06:00
DSM2::DSM2()
{
2015-03-10 20:02:35 -06:00
std::cout << "opening serial port..." << std::flush;
fd = serialOpen( "/dev/ttyAMA0", 115200 );
2015-03-16 18:06:01 -06:00
if( fd < 0 )
{
std::cout << "FAILURE" << std::endl;
}
else
{
std::cout << "SUCCESS " << fd << std::endl;
}
2015-03-10 20:02:35 -06:00
std::cout << "flushing serial..." << std::flush;
serialFlush(fd);
2015-03-16 18:06:01 -06:00
std::cout << "DONE" << std::endl;
}
2015-03-10 20:02:35 -06:00
void DSM2::update()
{
int i = 0;
2015-03-10 20:02:35 -06:00
if( serialDataAvail(fd) < 16 ) return;
while( serialDataAvail(fd) > 32 )
for( i = 0; i < 16; i++ )
serialGetchar(fd);
2015-03-10 20:02:35 -06:00
for( i = 0; i < 8; i++ )
data[ i ] = ( (int)(serialGetchar(fd)) << 8 ) + serialGetchar(fd);
2015-03-10 20:02:35 -06:00
*(values + 0) = data[7] & VALUE_MASK; //Throttle
*(values + 1) = data[1] & VALUE_MASK; //Aileron
*(values + 2) = data[3] & VALUE_MASK; //Elevator
*(values + 3) = data[5] & VALUE_MASK; //Rudder
*(values + 4) = data[4] & VALUE_MASK; //Gear
*(values + 5) = data[2] & VALUE_MASK; //Aux1
return;
}