Shop OBEX P1 Docs P2 Docs Learn Events
Need Help with PBASIC code for a Simple Test — Parallax Forums

Need Help with PBASIC code for a Simple Test

a2ta2t Posts: 5
edited 2012-05-17 07:01 in BASIC Stamp
I need some help writing some PBASIC code to run a test on a lock actuator. I am trying to test the durability of a small electric motor used for locking and unlocking the mechanism. I have bought and read some of the Syntax and Reference manual, as well as the Whats a Microcontroller book. However, Im having some difficulty coming up with the code I will need to actually perform this test.

I have bought the Board of Education USB along with the Basic Stamp 2 (BS2-IC). I have also bought one HB-25 motor controller. I have bought one 3 pin cable to connect the board of education to the motor controller, as well as plug in power sources for both the board and for the motor controller. Hopefully this is all I will need.

I have written below the order of operations for the test. Can someone help me put this into PBASIC code ? I very much appreciate any and all help!

PBASIC INSTRUCTIONS

' {$STAMP BS2}
' {$PBASIC 2.5}
  1. Turn motor on, Clockwise direction.
  2. Receive +12v dc input from lock limit switch within 2 seconds. If input is not received within 2 seconds, stop test and display “Lock limit switch out of time”
  3. If lock limit switch input is received within 2 seconds, turn motor off and pause 5 seconds.
  4. Turn motor on, Counterclockwise direction.
  5. Receive +12v dc input from unlock limit switch within 2 seconds. If input is not received within 2 seconds, stop test and display “Unlock limit switch out of time”
  6. If unlock limit switch input is received within 2 seconds, turn motor off, pause 5 seconds
Repeat steps 1-6 1,000 times. Have Debug terminal show the cycle count during the test.

Paul

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-02 07:59
    The HB-25 appears to the Stamp very much like an R/C servo motor as described in the "Robotics" tutorial and is used the same way except that, where the servo motors require a control pulse about 50 times a second, the HB-25 requires only a single pulse to set the speed and direction of motion. There is sample PBasic code in the documentation and via a link on the product webpage.

    Your switches are connected to +12V. This will damage the Stamp if connected directly to it. It would be easier to use +5V on the limit switches. There are plenty of PBasic examples for the use of pushbuttons or other switches in the "What's a Microcontroller?" tutorial. If you have to use +12V, you can use a voltage divider where the switch is connected to a 2.2K resistor, the other end of that resistor is connected to a 1K resistor, the other end of the 1K resistor is connected to ground, and a Stamp I/O pin is connected to the junction of the 2.2K and 1K resistors.

    The idea of a timed function is a little hard without an example. Let's say the switch is connected to I/O pin 1 and you have a voltage divider as described above. Let's say you have a word variable declared as "time" and the HB-25 is connected to I/O pin 0:
    pulsout 0, 1000     ' Turn on motor with 2ms pulse
    time = 40     ' We'll use 50ms as the time "tick"
    timeCheck:
    pause 50     ' Wait for the "tick"
    time = time - 1
    if time = 0 then     ' 2 second timeout has expired
       pulsout 0, 750     ' Stop motor with 1.5ms pulse
       debug "Lock limit switch out of time",cr
       stop
    endif
    if in1 = 1 then     ' Limit switch turned on
       pulsout 0, 750     ' Stop motor with 1.5ms pulse
       pause 5000     ' Pause 5 seconds
       goto nextStep
    endif
    goto timeCheck
    nextStep:
    
  • a2ta2t Posts: 5
    edited 2012-05-02 08:28
    Mike,

    Thanks for speedy reply. I am a bit over my head at this point as I don’t really understand PBASIC code all that well. Forgive some of these simple questions :

    - Is there supposed to be more code after the nextStep: command? That code screen doesn’t scroll down.

    - I can easily use 5.5v input for limit switches. I assume I can just run a lead off the board from Vdd to the mechanisms limit switch, then back to the input on the board

    - I thought I had to use the servo 3 pin ports to output to motor controller (the ones marked X4 and X5 on top right of board). Can I send inputs to motor controller using independent wires? IF so, how would I connect them to HB-25, since that seems to only accept the little 3 pin wire. I was told by Parallax the only way to communicate with the HB-25 was with the 3 wire cable I bought.

    - How are you reversing polarity to make motor spin the other way?


    Sorry for all these questions, this will be my 1st stab at programming PBASIC.

    Paul
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-02 08:47
    1) What I wrote is an example, drawn from your description. After nextStep is what will be your code for the next step in your description. Use my code as a model, but you'll have to spend the effort to understand what it's doing. It's not difficult and it will help you a lot.

    2) You need to use +5.0V, not +5.5V. I'm sure you meant 5.0V. Yes, you can run Vdd off board to supply the limit switches. Then you don't need the resistors.

    3) It's easier to use the 3 pin connectors, but you can run separate wires. You'll still need a 3 pin connector on the HB-25 end of things, so it's easier to use a ready-made jumper cable. If you need more distance, you can cut the jumper and solder additional wire between the pieces. The HB-25 needs +5V (red) and ground (black) in addition to the signal line (white or yellow) from the Stamp board. If you want to use separate wires on the Stamp board, red goes to Vdd, black to Vss, and the signal line goes to whatever Stamp I/O pin you want to use. If you use the 3 pin sockets, make sure to set the jumper so the red wires go to Vdd. If you set the jumper so the red wires go to Vin, you'll damage the HB-25. It's supposed to be set for Vdd, but check it.

    4) Read the documentation for the HB-25 and Robotics with the BoeBot. Servos (and the HB-25) accept a pulse roughly between 1ms and 2ms in width, repeated 50 times a second. 1ms represents either one end of the servo motor range of motion or maximum speed in one direction. 2ms represents either the other end of the servo motor range of motion or maximum speed in the other direction. 1.5ms represents either the mid-position or stopped depending on whether this is a standard servo or continuous motion servo (and HB-25). In what I posted, there's no reversing. I used "pulsout 0, 1000" which produces a 2ms pulse (see the PULSOUT statement documentation for details). What do you think you'll need for a 1ms pulse?
  • a2ta2t Posts: 5
    edited 2012-05-02 09:45
    >What do you think you'll need for a 1ms pulse?

    pulsout 0, 500 (hopefully)

    I appreciate all this help. Think I have much better understanding now. I will try to write the full code for this, then post back in here and hopefully you can double check for me. Again, thanks very much for assistance. Im still a bit unclear as to how its measuring time but I will research more.

    Paul


  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-02 09:56
    The documentation on the various statements in the Basic Stamp Syntax and Reference Manual gives you all the timing information. If you look at the chapter on PULSOUT, you'll see that the time parameter (500 in this case) is in units of 2us for the BS2. Another good source of information for the Stamps is this website. Click on the "app-notes" link at the bottom of the page.
  • a2ta2t Posts: 5
    edited 2012-05-16 12:06
    OK Ive made great strides here and was able to get some code together that will alternate motor direction with a few seconds delay in between.

    However, I am finding this to be erratic. For some reason, the board sends commands to the motor that are not in the current program. When I 1st switch on the board, the motor will begin running somewhat randomly. Perhaps it still has parts of other programs in the memory?

    If I turn it off and let it all rest for a while, it runs my program with no issue. But if I make a change and try to re-run the program it seems to get hung up.

    Ive added the code :

    LOW 15
    PAUSE 100

    at the beginning and that has helped alot, I guess it lets the motor driver boot up or something. But I am still getting somewhat random impulses to motor every now and then. Is there some way to completely clear out the board memory? Ive tried pressing reset button but that isnt clearing memory its just restarting whatever program is in there.

    Another question : I am trying to input the lock and unlock limit switch signals. Can I just run a lead from Vdd to the white section of the input # ? Or should it go to the black section of the input # ? Or does it not matter?

    Thanks for tremendous help, I think I am getting very close. I just need to figure out how to clear out board memory so it can run my program and not snippets of a bunch of programs.

    Paul
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-16 12:40
    When you download a new program to a Stamp, it clears out all that matters. Whatever may be left over from previous downloads cannot affect the new download, so it's not necessary to "clear it all out". The HB-25 takes a little while to initialize itself when it's powered on. Look at the examples in the HB-25 documentation for how to account for this. If you're getting unaccounted for behavior from your program and the HB-25, it's most likely a bug in your program. You'll have to attach your source code to a forum message (Do Not copy and paste into a message). Use the "Go Advanced" button and the Attachment Manager to upload your source code file.

    As far as attaching limit switches to your Stamp, download and read the "What's a Microcontroller?" tutorial from Parallax's Downloads webpage under Educational Tutorials and Translations. This has plenty of examples of wiring up switches and testing for them in a program. I have no clue what you mean by "white section of the input #" or "black section of the input #". In either case, don't do it. Follow the descriptions in the tutorial.
  • a2ta2t Posts: 5
    edited 2012-05-17 06:38
    Thanks once again.

    I re-read the manual (I did buy the book and the syntax manual) and got the inputs wired in correctly. It must of been acting erratically due to improper wiring of the inputs. Now everything seems to function, I just wired in 2 switches to manually control the lock limit and unlock to test this before I wire in the lock actuator.

    I have 1 more question : I have added some code to measure the time it takes for the limit inputs to be received once the motor has begun. This is currently being displayed on the debug terminal as "Lock Time" and "Unlock Time" for each cycle. My goal is to plot the time versus cycles in excel. As the motor wears over 30,000 lock/unlock cycles, we will see the times grow longer and longer. I want to compare this curve to the OEM lock actuator and use it for my analysis.

    Is there some way to export the debug terminal information to excel? I have tried to cut/paste the results to notepad but Windows XP isnt letting me copy anything out of debug terminal. I can highlight but thats it. I could add some formatting to debug terminal to get the results to show in columns on debug screen, but if I cannot copy/paste, how will I bring this into excel?

    Paul
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-17 07:01
    Look at either PLX-DAQ or StampPlot Pro on the Parallax Downloads page under Stamp Software. Both are PC programs that your Stamp program can talk to and will import data to an Excel spreadsheet (PLX-DAQ) or plot data and save it to a file among other things (StampPlot Pro).
Sign In or Register to comment.