On-Board Artificial Intelligence for Failure Detection and Safe Trajectory Generation