Recent

Author Topic: Light tracker demo  (Read 7571 times)

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Light tracker demo
« on: February 25, 2022, 08:48:54 pm »
I had some fun building a light tracker with an attiny24 controller, 4x light dependent resistors and a home made pan & tilt platform driven by two servos.  Firmware written in Pascal of course!

Short video and project code.

dbannon

  • Hero Member
  • *****
  • Posts: 3647
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Light tracker demo
« Reply #1 on: February 26, 2022, 11:00:21 am »
cool !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Light tracker demo
« Reply #2 on: February 26, 2022, 01:27:07 pm »

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: Light tracker demo
« Reply #3 on: February 26, 2022, 09:26:09 pm »
I watched the video, you did a great job, congratulations.
I'm asking this out of curiosity, don't laugh.
Can this project be modified to track a fly in the air?
The more memory computers have, the less memory people seem to use. 😅

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Light tracker demo
« Reply #4 on: February 26, 2022, 10:09:40 pm »
Can this project be modified to track a fly in the air?
It would require a different kind of detector, perhaps something like a matrix of directional microphones tuned to the typical frequency of a fly's wing beat. Or maybe image processing can be used to identify the location of a fly based on a video stream - but this would probably be beyond the capability of a humble 8 bit processor.

Anyway tracking something small like a fly will be a lot more challenging than tracking a light source.

dbannon

  • Hero Member
  • *****
  • Posts: 3647
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Light tracker demo
« Reply #5 on: February 27, 2022, 12:48:06 am »
No, No, not a fly, a Mozzie, and as its flight pattern is analyzed fire a small lazer .....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dseligo

  • Hero Member
  • *****
  • Posts: 1650
Re: Light tracker demo
« Reply #6 on: February 27, 2022, 01:12:10 am »
No, No, not a fly, a Mozzie, and as its flight pattern is analyzed fire a small lazer .....

If a Mozzie is a mosquito I was thinking same thing. :)

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Light tracker demo
« Reply #7 on: February 27, 2022, 02:13:27 am »
Very cool it looks pretty stable. And really elegant code

I worked on something similar for a short while for stage light tracking. Basically the same concept but the light generated a pseudo random sequence with a high autocorrelation which meant that it becomes a lot more resilient to noise. Not sure it might be realistic in an avr24 though :) But maybe with the power of fpc it might be!

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Light tracker demo
« Reply #8 on: February 27, 2022, 07:10:57 am »
Thanks Laksen!

dbannon

  • Hero Member
  • *****
  • Posts: 3647
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Light tracker demo
« Reply #9 on: February 27, 2022, 07:37:59 am »
If a Mozzie is a mosquito I was thinking same thing. :)

Yep !

And a mosquito has a very small body mass so very little energy would be required IMHO. What a service to mankind that would be.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Light tracker demo
« Reply #10 on: February 27, 2022, 07:39:59 am »
It seems that sound tracking is doable, see this Arduino project or this PIC32 project.  Perhaps too much code for an attiny, but perhaps an atmega can run this. One challenge I see is the trigonometry required to calculate phase shift which probably need floating point support (or an integer math implementation), which is not yet available in FPC.

Perhaps the alternative is to build the phase shift sensor using discrete components, thus making the firmware code much simpler.

Or move to a more capable controller, such as ESP32 or a Raspberry Pi (but this has so much computing power there is no challenge left) and still use FPC and just a few of microphones.

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Light tracker demo
« Reply #11 on: February 27, 2022, 02:33:42 pm »
Maybe PLLs could be used instead of FFTs? Running 2 PLLs per axis would result in two phase outputs where the difference would be directly correlated to angle, something like the following pseudocode. If you run at 8 MHz and sample all 4 sensors at 40 kHz you would have 50 cycles per sensor reading. Should be doable with an AVR with hardware multipliers
Code: Pascal  [Select][+][-]
  1. const
  2.   LoopFilterCoefAlpha = ??;
  3.   LoopFilterCoefBeta = ??;
  4.  
  5. var
  6.   LFAccumulatorX0, LFAccumulatorX1: smallint;
  7.   NCOX0, NCOX1: smallint;
  8.  
  9. function ProcessSampleX(input0, input1: smallint): longint;
  10. var
  11.   error0, error1,
  12.   LFFeedback0, LFFeedback1: smallint;
  13. begin
  14.   error0:=FixedPointMul16x16(input0, sin_table[NCOX0 shr (NCObits-LUTbits)]);
  15.   // Run loopfilters
  16.   LFAccumulatorX0:=LFAccumulatorX0+FixedPointMul16x16(error0, LoopFilterCoefBeta);
  17.   LFFeedback0:=LFAccumulatorX0+FixedPointMul16x16(error0, LoopFilterCoefAlpha);
  18.   // Update NCO
  19.   NCOX0:=NCOX0+LFFeedback0;
  20.  
  21.   error1:=FixedPointMul16x16(input1, sin_table[NCOX1 shr (NCObits-LUTbits)]);
  22.   // Run loopfilters
  23.   LFAccumulatorX1:=LFAccumulatorX1+FixedPointMul16x16(error1, LoopFilterCoefBeta);
  24.   LFFeedback1:=LFAccumulatorX1+FixedPointMul16x16(error1, LoopFilterCoefAlpha);
  25.   // Update NCO
  26.   NCOX1:=NCOX1+LFFeedback1;
  27.  
  28.   // Calculate absolute difference in tracked phase
  29.   result:=NCOX0-NCOX1;
  30. end;
  31.  

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Light tracker demo
« Reply #12 on: February 28, 2022, 12:23:04 am »
The following is actually working with a simple xor phase detector. My rough estimate is that it can run in 43 cycles on any AVR with RAM, but haven't measured it with FPC yet. Could probably be optimized a lot. Don't have any working AVR boards or microphones around sadly.. :)
Code: Pascal  [Select][+][-]
  1. const
  2.   alphaShift = 9;
  3.   betaShift = 10;
  4.  
  5. var
  6.   NCO: word; // Scaled radians [0;2*pi) / (2*pi)*65536
  7.   lfAcc: smallint = 4000; // round(tone/samplerate * 32768)
  8.  
  9. procedure ProcessSinglePLL(inputNeg: boolean);
  10. var
  11.   errorAlpha, errorBeta, addend: smallint;
  12. const
  13.   alphaConst = SarSmallint(high(smallint),alphaShift);
  14.   betaConst  = SarSmallint(high(smallint),betaShift);
  15. begin
  16.   // Phase error detector
  17.   errorAlpha:=alphaConst;
  18.   errorBeta :=betaConst;
  19.  
  20.   if inputNeg xor (NCO>$8000) then
  21.   begin
  22.     errorAlpha:=-errorAlpha;
  23.     errorBeta :=-errorBeta;
  24.   end;
  25.  
  26.   lfAcc :=lfAcc+errorBeta;
  27.   addend:=lfAcc+errorAlpha;
  28.  
  29.   NCO:=NCO+addend;
  30. end;
  31.  

ccrause

  • Hero Member
  • *****
  • Posts: 1086
Re: Light tracker demo
« Reply #13 on: February 28, 2022, 09:20:14 am »
The following is actually working with a simple xor phase detector. My rough estimate is that it can run in 43 cycles on any AVR with RAM, but haven't measured it with FPC yet. Could probably be optimized a lot. Don't have any working AVR boards or microphones around sadly.. :)
Code: Pascal  [Select][+][-]
  1. const
  2.   alphaShift = 9;
  3.   betaShift = 10;
  4.  
  5. var
  6.   NCO: word; // Scaled radians [0;2*pi) / (2*pi)*65536
  7.   lfAcc: smallint = 4000; // round(tone/samplerate * 32768)
  8.  
  9. procedure ProcessSinglePLL(inputNeg: boolean);
  10. var
  11.   errorAlpha, errorBeta, addend: smallint;
  12. const
  13.   alphaConst = SarSmallint(high(smallint),alphaShift);
  14.   betaConst  = SarSmallint(high(smallint),betaShift);
  15. begin
  16.   // Phase error detector
  17.   errorAlpha:=alphaConst;
  18.   errorBeta :=betaConst;
  19.  
  20.   if inputNeg xor (NCO>$8000) then
  21.   begin
  22.     errorAlpha:=-errorAlpha;
  23.     errorBeta :=-errorBeta;
  24.   end;
  25.  
  26.   lfAcc :=lfAcc+errorBeta;
  27.   addend:=lfAcc+errorAlpha;
  28.  
  29.   NCO:=NCO+addend;
  30. end;
  31.  

I am not familiar with the application of signal processing or PLL, do you have a reference or a short explanation of how the algorithm above works?  What is InputNeg, is it the difference in values of two signals calculated from successive measurements? .  Would a sound signal with multiple harmonics need something like a low pass filter, or will the PLL loop above handle the higher harmonics as long as the PLL update frequency is some factor higher than the highest significant harmonic?

I will buy some cheap electret microphones and a couple of op amps to start playing around, so I could also implement your PLL idea if I manage to understand enough of the implementation details.

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Light tracker demo
« Reply #14 on: February 28, 2022, 10:12:52 am »
I am not familiar with the application of signal processing or PLL, do you have a reference or a short explanation of how the algorithm above works?
It's a simple control system for acquiring and tracking a tone basically. With a second order loop filter (PI) it can track the tone with a zero phase offset meaning you can reconstruct the absolute phase of the input signal.

I've drawn a diagram here: PLL diagram

Quote
What is InputNeg, is it the difference in values of two signals calculated from successive measurements?
In the case of this code it assumes the input has the format of sin(x), so whether that is negative or positive (1 bit quantization). To do a phase comparison you would run a PLL for each microphone and then compare the NCO counter to tell the absolute phase difference between them.

Quote
Would a sound signal with multiple harmonics need something like a low pass filter, or will the PLL loop above handle the higher harmonics as long as the PLL update frequency is some factor higher than the highest significant harmonic?
It might lock to a harmonic or even one of the subharmonics if you are unlucky. It's best if you can tell which exact frequency range the harmonic will be in, in that case you can clamp the value of lfAcc to stay inside that frequency range.
That's the downside with a PLL approach, with an FFT you would be able to find the global maximum

Quote
I will buy some cheap electret microphones and a couple of op amps to start playing around, so I could also implement your PLL idea if I manage to understand enough of the implementation details.
Could be cool to see :)

 

TinyPortal © 2005-2018