Home FlytBase Forums API Reference

Onboard C++

Execute built-in Demo Apps

FlytOS comes with pre-installed Demo apps which can be executed to do simple navigation tasks. All the sample applications can be found on our github repository.

Demo App 1

This demo app makes the drone takeoff, move in a square trajectory of side length 5m and land once the entire mission is over.

Below is a demo youtube video of the same app when run on FlytPOD.


Execution

The source code of this app is located at /flyt/flytapps/onboard/src/demoapp1 and its executable file demoapp1 is at /flyt/flytapps/onboard/install. To execute this app run the following command in your terminal.

$ /flyt/flytapps/onboard/install/demoapp1

Code

#include <cpp_api/navigation_bridge.h>

Navigation nav;
int main(int argc, char *argv[])
{
    nav.take_off(3.0);            //Taking Off
    nav.position_set(5,0,-3);       //Sending Position Setpoints
    nav.position_set(5,5,-3);
    nav.position_set(0,5,-3);
    nav.position_set(0,0,-3);
    nav.land(false);                  //Landing
}

Code Explained

  • You must include the following header file to make FlytAPI-navigation available for the script.

    #include <cpp_api/navigation_bridge.h>
    
  • Create an object of class Navigation, through which you can call any navigation FlytAPI.

    Navigation nav;
    
  • TakeOff command can be sent to vehicle with relative takeoff altitude in meters as argument. Over here, the takeoff altitude is 3m.

        nav.take_off(3.0);            //Taking Off
    

    Caution

    You must ensure to call takeoff() before sending any other position setpoints. takeoff() inherently calls arm(), hence calling arm() directly also arms the vehicle and makes it responsive towards next setpoint commands.

  • Position Setpoints could be sent to the vehicle with (x,y,z) in meters in Local-NED Frame as argument.

        nav.position_set(5,0,-3);       //Sending Position Setpoints
        nav.position_set(5,5,-3);
        nav.position_set(0,5,-3);
        nav.position_set(0,0,-3);
    
  • Land command must be used to send the vehicle into Landing mode.

        nav.land(false);                  //Landing
    
  • Please refer to FlytAPIs to get more information on the available list of APIs.

Demo App 2

Note

This demo requires arguments to be passed.

This demo app makes the drone takeoff, move in a square trajectory of side length provided as an argument to the script and land once the entire mission is over.

Execution

The source code of this app is located at /flyt/flytapps/onboard/src/demoapp2 and its executable file demoapp2 is at /flyt/flytapps/onboard/install. To execute this app run the following command in your terminal.

$ /flyt/flytapps/onboard/install/demoapp2 3
# here '3' is passed as an argument, one could send any other float value.

Code

#include <cpp_api/navigation_bridge.h>
#include <iostream>
#include <ros/package.h>

Navigation nav;
int main(int argc, char *argv[])
{
  if(argc < 2){
    std::cout<<"\nThis app expects arguments\n";
    exit(0);
  }
  
    float side_length = std::stof(argv[1]);             //Convert Argument from string to float
    nav.take_off(3.0);                                   //Taking Off
    nav.position_set(side_length,0,-3);                 //Sending Position Setpoints with side length accepted from script
    nav.position_set(side_length,side_length,-3);
    nav.position_set(0,side_length,-3);
    nav.position_set(0,0,-3);
    nav.land(false);                                         //Landing
}

Create and Compile custom app

  • Create a directory where you want to keep the source files of your onboard apps:

    $ mkdir onboard_apps
    
  • Create a directory inside onboard_apps for your first app:

    $ mkdir my_first_cpp_app
    
  • Create your own my_first_cpp_app.cpp file. You can use the following snippet to start building your app.

    #include <cpp_api/navigation_bridge.h>
    
    Navigation nav;
    int main(int argc, char *argv[])
    {
        nav.takeoff(3.0);   //OR nav.arm();
        /* Write your own logic below */
    }
    
  • Copy CMakeLists.txt from the downloaded DemoApp1, and paste it inside my_first_cpp_app. This is the CMakeLists.txt file of DemoApp1. To find how to configure this file for my_first_cpp_app, go to CMakeLists.txt - Explained.

    cmake_minimum_required(VERSION 2.8.3)
    project(demoapp1)
    
    SET(CMAKE_INSTALL_PREFIX /usr/local/flytos/flytapps CACHE PATH "Cmake install prefix path for flytapps" FORCE)
    
    add_definitions(-std=c++11)
    
    find_package(catkin REQUIRED COMPONENTS cpp_api)
    find_package(Boost REQUIRED COMPONENTS system python)
    find_package(PythonLibs 2.7 REQUIRED)
    include_directories(${catkin_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
    
    add_executable(demoapp1 demoapp1.cpp)
    target_link_libraries(demoapp1 ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) 
    
    install(TARGETS demoapp1
      DESTINATION /flyt/userapps/onboard_user/install COMPONENT Runtime
    )
    
  • Create a build directory to host all your build files:

    $ mkdir build
    $ cd build
    
  • Inside build directory, run the cmake command:

    $ cmake ..
    
  • To build your cpp file, run the make command:

    $ make
    

CMakeLists.txt - Explained

  • Enter your project name - my_first_cpp_app

    cmake_minimum_required(VERSION 2.8.3)
    project(my_first_cpp_app)
    
    SET(CMAKE_INSTALL_PREFIX /usr/local/flytos/userapps CACHE PATH "Cmake install prefix path for flytapps" FORCE)
    
    add_definitions(-std=c++11)
    
  • Make Navigation FlytAPI Library - cpp_api and other dependencies available for your my_first_cpp_app.cpp.

    cmake_minimum_required(VERSION 2.8.3)
    find_package(catkin REQUIRED COMPONENTS cpp_api)
    find_package(Boost REQUIRED COMPONENTS system python)
    find_package(PythonLibs 2.7 REQUIRED)
    include_directories(${catkin_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
    
  • Give a suitable name (for example my_first_cpp_app) to your executable file and link it with libraries.

    add_executable(demoapp1 demoapp1.cpp)
    target_link_libraries(demoapp1 ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) 
    
  • Add the following install command to install your created my_first_cpp_app executable target to install space - /flyt/userapps/onboard_user/install. This would allow web/mobile apps to execute your installed scripts remotely. Visit this link for details about the corresponding API call.

    install(TARGETS demoapp1
      DESTINATION /flyt/userapps/onboard_user/install COMPONENT Runtime
    )
    

Execute custom app

  • After compiling your my_first_cpp_app project, your executable my_first_cpp_app will be created inside build directory.
  • If FlytOS/FlytSim is not launched, launch FlytOS or FlytSim.
  • Execute your my_first_cpp_app cpp executable from terminal.

Install custom app

To install your app into /flyt/userapps/onboard_user/install space, so that your Android/Web App could execute it remotely, run this command in your terminal:

$ sudo make install