Basic Stamp Cruise Control

I have a motor control project that has been Basic Stamp for a long time. I keep upgrading the type of STAMP I use for more speed, etc. I can use SX or anything version I need.

What I need to do here is use a command speed (0 to 255) and use it to control a motor that drives my project. In this case it is a Train.

I get feedback that is MPH (again in the form of 0 - 255).

I am looking for way to compare the command speed ("peddle" 0-255), look at the actual speed ("mph" 0-255)
and generate a new speed command (throttle - 0-255).

I will need a couple of variables I can adjust to close the loop but this is a routine I am unfamiliar with.

I would appreciate any help I can get. I would like to use my normal speed control to get myself to a particular speed then turn on the sub-routine so that the STAMP program will then hold the speed.

Anyone care to chime in here?

Greg

Comments

  • 14 Comments sorted by Date Added Votes
  • Just make sure the brake pedal disables your CC at a hardware level. I wouldn't trust my programming to operate the throttle on my car, no matter how good a programmer I think I am!

    -Phil
    “Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
  • Is this a model train?
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • I'm with Phil on the safety concern.

    That aside, how are you presently sending the throttle value and getting the actual speed back?
    Jon McPhalen
      *It's "Jon" or "JonnyMac" -- please don't call me "Jonny"
  • It is sort of a model train...
    Its 1/8th scale.

    I have a Dial on a Pot that I read with an A to D Converter. I generate a throttle number (0-255) and send it to a Hobby Servo that operates throttle on a carburetor.

    I am using a gear tooth sensor on a wheel and COUNT to get my actual speed.

    What I have now works well -- This is a rideable train.

    When I go up small inclines I need to give the throttle a boost to keep up my speed. When I go down, I need to let up a little. Basic cruise control.

    I just don't have a clue how to do such interactive programming.

    If you want to see my train, go to :

    Greg
  • BTW -- I have plenty of Safety features built in, including a hardware KILL switch.

    Stamp controls everything on my train. Brakes, throttle, headlights, horn....

    My current reliability is high. I would have my control loop turn the cruise control off if I touch any other control.

    For anyone interested, I would be happy to describe the control system further but it is pretty boring.

    Greg

  • If you go about 1:30 in on the Youtube video, you can see my display panel that shows me going about 4.5 mph. (upper left hand display)

    4.5 mph is represented as 045 out of 255. Max speed is about 10 mph

    On flat level ground with a small load, my throttle command of about the same number will give me the same speed.

    Greg
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 21,240
    edited October 3 Vote Up0Vote Down
    Omigosh! That is so cool! And it's not just one track, but a whole network of them. I totally want a ride on that train! Where is your railway located?

    -Phil
    “Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
  • Jeff HaasJeff Haas Posts: 227
    edited October 3 Vote Up0Vote Down
    San Mateo, CA
  • msrobotsmsrobots Posts: 1,686
    edited October 3 Vote Up0Vote Down
    oh god, I enjoyed every second of that train ride!

    Just wonderful, thank you Greg!

    Can you maybe post the stamp code you have or describe more precisely how the wonderful Nautilus is using the stamp(s)?

    Because a cruise control with say up/down/clear/set is doable and just needs to fit into your existing setup.

    And don't even think that this might be boring for anyone here...

    nevermind, I found your youtube viedeos. Just wonderful.

    Enjoy!

    Mike
    I am just another Code Monkey.

    A determined coder can write COBOL programs in any language. -- Author unknown.

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this post are to be interpreted as described in RFC 2119.
  • G McMurry wrote: »
    If you go about 1:30 in on the Youtube video, you can see my display panel that shows me going about 4.5 mph. (upper left hand display)

    4.5 mph is represented as 045 out of 255. Max speed is about 10 mph

    On flat level ground with a small load, my throttle command of about the same number will give me the same speed.

    Greg

    Basically what you need to do is use the difference between the speed the dial pot is set to and the actual speed you are measuring to adjust the throttle position. The difference between the set speed and the actual speed is the error, which would be used to determine how far and in what direction the throttle needs to move.

    So the calculations are:

    error = set speed – actual speed

    throttle = error * (some function)

    Determining the optimum function to use is the challenge. You might start with a linear function (error * X )
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • Here's a simple bit of code that is based a little on a camera controller I coded for a friend, and the previous post. Assuming you have just read the throttle position ad the actual speed, this will take the delta (error), scale that, then add it back into the final output. Since PBASIC doesn't handle negative values nicely, this is a bit verbose.
    Set_Speed:
      IF (speedIn = throttle) THEN
        speedOut = throttle
    
      ELSEIF (speedIn < throttle) THEN
        error = throttle - speedIn
        IF (error > 5) THEN
          error = error */ $0019                      ' multiply by 10%
        ELSE
          error = 0
        ENDIF
        speedOut = throttle + error
    
      ELSE
        error = speedIn - throttle
        IF (error > 5) THEN
          error = error */ $0019                      ' multiply by 10%
        ELSE
          error = 0
        ENDIF
        speedOut = throttle - error
      ENDIF
    
      ' send speedOut to motor controls
    
      RETURN
    

    Jon McPhalen
      *It's "Jon" or "JonnyMac" -- please don't call me "Jonny"
  • Thanks everyone...
    I had to go offline for a day or so.
    I have seen the pid-control article. Maybe I had had a couple of martini's when I read it but it mostly confused me.
    I appreciate your sample code above. I am going to give it a whirl again now that I have a little time to concentrate.

    Thanks to all. I'll keep you posted.

    I will start another thread to explain the project. I am just a bBasic hack so my code is not that great, however it works.

    Greg
  • OK --
    I'm back on this project and looking at your suggested code.

    As I am not much of a software guy, I wince at the complicated IF THEN commands in PBasic.

    First of all -- my loop is very slow. Currently, its 2 times a second.

    Your suggested code offers a solution that determines how far off the actual speed is from the throttle, then corrects by 10% at a time. I get that and it seems like a good approach.

    However, I need to ask about the code you suggest and the nesting within IF THEN, ELSEIF, ENDIF, etc.

    I admit my eyes roll up when I try to grok the intricacies of IF THEN but I always assumed that ENDIF concluded the statement.

    In your suggestion, you have an ENDIF, a statement and then follow with ELSE.

    ELSEIF (speedIn < throttle) THEN
    error = throttle - speedIn
    IF (error > 5) THEN
    error = error */ $0019 ' multiply by 10%
    ELSE
    error = 0
    ENDIF
    speedOut = throttle + error

    ELSE
    error = speedIn - throttle
    IF (error > 5) THEN ...


    I need to get that clear in my head first.

    Thanks for your help --

    Greg

    Is this correct?


Sign In or Register to comment.