Shop OBEX P1 Docs P2 Docs Learn Events
cognew not working...new to prop — Parallax Forums

cognew not working...new to prop

bulkheadbulkhead Posts: 405
edited 2007-07-28 04:36 in Propeller 1
**problem solved (I forgot each cog has its own dira, outa registers, so I had to reset my dira[noparse][[/noparse]pins]**
I have in my main method:
cognew(startMotors(10), @stack[noparse][[/noparse]0])

where stack is stack[noparse][[/noparse]60]
Here is the startMotors method:
PUB startMotors(startPin) | T,dt,motorLeftSpeed,motorRightSpeed



I have an LED light up for 1 second if the startMotors method runs, but I never see the LED light. However, the loop after the "cognew" line executes fine (as indicated by LEDs). What am I doing wrong? Here is the entire code:

'' File: SumoBot.spin
'' controls sumobot!

VAR
    byte motorLeft   'duty cycle, -100 to 100 %
    byte motorRight
    
    long stack[noparse][[/noparse]60]
CON
    _xinfreq = 5_000_000                     ' 5 MHz external crystal 
    _clkmode = xtal1 + pll16x                ' 5 MHz crystal multiplied → 80 MHz
PUB mainMethod

    motorLeft :=0
    motorRight :=0  

    dira[noparse][[/noparse]21..23]~ 'switches
    dira[noparse][[/noparse]4..9]~~ 'LED's
    outa[noparse][[/noparse]4..9]~  'LED's off
    
    cognew(startMotors(10), @stack[noparse][[/noparse]0])
    repeat                              '**THIS LOOP WORKS**
      {{loop stuff}}
     

' 6 pins....en1, en2, c1,c2,d1,d2       
PUB startMotors(startPin) | T,dt,motorLeftSpeed,motorRightSpeed

    dira[noparse][[/noparse]startPin..startPin+5]~~ 'change to output
    outa[noparse][[/noparse]startPin..startPin+5]~ 'motors off
    dT := clkfreq / 80                  ' 1kHz refresh rate
    outa[noparse][[/noparse]7]:=1       '**NEVER GOES ON**
    waitcnt(cnt+80_000_000)
        
    repeat
      {{lots of stuff here}}

Post Edited (bulkhead) : 7/28/2007 3:25:01 AM GMT

Comments

  • CJCJ Posts: 470
    edited 2007-07-28 01:49
    try puting a dira[noparse][[/noparse]7]:= 1 just before your outa, each cog has its own direction and output registers

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • bulkheadbulkhead Posts: 405
    edited 2007-07-28 02:23
    **EDIT: I fixed it, I was waitingcnt(cnt) so it never got there. I added some code to take care of the "0" cases for motorLeftSpeed and motorRightSpeed so it works now**
    CJ, I just tried that, and now that part works. Thanks! However, now I have another problem; the indicator LED lights when the cog first starts the method and when it enters the loop, but it never keeps repeating the loop infinitely (the indicator LED lights up once in the loop and shuts off). Here is that method:
    ' 6 pins....en1, en2, c1,c2,d1,d2    
    PUB startMotors(startPin) | T,dt,motorLeftSpeed,motorRightSpeed
    
        dira[noparse][[/noparse]startPin..startPin+5]~~   'change to output
        outa[noparse][[/noparse]startPin..startPin+5]~    'motors off
        dT := clkfreq / 80             ' 1kHz refresh rate
        dira[noparse][[/noparse]4..9]~~
        outa[noparse][[/noparse]6]:=1                     'pin4 = indicator LED
        waitcnt(cnt+80_000_000)        '**INDICATOR ON 1 SEC**
        outa[noparse][[/noparse]6]:=0
            
        repeat
          outa[noparse][[/noparse]6]:=1                   '**FLASH INDICATOR-HAPPENS ONCE**
          waitcnt(clkfreq/5+cnt)
          outa[noparse][[/noparse]6]:=0
          waitcnt(clkfreq+cnt)
          
          motorLeftSpeed:=motorLeft
          motorRightSpeed:=motorRight
          
          motorLeftSpeed<#=100                 'limit input from -100% to 100%
          motorLeftSpeed#>=-100
          motorRightSpeed<#=100
          motorRightSpeed#>=-100  
           
          if motorLeftSpeed < 0                      'set motor directions
            outa[noparse][[/noparse]startPin+2..startPin+3]:=%10
          elseif motorLeftSpeed>0
            outa[noparse][[/noparse]startPin+2..startPin+3]:=%01
          if motorRightSpeed < 0                      
            outa[noparse][[/noparse]startPin+4..startPin+5]:=%10
          elseif motorRightSpeed>0
            outa[noparse][[/noparse]startPin+4..startPin+5]:=%01
            
          outa[noparse][[/noparse]startPin..startPin+1]:=%11                  'high duration
          outa[noparse][[/noparse]7]:=1
          outa[noparse][[/noparse]9]:=1
    
          if||motorLeftSpeed < ||motorRightSpeed        
            waitcnt(cnt+dT*(||motorLeftSpeed) )
            outa[noparse][[/noparse]startPin]:=0
            outa[noparse][[/noparse]7]:=0
            waitcnt(cnt+dT*(||motorLeftSpeed-||motorRightSpeed) )      'low duration
            outa[noparse][[/noparse]startPin+1]:=0
            outa[noparse][[/noparse]9]:=0
            waitcnt(cnt+dT*(100-||motorRightSpeed) )
          else
            waitcnt(cnt+dT*(||motorRightSpeed) )
            outa[noparse][[/noparse]startPin+1]:=0
            outa[noparse][[/noparse]9]:=0
            waitcnt(cnt+dT*(||motorRightSpeed-||motorLeftSpeed) )        'low duration
            outa[noparse][[/noparse]startPin]:=0
            outa[noparse][[/noparse]7]:=0
            waitcnt(cnt+dT*(100-||motorLeftSpeed) )
    

    Post Edited (bulkhead) : 7/28/2007 2:51:36 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-28 03:02
    When you compute dT, you use clkfreq / 80. For an 80MHz system clock, that's the number of clocks in 1/80 of a second (about 12ms).
    I suspect that one of your waitcnt calculations results in a value of cnt+0 which gives a very long pause (about a minute).
  • bulkheadbulkhead Posts: 405
    edited 2007-07-28 04:36
    Mike, you were right, I did it backwards (I wanted the number to be 80, so I changed it to be "clkfreq/100000". What I learned today: each cog has its own dira...outa registers and waitcnt(cnt) stops the cog from running. I fixed these problems and now my program runs fine.
Sign In or Register to comment.