|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Jun 2009
Messaggi: 5
|
[visual c++] Pilotare un elicottero via porta seriale
Buongiorno,
ho un elicottero da modellismo (un quadrirotore) che permette di essere controllato via pc inviando dei dati su porta seriale, utilizzando un protocollo dati di 18 bytes, l'elicottero è il seguente : www.asctec.de/downloads/researchpilot_manual.pdf Il dato da mandargli è il seguente: 1)startstring 1: > (0x3e in hex) 2)startstring 2: * ( 0x2A) 3)startstring 3: > (0x3e in hex) 4)packet descripotr -> always 0x17 5)length of packet - lowbyte 6)length of packet - highbyte 7)pitch - low byte 8)pitch - high byte 9)roll -low byte 10)roll - high byte 11)thrust - low byte 12)thrust - high byte 13)yaw - low byte 14)yaw - high byte 15)flags -low byte --->> every single bit of this one enable the communication between serial port and a parts of my copter (epecially, an 1 in 8th position enable comm via serial interface, the 1 in 3rd position enable thrust control through serial interface , in binary is 1000 0100 (0x84 in hex) 16)flags - high byte --> sendrate of packet, values from 5 to 255 admitted 17)crc16 - high byte 18)crc16- low byte Ho realizzato un programma console con Visual C++ Express 2010 (il SO è Vista) che è in grado di inviare correttamente questi 18 caratteri, tant'è è che nella risposta dell'elicottero, fra i vari bytes, ce n'è uno di packet descriptor che vale 0x19, così come indicato dover essere dalla mia guida. I metodi di r/w che utilizzo sono quelli della classe serialPort (tipo serialPort->Write(buffer,int, int) per intenderci). Il problema è che non ho una risposta (accensione motori) dall'elicottero, anche se credo di aver fissato correttamente i parametri per attivare i motori (ad esempio, se al byte chiamato flags_low invio 0x84, in binario vale 1000 0100, di cui il primo 1 abilita la comunicazione seriale e il secondo abilita i motori via seriale). Col modellino mi hanno fornito un test software che invece funziona, riesce ad accendere i motori e ad incrementarne la spinta. quello che ho fatto quindi è di analizzare il flusso dati della porta serial, utilizzando Hdd Serial Monitor, sia di questo test software che del mio programma. Una vistosa differenza fra i due programmi è che io invio sempre e solo 18 bytes, che sono sempre gli stessi, mentre il test software a volte sembra inviare più di 18 bytes prima di scatenare la ricezione, e soprattutto i dati della stringa inviata ogni tanto cambiano, senza che io debba intervenire cliccando qualche bottone dell'interfaccia grafica. La ricezione dei dati dall'elicottero al pc utilizzando il test software è anche diversa, a volte ricevo più dati, a volte meno. Insomma, questo testsoftware riceve ed invia dati in numero che a me pare randomico, mentre io nel mio programma fisso il numero di dati da ricevere utilizzando un ciclo for che si ferma ad un numero fisso, tipicamente 16 byte in scrittura e 52 byte in ricezione, come scritto nella mia guida. testsoftware that works http://www.megaupload.com/?d=2SZ4Z4HQ my code, not working http://www.megaupload.com/?d=K8R9IIHX Penso che uno dei problemi del mio programma siano nelle rigidità che introduco, forse dovrei pensare a qualcosa come un ring buffer, almeno in ricezione dati (per la scrittura invece, non saprei proprio come fare, la guida mi parla solo di inviare i 18 byte). ...Ma poi la ricezione dei dati dall'elicottero sarà proprio necessaria per poter accendere i motori? E' il momento per me di tornare ad una buona teoria, spero che possiate indicarmi dove iniziare a guardare. Buon Sabato! Marco. ecco le parti salienti del mio programma Codice:
// sendPacket.cpp : file di progetto principale.
//initialize command data structure to send
struct SCIENTIFIC_COMMANDDATA
{
//always 0x17
unsigned char packetdescriptor;
//pitch, roll, thrust, yaw commands. 0..4095 2048=middle
unsigned short pitch;
unsigned short roll;
unsigned short thrust;
unsigned short yaw;
//flags
//Bit 0(0x01): Pitch control through serial interfacae enabled
//Bit 1(0x02): Roll control through serial interface enabled
//Bit 2(0x04): Thrust control through serial interface enabled
//Bit 3(0x08): Yaw control through serial interface enabled
//Bit 4(0x10): ACC-Mode on/off
//Bit 5(0x20): Height control - on/off (only with ACC)
//Bit 6(0x40): overwrite ACC/Height mode control
//(0=mode selected by RC 1=mode selected by Bit 4 and 5)
//Bit 7(0x17): Trigger Scientific status packet (triggers a response
//with the actual scientific state)
//Bit 8..15: sendrate of the scientific packet in 5Hz
//(0=off;1=5Hz, 20=100Hz, 200=1kHz)
//Scientific packet is send for three seconds max.
//after the last command_data packet
FLAGS flags; //insert a FLAG structure in a SCIENTIFIC_COMMANDDATA
};
//send-data methods
void sendData_String(array<unsigned char> ^ send_data)
{
StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
// Create a new SerialPort object with default settings.
SerialPort^ _serialPort = gcnew SerialPort("COM3");
// Allow the user to set the appropriate properties.
_serialPort->BaudRate = 57600 ;
_serialPort->Parity = Parity::None;
_serialPort->DataBits = 8;
_serialPort->StopBits = StopBits::One;
_serialPort->Handshake =Handshake::None;
//variables
array<unsigned char> ^ received_data =gcnew array<unsigned char>(50);
//open-send-close port
_serialPort->Open();
while(!_kbhit())
{
printf("\nsending data...\n");
for(int ii=0;ii<=send_data->Length;ii++)
{
Thread::Sleep(50);
_serialPort->Write(send_data,ii,1);
printf("%d", send_data[ii]);
Thread::Sleep(50);
}
printf("\nreceiving data...\n");
for (int ii=0;ii<=received_data->Length; ii++)
{
_serialPort->Read(received_data,ii,1);
printf("%d", received_data[ii]);
};
}
_serialPort->Close();
}
//MAIN
int main(array<System::String ^> ^args)
{
//data to be sent
array<unsigned char> ^ send_data = {startString[0], //1
startString[1], //2
startString[2], //3
length_high,//4
length_low,//5
scientific_commanddata.packetdescriptor,//6
scientific_commanddata.pitch&0xFF,//7
scientific_commanddata.pitch>>8,//8
scientific_commanddata.roll&0xFF,//9
scientific_commanddata.roll>>8,//10
scientific_commanddata.thrust&0xFF,//11
scientific_commanddata.thrust>>8,//12
scientific_commanddata.yaw&0xFF,//13
scientific_commanddata.yaw>>8,//14
flags_low,//15
flags_high,//16
crc_high,//17
crc_low};//18
//some string catched from testsoftware
array<unsigned char> ^ send_data1 = {72, 0x43, 0x43, 0x00, 0x0B, 0x17, 0xF0, 0x07, 76, 0x07, 0x00, 0x00, 0xF0, 0x07, 0x8C, 0x04, 0x79, 0};
array <unsigned char> ^send_data2 = {0x3E, 0x2A, 0x3E, 0x00, 0x0B, 0x17, 0xF0, 0x07, 0xF0, 0x07, 0x00, 0x00, 0xF0, 0x07, 0x00, 0x04, 0x5C, 0x3F};
array <unsigned char> ^send_data3 = {0x3E, 0x2A, 0x3E, 0x00, 0x0B, 0x17, 0xF0, 0x07, 0xF0, 0x07, 0x00, 0x00, 0xF0, 0x07, 0x80, 0x04, 0xD0, 0xF3};
array <unsigned char> ^send_data4 = {0x3E, 0x2A, 0x3E, 0x00, 0x0B, 0x17, 0x50, 0x09, 0xF0, 0x07, 0x00, 0x00, 0xF0, 0x07, 0x00, 0x04, 0x89, 0xA9};
array<unsigned char> ^ send_data5 = {0x3E, 0x2A, 0x3E, 0x00, 0x0B, 0x80, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x50, 0xFF, 0x79, 0x53};
//launch methods
sendData_String(send_data);
return 0;
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 23:10.



















