Calling Variables in Cogs
HayONU
Posts: 3
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:
Post Edited (HayONU) : 4/13/2009 10:54:21 PM GMT
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:
{{ Pin connections: P0 Program Switch 0 P21 7 Segment Display - B P1 Program Switch 1 P22 7 Segment Display - A P2 Program Switch 2 P23 7 Segment Display - DP P3 Program Switch 3 P24 Spare P4 Program Switch 4 P25 Relay P5 ADC CLOCK P26 Relay P6 ADC DOUT P27 Relay P7 ADC CS TB1 P28 SCL EEPROM P8 ADC CS TB2 P29 SDA EEPROM P9 ADC CS TB3 P30 Programming Port P10 ADC CS TB4 P31 Programming Port P11 ADC CS TB5 P12 ADC CS TB6 P13 ADC CS TB7 P14 ADC CS TB8 P15 Relay P16 7 Segment Display - G P17 7 Segment Display - F P18 7 Segment Display - E P19 7 Segment Display - D P20 7 Segment Display - C }} 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 ADC_CLK = 5 ADC_DIN = 6 ADC_CS5 = 7 ' 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 BacktoBack Repeat case back_left 'or back_right 'If detected stay full speed 0..440: 'displays a zero outa[noparse][[/noparse]16..22]:= %0111111 outa[noparse][[/noparse]27]:=0 outa[noparse][[/noparse]26]:=0 441..880: 'displays a one Outa[noparse][[/noparse]16..22]:= %0000110 outa[noparse][[/noparse]27]:=0 outa[noparse][[/noparse]26]:=0 881..1320: 'displays a two Outa[noparse][[/noparse]16..22]:= %1011011 outa[noparse][[/noparse]27]:=0 outa[noparse][[/noparse]26]:=0 1321..1760: 'displays a three Outa[noparse][[/noparse]16..22]:= %1001111 outa[noparse][[/noparse]27]:=0 outa[noparse][[/noparse]26]:=0 1761..4095: 'displays a four Outa[noparse][[/noparse]16..22]:= %1100110 outa[noparse][[/noparse]27]:=0 outa[noparse][[/noparse]26]:=0 other: 'displays a five Outa[noparse][[/noparse]16..22]:= %1101101' return Pub SidetoSideOppDir 'Repeat 'Go backward 1 sec 'Spin to the right 10 degrees 'If object detected 'OUTA[noparse][[/noparse]27]:=0 full speed ahead 'OUTA[noparse][[/noparse]27]:=0 'elseif spin 10 degrees to the right return Pub SidetoSideSameDir return PUB Read_IR_Sensor dira[noparse][[/noparse]0..4]:= %00000 'Program Switches Output dira:= 1 'ADC Clock Output dira[noparse][[/noparse]6]:= 0 'ADC Data IN Input dira[noparse][[/noparse]7..14]:= %11111111 'ADC Converters Output dira[noparse][[/noparse]15]:= 1 'Relay Output dira[noparse][[/noparse]16..23]:= %11111111 'Seven Segment Display Output dira[noparse][[/noparse]25..27]:= %111 'Relays Output OUTA[noparse][[/noparse]15]:=1 OUTA[noparse][[/noparse]25..27]:= %111 OUTA[noparse][[/noparse]ADC_CS5]:=1 OUTA[noparse][[/noparse]ADC_CS6]:=1 OUTA[noparse][[/noparse]ADC_CS7]:=1 OUTA[noparse][[/noparse]ADC_CS8]:=1 OUTA[noparse][[/noparse]ADC_CS9]:=1 OUTA[noparse][[/noparse]ADC_CS10]:=1 OUTA[noparse][[/noparse]ADC_CS11]:=1 OUTA[noparse][[/noparse]ADC_CS12]:=1 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 PUB i_o_setup dira[noparse][[/noparse]0..4]:= %00000 'Program Switches Output dira:= 1 'ADC Clock Output dira[noparse][[/noparse]6]:= 0 'ADC Data IN Input dira[noparse][[/noparse]7..14]:= %11111111 'ADC Converters Output dira[noparse][[/noparse]15]:= 1 'Relay Output dira[noparse][[/noparse]16..23]:= %11111111 'Seven Segment Display Output dira[noparse][[/noparse]25..27]:= %111 'Relays Output OUTA[noparse][[/noparse]15]:=1 OUTA[noparse][[/noparse]25..27]:= %111 OUTA[noparse][[/noparse]ADC_CS5]:=1 OUTA[noparse][[/noparse]ADC_CS6]:=1 OUTA[noparse][[/noparse]ADC_CS7]:=1 OUTA[noparse][[/noparse]ADC_CS8]:=1 OUTA[noparse][[/noparse]ADC_CS9]:=1 OUTA[noparse][[/noparse]ADC_CS10]:=1 OUTA[noparse][[/noparse]ADC_CS11]:=1 OUTA[noparse][[/noparse]ADC_CS12]:=1 return
Post Edited (HayONU) : 4/13/2009 10:54:21 PM GMT
Comments
-Phil
2) There are no limits that I know of regarding calling methods within one object (file). There are also no limits regarding the use of global variables throughout one object (file). The only limitations have to do with calling from one object to another. There is no direct means to access variables in one object from another. You can access named constants and PUBlic methods from another object using the name of the object (object#constant and object.method).
3) In an example as long and complex as yours, you really have to be much more specific about what's happening or not happening. It helps a lot, if possible, to shorten your program by eliminating things not necessary to illustrate what's happening.
two things:
1.) the codeline DIRA := 1 seems strange to me
there is no IO-Pin specified like DIRA[noparse][[/noparse]17] := 1
2.) to test if the VARIABLES work OK modify your read_adc-method
that it returns a HARDCODED value
f.e. simple the adc_channel-number which is the parameter
so ONLY for this debugging add a line of code
If this is working properly you know it is NOT the variable
As you are working with 80MHz
a delay of 80_000_000 means 1 second
replace the codelines
with
best regards
Stefan
It's the same as
DIRA[noparse][[/noparse]0] := 1
But I guess here it is simply the formatting-problem "[noparse][[/noparse] 5 ]" is taken as font-size, if you don't have spaces between [noparse][[/noparse] and 5 and ].
For better reading it's of course OK to have several dira assignments, but you can do it readable in one command-line as well:
That's readable and does not need as much memory.
Why don't you call the i_o_setup in Read_IR_Sensor as well? At a first glance, the setup-code in Read_IR_Sensor is doing the same.
In your original post you state: "Are there any limitations to calling methods from one cog to another ..."
If your program is running in COG 1 you can't call methods in COG 2 from there. If you call a method in COG 1 it will also run in COG 1. If you want to run functions in COG 2 triggered by COG 1 you need a variable that both use. COG 1 writes the number of the command into it and COG 2 polls the variable to see if it should do something setting the value to 0 when it's done. So, COG 1 can wait for it or at leas wait until writing the next command value.
What do you mean with 'calling variables'?
All the methods like BackToBack, HeadToHead ... are endless loops. As they all run in one single COG, this COG will be blocked. Maybe what you intented to do was:
Main program in COG 1
Start sensor reading in COG 2
repeat reading the·program switches
->·· if program switches·set,·start the·according method in COG 3 (using COGINIT)
Problem here might be that COGINIT will stop the previous method wherever it was. Depending on the functionality, there might be a need to synchronize that, so that the method is stopped at a known place.
Post Edited (MagIO2) : 4/14/2009 8:15:12 AM GMT