• AVR Freaks

Hot!Logic for turning on outputs

Author
jalind
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2020/04/26 18:34:37
  • Location: 0
  • Status: offline
2021/03/01 20:07:50 (permalink)
0

Logic for turning on outputs

I have a project where I'm trying to monitor 7 inputs (status input - high when motor is available to run , low when motor is offline ) I would like to run 2 motors at a time even if all of them are available to run. They are setup so motor 1,2,3 are on one supply (A) and 4,5,6,7 are on another supply (B). Id like to run them in pairs for example motor 1 and motor 4, or 2 and 5, or any combination of one from supply A and 1 from supply B. I need to output a high signal back to the motor controller to enable it to run.

Any suggestions on the easiest/best way to setup the code to accommodate a setup like this?
#1

19 Replies Related Threads

    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/01 20:16:29 (permalink)
    0
    What PIC?
    What language?
    This sounds like a pretty straight forward exercise, but I suspect there are more conditions about which motor to select that you haven't mentioned yet.
    You also have not mentioned how you are communicating with this "motor controller". Is it just one PIC GPIO pin per motor? Are they all on the same PIC port?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/01 20:28:52 (permalink)
    0
    I'm using a PIC18F26k22 with inputs being RA2 thru RC0 (Pin 4-11) and outputs RC1 thru RC7 (pin 12 thru 18). Programming in C using mplab. My original thought on it was a slew of if statements and overall was turning into a complete mess. I feel it should be very simple yet I can’t think how to get it accomplished. There are 2 pins per controller, one that is a status that is 5v when the motor is able to be run and one that looks for a 5v signal to actually make it run.
    #3
    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/01 20:30:52 (permalink)
    0
    What happens if there is no motor available in one bank or the other?
    If more than one available, just pick the first, or try to randomise it or use them sequentially?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/01 20:43:00 (permalink)
    0
    I think this is were I started really going down the rabbit hole of over complication. If one bank has none available I’ll just run one from the other bank until one becomes available. Originally I was going to go for randomizing to help spread how often a particular motor or pair gets used.
    #5
    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/01 20:43:20 (permalink)
    -1 (1)
    Something like this?
    (often brute force is the simplest way to code...)
    #define M1_RDY PORTAbits.RA2
    #define M2_RDY PORTAbits.RA3
    #define M3_RDY PORTAbits.RA4
    #define M4_RDY PORTAbits.RA5
    #define M5_RDY PORTAbits.RA7
    #define M6_RDY PORTAbits.RA6
    #define M7_RDY PORTCbits.RC0

    #define M1_ON LATCbits.LATC1
    #define M2_ON LATCbits.LATC2
    #define M3_ON LATCbits.LATC3
    #define M4_ON LATCbits.LATC4
    #define M5_ON LATCbits.LATC5
    #define M6_ON LATCbits.LATC6
    #define M7_ON LATCbits.LATC7

    //handle bank 1
        if (M1_RDY)
        {
            M1_ON = 1;
            M2_ON = 0;
            M3_ON = 0;
        } else
        {
            if (M2_RDY)
            {
                M1_ON = 0;
                M2_ON = 1;
                M3_ON = 0;
            } else
            {
                if (M3_RDY)
                {
                    M1_ON = 0;
                    M2_ON = 0;
                    M3_ON = 1;
                } else
                {
                    //oops no motors available
                }
            }
        }
    //handle bank 2
        if (M4_RDY)
        {
            M4_ON = 1;
            M5_ON = 0;
            M6_ON = 0;
            M7_ON = 0;
        } else
        {
            if (M5_RDY)
            {
                M4_ON = 0;
                M5_ON = 1;
                M6_ON = 0;
                M7_ON = 0;
            } else
            {
                if (M6_RDY)
                {
                    M4_ON = 0;
                    M5_ON = 0;
                    M6_ON = 1;
                    M7_ON = 0;
                } else
                {
                    if (M7_RDY)
                    {
                        M4_ON = 0;
                        M5_ON = 0;
                        M6_ON = 0;
                        M7_ON = 1;
                    } else
                    {
                    //oops no motors available
                    }
                }
            }
        }   


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/01 20:56:47 (permalink)
    0
    This does seem very straight forward. I’m not sure what exactly I was trying to do now that you posted that. I was laying out all the possible combinations instead of a simple pick one if it’s not ready pick the next.... thanks!
    #7
    1and0
    Access is Denied
    • Total Posts : 12246
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Logic for turning on outputs 2021/03/01 21:34:23 (permalink)
    0
        WREG = 0;

             if (PORTAbits.RA2) WREG |= 0b00000010;
        else if (PORTAbits.RA3) WREG |= 0b00000100;
        else if (PORTAbits.RA4) WREG |= 0b00001000;

             if (PORTAbits.RA5) WREG |= 0b00010000;
        else if (PORTAbits.RA6) WREG |= 0b00100000;
        else if (PORTAbits.RA7) WREG |= 0b01000000;
        else if (PORTCbits.RC0) WREG |= 0b10000000;

        LATC = WREG;

    #8
    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/01 22:07:10 (permalink)
    +1 (1)
    I avoided that sort of optimisation to allow the output pins to be reassigned to any pin easily. :)

    I would guess that
    WREG |= 0b00000010;
    and
    WREG = 0b00000010;
    Would both compile to a single instruction,



    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/01 22:07:10 (permalink)
    0
    I avoided that sort of optimisation to allow the output pins to be reassigned to any pin easily. :)

    I would guess that
    WREG |= 0b00000010;
    and
    WREG = 0b00000010;
    Would both compile to a single instruction,



    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    1and0
    Access is Denied
    • Total Posts : 12246
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Logic for turning on outputs 2021/03/01 22:17:05 (permalink)
    0
    ric
    I avoided that sort of optimisation to allow the output pins to be reassigned to any pin easily. :)

    Can always use #define symbols  ;)  I see your point. ;)
     

    I would guess that
    WREG |= 0b00000010;

    ... will compile to a single BSF instruction.
     

    and
    WREG = 0b00000010;
    Would both compile to a single instruction,

    ... may not compile to a single instruction, as I've seen it does MOVLW k/MOVWF WREG.  LOL!
     
    <edit> I have even seen
    WREG = 0;

    compile to
    movlw   0
    movwf   WREG

    Go figure! ;)
    post edited by 1and0 - 2021/03/01 23:13:16
    #11
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 01:10:01 (permalink)
    0
    Ok, so I thought this made sense running through how the code would execute. However when I loaded the code I end up with all the motors running. If one sends the offline signal it turns of the run command to that particular motor.
    #12
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 01:20:42 (permalink)
    0
    I’m crazy, I only updated the code on one motor controller not all 7. Of course it doesn’t work when the control isn’t outputting a signal or looking for one to be present!
    #13
    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/02 05:21:19 (permalink)
    0
    So all good now?

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    Murton Pike Systems
    Super Member
    • Total Posts : 247
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 06:25:07 (permalink)
    +1 (1)
    ric
    So all good now?


    Just need to send him the bill now Ric !
     
    #15
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 07:28:38 (permalink)
    0
    Yes, all seems to work switching the next on when one becomes unavailable. Is there a way to not automatically go back to motor 1 as soon as it becomes available?
    #16
    Jan Audio
    Super Member
    • Total Posts : 192
    • Reward points : 0
    • Joined: 2018/09/24 08:12:24
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 08:17:52 (permalink)
    0
    Programming cost time because all the testing.
    These things are simple if you can program, you still have to test all possibilities of the code, it cost time.
    I would not recommend copying cheap forum code.
     
    You start with filling understandable variables in your current code, then act on that.
    char numMotorsRunning = ?
    char currentMotorsRunning;
    etc etc, just make your own text.
     
    #17
    ric
    Super Member
    • Total Posts : 30197
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Logic for turning on outputs 2021/03/02 12:20:01 (permalink)
    0
    jalind
    ... Is there a way to not automatically go back to motor 1 as soon as it becomes available?

    Of course, it just takes more logic in the code.
    For that, you have to sit down and decide WHAT you want it to do.
    That is the sort of thing I was after in post#4. You have to decide what you want it to do BEFORE you try to code it up.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #18
    PStechPaul
    Super Member
    • Total Posts : 3046
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 14:32:00 (permalink)
    0
    You could start with a set of possible motor combinations, try each one in sequence, and advance to next pattern if the arrays do not match. Following code is just a general idea:

    // You will have to change these and set MAXPATTERN accordingly
    unsigned char   motors_on[] = {0x03, 0x05, 0x09, 0x0b, ..., 0xc0};
    unsigned char   motors_ready[] = {0x03, 0x05, 0x09, 0x0b, ..., 0xc0};
    MAXPATTERN = sizeof( motors_on );
    int  pattern = 0;
    while(1) {
    while (motors_ready[pattern] = motors_on[pattern]) { // desired motors running
      motors_ready[pattern] = ( PORTA | 0b001111111 ) | ( ( PORTC & 1 ) << 6 ); }
    LATC = 0; // motors off
    if( pattern < MAXPATTERN )
      pattern++;
    else
      pattern = 0;
    // You may add code to verify if the selected pattern is viable, and if not, advance to next pattern.
    LATC = motors_on[pattern];
    }

     

     
    #19
    jalind
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2020/04/26 18:34:37
    • Location: 0
    • Status: offline
    Re: Logic for turning on outputs 2021/03/02 17:32:20 (permalink)
    0
    This was along the lines of the route I started with, it just started getting very messy. I have a few ideas now to ponder on. Thank you all for your input, it’s much appreciated.
    #20
    Jump to:
    © 2021 APG vNext Commercial Version 4.5