# Poshold with optical flow works weird

**URL:** <https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432>\
**Category:** PX4 Autopilot\
**Created:** [February 12, 2017, 11:19am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432 "2017-02-12T11:19:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doolly](https://discuss.px4.io/user_avatar/discuss.px4.io/doolly/32/5233_2.png) [@Doolly](https://discuss.px4.io/u/Doolly)\
**Post date:** [February 12, 2017, 11:19am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/1 "2017-02-12T11:19:45Z")

</div>

I’m using firmware ver-1.5.5 (LPE) with my pixhawk and try using optical flow  
and I have same issue with

> [@LPE with Optic flow won't go into Position control during flight](http://discuss.px4.io/t/lpe-with-optic-flow-wont-go-into-position-control-during-flight/766):
>
> I’m trying to get a copter flying with Optic flow only (LPE\_GPS\_ON is set to 0) in position control mode. Yesterday was working fine, this morning I upgraded QGC, let QGC upgrade my PX4 firmware (see that problem in previous post I made), and now things aren’t working so great. When I hold the copter in my hand (disarmed) high enough above the ground that the flow quality threshold is happy, I can put the copter in position control mode with no problem. Then I put it down on the ground (qualit…

and a little difference is I sometimes can get in to poshold mode but my copter flies crazy and crash down  
and one more question what is MPC\_ALT\_MODE?  
what is the difference between ‘terrain following’ and ‘altitude following’ ??

---

<div class="post-metadata">

**Author:** ![Nicolas](https://discuss.px4.io/user_avatar/discuss.px4.io/nicolas/32/4467_2.png) [@Nicolas](https://discuss.px4.io/u/Nicolas)\
**Post date:** [February 13, 2017, 2:35am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/2 "2017-02-13T02:35:44Z")

</div>

Can you post a log? It’s hard to help otherwise.

Re `MPC_ALT_MODE`: “altitude following” means the vehicle will perform altitude hold (position control in the z axis) with respect to the estimated altitude, which is based on baro and/or GPS. in “terrain following” the vehicle will control its altitude such that the height above ground (based on range measurements) remains constant (unless you change the altitude setpoint of course).  
If you are flying over completely flat ground, there is essentially no difference. If you are flying e.g. up a hill, “terrain following” will result in the vehicle climbing in order to keep its height above ground, altitude following" will result in the vehicle getting closer and closer to the ground.

---

<div class="post-metadata">

**Author:** ![Doolly](https://discuss.px4.io/user_avatar/discuss.px4.io/doolly/32/5233_2.png) [@Doolly](https://discuss.px4.io/u/Doolly)\
**Post date:** [February 14, 2017, 9:21am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/3 "2017-02-14T09:21:26Z")

</div>

thank you for the reply and here are two logs  
[http://logs.uaventure.com/view/ZfYtmfYoFKbZaAZgreYU8J](http://logs.uaventure.com/view/ZfYtmfYoFKbZaAZgreYU8J) this is one tested outdoors and thrust ctrl had some problem, if I change in to alt\_hold, copter just rises and if I get in to pos\_hold than it seems to be rejected and even not rejected for few seconds, performance was not that reliable

[http://logs.uaventure.com/view/U4o9Ddq46KhzikVLMmk8n5](http://logs.uaventure.com/view/U4o9Ddq46KhzikVLMmk8n5) this is one tested indoors and alt\_hold was fine(no rising) but after switching to pos\_hold my copter flew crazy and crashed…😢

---

<div class="post-metadata">

**Author:** ![Nicolas](https://discuss.px4.io/user_avatar/discuss.px4.io/nicolas/32/4467_2.png) [@Nicolas](https://discuss.px4.io/u/Nicolas)\
**Post date:** [February 14, 2017, 6:34pm UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/4 "2017-02-14T18:34:01Z")

</div>

One problem that I see is that your range sensor is very noisy. Most of the time it is not actually fused into the HAGL estimate, which is fatal for flow.

You should see in the message logs if/when lidar and flow measurements begin/stop to be fused with messages like “[lpe] lidar init: mean X cm stddev Y cm”.  
You can find the message log in the root directory of the SD card, the names are the timestamps of the boot-time.

You are using the sonar on the PX4FLOW board, correct? Do you have another range sensor (lidar lit/TeraRanger) at hand to try?

---

<div class="post-metadata">

**Author:** ![Doolly](https://discuss.px4.io/user_avatar/discuss.px4.io/doolly/32/5233_2.png) [@Doolly](https://discuss.px4.io/u/Doolly)\
**Post date:** [February 17, 2017, 4:36am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/5 "2017-02-17T04:36:02Z")

</div>

2016\_11\_16\_10\_26\_43: [lpe] Sonar detected with ID 0  
2016\_11\_16\_10\_26\_43: [lpe] land init  
2016\_11\_16\_10\_26\_43: [lpe] baro init -131 m std 28 cm  
2016\_11\_16\_10\_26\_44: [lpe] z resume  
2016\_11\_16\_10\_26\_44: [lpe] tz resume  
2016\_11\_16\_10\_26\_59: ARMED by RC  
2016\_11\_16\_10\_26\_59: [blackbox] /fs/microsd/log/2016-11-16  
2016\_11\_16\_10\_26\_59: [blackbox] recording: 10\_26\_59.px4log  
2016\_11\_16\_10\_27\_05: [lpe] sonar timeout  
2016\_11\_16\_10\_27\_06: DISARMED by RC  
2016\_11\_16\_10\_27\_07: [blackbox] stopped (333 drops)  
2016\_11\_16\_10\_27\_09: [lpe] sonar init std \> min  
2016\_11\_16\_10\_27\_13: [lpe] flow init: quality 254 std 0  
2016\_11\_16\_10\_27\_13: [lpe] xy resume  
2016\_11\_16\_10\_27\_16: [lpe] flow timeout  
2016\_11\_16\_10\_27\_20: [lpe] sonar timeout  
2016\_11\_16\_10\_27\_24: [lpe] xy timeout  
2016\_11\_16\_10\_28\_20: PREFLIGHT FAIL: GPS RECEIVER MISSING  
2016\_11\_16\_10\_28\_20: MANUAL CONTROL LOST (at t=102134ms)  
2016\_11\_16\_10\_28\_20: MANUAL CONTROL REGAINED after 2075ms  
2016\_11\_16\_10\_28\_33: ARMED by RC  
2016\_11\_16\_10\_28\_33: [blackbox] /fs/microsd/log/2016-11-16  
2016\_11\_16\_10\_28\_33: [blackbox] recording: 10\_28\_33.px4log  
2016\_11\_16\_10\_28\_35: Takeoff detected  
2016\_11\_16\_10\_28\_39: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_40: [lpe] flow init: quality 250 std 10  
2016\_11\_16\_10\_28\_40: [lpe] xy resume  
2016\_11\_16\_10\_28\_41: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_42: no gps  
2016\_11\_16\_10\_28\_42: failsafe mode on  
2016\_11\_16\_10\_28\_42: LOW BATTERY, RETURN TO LAND ADVISED  
2016\_11\_16\_10\_28\_43: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_44: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_46: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_48: CRITICAL BATTERY, LANDING ADVISED!  
2016\_11\_16\_10\_28\_48: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_50: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_52: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_53: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_54: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_55: failsafe mode off  
2016\_11\_16\_10\_28\_58: [lpe] flow timeout  
2016\_11\_16\_10\_28\_58: [lpe] sonar init std \> min  
2016\_11\_16\_10\_29\_05: DISARMED by RC  
2016\_11\_16\_10\_29\_05: [lpe] xy timeout  
2016\_11\_16\_10\_29\_05: [lpe] sonar timeout  
2016\_11\_16\_10\_29\_05: [lpe] land fault, beta 19.71  
2016\_11\_16\_10\_29\_06: [blackbox] stopped (0 drops)  
2016\_11\_16\_10\_29\_07: Landing detected

2016\_11\_16\_10\_26\_43: [lpe] Sonar detected with ID 0  
2016\_11\_16\_10\_26\_43: [lpe] land init  
2016\_11\_16\_10\_26\_43: [lpe] baro init -131 m std 28 cm  
2016\_11\_16\_10\_26\_44: [lpe] z resume  
2016\_11\_16\_10\_26\_44: [lpe] tz resume  
2016\_11\_16\_10\_26\_59: ARMED by RC  
2016\_11\_16\_10\_26\_59: [blackbox] /fs/microsd/log/2016-11-16  
2016\_11\_16\_10\_26\_59: [blackbox] recording: 10\_26\_59.px4log  
2016\_11\_16\_10\_27\_05: [lpe] sonar timeout  
2016\_11\_16\_10\_27\_06: DISARMED by RC  
2016\_11\_16\_10\_27\_07: [blackbox] stopped (333 drops)  
2016\_11\_16\_10\_27\_09: [lpe] sonar init std \> min  
2016\_11\_16\_10\_27\_13: [lpe] flow init: quality 254 std 0  
2016\_11\_16\_10\_27\_13: [lpe] xy resume  
2016\_11\_16\_10\_27\_16: [lpe] flow timeout  
2016\_11\_16\_10\_27\_20: [lpe] sonar timeout  
2016\_11\_16\_10\_27\_24: [lpe] xy timeout  
2016\_11\_16\_10\_28\_20: PREFLIGHT FAIL: GPS RECEIVER MISSING  
2016\_11\_16\_10\_28\_20: MANUAL CONTROL LOST (at t=102134ms)  
2016\_11\_16\_10\_28\_20: MANUAL CONTROL REGAINED after 2075ms  
2016\_11\_16\_10\_28\_33: ARMED by RC  
2016\_11\_16\_10\_28\_33: [blackbox] /fs/microsd/log/2016-11-16  
2016\_11\_16\_10\_28\_33: [blackbox] recording: 10\_28\_33.px4log  
2016\_11\_16\_10\_28\_35: Takeoff detected  
2016\_11\_16\_10\_28\_39: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_40: [lpe] flow init: quality 250 std 10  
2016\_11\_16\_10\_28\_40: [lpe] xy resume  
2016\_11\_16\_10\_28\_41: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_42: no gps  
2016\_11\_16\_10\_28\_42: failsafe mode on  
2016\_11\_16\_10\_28\_42: LOW BATTERY, RETURN TO LAND ADVISED  
2016\_11\_16\_10\_28\_43: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_44: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_46: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_48: CRITICAL BATTERY, LANDING ADVISED!  
2016\_11\_16\_10\_28\_48: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_50: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_52: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_53: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_54: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_55: failsafe mode off  
2016\_11\_16\_10\_28\_58: [lpe] flow timeout  
2016\_11\_16\_10\_28\_58: [lpe] sonar init std \> min  
2016\_11\_16\_10\_29\_05: DISARMED by RC  
2016\_11\_16\_10\_29\_05: [lpe] xy timeout  
2016\_11\_16\_10\_29\_05: [lpe] sonar timeout  
2016\_11\_16\_10\_29\_05: [lpe] land fault, beta 19.71  
2016\_11\_16\_10\_29\_06: [blackbox] stopped (0 drops)  
2016\_11\_16\_10\_29\_07: Landing detected

yes I’m using my sonar on px4flow  
these are the log msgs and I ordered lidar today

2016\_11\_16\_10\_28\_39: [lpe] sonar init std \> min  
2016\_11\_16\_10\_28\_40: [lpe] flow init: quality 250 std 10  
2016\_11\_16\_10\_29\_05: [lpe] xy timeout

can you explain what this log means? and does ‘timeout’ means it just doesn’t work?

---

<div class="post-metadata">

**Author:** ![Nicolas](https://discuss.px4.io/user_avatar/discuss.px4.io/nicolas/32/4467_2.png) [@Nicolas](https://discuss.px4.io/u/Nicolas)\
**Post date:** [February 17, 2017, 6:13pm UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/6 "2017-02-17T18:13:24Z")

</div>

You can see that the height above ground estimate is re-initialized several times due to poor/invalid range measurements.

“sonar timeout” is printed here and depends on the `_time_last_sonar` timestamp which only gets updated if the range measurement is valid:

> <https://github.com/PX4/Firmware/blob/c28cd76e5f9fd3c10b2e682312acddb42f44e576/src/modules/local_position_estimator/sensors/sonar.cpp#L144>

“flow timeout” can happen for a variety of reasons, including bad height above ground estimate.

Since you do not have a GPS connected you have no position estimation anymore once flow times out, causing the “xy timeout”, and the quad goes into failsafe mode.

You could try flying over other surfaces that might be more reflective to sonar, but ultimately you’ll be better off with a light based TOF range sensor.

---

<div class="post-metadata">

**Author:** ![Doolly](https://discuss.px4.io/user_avatar/discuss.px4.io/doolly/32/5233_2.png) [@Doolly](https://discuss.px4.io/u/Doolly)\
**Post date:** [February 28, 2017, 8:34am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/7 "2017-02-28T08:34:50Z")

</div>

I got lidar and I did  
[https://pixhawk.org/peripherals/rangefinder?s[]=lidar](https://pixhawk.org/peripherals/rangefinder?s%5B%5D=lidar)  
these installations but I couldn’t get my lidar work  
I coudn’t get lpe lidar init message and my pixhawk still uses sonar for distance sensing

I gave power from RCIN pwm port  
I did check 5v comming out from aux6  
I uncommented //if (\_lidarInitialized && (\_lidarFault \< fault\_lvl\_disable)) { return; }  
this line from lpe sensor sonar code

by the way how do I make direct link from github code like you did??

---

<div class="post-metadata">

**Author:** ![Nicolas](https://discuss.px4.io/user_avatar/discuss.px4.io/nicolas/32/4467_2.png) [@Nicolas](https://discuss.px4.io/u/Nicolas)\
**Post date:** [February 28, 2017, 9:57am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/8 "2017-02-28T09:57:19Z")

</div>

Is the lidar working in general? I.e. do you see range measurements with only the lidar attached, flow module detached?  
Do you have a lidar v3? I start mine (on a PixFalcon) with `ll40ls -X start` (note the `-X` for expansion I2C bus option).

You may have a conflict when you have two range sensor devices attached, which will both publish on the same topic. The driver that is started first will be the one that LPE ends up using.

You should be able to fix that by ensuring that the lidar driver is started first, e.g. by inserting the start command in `rcS`, somwhere above this line:

> <https://github.com/PX4/Firmware/blob/master/ROMFS/px4fmu_common/init.d/rcS#L767>

Re github link: Simply click on the line number you want to reference, this will add the line number to the URL. Paste it into discuss with a blank line above and below, discuss will then expand it.

---

<div class="post-metadata">

**Author:** ![Doolly](https://discuss.px4.io/user_avatar/discuss.px4.io/doolly/32/5233_2.png) [@Doolly](https://discuss.px4.io/u/Doolly)\
**Post date:** [February 28, 2017, 10:17am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/9 "2017-02-28T10:17:15Z")

</div>

thank you so much for the reply

I think my lidar does not work in general I can’t see distance value through QGC analyze when I detach px4flow  
what could be gone wrong?  
yes I’m using v3 though PWM aux pin

> <https://github.com/PX4/Firmware/blob/94ce6e12c196a9c50e487945d33f40f58a600790/src/modules/local_position_estimator/sensors/sonar.cpp#L83>

and this is the line I uncommented to override sonar, is this not enough? or is this the line should be commented?

---

<div class="post-metadata">

**Author:** ![Nicolas](https://discuss.px4.io/user_avatar/discuss.px4.io/nicolas/32/4467_2.png) [@Nicolas](https://discuss.px4.io/u/Nicolas)\
**Post date:** [February 28, 2017, 10:24am UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/10 "2017-02-28T10:24:28Z")

</div>

I have no experience with the lidar on PWM aux pin, so I can’t offer any advice.

I don’t think you need to uncomment that line. It appears to be a relic. Neither `_lidarInitialized` nor `_lidarFault` appear anywhere else in the code. LPE does not actually differentiate between different range measurement sources, it just uses whatever is on the `distance_sensor` topic, which is then fused in `sonar.cpp`.

---

<div class="post-metadata">

**Author:** ![Doolly](https://discuss.px4.io/user_avatar/discuss.px4.io/doolly/32/5233_2.png) [@Doolly](https://discuss.px4.io/u/Doolly)\
**Post date:** [February 28, 2017, 12:50pm UTC](https://discuss.px4.io/t/poshold-with-optical-flow-works-weird/2432/11 "2017-02-28T12:50:55Z")

</div>

ok I will try other way thank you Nicolas !😀
