Picar-X and cliff avoidance

Has anyone got some Python code which shows how to use the grey scale sensor to detect a cliff and avoid a fall?

My home office is upstairs and on some of my tests the Picar-X has gone out of the door and I’ve had to quickly follow it and catch it before it falls down the stairs.

Would be useful to add such logic to my current scripts to prevent falling off of table tops or down stairs.

Correct. You can use ezblock studio app to control.
The app has a cliff detection example, block example, and python example, the example is to use the grayscale sensor to detect the cliff and avoid falling. For detailed instructions, please see our tutorial.
https://docs.sunfounder.com/projects/picar-x/en/latest/ezblock/play_with_ezblock.html