Hi again everyone.
As mentioned before in a few threads I'm building a tank with a gun the I want to adjust itself depending on the angle of the tank.
I've currently got the accelerometer mounted on a servo, working nicely on a 28X2 working at 32Mhz.
To start off with I tried running a simple routine of:
The reasoning behind this code is that the scale the accelerometer works on (about 485-715) is different to the scale of the servo (75-225). This routine worked quite well but as you can imagine was a bit sluggish when the error is large.
So, I need to be able to convert the readings on the accelerometer to their corresponding servo values. To do this I first took a reading a the the endpoints of the servo (75 & 225) then filled in the gaps. After much calculating and re-arranging to stop the numbers overflowing, I got a formula working. I debugged this in real time on the PICAXE but found that only the endpoint values were correct and that when the sensor was in-between the endpoints, it produced a slightly incorrect number.
So, I had to find out what was going on in the middle:
Attached is what I found. I shoved the numbers into excel and plotted a graph and before long I realised that (obviously) it was roughly a cosine graph: The difference on the sensor between 89-90 degrees is minimal but between 0-2 is a lot. (attached is my excel muking arounds. Rename it to .xls)
I then used excel to create a trendline for this graph (because the actual readings aren't accurate) and it could also give the equation for this trendline:
This trendline is very accurate but when put in the form of "x=" (to get the servo value from the accel' reading) it becomes very complicated and would never work on PICAXE.
I also created my own cosine graph which is quite close (and should be what's expected from the accelerometer but isn't due to something):
When this formula is put in the form of "x=" it isn't too bad but obviously involves the function "cos^-1" which (I don't think) PICAXE can do.
Is this the only way to get the accelerometer readings to a corresponding servo position:
Which will of course take ages to run through, even at 32Mhz.
Thanks in advance, I've read my maths limit
David.
P.S. I will attach in a reply my "standard" code that I use as the base for muking everything around and trying new things.
As mentioned before in a few threads I'm building a tank with a gun the I want to adjust itself depending on the angle of the tank.
I've currently got the accelerometer mounted on a servo, working nicely on a 28X2 working at 32Mhz.
To start off with I tried running a simple routine of:
Code:
if accelerometer > desired pos then: let servo = servo -1
else: let servo = servo +1
(obviously this is just the general "gist" of the code
So, I need to be able to convert the readings on the accelerometer to their corresponding servo values. To do this I first took a reading a the the endpoints of the servo (75 & 225) then filled in the gaps. After much calculating and re-arranging to stop the numbers overflowing, I got a formula working. I debugged this in real time on the PICAXE but found that only the endpoint values were correct and that when the sensor was in-between the endpoints, it produced a slightly incorrect number.
So, I had to find out what was going on in the middle:
Code:
sertxd ("start,")
main: servopos B.7,b5
pause 500
readadc10 1,w0
sertxd (#w0,",")
let b5 = b5 + 1
if b5 = 225 then end
endif
goto main
I then used excel to create a trendline for this graph (because the actual readings aren't accurate) and it could also give the equation for this trendline:
Code:
y = 0.0001x3 - 0.028x2 + 0.3857x + 710.63
Where each "x" is actually the servo position minus 74)
I also created my own cosine graph which is quite close (and should be what's expected from the accelerometer but isn't due to something):
Code:
y=(COS(((x-74)/48.5)-0.105)*103)+608.35
Where "x" is again the servo reading minus 74
Is this the only way to get the accelerometer readings to a corresponding servo position:
Code:
if reading = 716 then: let reading = 75: endif
if reading = 714 then: let reading = 76: endif
if reading = 713 then: let reading = 77: endif
etc...
Thanks in advance, I've read my maths limit
David.
P.S. I will attach in a reply my "standard" code that I use as the base for muking everything around and trying new things.
Attachments
-
748 bytes Views: 3
-
70 KB Views: 6