Shop OBEX P1 Docs P2 Docs Learn Events
Cog problem — Parallax Forums

Cog problem

I can't get the LEFT and the RIGHT routines to both run at the same time.
The RIGHT routine works fine, but I can only get the LEFT routine to run by disabling the RIGHT routine.
To me they appear to be completely independent, but something is getting in the way.
CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000

var

long PWMstack,LeftStack,RightStack,a,b
PUB Main

cognew(Left,@Leftstack[3000] )         'Command the left wheels
cognew(Right,@RightStack[3000] )       'Command the right wheels
cognew(PWMCLocK,@PWMstack[90] )       'Internal PWM clock 

dira[3]~  'set the I/O ports 3,5,7,9, and 11 to inputs
dira[5]~
dira[7]~
dira[9]~
dira[11]~
dira[8]~~   'set the I/O ports 8,10,12, and 14 to outputs      
dira[12]~~
dira[10]~~
dira[14]~~
 
repeat         ' CheckingInputs

    if ina[5]==0     'Left forward on        
      outa[10]:=1      
      outa[14]:=0 
    if ina[7]==0    'Left reverse on    
      outa[10]:=0      
      outa[14]:=1       
    if ina[5]==1 and ina[7]==1  'Left forward off and left reverse off
      outa[10]:=1       
      outa[14]:=1
  
    if ina[9]==0     'Right forward on        
      outa[8]:=1      
      outa[12]:=0 
    if ina[11]==0    'Right reverse on    
      outa[8]:=0      
      outa[12]:=1       
    if ina[9]==1 and ina[11]==1  'Right forward off and right reverse off
      outa[8]:=1       
      outa[12]:=1
 

Repeat                 

PUB Right     'Control the right wheels
dira[4]~~
outa[4]~
dira[17]~~
outa[17]~
 
repeat
  b:=600 
    repeat while ina[9]==0 or ina[11]==0
      outa[17]~~ 
       
    '******* Ramp up to speed
      repeat while b<9000             ' steps per iteration.  9999 maximum pulse width
        b+=50               
        waitpeq(%0010, %0010,0)  'Waiting for the PWM clock to go high on I/O port 1 (0010) 
        outa[4]~~
        waitcnt(cnt+b)
        outa[4]~
      '***********  Travel  ************
               
      waitpeq(%0010, %0010,0)  'Waiting for the PWM clock to go high  
      outa[4]~~
      waitcnt(cnt+b)
      outa[4]~
     
        '****** Ramp down *****      
   repeat while b > 700
     b:=b-100 
     waitpeq(%0010, %0010,0) 'Waiting for the PWM clock to go high
     outa[4]~~
     waitcnt(cnt+b)
     outa[4]~
  outa[17]~


PUB Left     'Control the left wheels
dira[6]~~
outa[6]~
dira[16]~~

repeat
  a:=600 
    repeat while ina[5]==0 or ina[7]==0        
      
      outa[16]~~
    
    '******* Ramp up to speed
      repeat while a<9000             ' steps per iteration.  9999 maximum pulse width
       a+=50               
       waitpeq(%0010, %0010,0)  'Waiting for the PWM clock to go high on I/O port 1 (0010) 
       outa[6]~~
        waitcnt(cnt+a)
        outa[6]~
      '***********  Travel  ************
               
      waitpeq(%0010, %0010,0)  'Waiting for the PWM clock to go high  
      outa[6]~~
      waitcnt(cnt+a)
      outa[6]~
     
        '****** Ramp down *****      
  repeat while a > 700
     a:=a-100 
      waitpeq(%0010, %0010,0) 'Waiting for the PWM clock to go high
      outa[6]~~
      waitcnt(cnt+a)
      outa[6]~
  outa[16]~




'******* CLOCK *******
PUB PWMCLocK   
dira[1]~~
repeat
  outa[1]~~
  waitcnt(500 + cnt)    
  outa[1]~
  waitcnt(20000+cnt)
'***********************      

Comments

  • The most obvious problem is that you've allocated only one long word for each stack and you probably need 10 to 20. Your long declaration should be:

    long PWMstack[10], LeftStack[20], RightStack[20], a, b

    Leave out the [3000] and [90] in the cognew statements.

    The stack sizes can be bigger. I'd probably use 50 and look carefully at the program after it's working to see if a lesser amount is really needed.
  • Thanks Mike. I'll give it a try.
    My programming is rather rusty.
    Best regards.
  • Hi Laser Steve

    I must confess, my knowledge about P1 is very limited, so I need to resort to logical comparisons, in order to find something weird in a piece of code.

    Apart from the problem Mike Green did well spoted at the above post, the right and left routines seem to be very simmetric twins, which called my attention to the lack of an initial setup at pin [16] as I've done in the following code snippet:
    PUB Left     'Control the left wheels
    dira[6]~~
    outa[6]~
    dira[16]~~
    outa[16]~
    

    Hope it helps

    Henrique
  • It is working nicely, thank you.
  • If you need to be precise about your pwm clock you can use a synchronized loop. You can also use a counter to generate a very precise pulse.
Sign In or Register to comment.