During its sixth flight on the red planet, the Ingenuity helicopter experienced a little mishap - but, thanks to planning and built-in failsafes, it survived to fly another day.

This is actually great news, because it demonstrates the multiple ways in which the vehicle is robust to errors - as well as giving engineers here at home an opportunity to patch them.

The event took place on 22 May 2021 - the 91st Martian day of the Perseverance mission. The sky was clear and bright, perfect for Ingenuity's task: demonstrating its ability to take aerial stereo images.

The little helicopter was to vertically ascend to a height of 10 meters (33 feet), then make a horizontal flight of three legs - southwest for 150 meters, at which point it would turn and move 15 meters in a southward direction while taking images, before turning northeast and flying for another 50 meters before landing, all at a speed of about 4 meters per second (8.9 mph).

But, just as it reached the end of that first 150-meter leg, Ingenuity started acting really weird.

"Ingenuity began adjusting its velocity and tilting back and forth in an oscillating pattern," wrote Ingenuity pilot Håvard Grip of NASA's Jet Propulsion Laboratory in a blog post.

"This behavior persisted throughout the rest of the flight. Prior to landing safely, onboard sensors indicated the rotorcraft encountered roll and pitch excursions of more than 20 degrees, large control inputs, and spikes in power consumption."

The problem, the Ingenuity team found, had something to do with the way the helicopter navigates. Its main navigation system, the on-board inertial measurement unit, constantly tracks Ingenuity's position, velocity and attitude (orientation in three-dimensional space).

To support this, it also has "eyes" - a navigation camera that constantly takes images of the ground below, at a rate of 30 images per second.

The nav system looks at the timestamp of the image to know when it was taken, and uses this information to compare what the camera sees based on what it should have been seeing at that time. If this doesn't stack up, the helicopter makes adjustments to its position, velocity and attitude.

This is where flight six went wrong. A glitch in the image delivery pipeline caused one - just one - of the nav-cam images to drop out before it could reach the nav system. This meant that every subsequent image had an inaccurate timestamp.

So the information Ingenuity's nav system was receiving about its surroundings was ever-so-slightly askew, causing the helicopter to want to correct for errors that weren't actually there. This led to the wobbly flight - but Ingenuity was still able to land perfectly, within 5 meters of the planned location.

This was partly because the helicopter's engineers made the decision to have Ingenuity ignore nav-cam images below one meter in altitude, since they may be obscured by dust close to the ground, confusing the nav system. This ensures smooth take-off and landing - but it also meant that Ingenuity could stop oscillating during this critical moment.

Ingenuity was also designed to have a "stability margin", in which it can tolerate large errors - including errors in the image timestamps - without becoming unstable.

"Looking at the bigger picture, Flight Six ended with Ingenuity safely on the ground because a number of subsystems - the rotor system, the actuators, and the power system - responded to increased demands to keep the helicopter flying. In a very real sense, Ingenuity muscled through the situation, and while the flight uncovered a timing vulnerability that will now have to be addressed, it also confirmed the robustness of the system in multiple ways," Grip wrote.

"While we did not intentionally plan such a stressful flight, NASA now has flight data probing the outer reaches of the helicopter's performance envelope. That data will be carefully analyzed in the time ahead, expanding our reservoir of knowledge about flying helicopters on Mars."

That's so freaking cool.