First program with OttoFly¶
This section starts explaining working with OttoFly.
OttoFly ‘Hello World!’¶
The following example takes off the drone for 4 meters. Then, it lands.
#include <iostream>
#include <ottofly/vehicle.hpp>
#include <chrono>
int main(int argc, char* argv[]) {
try {
ottofly::vehicle vehicle_copter; //Creating the drone controller
vehicle_copter.connect_udp("127.0.0.1", 14552); //Change according to your settings
vehicle_copter.arm(); //Arming the motors (they will start turn on)
vehicle_copter.takeoff(4); //Taking the drone off to 4 meters altitude
vehicle_copter.land(); //Landing
std::this_thread::sleep_for(std::chrono::seconds(2));// Waiting for two seconds
vehicle_copter.disarm(); //Disarming the motors
} catch(const std::exception& ex) {
std::cerr << ex.what(); //Display any exception
}
}
Warning
This example is so simple. It is NOT safe. The result of each operation was not checked. If arming failed we should not try to take off for example. However, in a happy world, this Hello World example will work smoothly.
Building OttoFly with Cmake¶
After installing OttoFly SDK, Add the code above in a cpp file, for example, test1.cpp. In the same folder add an CMakeLists.txt file which contains:
###############################################################################
# OttoFlyDemo
###############################################################################
#CMake and compiler settings
cmake_minimum_required(VERSION 3.0)
if(NOT CMAKE_DEBUG_POSTFIX)
set(CMAKE_DEBUG_POSTFIX d)
endif()
set (CMAKE_CXX_STANDARD 14)
#Project settings
set(PROJECT_NAME "OttoFlyDemo")
set(PROJECT_VERSION 1.0.0)
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION})
#Adding OttoFly SDK
find_package(OttoFly REQUIRED)
#Adding Boost
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost COMPONENTS system filesystem regex thread date_time log log_setup REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
#Adding the source files
set(CLIENT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/)
file(GLOB CLIENT_SOURCE_FILES ${CLIENT_SOURCE_DIR}/*.cpp ${CLIENT_SOURCE_DIR}/*.hpp ${CLIENT_SOURCE_DIR}/*.h)
#Populating the project as an excutable file (e.g an exe)
add_executable(${PROJECT_NAME} ${CLIENT_SOURCE_FILES})
#Linking with OttoFly SDK and Boost
target_link_libraries(${PROJECT_NAME} PRIVATE OttoFly::OttoFly ${Boost_LIBRARIES})
#This is only for MS Windows. Choosing the correct Windows SDK version. It is neascsery for Boost Logging
if (WIN32)
macro(get_WIN32_WINNT version)
if (CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
# Check for Windows 10, b/c we'll need to convert to hex 'A'.
if ("${verMajor}" MATCHES "10")
set(verMajor "A")
string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
endif ("${verMajor}" MATCHES "10")
# Remove all remaining '.' characters.
string(REPLACE "." "" ver ${ver})
# Prepend each digit with a zero.
string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
endif(CMAKE_SYSTEM_VERSION)
endmacro(get_WIN32_WINNT)
get_WIN32_WINNT(ver)
add_definitions(-D_WIN32_WINNT=${ver})
endif(WIN32)
When calling Cmake command on this file. These options should be set:
- OttoFly_DIR = “X:/XXXX/OttoFly/lib/cmake/OttoFly”
- BOOST_INCLUDE_DIR = “X:/XXXX/Boost”
Warning
We are working on providing more detailed examples for building your application.
Testing on Simulator¶
An essential step in developing an efficient and effective application is simulating the operation of your code. The OttoFly SDK provides full simulation support so you can test and verify the operation of your code on your computer before uploading and testing your code on a real drone.
Tip
Simulator first then real drone!.
Before continuing on in this section you should have installed the following software:
- SITL Ardupilot simulator.
- Mavproxy GCS.
- Mission Planner / APM Planner2 (optional).
Once you have installed these software components, continue by following the procedure outlined below.
A. Start SITL¶
a. Go to the folder where you cloned the ardupilot repo. If you have installed SITL using anything other than Vagrant, run the following command using terminal/ CMD:
sim_vehicle.py -j 2 --console --map -v ArduCopter
b. If you have installed SITL using Vagrant, run the following command:
sudo vagrant ssh -c "sim_vehicle.py -j 2 --console --map -v ArduCopter"
B. Start Mavproxy¶
Mavproxy is a light-weight GCS command-line, console based app. Use Mavproxy to open a specific port for your application. For example, if you want to connect to SITL using the port 14559, you should do the following:
On Ubuntu:
mavproxy.py --master=127.0.0.1:14550 --sitl 127.0.0.1:5501 --out 127.0.0.1:14559
On Windwos: Go to the path where mavproxy.exe is and run the following command using CMD.
mavproxy.exe --master=127.0.0.1:14550 --sitl 127.0.0.1:5501 --out 127.0.0.1:14559
Note
if you added the path of the mavproxy folder to the system variable environment, you can run the command directly in any path.
C. Start Mission Planner/APM Planner2¶
This step is optional. You can start another GCS if you want to read additional information from the drone during simulation. You should open another port for this GCS.
D. Start testing using OttoFly¶
After completing all of the previous steps, you are ready to run your program and observe the operation and results of your application using the SITL.