Shop OBEX P1 Docs P2 Docs Learn Events
propb ?: need help adding a cog to make four lines run at same time..totally lost..? — Parallax Forums

propb ?: need help adding a cog to make four lines run at same time..totally lost..?

cazman128cazman128 Posts: 25
edited 2011-05-17 11:42 in Propeller 1
Hello...my code for my senior project is almost perfect, which a due date looming i need some help..

My code is for two motors to run off a r/c transmitter. The pulses are perfect..the only problem now is the two motors will not run at same time...i need helping writing code for this portion.. : "question at bottom"
CODE:

PULSIN CHa, 1, X
PULSIN CHb, 1, Y

PAUSE 18

IF X < 1400 THEN M1R
IF X > 1600 THEN M1F
IF Y > 1625 THEN M2F
IF Y < 1375 THEN M2R

PAUSE 18

GOTO START

PAUSE 18

M1F:
LOW MA
HIGH MB
PAUSE 18
GOTO START

M1R:
LOW MB
HIGH MA
PAUSE 18
GOTO START

M2F:
LOW MC
HIGH MD
PAUSE 18
GOTO START

M2R:
LOW MD
HIGH MC
PAUSE 18
GOTO START

Question :
I need to be able to run the X and Y pulsins at same time as well the IF statements (2 for x, 2 for y) at sime time...and also need to run two of the four labels at same time.. ik you need to use cogs and such to make it work, this is where im totally lost with a due date looming, any help is appreciated....... i also attached the rest of the code to make it easier to read... THANKS :frown:

Thank you,,,,

CAZ
«1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-12 20:29
    You've written your PropBasic program just as if it were a PBasic program for a Stamp. It runs in one cog and, just like a Stamp, can only do one thing at a time. You need to separate the program into two parts, one part that just deals with CHa and motor #1 and another part that just deals with CHb and motor #2. The second part you'll need to define as a TASK and you'll need to use COGSTART to start it up (or the AUTO option on the TASK statement). The default initial task (your main program) will continue to run and will deal with CHa and motor #1.

    Sorry about the looming due date, but this is a senior project. You're supposed to have come up with a test version early enough to figure out that it didn't work as expected. You'll have to come up with the new version yourself. It's a simple enough program and PropBasic takes care of a lot of the work for you.
  • cazman128cazman128 Posts: 25
    edited 2011-05-12 20:30
    Mike Green wrote: »
    You've written your PropBasic program just as if it were a PBasic program for a Stamp. It runs in one cog and, just like a Stamp, can only do one thing at a time. You need to separate the program into two parts, one part that just deals with CHa and motor #1 and another part that just deals with CHb and motor #2. The second part you'll need to define as a TASK and you'll need to use COGSTART to start it up (or the AUTO option on the TASK statement). The default initial task (your main program) will continue to run and will deal with CHa and motor #1.

    Wow, deja vu, my teacher pretty much said same thing today, haha.. thankss for the start!
  • cazman128cazman128 Posts: 25
    edited 2011-05-12 20:34
    We did start the code a little late, we decided to do all the mechanical work first, which wasn't so bad because all we gotta do is plop this on and mount it and we are all set, Thanks for all the help, we will see how it goes
  • cazman128cazman128 Posts: 25
    edited 2011-05-13 06:38
    I was able to write some code, could you possibly take a look and see whats wrong..it doesnt have syntax errors but only the first motor works not the one in the task...


    thanks....
    CAZ
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-13 06:50
    First, some simple questions:
    Why the PAUSE 18 between the GOTO START and the M1F:?
    Why the GOTO MOTOR2CODE just before the ENDTASK?

    The BIG question:
    Why the COGSTART MOTOR2? That's executed every time your main loop executes. What effect would that have? What about the AUTO option?

    Hmmm. "Just plop this on", eh?

    Hmmm. "Deja vu", eh?
  • cazman128cazman128 Posts: 25
    edited 2011-05-13 08:13
    Mike Green wrote: »
    First, some simple questions:
    Why the PAUSE 18 between the GOTO START and the M1F:?
    Why the GOTO MOTOR2CODE just before the ENDTASK?

    The BIG question:
    Why the COGSTART MOTOR2? That's executed every time your main loop executes. What effect would that have? What about the AUTO option?

    Hmmm. "Just plop this on", eh?

    Hmmm. "Deja vu", eh?


    good question, i kind of just used pauses in case it got bogged down, i was using a pdf file on syntax and on propbasic to write the code for cogs and it said to use AUTO if you want it to run at start of program, it also said COGSTART task to start the cog, its all confusing to me
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 04:33
    Mr. Green i also tried getting rid of the auto and keeping the COGSTART with no luck, i also got rid of the random pauses. The syntax is correct its just not working, could there not be enough power? i cant figure it out. Or is the code still wrong. ?

    Thanks everyone
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-16 06:19
    You haven't included your latest version of the program, so I'm just guessing from your last comment, but "Why the COGSTART MOTOR2"? That's executed every time your main loop executes. What happens when the COGSTART executes? What does COGSTART do? What happens when it's executed again as the main loop repeats?
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 07:07
    Mike Green wrote: »
    You haven't included your latest version of the program, so I'm just guessing from your last comment, but "Why the COGSTART MOTOR2"? That's executed every time your main loop executes. What happens when the COGSTART executes? What does COGSTART do? What happens when it's executed again as the main loop repeats?


    i read in the pdf to add COGSTART task to make it run, and my task is called MOTOR2, so what you mean is when i define my task in begining of program it runs?? so i dont need to have COGSTART??
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-16 07:20
    Throw out the COGSTART. You don't seem to understand how it works. Consider reading the Propeller Manual to learn. What's your other choice for starting up tasks?

    Get used to attaching your current version of your program to your messages.
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 07:30
    ok when i get to class at 12 00 i will try that and see how it goes. Thanks
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 09:35
    this is what i have so far and i cant get the task to run at all, that second motor will not move, the first one runs perfect.....
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 09:52
    Task, cognew, etc, start a new Cog, if you continuously start cogs, as you are, you will run out of cogs fast. you end up with 7 cogs doing the same thing. Looking at your code, were is the Motor2 label? Maybe something to do with TASK, I never use the TASK keyword.
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 09:55
    Task, cognew, etc, start a new Cog, if you continuously start cogs, as you are, you will run out of cogs fast. you end up with 7 cogs doing the same thing. Looking at your code, were is the Motor2 label? Maybe something to do with TASK, I never use the TASK keyword.


    The motor2 is the code, i only need two cogs, one for the first part and another for motor2 task, i got like two hours left....
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-16 10:02
    What's the difference between the main loop and the MOTOR2 loop? The code you've written for those two looks different. Why?

    What does AUTO do?
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 10:09
    Mike Green wrote: »
    What's the difference between the main loop and the MOTOR2 loop? The code you've written for those two looks different. Why?

    What does AUTO do?

    The motor2 task is for the second motor while anything before task motor2 is the first motor that works perfect, also the m2c label is so the goto mc2 in the other labels go to the start of the task.

    Thanks.... running out of time
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 10:13
    Duh: Did you notice that start is not part of the task for motor 2? You can not jump there if there is not in the cog to jump to.
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 10:15
    Ignore that last, you had already made that correction and I missed it.
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 10:17
    You are defining Mc and Md as outputs for the task running in the motor 1 cog, not as outputs for the code running in the motor 2 cog. Each cog has its own DIRA register that is used to determine what pins are outputs and inputs. Also you did not define CHb as an input for the motor 2 cog, only for motor 1.
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 10:34
    You are defining Mc and Md as outputs for the task running in the motor 1 cog, not as outputs for the code running in the motor 2 cog. Each cog has its own DIRA register that is used to determine what pins are outputs and inputs. Also you did not define CHb as an input for the motor 2 cog, only for motor 1.


    i read that cogs share outputs and inputs...
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 10:39
    cazman128:
    Yes they do share the inputs and outputs, BUT each one has its own definition register (DIRA) for the input/output use of the pins (the DIRection on port A).
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 10:44
    cazman128:
    Yes they do share the inputs and outputs, BUT each one has its own definition register (DIRA) for the input/output use of the pins (the DIRection on port A).

    OK i attached my code...now what..i need help badly....
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 10:53
    I normally will not write code for some one, though this is a simple mod. Hope you can learn form it.

    See if this works:
    DEVICE          P8X32A, XTAL1, PLL16X
    XIN             5_000_000
    
    
    
    MOTOR2 TASK
    MOTOR1 TASK
    
    PROGRAM Start
    
    Start:
    
    TASK MOTOR1
    Ma PIN 20 OUTPUT
    Mb PIN 19 OUTPUT
    CHa PIN 0 INPUT
    X VAR Long
    
    LOW MA
    LOW MB
    
    MOTOR1C:
    
    PULSIN CHa, 1, X
    
    IF X < 1400 THEN M1R
    IF X > 1600 THEN M1F
    
    
    M1F:
    LOW MA
    HIGH MB
    PAUSE 18
    
    GOTO MOTOR1C
    
    M1R:
    LOW MB
    HIGH MA
    PAUSE 18
    
    GOTO MOTOR1C
    
    ENDTASK
    
    
    TASK MOTOR2
    
    Mc PIN 17 OUTPUT
    Md PIN 16 OUTPUT
    CHb PIN 1 INPUT
    Y VAR Long
    
    LOW MC
    LOW MD
    
    MOTOR2C:
    
    PULSIN CHb, 1, Y
    
    IF Y > 1625 THEN M2F
    IF Y < 1375 THEN M2R
    
    
    M2F:
    LOW MC
    HIGH MD
    GOTO MOTOR2C
    
    M2R:
    LOW MD
    HIGH MC
    GOTO MOTOR2C
    
    ENDTASK
    
    
    
    
    
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 10:54
    All I did is put the loops back.
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 11:00
    If that does not work:

    This goes back to just using 2 cogs and restores your original structure, this should work if your original got one motor to work (why have a cog do nothing):
    DEVICE          P8X32A, XTAL1, PLL16X
    XIN             5_000_000
    
    
    
    MOTOR2 TASK
    
    PROGRAM Start
    
    Start:
    
    Ma PIN 20 OUTPUT
    Mb PIN 19 OUTPUT
    CHa PIN 0 INPUT
    X VAR Long
    
    Start2:
    LOW MA
    LOW MB
    
    
    PULSIN CHa, 1, X
    
    IF X < 1400 THEN M1R
    IF X > 1600 THEN M1F
    
    
    M1F:
    LOW MA
    HIGH MB
    PAUSE 18
    GOTO Start2
    
    M1R:
    LOW MB
    HIGH MA
    PAUSE 18
    GOTO Start2
    
    
    TASK MOTOR2
    
    Mc PIN 17 OUTPUT
    Md PIN 16 OUTPUT
    CHb PIN 1 INPUT
    Y VAR Long
    
    MOTOR2C:
    
    LOW MC
    LOW MD
    
    
    PULSIN CHb, 1, Y
    
    IF Y > 1625 THEN M2F
    IF Y < 1375 THEN M2R
    
    
    M2F:
    LOW MC
    HIGH MD
    GOTO MOTOR2C
    
    M2R:
    LOW MD
    HIGH MC
    GOTO MOTOR2C
    
    ENDTASK
    
    
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 11:10
    okkk thanks, ill have to try tomorrow i ran out of time today
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-16 11:21
    cazman128:
    Might I recommend having a Propeller board at home and the components that you are working with, so that you are not limited to the time in class?
  • cazman128cazman128 Posts: 25
    edited 2011-05-16 13:18
    cazman128:
    Might I recommend having a Propeller board at home and the components that you are working with, so that you are not limited to the time in class?


    I usually do do that but i just didnt have time today to pack upp and take it homee
  • cazman128cazman128 Posts: 25
    edited 2011-05-17 10:07
    If that does not work:

    This goes back to just using 2 cogs and restores your original structure, this should work if your original got one motor to work (why have a cog do nothing):
    DEVICE P8X32A, XTAL1, PLL16X
    XIN 5_000_000
    
    
    
    MOTOR2 TASK
    
    PROGRAM Start
    
    Start:
    
    Ma PIN 20 OUTPUT
    Mb PIN 19 OUTPUT
    CHa PIN 0 INPUT
    X VAR Long
    
    Start2:
    LOW MA
    LOW MB
    
    
    PULSIN CHa, 1, X
    
    IF X < 1400 THEN M1R
    IF X > 1600 THEN M1F
    
    
    M1F:
    LOW MA
    HIGH MB
    PAUSE 18
    GOTO Start2
    
    M1R:
    LOW MB
    HIGH MA
    PAUSE 18
    GOTO Start2
    
    
    TASK MOTOR2
    
    Mc PIN 17 OUTPUT
    Md PIN 16 OUTPUT
    CHb PIN 1 INPUT
    Y VAR Long
    
    MOTOR2C:
    
    LOW MC
    LOW MD
    
    
    PULSIN CHb, 1, Y
    
    IF Y > 1625 THEN M2F
    IF Y < 1375 THEN M2R
    
    
    M2F:
    LOW MC
    HIGH MD
    GOTO MOTOR2C
    
    M2R:
    LOW MD
    HIGH MC
    GOTO MOTOR2C
    
    ENDTASK
    
    

    When i use that code the motors do not move at all.....
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-17 10:15
    And the other?
Sign In or Register to comment.