Shop OBEX P1 Docs P2 Docs Learn Events
Help Please. Loop doing strange things — Parallax Forums

Help Please. Loop doing strange things

archerbrad700archerbrad700 Posts: 18
edited 2011-10-20 05:42 in Propeller 1
I have been troubleshooting this code for some time now but to now avail. I hate to bother the forum with this but I'm at a standstill.

The code scans 4 push-buttons and starts a new method on a specified cog hen the conditions are met. The code works only when I comment out any of the cognew, coginit, and cogstop commands. The code works if I do everything in the same cog but I as soon as I try a new cog the loop will spit out a weird symbol and stop working altogether.
PUB Main
{{Senses pins XPlus, XMinus, YPlus, YMinus logic state to control
outputs on pins XDir, XStep, YDir, YStep}}

  Debug.Start(9600)

  waitcnt(clkfreq/8 + cnt)

  Debug.Str(String("Program is running..."))
  Debug.NewLine

  XCount := 50                  'Set XCount variable to 50
  YCount := 50                  'Set YCount variable to 50
                                
  dira[XDir..XStep]~~           'Set stepper control pins as inputs
  dira[YDir..YStep]~~           'Set stepper control pins as inputs
  dira[0..3]~~                  '-----Set test LED pins as inputs
  dira[25..27]~~

  !outa[0..3]                   '-----Test the LED outputs by cycling once at one Hz
  !outa[25..27]
  waitcnt(clkfreq/2 + cnt)
  !outa[0..3]
  !outa[25..27]

'End of initialization

  repeat                                                                        'Loop scans input pins for current state
    if (ina[XPlus] == 1) and (ina[XMinus] <> 1) and (XCount < XMAX)             'If button is depressed, without the other button being pressed and XCount is not at its max value, activate the corresponding method
      'cogstop(1)
      debug.str(String("Step X CW"))
      debug.newline                                                             'Corresponding method started in a new cog
      'coginit(1, motorStep(CW, XStep), @Stack1)
      'cognew(motorStep(CW, XStep), @Stack1)              
      XCount++
      Debug.Dec(XCount)
      Debug.NewLine
      'waitcnt(clkfreq/5 + cnt)
                         
    elseif (ina[XMinus] == 1) and (ina[XPlus]  <> 1) and (XCount > XMIN)
      'cogstop(1)
      debug.str(String("Step X CCW"))
      debug.newline 
      'coginit(1, motorStep(CCW, XStep), @Stack1)
      'cognew(motorStep(CCW, XStep), @Stack1)
      XCount--
      Debug.Dec(XCount)
      Debug.NewLine
      'waitcnt(clkfreq/5 + cnt)                           
  
    if (ina[YPlus]  == 1) and (ina[YMinus] <> 1) and (YCount < YMAX)
      'cogstop(2)
      debug.str(String("Step Y CW"))
      debug.newline
      'coginit(2, motorStep(CW, YStep), @Stack2)
      'cognew(motorStep(CW, YStep), @Stack2)
      YCount++
      Debug.Dec(YCount)
      Debug.NewLine
      'waitcnt(clkfreq/5 + cnt)         
      
    elseif (ina[YMinus] == 1) and (ina[YPlus]  <> 1) and (YCount > YMIN)
      'cogstop(2)
      debug.str(String("Step Y CCW"))
      debug.newline
      'coginit(2, motorStep(CCW, YStep), @Stack2)
      'cognew(motorStep(CCW, YStep), @Stack2)
      YCount--
      Debug.Dec(YCount)
      Debug.NewLine
      'waitcnt(clkfreq/5 + cnt)            

I have tried every way I could think of to start the "motorStep" method on another cog but I just can't get it to work. Thanks in advance for your help

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-10-19 16:42
    From the looks of it you're running the Parallax Serial Terminal object. This object - when active - consumes a cog (PASM driver). Which simply means one of your cogstop commands is killing the terminal driver. If you absolutely must stop running cogs then record the ID returned from cognew and stay away from everything else. Also, your output pins are set in the context of (I'm guessing here) cog 0 which means the step method - if it's driving those pins - won't work in a different cog (every cog has its own dira/outa register pair).
  • 4x5n4x5n Posts: 745
    edited 2011-10-19 16:53
    Hard to tell from the code you posted but it appears that you set the direction of the pins in one cog and then try to run the motor from another. Do you set the pins to be outputs in the cog that's actually using the pin? Each cog has it's own dira register.
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-10-20 01:16
    Firstly, welcome to the forum.

    I hope you realise that it does take some time to start a cog. It has to load the code from hub and then begin executing.

    Since you have not posted all the code, it is hard to tell what is wrong. Likely it is what kuroneko has said.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-10-20 01:19
    Please post all of your code, it could be as simple as maybe not allocating enough stack space (or too much) for Stack1 etc
  • archerbrad700archerbrad700 Posts: 18
    edited 2011-10-20 05:42
    Thanks everyone for the help. Kuroneko hit the nail right on the head. I was stopping the Cog that was running the serial terminal and I didn't realize that I had to change the directory from the current cog. It works now but I still have a lot of refining to do. Thanks again.
Sign In or Register to comment.