Shop OBEX P1 Docs P2 Docs Learn Events
code problem...passing variable, either cog or object related — Parallax Forums

code problem...passing variable, either cog or object related

mikeamikea Posts: 283
edited 2015-02-25 18:48 in Propeller 1
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:
'' 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

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-25 15:29
    cognew(ir.init(1,2,0),@stack)
    

    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.
  • mikeamikea Posts: 283
    edited 2015-02-25 15:39
    Thanks Duane, i'll start the new cog from inside the child object. Rookie question, what's the best way to post code as an archive?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-25 15:42
    mikea wrote: »
    Thanks Duane, i'll start the new cog from inside the child object. Rookie question, what's the best way to post code as an archive?

    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.
  • mikeamikea Posts: 283
    edited 2015-02-25 16:07
    I have it archived now, just having a problem getting it here. I've tried cut,copy, drag and drop. The first two didn't work, the last one had a problem with it being zipped.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-25 16:29
    If you click on "Go Advanced", there will the a "Manage Attachments" button below the editor window. From "Manage Attachments" you should be able to upload the zip file.
  • mikeamikea Posts: 283
    edited 2015-02-25 17:06
    I got to the upload screen, but the archives weren't there. It says they're saving in the library, but they're not there. I opened the file/archive,save box from the prop tool where I saved them previously and tried to drop them again. That did't work. I tried unzipping them to go from there and the computer had an error. I'll try again tomorrow, I'm going to look for a hammer!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-25 17:21
    Windows doesn't like files to be saved in the "Program Files" folder. If you installed the Propeller Tool in the default location, Windows will hide your files in the Virtual Store.

    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.

    attachment.php?attachmentid=113276&d=1424913386

    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.
    819 x 44 - 20K
  • mikeamikea Posts: 283
    edited 2015-02-25 18:39
    Thanks for sharing the info about windows, I'm still trying to track down the virtual store or their exact location.( I got the code to work! It seems to be related to the counters/cogs.)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-25 18:48
    I think you need to make hidden folders visible to find the Virtual Store.
Sign In or Register to comment.