Hovering with Optical Flow

In order for the drone to be capable of autonomous flight it needs some reference or estimate of it’s position in the world. Outdoors we can use GPS. Indoors GPS doesn’t work. If we have hundreds of thousands of dollars and a dedicated flight space, we can use a motion capture system for position information. Since we don’t assume we have access to such a facility, we will be using optical flow to get some sense of our position relative to the position we started.

The theory and algorithms behind optical flow are beyond the scope of this module. Suffice it to say that optical flow is a way of using a video stream from an onboard camera in order for the drone to estimate its velocity which, in turn, can be used to estimate where the drone is relative to some arbitrary starting point. At this time we are only concerned with integrating existing libraries and hardware to enable optical flow on the Intel RTF Drone; any further discussion of the concepts and theory behind optical flow are left for a later module.

Step 1: Install Libraries

Ensure that you have completed these setup steps


At the time of this writing there was an unresolved, intermittent bug that could cause error messages to spam /var/log/syslog so badly that it threatens to overfill the entire hard drive on the drone. To ensure this does not happen, check the size of that directory with

sudo du -sh /var/log

If at any time in this process that directory it is growing at a more than a MB every few seconds, shut down the drone and try restarting this process

Although they are part of the library install instructions, it is worth repeating that following parameters need to be set using QGroundControl

  • EKF2_AID_MASK: 2

  • EKF2_HGT_MODE: 2

  • COM_POS_FS_EPH: 50000

Step 2: Start Optical Flow Services

Follow these steps to run the start the optical flow service

  • Power on remote control

  • Power on the Intel RTF

  • Connect to UAV via QGroundControl (need to know IP address from previous flights or connect a monitor and run ifconfig)

  • SSH into UAV (need to know IP address from previous flights or connect a monitor and run ifconfig)

  • Start the teraranger and optical flow services

sudo systemctl start aero-teraranger.service
sudo systemctl start aero-optical-flow.service

Step 3: Verify Services

To make sure things are working as expected, run the following steps:

  • Check that syslog is not being spammed and at risk of filling the disk. Run the following command and to ensure that this directory is not growing extremely quickly

sudo du -sh /var/log/
  • Check quality of optical flow in QGroundControl: Widgets -> MAVlink Inspector -> Vehicle 1 -> OPTICAL_FLOW_RAD -> quality should be around 100 or more to be comfortable

  • Check distance sensor is giving reasonable results: QGroundControl: Widgets -> MAVlink Inspector -> Vehicle 1 -> DISTANCE_SENSOR -> current_distance. If drone is sitting on the ground, the value should either be -Inf or equal to min_distance

  • Make sure your local position estimate is reasonable in QGroundControl -> Widgets -> Analyze and check the following values: LOCAL_POSITION_NED.x, LOCAL_POSITION_NED.y, LOCAL_POSITION_NED.z

If any of these parameters are not in MAVLink Inspector or Analyze, you know that something is wrong

Step 4: Test Flight

If everything in step 3 appear to be working correctly, you are ready to try a test flight.

  • Using the RC, switch to position control before arming. Your ground station should audibly inform you that you are in position control mode

  • Arm the drone

  • Once armed, slowly increase the throttle. Nothing should happen until the throttle reaches a predefined threshold that causes the drone the take-off in one (abrupt) “jump” and hover around 1 meter high.

  • If the drone seems to ascend slowly while in position control, drop the throttle a small amount (it is registering a throttle above the “hover in place” threshold and thinks you want to ascend). If you drop the throttle enough, it will land automatically

[ ]: