Shop OBEX P1 Docs P2 Docs Learn Events
Please help me with cog control on my project!!! — Parallax Forums

Please help me with cog control on my project!!!

Tony_tsiTony_tsi Posts: 98
edited 2011-10-05 17:45 in General Discussion
This will be a easy fix for some one but I just cant figure it out. I am making a variable time/variable intensity light controller. I have notes in the code to tell where I need the cogs to be. lines 355-375 is where I need help.
«1

Comments

  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 18:11
    In the timer pub I need a accurate count down timer that shuts down the cog running the pwm when the clock reaches 00:00. It will need to use the variables time_m and time_s.
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-04 18:15
    Cog functions: To start a cog, enter:
    cogIdVariable := cognew(FunctionName,@stack)
    
    Where stack is a variable array of longs. This can be
    VAR
      long stack[100]
    
    Stack can be named anything, just change the label in cognew.

    To stop a cog, use
    cogstop(cogIdVariable}
    
    Any cogs you plan on stopping, make sure to get that variable when you start it. If you don't, you can use cogid to get the current cog the function is running in.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 18:19
    When I use cognew commands it always freezes my cog that launched the new cog.
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-04 18:20
    Here's a good timer routine that should do what you want:
    PUB Timer
      
      waitcnt(clkfreq + cnt)
      if time_s == 0 and time_m == 0
        cogstop(1)        ' Change this to the cog ID or the cog ID variable you want
      if time_s == 0
        time_s := 59
        time_m--
      else
        time_s--
    
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-04 18:23
    Tony_tsi wrote: »
    When I use cognew commands it always freezes my cog that launched the new cog.

    Are you calling functions from an object started in another cog? For example, if you start the PWM object with Cog 0, then you cannot call functions from the PWM object from Cog 1. You can get around this by starting the other cog (in this example, the PWM object) in the cog you need the functions.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 18:27
    For example i just put cognew(PWM_C, @STACK) in line 356. When it reaches line 356 nothing happens when I push the back button ina[1] instead of going back it starts the pwm_c cog then I am stuck and the back button will not work.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 18:43
    If put cognew(pwm_c, @stack[100]) in line 356 it doesn't lock it up but is also doesn't start pwm_c.
    I made a long var called stack
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 19:01
    Timer works awesome.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 19:26
    WHY DOES THIS WORK?

    I made some changes to the code.
    the timer cog works it is launched in line 360.
    the pwm cog does not work it is launched in line 359.
    they use the same command how come one works and the other doesn't?
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-04 20:56
    If I can figure out why line 359 does not work I will be done with my prototype.
    I have tried every thing I know and everything I could find in my book. I give up for tonight.
    .
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-05 05:45
    The command "outa" works with true or false, not 1 or 0 like ina. (at least to my understanding) Replace "outa [5] :=1" with "outa[5]~~" and "outa[5] :=0" with "outa[5]~". The last ones are the same, but you might as well change it for convention. Keep in mind: True is not 1, but rather -1, so it would freeze up because it is an invalid statement.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 08:51
    I will try this when I get out of class, however the pwm_c is the same as the test pub and the test pub works without a prolbem. That leads me to believe it is a cog prolbem instead of a prolbem in the pub.
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-05 08:57
    I've just spotted another vital problem: You set dira[5] to an input, and then are trying to use it as an output. To set it to an output, you would have to use "~~" not "~" !
    Doing this can freeze up your cog or even the whole chip. If this is not the problem, I don't know what is. The cognew commands should work fine.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 09:05
    I seen that last night changed it to ~~ still did not work. Just a thought I don't understand how this works but both PWM_ON and PWM_OFF are long values the cog is trying to use both values but only has a long to use its self so it is trying to put two long varables in its little run space that is only equal to one long. Hope this makes sense. Is this a possible problem?
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 10:16
    The timer pub uses 2 bytes so they will fit in the long that the cog is running in and still allow for the extra space to do the math. ?I think?
    so if I made pwm_on and pwm_off bytes instead of longs it might work
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 14:18
    Its fixed but i don't know how.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 14:19
    How do I mark this thread solved?
  • kuronekokuroneko Posts: 3,623
    edited 2011-10-05 17:24
    Tony_tsi wrote: »
    How do I mark this thread solved?
    Before you do that (edit your top post in advanced mode) see if you can figure out this problem. How long does the following program run?
    PUB main
    
      main
    
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-05 17:25
    Click "Edit Post" at the bottom of your first post, click "Go Advanced" then change to solved and click "Save Changes".

    May I ask what you did to get it to work?
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-10-05 17:26
    kuroneko wrote: »
    Before you do that (edit your top post in advanced mode) see if you can figure out this problem. How long does the following program run?
    PUB main
    
      main
    

    I don't know what code you are looking at, but I can't see it in the one he just uploaded, nor the old one.....
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 17:28
    kuroneko wrote: »
    Before you do that (edit your top post in advanced mode) see if you can figure out this problem. How long does the following program run?
    PUB main
    
      main
    

    Forever???
  • kuronekokuroneko Posts: 3,623
    edited 2011-10-05 17:29
    I don't know what code you are looking at, but I can't see it in the one he just uploaded, nor the old one.....
    It's called an example. But if you insist:
    pub [COLOR="blue"]main [/COLOR]     
                       
        ...
        lcd.putc(1)
        waitcnt(clkfreq/4 + cnt)
        lgt := 10
        repeat
          
         if ina [1] == 0
           manual
    
         if ina [2] == 0
           [COLOR="red"]auto[/COLOR]
                       
    pub [COLOR="red"]auto[/COLOR]
    
        ...
        lcd.putc(1)
        waitcnt(clkfreq/4 + cnt)
       
        repeat
         
         if ina [1] == 0
          [COLOR="blue"]main[/COLOR]
          
         if ina [2] == 0
          select_auto 
    
  • kuronekokuroneko Posts: 3,623
    edited 2011-10-05 17:30
    Tony_tsi wrote: »
    Forever???
    No, that's the problem. Method calls are not like GOTO but GOSUB, i.e. the have to return. Otherwise these calls gobble up stack and your program eventually just stops (best case).
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 17:33

    May I ask what you did to get it to work?

    I think it was fixed when I put dira[5]~~ in the pub pwm_c but i am not entirely sure. do you have to list your dira in each cog.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 17:34
    kuroneko wrote: »
    No, that's the problem. Method calls are not like GOTO but GOSUB, i.e. the have to return. Otherwise these calls gobble up stack and your program eventually just stops (best case).

    So instead of telling it to go to main i should tell it to end??
  • kuronekokuroneko Posts: 3,623
    edited 2011-10-05 17:37
    Tony_tsi wrote: »
    So instead of telling it to go to main i should tell it to end??
    You may have to restructure your program a bit (ATM you jump all over the place) but that's effectively it. Simply exit the method (optionally with a result) so it returns to the caller. E.g. in post #23, instead of calling main in auto either call quit (exits repeat loop and then the method) or return which just exits.
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 17:40
    kuroneko wrote: »
    It's called an example. But if you insist:
    pub [COLOR="blue"]main [/COLOR]     
                       
        ...
        lcd.putc(1)
        waitcnt(clkfreq/4 + cnt)
        lgt := 10
        repeat
          
         if ina [1] == 0
           manual
    
         if ina [2] == 0
           [COLOR="red"]auto[/COLOR]
                       
    pub [COLOR="red"]auto[/COLOR]
    
        ...
        lcd.putc(1)
        waitcnt(clkfreq/4 + cnt)
       
        repeat
         
         if ina [1] == 0
          [COLOR="blue"]main[/COLOR]
          
         if ina [2] == 0
          select_auto 
    

    What exactly is the problem here?

    so what if i replace if ina [1] == 0 main with if ina [1] == 0 exit
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 17:41
    Is exit a real command?
  • kuronekokuroneko Posts: 3,623
    edited 2011-10-05 17:43
    Tony_tsi wrote: »
    Is exit a real command?
    Either use quit or return (see edit in #27).
  • Tony_tsiTony_tsi Posts: 98
    edited 2011-10-05 17:43
    Tony_tsi wrote: »
    Is exit a real command?

    Just seen your post about quit and return.
Sign In or Register to comment.