Code Monkey home page Code Monkey logo

simple-mavlink-c-rover's Introduction

Simple Mavlink C++ example: Rover


Mavlink (Micro Air Vehicle Link) is a protocol for unmanned vehicle (drone / rover). It is used to communication with the mobile from the ground control station or companion computer. This repository aims to provide an easy explanation on Mavlink, how to communication with a Pixhawk and link useful resources.

The proposed code is used to control a small rover equiped with Pixhawk2 from a companion computer (Raspberry3) in C++ using mavlink v2.

Finally, if your project can be done in python, I recommend using the dronekit library, which is very complet and easy to use, you can still use the resources to get a better understanding of mavlink protocol.


The first experience with Mavlink - Ardupiplot - Mission planner can be difficult but I hope this documents will help you. If you see any mistake or want to help please feel free to push any change.

Mavlink

Architecture

Packet structure

  • STX: Packet start sequence
  • LEN: Payload length ( 0 - 255)
  • SEQ: sequence number for each component to detect packet loss
  • SYS: System ID. The system ID is of the SENDING system.
  • COMP: Component ID. The Component ID is the identifier of the message target inside the autopilot. I only used 1 component, but if you had component to your pixhawk (Gimbal - ToF sensor) you can specify the target. COMP = 0 for broadcast in the autopilot.
  • MSG: Message - command ID. Mavlink assigns an unique ID for a command (list)
  • PAYLOAD: size depends on the specific commands and number of parameters
  • CKA - CKB: check sum

Program structure

If you want to design your own mavlink message communication program, you will need to ensure the following step:

  1. Ensure connectivity with the vehicle: UART - UDP - TCP
  2. Receive data from the vehicle:
    1. Parse incoming data: mavlink_parse_char byte-to-byte basis
    2. Handling the message: mavlink_decode decode message based on the message ID
    3. Example: 1) heartbeat to get all setting information 2) Other requested data
  3. Send data to vehicle
    1. Format a new mavlink message structure with target id / command id / parameters...
    2. Pack and write the message to the communication port: mavlink_finalize_message

Mavlink message in C++

Starting point

The best (only) official example for C++ mavlink message is c_uart_interface_example. It shows a complete solution to control an autopilot, but it may looks a bit complicated (at least for me). So I only used the structure and the serial communication functions.

Code sample

compile with g++ -I mavlink/include/mavlink/v2.0 class_rover_test.cpp serial_port.cpp rover.cpp -o class

int main()
{
	// Create a rover:
	Rover roverA;
	
	// Set to guided mode:
	roverA.guided_mode();
	printf("Setting guide mode...\n");
	sleep(0.5);
	printf("\nDone! Arming the rover...\n");
	roverA.arm(1);
	printf("\nDone! Ready to go...\n");
	sleep(0.5);
  
	roverA.setAngleSpeed(30, 0.4);
	sleep(4);
	roverA.arm(0);	
	
	return(0);
}

Used function

I recommend to search on both Ardupilot - Mavlink command list to make sure the message is supported by ardupilot, that there is no format change and for a better understanding of the message. For example:

List

Change mode (guided mode=15):

YAW - SPEED:

Currently working on

  • Multi-thread for receiving-sending
  • Generate local way point
  • Battery monitoring message

Resouces

Mavlink for dummy

Example

Japanese explaination

Clear and Good C++ example (on simulator):

Clear description of the communication flow + sample code (on simulator) +++

Communication flow on CBG: good to have an inside ++

Mavlink message from the "drone": good to understand how the vehicle generate message ++

Give inside on Mavlink and industrial application +

Complete-Complex example with threading mgmt (for drone) +++

Good simple C++ example (with thread) ++

Rc-overwrite example

Complet example inspired by the c_uart example (with multiplatform support)

simple-mavlink-c-rover's People

Contributors

benbenio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

simple-mavlink-c-rover's Issues

Compiling problem in QT under Windows10

Hello BenBenIO.
I tried to play with your sample code, and got an annoying compiling error in QT under Windows.
The "termios.h" file is not found, whereas I added a valid "termios.h" header file in my project.
I suspect this is because I'm not using Linux.
Any clue? Your feedback would be welcome. Thanks.

002

setAngleSpeed values not clear

Hi Ben,

Thanks for this rover example - it's really great. Simple and has all it needs.
Great job!
One question if you don't mind about values for setAngleSpeed function:

set_yawspeed.param1 = angle; //angle (centridegree) [-4500 - 4500]

In comment you have "//angle (centridegree) [-4500 - 4500]" and "//speed normalized [0 - 1]".
In mavlink docs we have degrees and m/s https://mavlink.io/en/messages/common.html#MAV_CMD_NAV_SET_YAW_SPEED

Can you please comment/explain?
For example if I give "send_YAW_SPEED(rover, 90, 0.5)" does it mean that drone will head East with speed 0.5 m/s?

All the best

Witold

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.