So I have a wire going from APM UART0 Tx to Rx on an Arduino and I monitor the Arduino serial output via USB back to my computer. I have the following code to try and receive the sensor data that the APM is creating. It's a heartbeat, but I'd like to pull the needed data once I can get the protocol working! However, I run the code and there is complete gibberish on the output at 57600 despite setting the serial so in the code below. I've already gotten past the compiler errors regarding mavlink.h and fastserial.h. I've never gotten a reply from my other threads, so I'm really trying to learn as I go here and pushing the boundaries of my google fu, any help would be EXTREMELY HELPFUL.

Every time the arduino transmits, the serial monitor on my computer picks up a few random characters. I got the code when I started looking into this issue, from here (https://github.com/alaney/arduino-mavlink/blob/master/ArduinoMAVLink/ArduinoMAVLink.ino). Edit: IT actually just transmits gibberish even when the APM isn't connected.

// Arduino MAVLink test code.
#include <FastSerial.h>
#include "../mavlink/include/mavlink.h" // Mavlink interface
 
FastSerialPort0(Serial);
void setup() {
        Serial.begin(57600);
        
}
void loop() {
        /* The default UART header for your MCU */
 int sysid = 20; ///< ID 20 for this airplane
    int compid = MAV_COMP_ID_IMU; ///< The component sending the message is the IMU, it could be also a Linux process
    int type = MAV_TYPE_QUADROTOR; ///< This system is an airplane / fixed wing
 
// Define the system type, in this case an airplane
    uint8_t system_type = MAV_TYPE_FIXED_WING;
    uint8_t autopilot_type = MAV_AUTOPILOT_GENERIC;
 
    uint8_t system_mode = MAV_MODE_PREFLIGHT; ///< Booting up
    uint32_t custom_mode = 0; ///< Custom mode, can be defined by user/adopter
    uint8_t system_state = MAV_STATE_STANDBY; ///< System ready for flight
    // Initialize the required buffers
    mavlink_message_t msg;
    uint8_t buf[MAVLINK_MAX_PACKET_LEN];
 
// Pack the message
    mavlink_msg_heartbeat_pack(sysid,compid, &msg, type, autopilot_type, system_mode, custom_mode, system_state);
 
// Copy the message to the send buffer
    uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
 
// Send the message with the standard UART send function
// uart0_send might be named differently depending on
// the individual microcontroller / library in use.
     delay(1000);
     Serial.write(buf, len);
     comm_receive();
}
void comm_receive() {
 
       mavlink_message_t msg;
        mavlink_status_t status;
 
        // COMMUNICATION THROUGH EXTERNAL UART PORT (XBee serial)
 
        while(Serial.available() > 0 )
        {
                uint8_t c = Serial.read();
                // Try to get a new message
                if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {
                        // Handle message
 
                        switch(msg.msgid)
                        {
                         case MAVLINK_MSG_ID_HEARTBEAT:
                         {
                                 // E.g. read GCS heartbeat and go into
                                  // comm lost mode if timer times out
                         }
                         break;
                        case MAVLINK_MSG_ID_COMMAND_LONG:
                                // EXECUTE ACTION
                                break;
                        default:
                                //Do nothing
                                break;
                        }
                }
 
                // And get the next one
        }
}

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • are you sending binary data or ascii data ?  perhaps the data being sent is binary and will show up as gibberish ?

This reply was deleted.

Activity