Shop OBEX P1 Docs P2 Docs Learn Events
Determining phase difference between two signals — Parallax Forums

Determining phase difference between two signals

LukeHLukeH Posts: 22
edited 2010-10-18 21:13 in Accessories
This should be a real easy one.

I have a sensor which reads an angular position and returns the information by phase modulating a square wave. On a separate channel, the sensor also outputs a non-shifted reference square wave of the same frequency as the signal wave; the frequency varies somewhat, but at any given moment, both the reference and the data signal are always exactly the same frequency. Only the phase difference between the two signals is of interest; the frequency itself carries no information. Both are 50/50 duty cycle square waves and both are on a DC bias, so that they swing between 0 and +V. Conveniently, the phase shift of the sensor signal is directly equal to the angular position of the physical sensor. The frequency is relatively low, approx. 500 Hz plus or minus a couple hundred.

Given this relatively simple phase modulation, how best to interface it to a Propeller? I have little experience with the Prop's counters, but I think this problem lends itself readily to counter use. Send the reference signal and the data signal to a counter or counters, and use some kind of differential or logic mode to accumulate the phase difference somehow, then write this into a variable in the hub which gets updated every time the counters complete a measurement. I know in general how to set up the counters, but not sure which mode to use. Again I don't need to measure the frequency, only the phase difference.

Overall, I don't need more than a degree of resolution from this sensor, and something like 30 degrees of resolution/precision would even be acceptable.

Not looking for someone to solve the entire problem for me, but looking for a starting point. I haven't enough experience with counters, and think I could learn something from this. Ideas?

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-10-18 16:32
    Take a look at the counters' logic mode input. By programming it for an XOR function, you can count the number of Propeller clock cycles over which the two inputs are different. Then use waitpeq/waitpne to read the counter, along with cnt, over one clock period from your sensor. The ratio of the phsx reading vs. that from cnt will give you the angle offset.

    -Phil
  • LukeHLukeH Posts: 22
    edited 2010-10-18 19:00
    Using a simple XOR accumulation like this - wouldn't a phase shift to the left return the same result as an equal-magnitude phase shift to the right? These are 50/50 duty cycle square waves. Or is this accounted for somehow in the WAITPEQ/WAITPNE parts of the algorithm? I need coverage of an entire revolution (+/- 180 degrees, or -0/+360, either way works).

    I suspect I may need to use two counters; one in POSEDGE mode to count the frequency and thus determine the period to watch for; the other to run the XOR accumulation.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-10-18 19:15
    If negative phase shifts are possible (and that wasn't stated in your original post), a simple XOR won't work. Can you clarify the range of the phase shifts +/-180 vs. 0-360? D'oh! Of course those two conditions are identical! Also, does the phase shift occur over the entire cycle, of just half a cycle? A datasheet for the sensor would certainly be helpful here.

    -Phil

    Addendum: I guess I was a bit hasty in my analysis. But, at 500 Hz, this should be doable in software. Let me think some more about it.
  • LukeHLukeH Posts: 22
    edited 2010-10-18 21:13
    Thanks, I did not reveal all the details before because I don't want someone else to solve the whole entire problem for me, but here are the answers to your questions. There is no datasheet because this is a sensor I cobbled together myself, inspired by another thread in this forum.

    Inspired by a different thread, I am trying to build a simple assembly which roughly simulates the way a heat-seeking missile seeker works. A small motor spins a reticle, half clear half opaque, through which incoming light passes, on its way to a single phototransistor. A separate LED+phototransistor around the very edge, out of the lens field of view, provides the reference signal. The whole thing is very crude and needs some discrete components to amplify and filter the noisy signal, but works well enough in a dark room when a bright light (laser pointer) is aimed through different quadrants of the reticle, on to the primary (signal-generating) phototransistor. I should send a photo or diagram.

    The phase shift occurs over the entire period of the data signal, not just half. The domain of the sensor is exactly one complete revolution, and it wraps around when it passes <0 or >360. Thus -20 or +340 are functionally equivalent. This is also why I put in the reference signal, to give something to compare it to.

    I think my objective can be accomplished by sensing the rising edge of the reference wave, then timing until encountering the rising edge of the signal wave, then timing again until encountering the rising edge of the reference wave again. The ratio of the two times will represent the angle around the circle, from the reference point, where the incoming light beam is hitting. Now just to figure out the best way to do that with a prop (can probably even be done with analog components too).

    This is also kind of how some aircraft navigation systems (VOR) work, as a very rough analogy.

    By the way, I overestimated the speed of the motor, the sensor actually generates a signal closer to 120-150 Hz. Plenty of time to do computations in software.
Sign In or Register to comment.