code problem...passing variable, either cog or object related
mikea
Posts: 283
Hi, I have two objects that use counters. the parent :TestIrDutyDistanceDetector, and the child object: ir detector. Ir detector calculates a value that the method "scale_output" in the parent oblect calls for, then scale_output uses the value to turn on an led to varying brightness. I've copied the way parallax serial terminal called for the same value from the child object(I no longer use serial terminal) so I know it works, but in the scale_output method it's not. So now I'm not sure if it's a cog issue because both objects use counters. There are extra cogs, so I have each object in there own. Can someone take a peek and see where I'm going wrong. Thanks.
parent:
child:
parent:
'' TestIrDutyDistanceDetector.spin '' Test distance detection with IrDetector object. CON _xinfreq = 5_000_000 _clkmode = xtal1 + pll16x ''''''''''''''''''parent object to ir detector'''''''''''''''''''''''''''''' var long stack [100] OBJ ir : "IrDetector" pst : "Parallax Serial Terminal" PUB scale_output | tc, tHa, t,trans, dist ctra[30..26] := %00100 ' Configure Counter A to NCO ctra[5..0] := 8 frqa := 1 dira[8]~~ cognew(ir.init(1,2,0),@stack) '(anode,cathode,ir detector in) repeat ' Repeat PWM signal dist:=ir.Distance '(((((((( ir.distance isn't updating here,(commenting ir.distance and putting in a value makes code work) trans:=(256-dist)*12 tHa:=trans tC := 3072' Set up cycle and high times t := cnt ' Mark counter time phsa := -tHa ' Set up the pulse t += tC ' Calculate next cycle repeat waitcnt(t) ' Wait outa[8]~
child:
CON scale = 16_777_216 ' 2³²÷ 256 ''''''''''''''''''''''child object to test ir duty distance detector'''''''''''''''''''''''' OBJ SquareWave : "SquareWave" ' Import square wave cog object VAR long anode, cathode, recPin, dMax, duty PUB init(irLedAnode, irLedCathode, irReceiverPin) anode := irLedAnode cathode := irLedCathode recPin := irReceiverPin PUB distance : dist {{ Performs a duty sweep response test on the IR LED/receiver and returns dist, a zone value from 0 (closest) to 256 (no object detected). }} 'Start 38 kHz signal. dira[0]~ SquareWave.Freq( anode,1, 38000) ' ctrb 38 kHz dira[anode]~~ 'Configure Duty signal. ctra[30..26] := %00110 ' Set ctra to DUTY mode ctra[5..0] := cathode ' Set ctra's APIN frqa := phsa := 0 ' Set frqa register dira[cathode]~~ ' Set P2 to output dist := 0 repeat duty from 0 to 255 ' Sweep duty from 0 to 255 frqa := duty * scale ' Update frqa register waitcnt(clkfreq/128000 + cnt) ' Delay for 1/128th s dist += ina[recPin] ' Object not detected? Add 1 to dist.
Comments
I don't know if it's your only problem but it's a major one.
You can only launch Spin methods from the same object. The code above will have unintended results.
Just call the "init" method without launching it in a new cog.
BTW, It's a lot easier to look at code when it's attached as an archive.
Edit: Since code from both objects is running in a single cog, I'd suggest using counter B in one of the methods.
Only start a new cog when it makes sense to do so. There's no need in this case.
When you start a new cog with a Spin method, the method should have an endless loop. You don't need the distance method to run in its own loop/cog.
After a program has been compiled you can use the "Archive" feature in the "File" menu of the Propeller Tool. I think the other Propeller editors also have this feature.
I used my DropBox as the install location for version 1.3 of the Propeller Tool but I used the default settings for version 1.27. When I used the default settings this is where Windows hid my files.
There is a way to keep Windows from doing this but I don't know how to do it.
I used to keep a shortcut to the Virtual Store on my desktop and periodically move the files back where they belonged.