Shop OBEX P1 Docs P2 Docs Learn Events
Need help calling variables in more than 1 cog — Parallax Forums

Need help calling variables in more than 1 cog

HayONUHayONU Posts: 3
edited 2009-04-14 02:34 in Robotics
Are there any limitations to calling methods from one cog to another and are there any limitations to calling variables throughout the program?

We have IR sensors with an ADC that are supposed to be on a separate cog from the rest of the program so we will be able to continuously monitor them. Our main code will determine what to do once the information is gathered from the ADC. We are having trouble getting the value for each sensor variable to transfer to our main program.

Here is our code:



CON
_clkmode = xtal1 + pll16x ' enable external clock and pll times 16
_xinfreq = 5_000_000 ' set frequency to 5 MHZ

ON = 1
OFF = 0

OUTPUT = 1
INPUT = 0
'Seven Segment Display Pin Numbers.

DISA = 22
DISB = 21
DISC = 20
DISD = 19
DISE = 18
DISF = 17
DISG = 16

'EEPROM Pin Numbers.
SCL = 28
SDA = 29
'Program Switches
PZERO = 0
PONE = 1
PTWO = 2
PTHREE = 3
PFOUR = 4

'Analog to Digital Converter
'Analog to Digital Converter
ADC_CLK = 5
ADC_DIN = 6
ADC_CS5 = 7 'Should be 7, but 2 for development work
ADC_CS6 = 8
ADC_CS7 = 9
ADC_CS8 = 10
ADC_CS9 = 11
ADC_CS10 = 12
ADC_CS11 = 13
ADC_CS12 = 14
var

long front_right
long front_left
long back_right
long back_left
long rightside_front
long rightside_back
long leftside_front
long leftside_back
long inc
long counter
long stack[noparse][[/noparse]20]
long sensor





pub main
i_o_setup
cognew(Read_IR_Sensor,@stack)
waitcnt(40_000_000+cnt)

REPEAT

IF INA[noparse][[/noparse]PZERO]== 0
Outa[noparse][[/noparse]16..22]:= %0111111
waitcnt(80_000_000+cnt)
HeadtoHead


ELSEIF INA[noparse][[/noparse]PONE]== 0
Outa[noparse][[/noparse]16..22]:= %0000110
waitcnt(80_000_000+cnt)
BacktoBack


ELSEIF INA[noparse][[/noparse]PTWO]== 0
Outa[noparse][[/noparse]16..22]:= %1011011
waitcnt(80_000_000+cnt)
SidetoSideOppDir


ELSEIF INA[noparse][[/noparse]PTHREE]== 0
Outa[noparse][[/noparse]16..22]:= %1001111
waitcnt(80_000_000+cnt)
SidetoSideSameDir

ELSEIF INA[noparse][[/noparse]PFOUR]== 0
Outa[noparse][[/noparse]16..22]:= %1100110





Pub HeadtoHead
Repeat
case front_left ' front_right 'If detected stay full speed
0..440: 'displays a zero
outa[noparse][[/noparse]16..22]:= %0111111
outa[noparse][[/noparse]15]:=0
outa[noparse][[/noparse]25]:=0

441..880: 'displays a one
Outa[noparse][[/noparse]16..22]:= %0000110
outa[noparse][[/noparse]15]:=0
outa[noparse][[/noparse]25]:=0
881..1320: 'displays a two
Outa[noparse][[/noparse]16..22]:= %1011011
outa[noparse][[/noparse]15]:=0
outa[noparse][[/noparse]25]:=0
1321..1760: 'displays a three
Outa[noparse][[/noparse]16..22]:= %1001111
outa[noparse][[/noparse]15]:=0
outa[noparse][[/noparse]25]:=0
1761..4095: 'displays a four
Outa[noparse][[/noparse]16..22]:= %1100110
outa[noparse][[/noparse]15]:=0
outa[noparse][[/noparse]25]:=0
other: 'displays a five
Outa[noparse][[/noparse]16..22]:= %1101101'


return

pUB Read_IR_Sensor
i_o_setup

front_right:=0
front_left:=0
back_right:=0
back_left:=0
rightside_front:=0
rightside_back:=0
leftside_front:=0
leftside_back:=0

repeat
outa[noparse][[/noparse]23]:=1
waitcnt(80_000_000+cnt)

leftside_front:=read_adc(ADC_CS5)
front_left:=read_adc(ADC_CS6)
leftside_back:=read_adc(ADC_CS7)
back_left:=read_adc(ADC_CS8)
back_right:=read_adc(ADC_CS9)
rightside_back:=read_adc(ADC_CS10)
rightside_front:=read_adc(ADC_CS11)
front_right:=read_adc(ADC_CS12)

outa[noparse][[/noparse]23]:=0
waitcnt(80_000_000 + cnt)


PUB read_adc(CS_PIN)
'All IR Sensors
OUTA[noparse][[/noparse]ADC_CLK]:=0 'ADC Clock off
OUTA[noparse][[/noparse]CS_PIN]:=0 'Bring Chip Select low (active)

repeat 5
counter:=cnt 'just a delay, no other purpose
OUTA[noparse][[/noparse]ADC_CLK]:=!OUTA[noparse][[/noparse]ADC_CLK] 'ADC Clock on
counter:=cnt 'just a delay, no other purpose

inc:=11 'start at 11 and go to 0, 12 bits
sensor:=0 'initialize the variable
repeat 12 'repeating 12 times because there are 12 bits that we are shifting in
OUTA[noparse][[/noparse]ADC_CLK]:=0 'ADC Clock off
counter:=cnt 'just a delay, no other purpose
if (INA[noparse][[/noparse]ADC_DIN]==1) 'if the bit is a one
sensor:=(sensor+(1<<inc)) 'adds the decimal value of the binary bit using <<inc (first bit is 2048, second 1024, etc)
inc:=inc-1 'decrement the inc variable
OUTA[noparse][[/noparse]ADC_CLK]:=1 'ADC Clock on
counter:=cnt 'just a delay, no other purpose
'OUTA[noparse][[/noparse]ADC_CLK]:=1 'ADC Clock off
OUTA[noparse][[/noparse]CS_PIN]:=1 'Bring Chip Select high (inactive)

return sensor
[noparse][[/noparse]code]

Post Edited (HayONU) : 4/13/2009 10:11:24 PM GMT

Comments

  • HayONUHayONU Posts: 3
    edited 2009-04-13 23:23
    HayONU said...
    Are there any limitations to calling methods from one cog to another and are there any limitations to calling variables throughout the program?

    We have IR sensors with an ADC that are supposed to be on a separate cog from the rest of the program so we will be able to continuously monitor them. Our main code will determine what to do once the information is gathered from the ADC. We are having trouble getting the value for each sensor variable to transfer to our main program.

    Here is our code:

    
    CON
      _clkmode = xtal1 + pll16x          ' enable external clock and pll times 16
      _xinfreq = 5_000_000             ' set frequency to 5 MHZ
    
      ON  = 1
      OFF = 0 
    
      OUTPUT = 1
      INPUT = 0
            'Seven Segment Display Pin Numbers.
          
      DISA = 22 
      DISB = 21
      DISC = 20
      DISD = 19
      DISE = 18
      DISF = 17
      DISG = 16
    
            'EEPROM Pin Numbers.   
      SCL = 28      
      SDA = 29
            'Program Switches
      PZERO  = 0
      PONE   = 1
      PTWO   = 2
      PTHREE = 3
      PFOUR  = 4
    
               'Analog to Digital Converter
                                    'Analog to Digital Converter
      ADC_CLK = 5 
      ADC_DIN = 6                    
      ADC_CS5 = 7                    'Should be 7, but 2 for development work
      ADC_CS6 = 8
      ADC_CS7 = 9
      ADC_CS8 = 10
      ADC_CS9 = 11
      ADC_CS10 = 12
      ADC_CS11 = 13
      ADC_CS12 = 14
    var
    
      long front_right
      long front_left
      long back_right   
      long back_left
      long rightside_front
      long rightside_back
      long leftside_front
      long leftside_back
      long inc
      long counter                            
      long stack[noparse][[/noparse]20]
      long sensor
    
    
    
     
    
    pub main
     i_o_setup
     cognew(Read_IR_Sensor,@stack)
     waitcnt(40_000_000+cnt)
     
    REPEAT
                
      IF INA[noparse][[/noparse]PZERO]== 0                         
          Outa[noparse][[/noparse]16..22]:= %0111111
           waitcnt(80_000_000+cnt)   
          HeadtoHead
       
    
      ELSEIF INA[noparse][[/noparse]PONE]== 0                         
        Outa[noparse][[/noparse]16..22]:= %0000110
         waitcnt(80_000_000+cnt)
        BacktoBack
        
    
      ELSEIF INA[noparse][[/noparse]PTWO]== 0                                    
        Outa[noparse][[/noparse]16..22]:= %1011011
         waitcnt(80_000_000+cnt)
        SidetoSideOppDir
        
    
      ELSEIF INA[noparse][[/noparse]PTHREE]== 0                                    
        Outa[noparse][[/noparse]16..22]:= %1001111
         waitcnt(80_000_000+cnt)
        SidetoSideSameDir
                                                                     
      ELSEIF INA[noparse][[/noparse]PFOUR]== 0
        Outa[noparse][[/noparse]16..22]:= %1100110
      
                                                 
               
        
        
    Pub HeadtoHead
      Repeat
        case front_left ' front_right  'If detected stay full speed
          0..440:                    'displays a zero
            outa[noparse][[/noparse]16..22]:= %0111111
            outa[noparse][[/noparse]15]:=0
            outa[noparse][[/noparse]25]:=0
                                  
          441..880:                 'displays a one
            Outa[noparse][[/noparse]16..22]:= %0000110
            outa[noparse][[/noparse]15]:=0
            outa[noparse][[/noparse]25]:=0      
          881..1320:                 'displays a two
            Outa[noparse][[/noparse]16..22]:= %1011011
            outa[noparse][[/noparse]15]:=0
            outa[noparse][[/noparse]25]:=0
          1321..1760:                 'displays a three
            Outa[noparse][[/noparse]16..22]:= %1001111
            outa[noparse][[/noparse]15]:=0
            outa[noparse][[/noparse]25]:=0
          1761..4095:                 'displays a four
            Outa[noparse][[/noparse]16..22]:= %1100110
            outa[noparse][[/noparse]15]:=0
            outa[noparse][[/noparse]25]:=0
          other:                      'displays a five            
            Outa[noparse][[/noparse]16..22]:= %1101101'
        
             
    return
    
    pUB Read_IR_Sensor
        i_o_setup
        
      front_right:=0           
      front_left:=0                   
      back_right:=0                   
      back_left:=0                    
      rightside_front:=0              
      rightside_back:=0               
      leftside_front:=0               
      leftside_back:=0       
         
    repeat     
      outa[noparse][[/noparse]23]:=1
      waitcnt(80_000_000+cnt)
           
      leftside_front:=read_adc(ADC_CS5)
      front_left:=read_adc(ADC_CS6)
      leftside_back:=read_adc(ADC_CS7)
      back_left:=read_adc(ADC_CS8)
      back_right:=read_adc(ADC_CS9)
      rightside_back:=read_adc(ADC_CS10)
      rightside_front:=read_adc(ADC_CS11)
      front_right:=read_adc(ADC_CS12)
      
      outa[noparse][[/noparse]23]:=0
      waitcnt(80_000_000 + cnt)  
      
    
    PUB read_adc(CS_PIN)  
          'All IR Sensors
      OUTA[noparse][[/noparse]ADC_CLK]:=0             'ADC Clock off
      OUTA[noparse][[/noparse]CS_PIN]:=0              'Bring Chip Select low (active)
    
      repeat 5
         counter:=cnt           'just a delay, no other purpose
         OUTA[noparse][[/noparse]ADC_CLK]:=!OUTA[noparse][[/noparse]ADC_CLK]         'ADC Clock on
      counter:=cnt           'just a delay, no other purpose  
      
      inc:=11                  'start at 11 and go to 0, 12 bits
      sensor:=0               'initialize the variable
      repeat 12                'repeating 12 times because there are 12 bits that we are shifting in                 
        OUTA[noparse][[/noparse]ADC_CLK]:=0         'ADC Clock off
        counter:=cnt           'just a delay, no other purpose 
        if (INA[noparse][[/noparse]ADC_DIN]==1)   'if the bit is a one
          sensor:=(sensor+(1<<inc))   'adds the decimal value of the binary bit using <<inc (first bit is 2048, second 1024, etc)   
        inc:=inc-1             'decrement the inc variable
        OUTA[noparse][[/noparse]ADC_CLK]:=1         'ADC Clock on           
        counter:=cnt           'just a delay, no other purpose
        'OUTA[noparse][[/noparse]ADC_CLK]:=1          'ADC Clock off
      OUTA[noparse][[/noparse]CS_PIN]:=1             'Bring Chip Select high (inactive)
    
    return sensor
    
    

  • P!-RoP!-Ro Posts: 1,189
    edited 2009-04-14 00:56
    I can't help you much, but just for a future reference, this post should have been placed in the Propeller forum. They could have helped you much faster there.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • SRLMSRLM Posts: 5,045
    edited 2009-04-14 01:16
    A couple of thoughts...

    Your waitcnt in Read_IR_Sensor would be better if you used clkfreq instead of 80_000_000. That way, if you change your clock speed you don't have to edit your code so much (like in this statement).

    your stack is also pretty small. Why not try changing it to something like 200? You probably have the space to be generous.

    Try posting your full code as an attachment.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-14 02:34
    It was posted there, and should be answered there. This is a dupe. Perhaps the moderators could remove it.

    HayONU, you're new to these forums so, first of all, welcome! I'm sure you'll find the help you need here. In the future, though, please do not duplicate your posts in different forums. It's against the rules. Thanks.

    -Phil
Sign In or Register to comment.