Shop OBEX P1 Docs P2 Docs Learn Events
Servo return to center on start up — Parallax Forums

Servo return to center on start up

LewLew Posts: 5
edited 2011-04-17 08:56 in BASIC Stamp
Hello all. I am brand new (obsessing on What's a M. manual for two days now!), hate to ask for help, but I haven't found this yet and thought I'd ask anyway.

On page 133, the exercise is to rotate the servo left and right using buttons:

' {$STAMP BS2}
' {$PBASIC 2.5}
duration VAR Word
duration = 750
PAUSE 1000
DO
IF IN3 = 1 THEN
IF duration > 500 THEN
duration = duration - 25
ENDIF
ENDIF
IF IN4 = 1 THEN
IF duration < 1000 THEN
duration = duration + 25
ENDIF
ENDIF
PULSOUT 14, duration
PAUSE 10
DEBUG HOME, DEC4 duration, " = duration"
LOOP

This does what I want it to do ... EXCEPT, when powered down then restarted, it goes back to center because of the "duration = 750". I am controling a lever that I need it to start where I left it when I powered down, NOT at center.

I thought maybe I could take the last position (DEBUG DEC4 duration) and write that to an EEPROM address, then read that into "duration = ___" somehow on start up? But I can't seem to find out how to do that automatically. My eyes are starting to cross trying to absorb so much so fast ... DATA? READ/WRITE? DEBUGIN? Other chips have "scratch pad memory"?

I am using a BS2 Rev F, with a Board of Education Rev. B, a 4.5 v. power adapter, and a USB adapter.

My "heat oppressed brain" would appreciate any pointing in the right direction and I'm all about learning, so please feel free to educate!

Later, - Lew


Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-13 19:01
    You'd use WRITE to record the position to an address in the EEPROM.
    10,000,000 writes / address, but all things must pass.
    Check out WRITE and READ in PBASIC Help.
    Are you going to write/record that position everytime it changes or only
    when you want to, based on some "orderly shutdown" cue? Things to
    think about.
  • Mike GMike G Posts: 2,702
    edited 2011-04-13 19:25
    I usually park the servo before shutting down. Still... recording the last position or parking the servo does not mean that the servo is in that position the next time you start up. I'll also create a method to move the servo slowly to a home position at start up then execute the program logic after a few moments. Leave enough time for the servo to make it to the home/parked position. If the servo is already parked (at home) at start up then it just looks like there is a delay at the beginning of your program.
  • W9GFOW9GFO Posts: 4,010
    edited 2011-04-13 19:31
    Mike G wrote: »
    I'll also create a method to move the servo slowly to a home position at start up then execute your logic after a few moments. If the servo is already parked (at home) at start up then it just looks like there is a delay at the beginning of your program.

    I don't see how you could do that without knowing the initial position of the servo.

    Rich H
  • Mike GMike G Posts: 2,702
    edited 2011-04-13 19:34
    I don't see how you could do that without knowing the initial position of the servo.

    Why do you need to know the current position if your just going to move the servos to a known starting position?
  • LewLew Posts: 5
    edited 2011-04-13 19:51
    Hey PJ et al,

    OK, here's the deal. I'm dreaming of using this as trim control for rudder and aileron on real (not model) planes. The ones that are used now are big, heavy, expensive, etc. and just use switch and relays to control a servo motor (which controls a small flap on the rudder or aileron) -- which stays put after the power is off. Once the trim is set for a particular passenger load, to compensate for a "heavy wing", etc. it usually doesn't need to be set again from flight to flight, or only minorly. This pulse control servo will do the trick, but as is, it will have to be reset every time the plane is powered up. I don't want to have to read a pulse duration at shut down (position of trim) and re-enter it by hand every time -- I want it to do it automatically based on the processor reading it's last pulse duration and entering that on start up ... if it can be done.

    Keep it coming! - Lew
  • W9GFOW9GFO Posts: 4,010
    edited 2011-04-13 20:04
    Mike G wrote: »
    Why do you need to know the current position if your just going to move the servos to a known starting position?

    You don't need to know the current position to move to a starting position, but to move it slowly to a starting position, I think you do.

    Rich H
  • Mike GMike G Posts: 2,702
    edited 2011-04-13 20:38
    Yep your right should not have said slowly.
  • YoshtiYoshti Posts: 108
    edited 2011-04-14 08:24
    Hi,
    My 2 cents...
    Options one: Use a DC motor with SCREW gear, so pressure, when flying, won't move it. And it does not need a pulse, simple switches for TRIM.

    Options two: Although it will center the servo at the beginning, it will re-position it-self. Good way to PRE-FLIGHT CHECK LIST functionnality. Use the servo approach, and just use a button to : Once happy with the TRIM, by pushing the button, it will store the value in memory. Upon Startup, the trim will NEUTRAL itself, read value in memory and re-position according to it. So the 10,000,000 of write limits will seem to be "UNLIMITED".

    Just a thought!
    CHeers
    Yoshi... FLying a Spitfire (RC) from Parkzone.
  • LewLew Posts: 5
    edited 2011-04-14 11:38
    Hey Yosh,

    Option one: Yup, old tried and trued, clunky technology -- I just like the challenge of improving on it, and these are very small trim tabs (not much pressure).

    Option two: My thought too ... now, how do I get that duration variable that I want, stored as a value into memory "by pushing the button"? Surely there's somebody out there that has wired up this magic button! The simplest low tech solution is to fly it with a laptop attatched to the BS2, get the number that's close to most applications, then make that "duration = __" instead of neutral.

    But where's the challenge? I just want to know how to translate the variable that's being sent to "DEBUG HOME, DEC4 duration" into a value that can be saved (written) then retrieved (read) upon startup, without the computer attached to BS2.

    I still think there must be a way (as in a spread sheet -- read calculated value of cell (x,y) , copy to cell (a,b) , then insert value of cell (a,b) into a formula in another cell.

    Then today, the pilot I'm working with says he kind of LIKES the idea of the tabs resetting to neutral and setting them every flight .... SHEESH!

    Later, - Lew
  • YoshtiYoshti Posts: 108
    edited 2011-04-15 04:37
    Hi Lew,
    The BS2 has an area where you can store some value. So no need for an external PC to read and write from every time.
    I'm no expert in BS2 ... kind of given up on it.
    So... In general. Upon start-up, with the BS2, you can intialize everything you need, then with a read command go fetch in memory the value, then apply the value for the servo. In flight, you noticed an adjustement for the TRIM, once happy and perfect , the pushbutton execute a routine to take that value and store it to replace the old one.

    Will need the big guns here, to help with the read and write commands. I've tried before, and never worked. all garble stuff. Gave up on this.

    I had my own project running a display on PC with a BS2, but that required a laptop to be hooked up.

    Yosh
  • LewLew Posts: 5
    edited 2011-04-15 15:59
    OK, for those interested, I'm bleary eyed but happy and here's what I came up with. This not only keeps the last position at shutdown/startup, but it gives me the option of whether to save the last position (push the button before shutting down) or not -- which means there is no re-write on EEPROM site so that it goes back to the last setting saved from the previous saved shutdown. (I could have left the "write" in the loop to automatically save the position instead of controled by the button, but I think then it would be constantly re-writing the EEPROM)

    This is such a rush! BTW, I'm 66, be gentle!

    Later, - Lew

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    duration VAR Word 'defines pulse variable
    saved VAR Word 'defines saved pulse variable
    READ 0, Word saved 'reads number saved in EEPROM at 0 into "saved"
    duration = saved 'sets position at last saved number

    DO
    IF IN3 = 1 THEN 'if switch on input 3 is 1
    'and duration is greater than the
    IF duration > 450 THEN 'lower limit of the servo,then reduce the
    duration = duration - 15 'pulse by 15, each cycle
    ENDIF
    ENDIF
    IF IN4 = 1 THEN 'if switch on input 4 ... etc.
    IF duration < 1050 THEN
    duration = duration + 15
    ENDIF
    ENDIF
    PULSOUT 14, duration
    PAUSE 10

    DEBUG HOME, DEC4 duration, " = duration" 'display duration
    'LOOP

    IF IN1 = 1 THEN 'defines button which when pushed, writes
    WRITE 0, Word duration 'current position to EEPROM position 0
    READ 0, saved 'reads position 0 into "saved" variable
    saved = duration 'so that when pushed, displayes saved position
    DEBUG HOME,CR, DEC4 duration, " = saved"
    ENDIF
    LOOP



    OOPS! Guess I should have attached a file -- the spacing is all off.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-15 16:08
    If it's doing what you want then it's a good deal.
    (I wouldn't say that if you were jacking hardware,
    but I don't grind anyone for his programming as such.)
  • LewLew Posts: 5
    edited 2011-04-15 16:51
    "Jacking hardware", PJ? Translation?

    Here's the file (I think).

    Later, - Lew
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-16 06:34
    Wanton abuse of transistors, using an ADC to "read"
    the output of a comparator, other like signs & wonders.
  • ercoerco Posts: 20,256
    edited 2011-04-17 08:35
    PJ Allen wrote: »
    using an ADC to "read"
    the output of a comparator

    That's the classic way to increase a comparator's resolution. Proof: that's exactly why they make an LM339 quad comparator AND an ADC0834 quad ADC! Now you can have 4 times 8 bit =32 bit resolution reading a pot or a photoresistor, using just 2 "chips"!
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-17 08:56
    Erco,
    What's the purpose in "measuring" the output of a comparator?
    A comparator's output is binary, it has two states: HI or LO, on or off, true or false.
    There's no middle ground.
Sign In or Register to comment.