Machine Automation Program - need peer feedback...


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    76

    Default Machine Automation Program - need peer feedback...

    I am a using PicBasic Pro to program machines at work. I have no peers around me that I can lean on for support or ideas. I know I'm not very good at this stuff and I learn best when there are others to tell me when there's a better way.

    I have attached a snippet of code as an example of how I am controlling a two-sided machine. The machine shares two stepper motors in movement and actuation. Otherwise it is mostly pneumatic controls and electric motors.

    The machine works and is producing parts but I have some bugs to work out and still think it could be done a lot better by someone that knows what they are doing. :-)

    The example below is for your critique. Beat it up please! I need feedback. I have not bothered to put in all of the up front stuff like naming of variables and such, it is merely to show a flow of machine operation.

    Thank you!

    Ross



    Code:
    '****************************************************************
    '*  Name    : Machine Operation Example.BAS                                                      *
    '*  Author  : Ross Freeman                                                                               *
    '*  Date    : 4/18/2013                                                                                    *
    '*  Version : 1.0                                                                                             *
    '****************************************************************
    
    'I just finished the mechanical design of a fairly complex machine that
    'automates a tedius process in the assembly of components in production.
    
    'I do not have any peers in the area of programming and do this as a hobby only
    'and seldom even as that. I have an electronics guru that helps me in that 
    'department. My expertise is in mechanical design but I cross into all areas.
    
    'I need critical feedback on my programming. What am I doing right and what am
    'I doing wrong. Specifically, I am operating multiple stepper motors at the 
    'same time in different areas of the machine. 
    
    'Currently, the program loops continuously at high speed through MAIN. The 
    'step operations go through branch instructions and back to MAIN. To send fifty
    'pulses to a stepper motor I loop through Main 50 times. Only one pulse is sent
    'during that step per loop. This means loops through MAIN must be fast and 
    'timing is critical. But if I do it carefully, I can control several motors
    'on each loop. Speed control for each stepper is done through loops within its
    'individual branch operation. (I hope that made sense)
    
    'To put it another way, I can accelerate or decelerate for each individual
    'stepper without slowing down loops through MAIN.
    
    'In this example, the machine is assembling components on two sides of the
    'machine and sharing stepper motors between them.
    
    'My Question: Is this how it is done??? Or am I out in left field? 
    
    'I have been sucessful on this project and wrote 35 pages of code in the
    'process. That's the longest program I've ever written and it has some bugs I'm
    'still working out... but we're making product and the operators love the 
    'machine.
    
    'Please critique my programming. This is a simple example of how I control the
    'machine below... I've not bothered to name variables and such... this is just
    'for discussion. THANK YOU!!! 
    
    Main:
            
            gosub left_side_operation
            Gosub right_side_operation
            gosub get_inputs
            gosub update_outputs
            gosub status
            goto main
            end
            
    Left_Side_Operation:
            
            while left_side_step <> 0
            branch left_side_step, [lstep_0,lstep_1,lstep_2,lstep_3]
            wend
            if start_button_pushed = yes and op_side = 0 then
            left_side_step = 1
            return
            
    LStep_0:
            
            Return
            
    LStep_1:
    
            'do these things
            left_side_step = 2
            return
            
    LStep_2:
    
            'do these things
            left_side_step = 3
            Return
            
    LStep_3:
    
            'do these things
            left_side_step = 0
            return
            
    Right_Side_Operation:
    
            while right_side_step <> 0
            branch right_side_step, [rstep_0,rstep_1,rstep_2,rstep_3]
            wend
            if start_button_pushed = yes and op_side = 1 then
            right_side_step = 1
            return
            
            
    RStep_0:
            
            Return
            
    RStep_1:
    
            'do these things
            left_side_step = 2
            return
            
    RStep_2:
    
            'do these things
            left_side_step = 3
            Return
            
    RStep_3:
    
            'do these things
            left_side_step = 0
            return
            
    Get_Inputs:
    
            'This is where I watch for sensors, switches, push_buttons, interlocks
            Inputs = portb
            return
            
    Update_Outputs:
    
            'This is where I turn on relays or solenoids, send pulses to steppers, 
            'alert the operator with lights or buzzers, or send info to the LCD
            Return
            
    Status:
    
            'This is where I determine what to do with changing input signals 
            'such as an operator pushing the start button or the pressure switch
            'sensing low or no pressure...


    Attachment 6975
    Last edited by rossfree; - 18th April 2013 at 19:55.
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  2. #2
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Theres alot of jumping around, half your code is gosubs and returns. its confusing me. and then theres all these steps, i'm wondering if your trying to do a texas 2 step dance.. is there a reason your using loops to control speed? you could do the same thing using an adjustable pause at the end of your gosubs in main.
    Chris


    Any man who has accomplished anything in electronics at one time or another has said... " STOP! WAIT! NOOO! Dangit.... Oh Well, Time to start over..."

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Your coding style looks good to me. Clean and precise. To each his own

    If I had to code this, I would probably avoid the 'branch'es and use a 'select case' right inside the Left side / right side operations routines. Reason being, there is very little else that you're doing in the branches.

    Another thing you can consider is using a table to fire the stepper motors instead of using the step0,1,2,3 idea. In the table you could just load the 4 bits of the stepper which you need to load at every step. It will probably save you the select case and the branch too. I won't elaborate on the idea. You can figure it out yourself.

  4. #4
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    76


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Quote Originally Posted by wdmagic View Post
    Theres alot of jumping around, half your code is gosubs and returns. its confusing me. and then theres all these steps, i'm wondering if your trying to do a texas 2 step dance.. is there a reason your using loops to control speed? you could do the same thing using an adjustable pause at the end of your gosubs in main.
    The point of this code example is to show how I am accomplishing multiple movements at the same time. Quite frankly I don't know another way and would like to know if others are doing it differently. With this code I can control multiple steppers at the same time. (or at least they will all look like they are moving at the same time).

    I am constantly looping through Main: On each loop I update inputs and outputs. I also operate things going on on the left and right sides of the machine. The left side could be on step seven, the right could be on step two. With the way I have organized it, it is very easy to add steps and move from one to another by simply adding additions to the branch statements.

    Conditions are built in to the "Left_Side_Operation" and "Right_Side_Operation" routines. They allow or disallow access to the Branch statements and subsequently to the "steps".

    One important objective was to maintain a high loop speed. When it came time to send pulses to stepper motors I didn't want them to crawl along.

    Thank you for your feedback.

    Ross
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  5. #5
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    76


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Hi Jerson,

    Thank you for your feedback.

    I don't have enough experience to know why I would use 'select case' over 'branch'. Is there a speed gain? Keep in mind my example is somewhat bare bones. I really appreciate your comments. Could you elaborate on the table to fire the stepper motors? I currently output pulses to a stepper controller off-board. I send pulse and direction only.

    Thank you,

    Ross
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Hi Ross

    I suggested a 'select case' simply because there is not much you are doing after 'branch'ing to the subroutine. You seem to be just incrementing the step variable and going back. That would be very wasteful speedwise because a branch usually takes longer to execute. I am not sure if a select case saves time; haven't checked.

    For the table approach, I usually keep the 4 bit stepper values in a table. These 4 bits are jammed to a port that drive the stepper coils. So, a typical step table would have values like
    HalfStep=[0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1000,0 b1001];

    Sorry, I do not have a PBP example at hand, so, I have given you a quick and easy 'C' table. At each step, you load the next value from the table and jam it out to the port. This table shows values to drive a stepper motor in a half step sequence. For full stepping, you just use every alternate value.

    Hope this helps

    Jerson

    PS: have you read through this thread? It might have something useful to you
    http://www.picbasic.co.uk/forum/showthread.php?t=101
    Last edited by Jerson; - 19th April 2013 at 16:05.

  7. #7
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    76


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Thanks again Jerson,

    I thought that's what you meant by a stepper coil table... but I have no need for that.

    The steps that I refer to in the 'BRANCH' command, are process steps in my machine... such as the following example: Step_1... raise sealing bar, Step_2... turn on suction cup vacuum, Step_3... turn Y stepper motor 1200 pulses CCW, Step_4... turn X stepper motor 3400 steps CW... etc. Keep in mind that the pulses on Step_3 are sent one pulse at a time per loop.

    I've re-written some code to illustrate. See below: (this differs from my original posted code)

    In step 2, I set variable i to the value 100 and increment the step to LStep_3. Then I loop back to MAIN.

    When I next BRANCH, I will wind up at LStep_4 and send one pulse to the the stepper controller (off board).
    Since the WHILE statement is true, the program decrements i by one and returns (to MAIN). It will continue to branch to
    LStep_4 sending one pulse at a time until the WHILE statement is no longer true, then set left_side_step = 5 and return to MAIN.

    Note: I haven't shown any acceleration or deceleration in this code. That bit is complicated. :-P


    Code:
    LStep_2:
    
            'do these things
            left_side_step = 3
            i = 100             'set variable i for the number of pulses in step
            Return              'three. (placed here for readability)
            
    LStep_3:
    
            pulsout portb.7,4   'send out one pulse to stepper controller
            while i <> 0          'while statement is true, decrement i by one and
            i = i -1                'return to main (loop) Continue sending one pulse
            return                 'each time the program loops here until the 'while'
            wend                  'statement is false... then increment 'left_side_step
            left_side_step = 4  'to LStep_4
            return
    Thank you for taking the time to look at this with me!

    Ross
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  8. #8
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Machine Automation Program - need peer feedback...

    Well, now that makes sense and seems justified.

Similar Threads

  1. Insteon Home Automation Software Development Kit
    By Stuartk in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 23rd November 2013, 17:52
  2. Replies: 3
    Last Post: - 26th November 2012, 11:20
  3. Automation project need help
    By microcnc05 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 5th January 2010, 03:17
  4. pic to pic to pic as peer to peer to peer
    By Geezer in forum General
    Replies: 1
    Last Post: - 5th September 2008, 06:53
  5. Replies: 8
    Last Post: - 8th May 2007, 11:15

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts