Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- || Interceptor ||
- || ||
- || Demonstraion of MavLink packet ||
- || recognition. ||
- //=============//
- Notes
- //=============//
- - Kye Morton
- */
- #define X25_INIT_CRC 0xffff
- #define X25_VALIDATE_CRC 0xf0b8
- #define UINT8_MAX 0xFF
- #define UINT16_MAX 0xFFFF
- #define UINT32_MAX 0xFFFFFFFF
- #define UINT64_MAX 0xFFFFFFFFFFFFFFFFULL
- #define CMD_ID_HEARTBEAT 0 //Define all the message IDs that you want to look for here
- #define SERIAL_0_BAUD 57600
- #define SERIAL_1_BAUD 57600
- #define MUTE_SYS_STATUS 0
- #define MUTE_DEBUG_STATUS 1
- #define MUTE_RAW_STATUS 1
- //=============//
- // Variables //
- //=============//
- #include <SoftwareSerial.h>
- //=============//
- // Variables //
- //=============//
- struct msgBuff_s //Temporary Storage While Reading Messages
- {
- unsigned int byteCount;
- unsigned char inputs[263];
- unsigned char expByteLeft;
- bool phraseInProgress;
- uint16_t id;
- };
- //=============//
- // Func.Protos //
- //=============//
- unsigned char comm_receive();
- char phraseMessage(unsigned char c, msgBuff_s &inMsg);
- void clearMsgBuff(msgBuff_s &msg);
- void decodeMessage(msgBuff_s &msgIn);
- static uint16_t crc_calculate(unsigned char *pBuffer, int length);
- static uint16_t crc_accumulate(unsigned char b, uint16_t crc);
- static void writeMessage(msgBuff_s &msg, uint32_t port);
- void updateHeartLED();
- //=============//
- // Variables //
- //=============//
- //Message CRC seeds. Each ID has a corresponding seed in this array. Heartbeat(ID:0)=50
- const unsigned char MAVLINK_MESSAGE_CRCS[] = {50, 124, 137, 0, 237, 217, 104, 119, 0, 0, 0, 89, 0, 0, 0, 0, 0, 0, 0, 0, 214, 159, 220, 168, 24, 23, 170, 144, 67, 115, 39, 246, 185, 104, 237, 244, 222, 212, 9, 254, 230, 28, 28, 132, 221, 232, 11, 153, 41, 39, 214, 223, 141, 33, 15, 3, 100, 24, 239, 238, 0, 0, 183, 0, 130, 0, 148, 21, 0, 52, 124, 0, 0, 0, 20, 0, 152, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 63, 54, 0, 0, 0, 0, 0, 0, 0, 19, 102, 158, 208, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 219, 208, 188, 84, 22, 19, 21, 134, 0, 78, 68, 189, 127, 42, 21, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 49, 170, 44, 83, 46, 247};
- uint16_t crcAccum = 0;
- msgBuff_s inMsgSerial; //Message buffer for Serial1
- unsigned long startTimeMilli = 0;
- unsigned long lastHeartMsg = 0;
- bool LEDStatus = false;
- unsigned int LEDPin = 13;
- bool MUTE = MUTE_SYS_STATUS; //Set to '1' to disable debug serial output
- bool MUTE_DEBUG = MUTE_DEBUG_STATUS; //Set to '1' to disable debug serial output
- bool MUTE_RAW = MUTE_RAW_STATUS; //Set to '1' to disable debug serial output
- //=============//
- // Main //
- //=============//
- void setup()
- {
- startTimeMilli = millis();
- Serial.begin(SERIAL_0_BAUD); //Monitoring Link
- if(!MUTE)
- {
- Serial.println("Opening communication serial read on Serial Port 0...");
- Serial.println();
- }
- Serial1.begin(SERIAL_1_BAUD); //Monitoring Link
- if(!MUTE)
- {
- Serial.println("Opening communication serial read on Serial Port 1...");
- Serial.println();
- }
- pinMode(LEDPin, OUTPUT);
- digitalWrite(LEDPin, LOW);
- clearMsgBuff(inMsgSerial);
- inMsgSerial.id = 1;
- }
- void loop()
- {
- unsigned long startTimeLoop = millis();
- unsigned long startTimeSecs = (startTimeLoop - startTimeMilli)/1000;
- if(!MUTE)
- {
- Serial.print("\nSystem has been online for ");
- Serial.print(startTimeSecs);
- Serial.println(" seconds.");
- }
- while (millis() - startTimeLoop < 1000) //Holds the program for 1 second to catch new data from the serial buffer
- {
- switch(comm_receive())
- {
- case 0: //A message was read by Serial0
- {
- break;
- }
- case 1: //A message was read by Serial1
- {
- break;
- }
- default: //No Message was read
- {
- break;
- }
- }
- }
- if((lastHeartMsg + 2000)<millis()) //No heartbeat has come through in 2 seconds
- {
- digitalWrite(LEDPin, HIGH);
- }
- }
- unsigned char comm_receive() //Reads in all available data in the serial buffer and passes it off to to be phrased
- {
- while(Serial1.available() > 0) //Duplicate this loop for multiple input serials
- {
- unsigned char c = Serial1.read();
- if(phraseMessage(c, inMsgSerial)>0) //Will return 1 (for Serial1) when a message has completed being read.
- {
- return 1;
- }
- }
- return 0;
- }
- char phraseMessage(unsigned char c, msgBuff_s &inMsg)
- {
- bool messageComplete = 0;
- uint16_t crcTemp = 0;
- uint16_t crcCheck = 0;
- int i = 0;
- if((c==0xFE)&&(!inMsg.phraseInProgress)) //If it a header byte (0xFE) is detected, and there is no message currently being phrased
- {
- clearMsgBuff(inMsg); //Reset message buffer and start recording message
- inMsg.phraseInProgress = 1;
- }
- if(inMsg.phraseInProgress==1)
- {
- inMsg.inputs[inMsg.byteCount] = c; //Appends the newest byte to the array
- inMsg.byteCount++;
- if(inMsg.byteCount>=(inMsg.inputs[1]+8)) //The expected packet length has been collected. "inMsg.inputs[1]" is "len", +8 for rest of header
- {
- inMsg.phraseInProgress = 0;
- messageComplete = 1;
- }
- }
- if(messageComplete)
- {
- crcTemp = crc_calculate(inMsg.inputs, inMsg.byteCount-2); //Calculate and CRC for the gathered messsage, excluding the sent CRC
- if (inMsg.byteCount > 5 && inMsg.inputs[0] == 254) //Make sure there is at least a header, and the first byte is 0xFE
- {
- crcTemp = crc_accumulate(MAVLINK_MESSAGE_CRCS[inMsg.inputs[5]], crcTemp); //Use magic to add to the crcTemp value
- }
- crcCheck = (inMsg.inputs[inMsg.byteCount-1] << 8) | (inMsg.inputs[inMsg.byteCount-2]); //Gather the value of the given CRC
- if(crcTemp==crcCheck) //If the CRCs match, message is valid
- {
- c = 0;
- decodeMessage(inMsg);
- return 1;
- }
- }
- return 0; //Discard message, as it is not valid
- }
- void decodeMessage(msgBuff_s &msgIn)
- {
- unsigned char sys = msgIn.inputs[3];
- unsigned char cmp = msgIn.inputs[4];
- unsigned char id = msgIn.inputs[5];
- switch(id) //Add case's to this to give options for different id's to be recognised
- {
- case CMD_ID_HEARTBEAT: //Heartbeat message
- {
- if(!MUTE_DEBUG)
- {
- Serial.println("Heartbeat message detected...");
- }
- void updateHeartLED();
- break;
- }
- default:
- {
- if(!MUTE_DEBUG)
- {
- Serial.println("Undefined MavLink message detected...");
- }
- break;
- }
- }
- }
- static uint16_t crc_accumulate(unsigned char b, uint16_t crc) //More Magic
- {
- unsigned char ch = (unsigned char)(b ^ (unsigned char)(crc & 0x00ff));
- ch = (unsigned char)(ch ^ (ch << 4));
- return (uint16_t)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
- }
- static uint16_t crc_calculate(unsigned char* pBuffer, int length) //Magic
- {
- if (length < 1)
- {
- return 0xffff;
- }
- uint16_t crcTmp;
- int i;
- crcTmp = X25_INIT_CRC;
- for (i = 1; i < length; i++) // skips header
- {
- crcTmp = crc_accumulate(pBuffer[i], crcTmp);
- }
- return (crcTmp);
- }
- void clearMsgBuff(msgBuff_s &msg) //Resets the provided buffer struct
- {
- msg.byteCount = 0;
- msg.phraseInProgress = 0;
- msg.expByteLeft = 0;
- for(int k=0;k<263;k++)
- {
- msg.inputs[k] = 0;
- }
- }
- static void writeMessage(msgBuff_s &msg, uint32_t port) //Pass a compiled msgBuff_s and the port you want msg.inputs to be writen to
- { //msg.inputs will have msg.byteCount outputs written to the serial
- switch(port)
- {
- case 0:
- for(int i=0;i<msg.byteCount;i++)
- {
- if(msg.inputs[i]<0x10)
- {
- Serial.print("0");
- }
- Serial.print(msg.inputs[i], HEX);
- Serial.print(" ");
- }
- Serial.println();
- break;
- case 1:
- for(int i=0;i<msg.byteCount;i++)
- {
- Serial1.write(msg.inputs[i]);
- }
- break;
- default:
- {
- break;
- }
- }
- }
- void updateHeartLED()
- {
- lastHeartMsg = millis();
- digitalWrite(LEDPin, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement