From d50fd3a6cdc5a21718457f41e8c15e7f14f9330e Mon Sep 17 00:00:00 2001
From: Julien Beraud <julien.beraud@parrot.com>
Date: Mon, 21 Nov 2016 18:04:20 +0100
Subject: [PATCH] AP_OpticalFlow: fix onboard init

mistake in patch to avoid segfault which enabled
AP_OpticalFlow_Linux instead of AP_OpticalFlow_Onboard.
Store a reference to ahrs in order to execute init later, when
ahrs is initialized.
---
 libraries/AP_OpticalFlow/OpticalFlow.cpp | 8 +++++---
 libraries/AP_OpticalFlow/OpticalFlow.h   | 1 +
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libraries/AP_OpticalFlow/OpticalFlow.cpp b/libraries/AP_OpticalFlow/OpticalFlow.cpp
index 0cf50e4626..22d6f18e85 100644
--- a/libraries/AP_OpticalFlow/OpticalFlow.cpp
+++ b/libraries/AP_OpticalFlow/OpticalFlow.cpp
@@ -60,6 +60,7 @@ const AP_Param::GroupInfo OpticalFlow::var_info[] = {
 // default constructor
 OpticalFlow::OpticalFlow(AP_AHRS_NavEKF &ahrs)
     : _last_update_ms(0)
+    , _ahrs(ahrs)
 {
     AP_Param::setup_object_defaults(this, var_info);
 
@@ -76,12 +77,13 @@ void OpticalFlow::init(void)
         backend = new AP_OpticalFlow_PX4(*this);
 #elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
         backend = new AP_OpticalFlow_HIL(*this);
-#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
-        backend = new AP_OpticalFlow_Linux(*this, hal.i2c_mgr->get_device(HAL_OPTFLOW_PX4FLOW_I2C_BUS, HAL_OPTFLOW_PX4FLOW_I2C_ADDRESS));
 #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP ||\
     CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_MINLURE ||\
     CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
-        backend = new AP_OpticalFlow_Onboard(*this, ahrs);
+        backend = new AP_OpticalFlow_Onboard(*this, _ahrs);
+#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
+        backend = new AP_OpticalFlow_Linux(*this, hal.i2c_mgr->get_device(HAL_OPTFLOW_PX4FLOW_I2C_BUS, HAL_OPTFLOW_PX4FLOW_I2C_ADDRESS));
+
 #endif
     }
 
diff --git a/libraries/AP_OpticalFlow/OpticalFlow.h b/libraries/AP_OpticalFlow/OpticalFlow.h
index 518de1f38b..793c6c4505 100644
--- a/libraries/AP_OpticalFlow/OpticalFlow.h
+++ b/libraries/AP_OpticalFlow/OpticalFlow.h
@@ -80,6 +80,7 @@ public:
 
 private:
     OpticalFlow_backend *backend;
+    AP_AHRS_NavEKF &_ahrs;
 
     struct AP_OpticalFlow_Flags {
         uint8_t healthy     : 1;    // true if sensor is healthy