Generally, this is not possible. This is because motors usually rotate very rapidly, creating rapidly fluctuating magnetic fields. Whether the disturbance is enough depends on how large the motors are.
For example, because I mounted an IMU (Inertial Measurement Unit) with magnetometers near some motors, and was forced to turn off the magnetometers to avoid the measurement affecting the state estimation.
In practice, the solutions most likely to solve your problem is:
- move the compass/magnetometer away from from the motors
use shielding material (basically material with high magnetic permeability). They do not block magnetic fields, but because they provide a path of low magnetic resistance, they attact the magnetic field (lines from north to south) to pass through their interior, so that the magnetic field intensity is lower elsewhere.
The best shape for magnetic shields is thus a closed container surrounding the shielded volume. The effectiveness of this type of shielding depends on the material's permeability, which generally drops off at both very low magnetic field strengths and at high field strengths where the material becomes saturated. So to achieve low residual fields, magnetic shields often consist of several enclosures one inside the other, each of which successively reduces the field inside it. - Wikipedia/Magnetic shielding
You can thus wrap the motors in shielding material, such as:
- Giron
- MagnetShield
- PaperShield
- Magnetic Shielding Foil
- Mag-Stop Plates
- MetGlas
- JointShield
- Finemet (for kHz frequency fields)
- CobalTex
A good site for comparison of these materials is LessEMF.com Magnetic Field Shielding
It is theoretically possible to correct for the magnetic disturbance without shielding. We do need to be aware that there are two possible sources - a rotating permanent magnet and/or current in the coils. If we have feedback of the rotor position, we can correct for the permanent magnet or coil position. If you perform some experiments, recording current and rotor position, you should be able to fit a model of the magnetic field. Fitting is necessary because the actual field strength is very hard to calculate because of how the magnetic field looks depends on the size and shape of both the magnet and coils.
In practice, it is difficult to do this - unless the motor is turning very slowly and your sensor and model is accurate enough. First, if the frequency is high, it can cause problems because of synchronization issues and communication delays. If you cannot synchronize the data obtained from the motor feedback (position and current) and that from your magnetometers, you will increase your uncertainty. Even if the frequency is low, you need to have good control of the accuracy of:
- the magnetometer
- the model
- the inputs to the model (position and current)
If any of the above are not accurate enough, the residue magnetic field (assumed to be the earth's magnetic field) could be very inaccurate.
In general, accuracy can be decreased by:
- frequency
- motor field strength (since for the equation $residue magnetic field = raw measurement - motor model$, if the two terms on the right hand side are large, even at 1% accuracy, taking the difference could result in large left hand side uncertainties)
- sensor resolutions