RasPi/CopterController/DSM2.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

#include "DSM2.h"
2015-03-10 20:02:35 -06:00
#include <iostream>
#include <cstdio>
#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) > 16 )
read( fd, raw, 16 );
//while( serialDataAvail(fd) > 32 )
//for( i = 0; i < 16; i++ )
//read(fd);
//for( i = 0; i < 8; i++ )
//data[ i ] = ( (int)(serialGetchar(fd)) << 8 ) + serialGetchar(fd);
2015-03-10 20:02:35 -06:00
for( i = 0; i < 8; i++ )
data[i] = ( ((int)raw[2*i]) << 8 ) + raw[ 2*i+1 ];
//printf( "%04X %04X %04X %04X %04X %04X %04X %04X\n", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7] );
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
//printf( "%04X %04X %04X %04X %04X %04X\n", values[0], values[1], values[2], values[3], values[4], values[5] );
return;
}