Shop OBEX P1 Docs P2 Docs Learn Events
the coginit-command problem — Parallax Forums

the coginit-command problem

hcchenhcchen Posts: 14
edited 2014-05-29 17:25 in Propeller 1
Dear all

I want to use different cog to do two things.
one cog be used for the motor, and anther be used for encoder.
I use the "coginit" , but nothing is happening
Anyone interest in helping.

the code:
CON  _clkmode = xtal1 + pll16x
  _clkfreq = 80_000_000
  
  MOTOR_L_B     = 19
  MOTOR_L_PWM   = 20
  MOTOR_L_A     = 21
  MOTOR_R_B     = 22
  MOTOR_R_PWM   = 23
  MOTOR_R_A     = 24


  #1, HOME, GOTOXY                                      ' PST Constants
  #8, BKSP, TAB, LF, CLREOL, CLRDN, CR
  #14, GOTOX, GOTOY, CLS


  
Obj
  Motors        : "Eddie Motor Driver"
  Encoders      : "Quadrature_Encoder"
  PST           : "Parallax Serial Terminal"
  term          : "FullDuplexSerial"
  ping          : "Ping"    
VAR
  long  ID
  long  JC
  long  JD  
  long  Stack
  long  Stack1
  byte  Cog
  byte  Cog1
  long  Time, Counter1, Last1, Delta1, Counter2, Last2, Delta2, Total2
  byte  Success


PUB main |  perl,perr


  perl:=$FFF
  perr:=$FFF
  
  coginit(1,go(perl,perr),@stack1)
  coginit(2,encoder,@stack) 


  'Success := (Cog := cognew(encoder, @Stack) + 1)

    
PUB go(perl,perr)


    Motors.start   
    
  repeat
   
    Motors.left(perl)
    Motors.right(perr)
      

PUB encoder | enc1,IC


    pst.Start(115200) 
    ID := 0    
    repeat      
      IC := enc1 
      enc1  := ina[2]     
      if IC <> enc1
         ID := ID + 1 
      else  
         ID := ID + 0                  
     pst.Str(String(pst#CE, pst#HM, "ID = "))
     pst.Dec(ID)
     
PUB Stop


 if Cog 
    cogstop(Cog~ - 1)


Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-27 11:36
    I see a couple of problems. The main problem is you're only use one long as the stack. Add a "[64]" (or other appropriate stack size) after both stack declarations.
    VAR
      long  ID
      long  JC
      long  JD  
      long  Stack[64]
      long  Stack1[64]
      byte  Cog
      byte  Cog1
      long  Time, Counter1, Last1, Delta1, Counter2, Last2, Delta2, Total2
      byte  Success
    

    It's also very dangerous to use coginit. cognew is generally the preferred way of starting a cog.

    Another issue is you just let cog #0 die. Why not use it for one of your tasks instead of starting a new cog?

    Instead of starting the method "encoder" in a new cog, your "main" method could just call "encoder" and have the method run in cog #0.

    I'm confused how you're using the encoders. It doesn't look like the object "Encoders" is ever used.
  • hcchenhcchen Posts: 14
    edited 2014-05-27 12:56
    After transforming the both stack declarations and "coginit". The both functions can work fine. Thank you for your correction:smile:

    I
    neglect the cog#0.:thumb:(carelessness)
    I don't know how to use "Ecoders".(Forget to remove the object)
    I try to write the encoder which can read pulse on pin2.
    if my code can't be use, I will find the information about "Encoders".
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-27 13:50
    I've posted some encoder code here. IIRC, the code in post #2 doesn't use PWM and is encoder only. One of the version includes a speed calculation.
  • hcchenhcchen Posts: 14
    edited 2014-05-28 21:35
    thank you.
    I will try to understand the code how to work.
  • edited 2014-05-29 16:02
    Duane Degn wrote: »
    It's also very dangerous to use coginit. cognew is generally the preferred way of starting a cog.

    Sorry but I take exception to this statement every time I see it. Coginit isn't inherently dangerous but, like every other command in every other language, you have to have a certain amount of knowledge before you can use it effectively. It, like some other commands, may not be suitable for people with little programming experience.

    Knowing how to use coginit properly will allow you to change the code a cog is running during program execution or run PASM in all 8 cogs.

    Basically it comes down to managing an array of 8 variables.

    Sandy
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-29 16:30
    Sorry but I take exception to this statement every time I see it. Coginit isn't inherently dangerous ...
    Sorry, but I take exception to your taking exception. :) Most objects that start new cogs use cognew, so there's no legitimate way to know a priori which cogs are available to use with coginit, once these objects have started.
    Knowing how to use coginit properly will allow you to change the code a cog is running during program execution or run PASM in all 8 cogs.

    True: those are both legitimate uses of coginit, but only to the extent required for reloading the cog you're running in with new code. In virtually every other case, it's unnecessary to assign code to any particular cog. Especially for just starting a new cog, cognew is still the best way to go.

    -Phil
  • edited 2014-05-29 17:25
    You have a point. Coginit isn't always the best choice but neither is cognew.

    A good mechanic will know the capabilities of every tool in his toolbox and know which is the appropriate tool for the job. That way you get to use all the tools.


    Most infantrymen believe that a piece of high explosive will solve any problem. One tool for everything!

    Sandy
Sign In or Register to comment.