## Turning a hard drive into a turntable controller

If you require help or assistance with anything then please post here

Moderators: electrogear, exonerate

### Turning a hard drive into a turntable controller

Hi!

Using code, I need to select one of 3 values. d1, d2 and d3.

I need the value of the variable that has the smallest absolute value.
for example
(3, -5, 10) = 3
(-10, -2, 5) = -2

If I had if statements and an abs() function I would do something like this:

Code: Select all
`a1 = abs(d1);a2 = abs(d2);a3 = abs(d3);if(a1 < a2 && a1 < a3) value = d1;if(a2 < a1 && a2 < a3) value = d2;if(a3 < a1 && a3 < a2) value = d3;}`

How would I do that in SM code?
Or would it be easier to use components?

Thanks!

edit: Renamed Thread from "Selecting a value depending on absolute value?"
Last edited by alphanimal on Mon Feb 22, 2010 9:06 pm, edited 1 time in total.
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

I think this works but the assembly people can easily trim some CPU usage off of this as there is a trick that, AFAIK, only works in ASM to get an absolute value in two ops verses the multiple ops that my rectify trick will cost.

There's also an issue of what to do when the absolute values are equal but the signs are different. I avoided the issue by allowing input 1 to win any equals comparison.
Code: Select all
`streamin d1;streamin d2;streamin d3;streamout value;float a1,a2,a3,c1,c2,c3;a1 = d1 - (d1<0 & 2*d1); // a1 = abs(d1)a2 = d2 - (d2<0 & 2*d2);a3 = d3 - (d3<0 & 2*d3);c1 = (a1>=a2 & a1>=a3) & d1; // c1 = d1 if a1>= a2 or a3c2 = (a2>a1 & a2>a3) & d2;c3 = (a3>a1 & a3>a2) & d3;value = c1 + c2 + c3; // the winner of the last step will assign to value, the other two should be zero// this is needed because you will run out of registers if you try this in one line.`
oddson
wiki guru

Posts: 3889
Joined: Sun Jul 03, 2005 6:44 pm

### Re: Selecting a value depending on absolute value?

Thanks oddson! You saved me a lot of headache

Your code gives the value of the greatest absolute. I wanted the one with the smallest, but that was easy to fix!

thax again and have a nice day!
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

Just for the record...
I'm trying to use a hard drive as some kind of turntable controller.

Hooking up the HDD motor to a stereo line audio input gives a pretty acurate signal of the current speed of the disk.
Watching that signal on a oscilloscope with X=left and Y=right shows a circle. I'm calculating the current angle of the disk with atan(y/x). Then I calculate the difference of the angle from the current sample to the previous one, to get the speed.

I needed this check (smallest absolute value) to compensate for the jumps like from 360° to 0°. e. g. if the current angle is 1° and the previous one was 259° that should give 2° and not -358°. But from 101° to 99° that should be -2° of course.

I'd still need a more accurate atan function. I currently use a rough approximisation formula.
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

alphanimal wrote:Just for the record...
I'm trying to use a hard drive as some kind of turntable controller.

Hooking up the HDD motor to a stereo line audio input gives a pretty acurate signal of the current speed of the disk.
Watching that signal on a oscilloscope with X=left and Y=right shows a circle. I'm calculating the current angle of the disk with atan(y/x). Then I calculate the difference of the angle from the current sample to the previous one, to get the speed.

I needed this check (smallest absolute value) to compensate for the jumps like from 360° to 0°. e. g. if the current angle is 1° and the previous one was 259° that should give 2° and not -358°. But from 101° to 99° that should be -2° of course.

I'd still need a more accurate atan function. I currently use a rough approximisation formula.

you're kidding, aren't you...?
stw
smanatic

Posts: 641
Joined: Mon Jun 30, 2008 2:55 pm

### Re: Selecting a value depending on absolute value?

That is not a new idea

I read somewhere that it works...

sunsynth
smaniac

Posts: 1496
Joined: Mon Sep 11, 2006 3:27 pm
Location: HH - Made in Germany

### Re: Selecting a value depending on absolute value?

Yes id does work indeed...
http://www.slipperybrick.com/2008/11/a- ... turntable/

mine does also work, but I got some kind of vibrato effect, which comes from the inaccurate atan calculation I think
Attachments
turntable.PNG (36.12 KiB) Viewed 7021 times
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

Does anyone have a SM code snippet to calulate an accurate atan?

I tried it with the infinite series function but that is only valid for values bewteen -1 and 1.

I don't care about performance I just need a reasonably accurate arctan function...
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

What;s the degree of accuracy of your x and y values? And how much accuracy does the output need? Just wondering if a lookup table might work (maybe followed by a bit of smoothing). Obviously (x/y) can go off to infinity, so you'd have to use a two dimensional table based on the indivdual x and y values. This still wouldn't be easy, but Taylor expansions are pretty much the norm for trig functions in SM as far as I can tell.
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.

trogluddite
smychopath

Posts: 3033
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

### Re: Selecting a value depending on absolute value?

alphanimal wrote:Does anyone have a SM code snippet to calulate an accurate atan?

I tried it with the infinite series function but that is only valid for values bewteen -1 and 1.

I don't care about performance I just need a reasonably accurate arctan function...

I've been trying to wrap my head around what you're doing and whether and why arctangent is needed.

But for now I'll take it as given.

Where do you need the most accuracy. The difference between 100 and infinity is about 0.01 so is there a point in the upper range that you're OK with pi/2 being the approximate output?

I think some for a calc table stored as an array might work if Taylor series doesn't work for you... but then how to index so that the accuracy is where you need it most. And then there's the issue of whether interpolation is needed.

Here's a simple version with linear indexing and no interpolation:
simple atan table.osm

But it's very inaccurate near zero which may be where you need the most accuracy.

If the indexing was non-linear you could get greater accuracy in the lower range but still support larger values. But you'd still need to decide at what level is pi/2 close enough.
oddson
wiki guru

Posts: 3889
Joined: Sun Jul 03, 2005 6:44 pm

### Re: Selecting a value depending on absolute value?

Attached you find a sample of the raw audio data I get from the Hard drive.
It contains a speed up and letting it slow down until it stops. Also some scratching kind of movements.

The graphs show the l/r ratio (the tangens value which I want to convert to an angle) when rotating fast, very slow and when it comes to a complete stop. When there's no movement I get a very low level of noise (-87db) which results in the noisy l/r graph.
Attachments
tan_slow.PNG (37.79 KiB) Viewed 6944 times
tan.PNG (43.25 KiB) Viewed 6937 times
HDD signal.zip
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

l/r value on stop
Attachments
tan_stop.PNG (93.09 KiB) Viewed 6932 times
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

oddson wrote:I've been trying to wrap my head around what you're doing and whether and why arctangent is needed.

The motor gives 2 signals (L/R) which you can think of as X/Y coordinates moving in a circle.
I want to calculate the speed of the rotation by comparing the angle in one sample to the one in the next sample.
For calculating the angle I need arctan.

oddson wrote:Where do you need the most accuracy. The difference between 100 and infinity is about 0.01 so is there a point in the upper range that you're OK with pi/2 being the approximate output?

I'd say the more accuracy the better the result will be.
How could I build the Taylor series so I can try out how much accuracy I need?
Attachments
circle.PNG (34.45 KiB) Viewed 6923 times
alphanimal
essemer

Posts: 42
Joined: Mon Jun 01, 2009 12:57 am

### Re: Selecting a value depending on absolute value?

I wonder whether asin and acos on x and y seperately would be more accurate (only -1 to +1 range needed) - comparing the two results ought to tell you which quadrant you're in. Also this might reduce the increasing influence of the noise component as y gets very small.
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.

trogluddite
smychopath

Posts: 3033
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

### Re: Selecting a value depending on absolute value?

I'll get back to you on the atan() approximation issue.

But it's the apparent phase relationship between the two signals that I think is important.

Both appear to be sinusoidal but they don't appear to be 90 degrees out of phase.

I would guess (but didn't calculate) the signals are 60 degrees out of phase because of the pole alignment of the motor.

I'm thinking which signal's peak is first will tell you direction and the peak amplitude will give you a better speed result than trying to calculate from one position to the next.

Once you work out direction an envelope follower could give you an analogue for disk speed.
oddson
wiki guru

Posts: 3889
Joined: Sun Jul 03, 2005 6:44 pm

Next