Autopilots exist for airplanes and drones, how is orientation measured with respect to ground? In particular how is the direction of gravity detected and how is it differentiated from acceleration due to thrust?
In analog-instrumented planes, orientation with respect to ground is established by a feature inside the attitude gyro called the erection system which senses the long-term average of the direction of gravity. It then slowly urges the gyro to establish and hold the correct position of the horizon while still indicating short-term excursions from level flight in pitch and roll.
In vacuum-driven gyros, the erection system consists of small air leaks in the rotor support gimbal which are opened or closed by small vanes that are hung so they swing freely under the influence of gravity. When they are open, they apply a tiny thrust to the gimbal which gently precesses or "drifts" the gyro into its erect position in which the horizon line is perfectly perpendicular to the direction of "down" as sensed by the vanes.
The time constant for the erection process is of order ~minutes- that is, it furnishes a multi-minute-long average of the downward direction.
From standard sensors, we can completely determine attitude
Attitude estimation is a mathematically rich domain. If we have sufficiently precise measurements across sufficient degrees of freedom we can completely, and uniquely, solve for the attitude. However, the way any particular application goes about solving it is highly dependent on the goals (autonomous navigation or just keeping the wings level and the nose pointed in the right direction?), so it's no surprise that there are a multitude of solutions, one written by yours truly.
SO(3) means roll-pitch-yaw
T(3) means x-y-z, i.e. latitude, longitude, and altitude
SE(3) means the combination of roll-pitch-yaw and x-y-z
Solving for SE(3) means being able to measure/infer both T(3) and SO(3).
T(3) is straightforward, as that easily comes from a GPS-style system, aka GNSS. (Although it could also come from some other ground-based reference system combined with an altimeter of some kind, so it's not restricted to GPS-style sensors.)
SO(3) is more complex to get just right, since the reference is to the ground and yet there are no deployed ground-based systems to measure the plane's roll, pitch, and yaw. Therefore, we have to make do with sensors mounted on the moving inertial system, i.e. the aircraft.
Without getting too much into the details, in modern autopilots we typically measure the turning rate with gyrometers and integrate that to get an attitude reference. Because these attitude solutions inevitably drift, we correct roll-pitch with accelerometer measurements, and yaw can be corrected with a yaw measurement such as a magnetometer.
In airplanes, which typically fly in a coordinated manner, we can isolate gravitational acceleration by taking the measured acceleration and subtracting the predicted centripetal acceleration (which in constant-speed flight and at a given bank angle is identical for all airplanes).
Combining position and attitude
It's very helpful to use the aircraft's position to refine its attitude estimation. After all if we hvae a good understanding of the aircraft's flight dynamics-- e.g. airplanes can only fly forward, helicopters can fly any direction, and balloons don't have a well-defined forwards direction-- then we can draw good conclusions about how the aircraft must be oriented. So fusing all these measurements together is how we can come up with a very solid estimate of attitude.
Recently, we are also seeing RTK-GNSS (with a precision of several cm) being used to fully characterize an airplane's SE(3) state. This is done by placing the receivers as far apart as possible and using the extended baseline to definitely locate the aircraft's extremities. If we know where the nose, tail, and wingtip are in T(3), then because we know the plane's shape we also know the plane's attitude and position.
The downside is that if the plane rolls over than the antennas no longer have a clear view of the sky, but this can be easily corrected for in the short term by using inertial measurements.
Definitely the sum is greater than the parts.
If you are interested in more details, I can refer you to what we did at Tau Labs, a drone autopilot ecosystem which existed from roughly 2011 to 2017.
This is a surprisingly hard problem. Initially, the answer was: Just look out of the window. Of course, this became less helpful when flying in clouds. With unmanned aircraft it has become the hard problem I mentioned.
The usual recommendation for glider pilots who get sucked up into a cloud is: Let go of stick and rudder, open the speedbrakes and hope for the best.
Accelerometers will only give you the combined result of gravity and inertial loads, and only the magnitude of the signal can be used to separate obviously false readings from probably correct ones. Combine them with gyros, a barometer and clever software, and always be aware that both, sensor and software, will not be error-free.
What you do is to use a well-calibrated high quality gyro and do some common sense checking: If the load factor is permanently above 1, don't use that attitude to reset the gyro. Instead, make sure there is enough flying time straight and level for a proper reset and your maneuvering does not take long enough for drift to build up to a significant level. You can even perform checks during flight with a small roll attitude change and then a check how the other parameters behave to establish what is level.
The modern solution is to use an Attitude and Horizon Reference System, or AHRS. The system includes both rotation rate sensors and linear accelerometers for all three axes. In the short term, the rotation rate sensors are the key players, but GPS data and magnetometer (magnetic compass) data is used to help seperate acceleration due to turning or changing airspeed from acceleration due to gravity, to allow the system to use gravity to correct the system for "drift" from various sources and accurately keep track of which way is "up" over a long flight. This system of correction and calibration means that it is not absolutely necessary to initialize the system on the ground with the aircraft level and stationary, though this step may help the system reach optimum performance more quickly.
Following the principles given in this related answer, it is possible to create a similar system without utilizing GPS data and magnetometer data to help with drift correction, but such systems invariably require much more expensive (and larger and heavier) components to achieve the same level of accuracy.
Naturally, some military navigation and attitude reference systems are designed to still work in the event that the GPS network is eliminated.