Help with Accelermoter/Servo Control
RubrChicken
Posts: 39
I'm having great fun (and success) in the Propeller world, OBEX is my best friend! Anyway, I'm trying to have proportional servo control based on the accelerometer. I get a value called size that goes from 0 (horizontal) to 45 (vertical) and a value called deg which goes 0-360. I used Servo32v3 for servo control. My servo has 1000 as the bottom and 2000 as the top, I've tested this elsewhere. I used a method called ChooseE to choose the direction the servo will move, 1 is down (1500-1000) and 2 is up (1500-2000). I have the up side working, but the down part doesn't! I have an update accel method, and it is straight from the example that came with it.
My code:
Update the accelerometer (this is run in its own cog)
ChooseE
Thanks!!
My code:
repeat waitcnt(cnt + 1_000_000) 'To make sure you can read the TV !outa[noparse][[/noparse]23] 'If you don't have a TV this says everythings working fine. term.dec(deg) term.str(string(" ")) 'These four commands format and output text to the TV term.dec(size) term.str(string(13)) elevator := chooseE if elevator == 1 outa[noparse][[/noparse]17] := 1 SERVO.Set(5,2000) if elevator == 2 outa[noparse][[/noparse]18] := 1 SERVO.Set(5,(size*500)/45+1500) else outa[noparse][[/noparse]17] := 0 outa[noparse][[/noparse]18] := 0 SERVO.Set(5,1500)
Update the accelerometer (this is run in its own cog)
PUB UpdateAccel | raw, mg, G_scale, temp G_scale := 2000/(256/3) repeat raw := acc.ro 'Get raw value for acceleration raw := raw / clk_scale 'the magnitude, really size := raw / G_scale 'scale mg value for screen display ' temp := acc.theta >> 19 'scale 32-bit value to a 13-bit value deg := temp*360/8192 if cnt // 3000000 == 0 dira[noparse][[/noparse]18]~~ !outa[noparse][[/noparse]dira]
ChooseE
pub ChooseE if deg > 90 if deg < 270 term.str(string("down")) return 1 else else if deg < 90 term.str(string("up")) return 2 if deg > 270 term.str(string("up")) return 2 else return 3
Thanks!!
Comments
Then the code does this:
The 'different stuff' will happen, because elevator is 1, not 2, and this erases the result from the very first IF.
You can change your code to use explicit IFs for all three cases, OR use ELSEIF, like this:
Jason
Post Edited (JasonDorie) : 2/15/2009 10:04:34 PM GMT