Shop OBEX P1 Docs P2 Docs Learn Events
what did i do wrong new cog in spin — Parallax Forums

what did i do wrong new cog in spin

AnubisbotAnubisbot Posts: 112
edited 2007-05-22 17:36 in Propeller 1
Hi, i want to start the the pwm.spin from pwm_update.spin in a new cog, and want to be able to later change the values of the lang array in the main cog. so the second cog with the pwm generator kann keep runnig continusly.

for debug i wanted to use the pc_text.spin

When i compile and run it i get only the text string not the cog value

and i cand find if its bposible to write to a lang arry from a nother cog

Thanx
AnubisBOT jumpin.gif

Post Edited (Anubisbot) : 5/22/2007 10:59:20 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-21 21:17
    For a start, you can't use a routine in another object with COGNEW. It just doesn't work that way.

    It's also impossible (deliberately) to read or write to any variable in another object. This is always done with a "helper" method in the object with the variables. Since your SetPWM routine only sets the values of pwm_state and your REPEAT loop only reads them, you don't need to use semaphores (locks). Replace your start routine with:
    VAR long cogStack[noparse][[/noparse]20]
    
    PUB Start
    
      SetPWM(9,1)                  ' initialize the table
      SetPWM(10,1)                  
      SetPWM(16,1)       
      return cognew(pwm_loop,cogStack)          
      
    PRI pwm_loop | t
      ' start the time value at zero
      ' PWM Loop
      dira[noparse][[/noparse]9] := 1                  ' Make pin 0, pin 1, and pin 2 all outputs
      dira[noparse][[/noparse]10] := 1
      dira[noparse][[/noparse]16] := 1
      
      t := 0
      repeat
        outa := pwm_state[noparse][[/noparse]t]        ' Set states of all 3 pwm pins simultaneously
        t += 1                      ' Change to the next state
        if t = TableSize            ' If t goes off the edge of the table  NOTE change
          t := 0                    ' start over at beginning
    
    


    Then just use "id := pwm.start" instead of the statement with the cognew in your main program.
    You also have to use pwm.SetPWM instead of the one in your main program.
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-21 21:40
    Thank you mIke for the quick response, but since im still a total new pilot on the prop, i still dont get it right.

    now the tv out works and it starts something, but the pwm funktion seem to only drive them high all the time,
    pwm.spin


    CON
    TableSize = 500

    VAR

    ' Holds the value the output should take at each instant in time.
    long pwm_state[noparse][[/noparse]TableSize]
    long cogStack[noparse][[/noparse]20]

    PUB Start

    SetPWM(9,1) ' initialize the table
    SetPWM(10,1)
    SetPWM(16,1)
    return cognew(pwm_loop,cogStack)

    PRI pwm_loop | t
    ' start the time value at zero
    ' PWM Loop
    dira[noparse][[/noparse]9] := 1 ' Make pin 0, pin 1, and pin 2 all outputs
    dira[noparse][[/noparse]10] := 1
    dira[noparse][[/noparse]16] := 1

    t := 0
    repeat
    outa := pwm_state[noparse][[/noparse]t] ' Set states of all 3 pwm pins simultaneously
    t += 1 ' Change to the next state
    if t := TableSize ' If t goes off the edge of the table NOTE change
    t := 0 ' start over at beginning

    PUB SetPWM(pin, percent) | i, count

    ' Determine how many pixels to turn on for any table size
    count := (percent * TableSize) / 500

    repeat i from 0 to TableSize - 1
    if i < count
    pwm_state |= (1 << pin)
    else
    pwm_state &= !(1 << pin)


    pwm_update.spin

    {{ PWM_Update.spin }}
    _clkmode = XTAL1 + pll16x
    _xinfreq = 5_000_000
    VAR


    OBJ

    pwm : "pwm"
    TV : "PC_Text" 'You can also use TV_Text or VGA_Text objects
    PUB Start |id



    TV.start(0)
    TV.str(string("debug...",13))
    id:= pwm.start


    TV.dec(id)
    repeat 20
    waitcnt(50_000_000 + cnt)
    pwm.setpwm(16,1)
    waitcnt(50_000_000 + cnt)
    pwm.setpwm(16,500)



    Thanks
    Anubisbot
    
    
    [noparse][[/noparse]code]

    Post Edited (Anubisbot) : 5/21/2007 9:45:05 PM GMT

  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-21 22:06
    Found it it it was the := and i needed ==

    Thanks mike...

    Anubisbot
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-21 22:09
    Where you compute "count := (percent * TableSize) / 500", it should be "count := (percent * TableSize) / 100".

    You should also put a check in SetPWM to make sure that percent is between 0 and 100.
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 01:45
    So far is it runniing, exept when i try to only set the pins 11..4 as an output since i want to use a remote i need pin 15 to be always an input

    here is my code..
    CON
      TableSize = 250
    
    VAR
      
      ' Holds the value the output should take at each instant in time.
      byte pwm_state[noparse][[/noparse]TableSize]
      long cogStack[noparse][[/noparse]20]
    
    PUB Start
    
      SetPWM(9,5)                  ' initialize the table
      SetPWM(10,0)                  
      SetPWM(11,10)      
      return cognew(pwm_loop,cogStack)          
      
    PRI pwm_loop | t
      ' start the time value at zero
      ' PWM Loop
      dira [noparse][[/noparse]11..4]~~                ' Make pin 0, pin 1, and pin 2 all outputs
                    
      t := 0
      repeat
        outa [noparse][[/noparse]11..4] := pwm_state[noparse][[/noparse]t] >> 5 ' Set states of all 3 pwm pins simultaneously
        t+ = 1                            ' Change to the next state
        if t == TableSize                 ' If t goes off the edge of the table NOTE change
          t := 0                          ' start over at beginning
    
    PUB SetPWM(pin, percent) | i, count
    
    ' Determine how many pixels to turn on for any table size
      count := (percent * TableSize) / 100
      
      repeat i from 0 to 100 - 1
        if i < count
          pwm_state[i] |= (1 << pin)
        else
          pwm_state[i] &= !(1 << pin[/i][/i]
    




    i dont see why it dosent work,

    Anubisbot jumpin.gif
  • mirrormirror Posts: 322
    edited 2007-05-22 02:50
    PUB Start
    
      SetPWM(9,5)                  ' initialize the table
      SetPWM(10,0)                  
      SetPWM(11,10)      
      return cognew(pwm_loop,cogStack)          
      repeat
    
    

    Maybe you need the·'repeat' as shown. It the same as·for(;[noparse];)[/noparse];·in c. It makes sure your prgram keeps running after starting the extra cog.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    It's not all that hard to count the number of grains of sand on the beach. The hardest part is making a start - after that it just takes time.·· Mirror - 15 May 2007
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 03:05
    no thats not the reason, when i change the code to wat mike told me it runs good as long i dont want to use the remote on oin 16

    PRI pwm_loop | t
      ' start the time value at zero
      ' PWM Loop
      dira [noparse][[/noparse]11..4]~~                ' Make pin 0, pin 1, and pin 2 all outputs
                    
      t := 0
      repeat
        outa [noparse][[/noparse]11..4] := pwm_state[noparse][[/noparse]t] >> 5 ' Set states of all 3 pwm pins simultaneously
        t+ = 1                            ' Change to the next state
        if t == TableSize                 ' If t goes off the edge of the table NOTE change
          t := 0
    



    when i change it to this it runs but it changes my pin 15 to out i think

    Anubisbot jumpin.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-22 04:07
    Anubisbot,
    Why are you doing the ">> 5"? pwm_state is already set up as a bitstring for all the I/O pins.
    Why are you doing the "outa[noparse][[/noparse]11..4]"? You can set all the output pins ("outa := pwm_state[noparse][[/noparse] t ]") because each cog has its
    own private copy of OUTA. If a bit in DIRA is set to zero, the corresponding OUTA bit isn't really used. You do need the initialization
    statement that sets bits 11 through 4 of DIRA, but you don't need to do that for OUTA.

    I think in your code that the ">> 5" would have to be a ">> 4" to work properly.
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 10:52
    Hi mike, your right, i changed it back to what was working, but i still have this strange problem,

    Attached are the files i use.

    when i start ir remorte test.spin it starts the debug PC_Text.spin

    Thats ok, then it reads the ir signal comming in on pin 15, i see the velue on the screen .

    but when i press 1 on my remote it starts the pwm.spin the lights go on and my remote stops working.

    I dont get i tried all things back and forth all night..

    Somebody out there how has an idea

    Anubisbot jumpin.gif
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 15:26
    It seams so that it doesent return to the main loop when i call the pwm.spin

    Thats strange, or di i miss a littele like the a . or something...

    Anubisbot
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-22 15:38
    You can't repeatedly call ir.start. You either have to call ir.stop before you call ir.start again or simply call ir.start during your program's initialization and never again. That's more like what is usually done on the Propeller. Once the IR receiver driver is started, you just watch the shared variable for a valid code.
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 16:03
    Good morning mike,

    no i dont think so, because there is a stop routine what i call every time i call ir.start

    PUB Start(Pin, addrMainCode) : result
    {{
       Pin - propeller pin connected to IR receiver
       addrMainCode - address of keycode variable in main memory
    }}
    
        stop
        byte[noparse][[/noparse]addrMainCode] := NoNewCode
        cog := cognew(getSonycode(Pin, addrMainCode), @Stack) + 1
        result := cog
    



    and the hole setup works until i press the 1 on the remote (start the pwm.spin)
    When i press 2 or 3 or anny other button on the remote it works just fine. and the remote is every time in cog2, cog1 is PC_Text.spin for debuging, and main cog is IR_Remote_Test.spin

    What i can see is as soon i call pwm.start , so it goes to pwm.spin and does the start routine sets the pwm values in a table and starts running, but i think it does it in the main cog, because my main loop stops totaly.. thats wierd..


    Anubisbot
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-22 17:10
    Good point about the ir.stop call ... I missed that.

    I still don't understand why you have to stop and restart the remote code receive routine for every code.

    I see one problem: In the COGNEW call in PWM.start, you need to pass the address of the stack array like: @cogStack
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 17:25
    Now i got it working, i dont understand why, i just put a nother return to the SETPWM PUB and now its working...
    i thought that when there is no endless loop it would atomatick return to here i called it...

    Does you now why.. maybe a new trap..

    Anubisbot
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-22 17:36
    No i checked against it , it was the @ and the return...

    But i thinck we can close this treat now ,

    And again a big thanx to Mike..

    Anubisbot
Sign In or Register to comment.