Detecting the period was hard. Fast fourier transform needed much more cycles than our flight time.
The best result was by testing all the possible periods on the last 1024 samples. Take the period which has the most similar repetitions. Get the maximum signal in the last period. Get the time of the maximum signal relative to the time of the current sample. The problem was when the most similar repetition was actually a region of 2 periods.
Next, it was an IR LED on the ground & the IR receiver from our 5 year old Picco Z in the air. That would not detect ambient IR. The problem was the receiver is digital. It would detect reflected IR from random surfaces & output the same level as the incident signal.
Finally came the poor lion's machine vision. Have an IR emitter on the copter & an analog IR receiver on the ground. That didn't detect enough of a difference from the abient IR. It would take some massive circuitry to get an analog IR receiver at 38khz.
All roads led back to the magnetometer.
HOW MAGNETOMETERS WORK
pnicorp.com actually has the theory of operation of the Micromag 3, so we ripped off a sensor from our Micromag 3 & built a more compact magnetometer, to avoid spending $40 & waiting 2 weeks.
The Micromag 3 has a 600uH inductor, biased by 2 47 ohm resistors. It measures the amount of time required for the inductor voltage to cross a certain threshold after alternating the 2 bias voltages. The inductance changes, depending on the amount of magnetic flux. It uses a standard microcontroller, not a custom ASIC.
The 1st thing you notice is normal inductors don't work. You need a core which is a lot more sensitive to ambient flux. The inductors in magnetometers use a thin film permalloy.
The 2nd thing you notice is the inductor saturates very easily, from a NdFeB magnet. Readings make sense until the magnet gets too close. Then it reports no flux, as its core is saturated.
The final trick is the reference voltage for the comparator is very important. Ours had to be exactly 1.0V. 1.1V or 0.9V didn't show any response to ambient flux. The inductor voltage only got to 1.4V & there was an optimum point in the waveform to detect flux variations.
To keep the inductor from becoming permanently gaussed, they use 2 comparators & alternate the polarity. Since we're just interested in a binary result, we use 1 comparator. It puts out a really faint signal. Micromag 3 stacks hundreds of readings to get more sensitivity.
The CPU on Marcy 2 actually has 2 voltage references, allowing us to test the battery against a fixed voltage & test the mag against a ratiometric voltage, but even then, the ratiometric voltage needs tweeking for every flight.
This would all be easier with a hall effect sensor & an instrumentation amplifier, luxuries beyond our useful payload.
Got a standard azimuth waveform on a flight without any cyclic control. Not as strong as the original Micromag 3, but think about the parts count.
As soon as cyclic was enabled, things fell apart. 1st of all, there's a lot of offset from having the motors & electronics in such a small space.
Secondly, the pulses from the tail rotor disturb the attitude enough to throw off the magnetometer waveform. You get 1 pulse, then the tail rotor goes crazy for a while. With such rapid attitude changes, zero crossings are no longer enough. Time for derivatives.
That was a complete 150 second flight. The derivative got us back to a centered waveform with clear 0 crossings. That nailed the azimuth every time. For the 1st time, we finally had enough cyclic control to hover something in the apartment, which introduced another problem.
The oscillation from Marcy 1 came back. Pulsing the tail rotor started the same oscillation & the tail rotor wasn't fast enough to stomp it out. Pulsing any spinning airframe just makes it swing like a pendulum.
Finally caved in & added weight. The vertical flap actually did improve matters slightly, but now the tail rotor's slow response showed. The blades on this copter have already run their course.
Set the tail rotor to always be on, which should have reduced the spinup time, but mainly decreased the difference between on & off. It definitely appears as if some filtering is interfering with the tail rotor commands. The main problem is the oscillation. The functioning tail rotor, flybar, counter rotation & vertical flap didn't add any more control than the bare Marcy 1.