Shop OBEX P1 Docs P2 Docs Learn Events
Cog help... — Parallax Forums

Cog help...

grasshoppergrasshopper Posts: 438
edited 2008-09-19 04:05 in Propeller 1
I am having trouble running Cogs that process data and pass it to another cog. All the example Cogs i see either just light up and led or pwm a pin. Then the other examples just start and immediately go into assembly code. Please list a good cog example that is commented well.

All i want to do is run a Cog - measure temperature and average it. Then My main Cog can read the temperature at will.

Also If i am running FLoat32 " it uses 2 cogs" to do math and I call it in 2 separate Objects, will it then load Float32 twice in effect taking up 4 cogs?

Thanks in advance.

Comments

  • grasshoppergrasshopper Posts: 438
    edited 2008-09-18 15:27
    Here is some code

    the Temp Getting Object that t i am trying to report to my main program

    {
    
       Read a/d and calculate temperatures 
    
       http://www.capgo.com/Resources/Temperature/Thermistor/ThermistorCalc.html
       to get A,B,C values
       
    }
    Con
    
      AD_CS         = 9             ' AD Chip Select
      AD_Din        = 10            ' AD Data IN
      AD_Dout       = 11            ' AD Data out
      AD_Dclk       = 8             ' AD clock
      
      Chan0         = %10010111     ' board temp
      Chan1         = %11010111     ' lm135ah                             
      Chan2         = %10100111     ' low wter flow                               
      Chan3         = %11100111     ' ext probe
      
      KelvinOS      = 273.15        ' To convert from C to kelvin
      FahrenheitOS  = 1.8           ' 9/5
      Fcon          = 32            ' °F = °C × 9/5 + 32      
      AtoDvolt      = 0.000076295   ' 5/65535 or 2^16 = 65536-1 
      VSource       = 4.997         ' Voltage total on Voltage Ref Chip
      one           = 1.0
    
      MSBPRE   = 0
      LSBPRE   = 1
      MSBPOST  = 2
      LSBPOST  = 3
      OnClock  = 4    
      
      LSBFIRST = 0
      MSBFIRST = 1
    
    
    Obj
    
        Math             : "Float32"    
    
    VAR
    
      long A,B,C,M,mb   
      long  cog
      long  command, cmdReturn
      
    
    
    PUB Start(Loop_Value,Channel) : Okay
    
      stop
      okay := cog := cognew(Loop(Loop_Value,Channel), @command) + 1
    
      
    PUB Stop
    
      if cog
        cogstop(cog~ - 1)
      command~
    
    Pub Loop(Loop_Value, Channel) : Avg | X , Temperature, Temperature2
    
        Repeat X from 1 to (Loop_Value)                                                                                                  
            Temperature := CalcTemp(Channel)
            Temperature2 := Temperature + Temperature2
            
        Temperature2 := Temperature2 / (Loop_Value)    
        Avg := Calc_Slope(Temperature2)
         
    PUB Calc_AtoD (ADChan) : RawVOLTS                       
      dira[noparse][[/noparse]AD_CS]~~
      outa[noparse][[/noparse]AD_CS]~~                                                               
      RawVOLTS := 0
       
      outa[noparse][[/noparse]AD_CS]~                                         
      SHIFTOUT(AD_Din,AD_DClk,ADChan,MSBFIRST,8)                                           
    
      RawVOLTS := SHIFTIN(AD_Dout,AD_DCLK,MSBPOST,16)             
      outa[noparse][[/noparse]AD_CS]~~                                 
     
    pub Calc_Slope (Y) : TempC |  subtracted
     ' M :=  135.5
     ' B :=  35693
     ' Y should be RawVolts value from AtoD
       M :=  135.5
       mB :=  35693
       TempC := 0
                                                                 
      subtracted := (Y - mB)                                               
      TempC := Math.FDiv(Math.ffloat(subtracted),M)                                  
    
    
    pub Calc_OhmsLAw (RawVolts, R1) : Resistance | AtoDVoltage, TotalCurrent
      ' V = IR
      AtoDVoltage := 0
      TotalCurrent := 0
      Resistance := 0
       
      Math.Ffloat(R1)
      AtoDVoltage := Math.fmul(AtoDvolt,(Math.Ffloat(RawVolts)))
      TotalCurrent := Math.fdiv(Math.fsub(VSource,AtoDVoltage),Math.Ffloat(R1))
      Resistance := Math.fdiv(AtoDVoltage, TotalCurrent)                          
    
    
    pub Calc_C (TempC) : celsius  
      ' C + 273.15
      celsius := 0
      
      celsius := Math.Fsub(TempC,KelvinOS) 
    
    Pub Calc_StHart (Resistance) : kelvin | B_x_Rlog , C_x_Rlog , Rcubed, Rlog, Addit, Addit2
      '   K = 1 /[noparse][[/noparse]A + B log[noparse][[/noparse]R]+ C log[noparse][[/noparse]R]³]
      A := 0.0011121937   
      B := 0.00023719463  
      C := 0.000000073183365
      
      B_x_Rlog := 0
      C_x_Rlog := 0
      Rcubed := 0
      Rlog := 0
      Addit := 0
      kelvin := 0
      Addit2 := 0
      
      Rlog := Math.log(Resistance)                      
      B_x_Rlog := Math.FMul(B,Rlog)                                                                              
      RCubed := Math.FMul(Rlog,Rlog)                         
      RCubed := Math.FMul(Rlog,RCubed)                        
      C_x_Rlog := Math.FMul(RCubed,C)                        
      Addit := Math.Fadd(C_x_Rlog,B_x_Rlog)                        
      Addit2 := Math.Fadd(Addit, A)                           
      Kelvin := Math.Fdiv(one,addit2)                         
      
        
    Pub CalcTemp(Channel): RealTemp
    
      RealTemp := 0
    
      If (Channel == 0)          'Just raw a/d data chan 0
        RealTemp := Calc_AtoD(chan0)
    
      If (Channel == 1)          'Just raw a/d data chan 1
        RealTemp := Calc_AtoD(chan1)
            
    
      If (Channel == 2)          'Just raw a/d data chan 2
        RealTemp := Calc_AtoD(chan2)
            
            
      If (Channel == 3)          'Just raw a/d data chan 3
        RealTemp := Calc_AtoD(chan3)
    
        
    PUB ShiftOut (Dpin, Cpin, Value, Mode, Bits)| bitNum
        outa[noparse][[/noparse]Dpin]:=0                                       
        dira[noparse][[/noparse]Dpin]~~                                           
        outa[noparse][[/noparse]Cpin]:=0
        dira[noparse][[/noparse]Cpin]~~
    
        If Mode == LSBFIRST                                    
           REPEAT Bits
              outa[noparse][[/noparse]Dpin] := Value                           
              Value := Value >> 1                           
              !outa[noparse][[/noparse]Cpin]                                     
              !outa[noparse][[/noparse]Cpin]
              waitcnt(1000 + cnt)                            
    
        elseIf Mode == MSBFIRST                                           
           REPEAT Bits                                                                
              outa[noparse][[/noparse]Dpin] := Value >> (bits-1)                          
              Value := Value << 1                               
              !outa[noparse][[/noparse]Cpin]                                              
              !outa[noparse][[/noparse]Cpin]                                                             
              waitcnt(1000 + cnt)                                            
        outa[noparse][[/noparse]Dpin]~                                           
    
    PUB ShiftIn (Dpin, Cpin, Mode, Bits) : Value | InBit
        dira[noparse][[/noparse]Dpin]~                                           
        outa[noparse][[/noparse]Cpin]:=0                                         
        dira[noparse][[/noparse]Cpin]~~                                           
                                                    
        If Mode == MSBPRE                                      
           Value:=0
           REPEAT Bits                                       
              InBit:= ina[noparse][[/noparse]Dpin]                                
              Value := (Value << 1) + InBit                   
              !outa[noparse][[/noparse]Cpin]                                     
              !outa[noparse][[/noparse]Cpin]
              waitcnt(1000 + cnt)                              
    
        elseif Mode == MSBPOST                                               
           Value:=0                                                          
           REPEAT Bits                                                          
              !outa[noparse][[/noparse]Cpin]                                                            
              !outa[noparse][[/noparse]Cpin]                                         
              InBit:= ina[noparse][[/noparse]Dpin]                                                     
              Value := (Value << 1) + InBit                                                           
              waitcnt(1000 + cnt)                                                        
                                                                     
        elseif Mode == LSBPOST                                                    
           Value:=0                                                                                         
           REPEAT Bits                                                                 
              !outa[noparse][[/noparse]Cpin]                                                             
              !outa[noparse][[/noparse]Cpin]                                                                             
              InBit:= ina[noparse][[/noparse]Dpin]                                                       
              Value := (InBit << (bits-1)) + (Value >> 1)          
              waitcnt(1000 + cnt)                                                       
    
        elseif Mode == LSBPRE                                              
           Value:=0                                                                                   
           REPEAT Bits                                                            
              InBit:= ina[noparse][[/noparse]Dpin]                                                  
              Value := (Value >> 1) + (InBit << (bits-1))         
              !outa[noparse][[/noparse]Cpin]                                                                
              !outa[noparse][[/noparse]Cpin]                                                                             
              waitcnt(1000 + cnt)                             
    
        elseif Mode == OnClock                                            
           Value:=0
           REPEAT Bits                                         
                                            
              !outa[noparse][[/noparse]Cpin]                                     
              waitcnt(500 + cnt)                               
              InBit:= ina[noparse][[/noparse]Dpin]                             
              Value := (Value << 1) + InBit                   
              !outa[noparse][[/noparse]Cpin]
              waitcnt(500 + cnt)
    
    





    And My main Cog code
    
    Pub Main
    
      TempObj.Start(10,1)  
    
    
    Pub Running  | X, eTime1, Temperature, Drive
      
      repeat
        Temperature := TempObj.Loop(10,1)  
    
    
    



    Problem is that the Temperature is not getting the TEmperature
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-18 16:58
    This is all messed up. In the main cog, Running will never be executed. It wouldn't work even if you called Running because you've already started Loop in a separate cog, now you would be trying to execute two copies of Loop. In the Read A/D object, Loop only executes a few times, then just stops. Ooooof!

    I think you have junk a lot of this and rethink it. I suspect that you need to separate the functions of reading the raw A/D data and doing the calculations, put just the reading of the A/D into one cog and all the calculations into your main routine.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-09-18 18:44
    Hello Grasshopper,

    for analysing the things in detail it is a MUST to have your COMPLETE code.

    In the PropIDE under main-menu there is a item archive... (see attached picture)

    If loaded your mainfile into the IDE and use archive ALL files related to that project
    will be packed together in a zipfile. If you attach this to a posting it's possible to download it and
    there will be everything inside for compiling your project

    Making superquick postings makes it superslow to get a solution
    because everybody has to ask back for the rest of the details

    So please please please archive your code and attach it to a posting

    Use the Reply-post function instead of the quick reply

    then click on Attachment-Manager you get a new Window
    search for your archive and upload it

    The forum is willing to help if you provide detailed information combined with a concrete question

    best regards

    Stefan
    420 x 298 - 15K
    453 x 450 - 33K
    558 x 531 - 20K
  • grasshoppergrasshopper Posts: 438
    edited 2008-09-18 22:11
    I am not sure you understand. My complete code is several thousand lines and possibly more than 12 Objects running 6-7 cogs. This is why i only posted a brief example Mike.

    I guess I was wanting someone to show me how to properly use cogs. I cant find reasonable information in the Propeller manual. One area i am suffering in is this

    
    Var
       Stack[noparse][[/noparse]20]
    
    
    



    I know that when starting a cog you need some stack space, but how much do i have to play with and how do i decide that i have allocated enough? Its this type of unclear information that i cant seem to get.

    Any help about running (loops), receiving, sending information to a custom cog would be greatly apreacated. I can light leds all day unfortunately my Boss want more than a Leds.

    P.S. -- Stock market is going mad (get in now) !!
  • Mike CookMike Cook Posts: 829
    edited 2008-09-18 22:19
    I'm getting ready to start making my own objects, to re-order some of my spaghetti code.

    This article by Jon Williams is what I'm starting with. In the article he describes a tool that Phil Pilgrim wrote to help determine the stack required by an object. Might be of some help.

    http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol7/col/NV134.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • grasshoppergrasshopper Posts: 438
    edited 2008-09-18 23:30
    Man this is a great help thanks.
  • hippyhippy Posts: 1,981
    edited 2008-09-19 03:01
    grasshopper said...
    I know that when starting a cog you need some stack space, but how much do i have to play with and how do i decide that i have allocated enough? Its this type of unclear information that i cant seem to get.

    There's no rule as it all depends on what you do with those Cogs, some need lots of stack some
    need very little. The best rule of thumb is to start high and reduce if RAM starts to get tight. Just
    20 stack items as in you example seems low to me.

    It is possible to analyse the code and determine how much stack is used from the entry of any
    particular method call but it's a lot of hard work and something left tot he compiler to report back
    on. Unfortunately PropTool doesn't provide that information and nor does any utility I know of. It's
    doable but no one's done it yet.
  • Mike CookMike Cook Posts: 829
    edited 2008-09-19 03:18
    Hippy,

    Have you looked at the *.pdf I posted, in my previous post? The tool that Phil came up with·is described on page 7 of that document and starts in the section titled: Stacking It All Up.

    Since I'm just now starting to write code that runs in parallel instead of linear, I was going to use this technique for determining the stack size of the objects that in intend on writing.

    Code that goes along with the article is here: http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol7/code/NV134.zip

    And I've attached my version of it modified to use the TV_Text object, in my code I have not yet added Phil's stack_monitor object.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike

    Post Edited (Mike Cook) : 9/19/2008 3:44:38 AM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2008-09-19 03:28
    Yea nice PDF Mike thanks a million [noparse][[/noparse]longs] I will run that Phil program tomorrow for sure and clean up my "trash" as Mike Green puts it [noparse]:)[/noparse]

    Hippy whats the max stack space available ? 512 is my best guess but not sure.
  • hippyhippy Posts: 1,981
    edited 2008-09-19 03:52
    @ Mike : Yes, Phil's tool is handy but it would be so much nicer if one could click on a method and have the PropTool calculate necessary stack depth which is certainly possible to do.

    @ grasshopper : You can have as much stack space as you want; it's just a long array of any size.
  • Cluso99Cluso99 Posts: 18,069
    edited 2008-09-19 04:05
    I am just about to release my spin debugger (in the tidyup process as all is working). It monitors the stack length and displays up to the top 4 items on the stack. This may be of help, but remember, it is effectively single stepping all the code (including the interpreter pasm) so it is much slower. You can turn on the assembler tracing thru the Interpreter code also.
Sign In or Register to comment.