Shop OBEX P1 Docs P2 Docs Learn Events
Propeller and IR object detection in Spin — Parallax Forums

Propeller and IR object detection in Spin

Martin_HMartin_H Posts: 4,051
edited 2012-11-29 00:14 in Propeller 1
I'm using a Propeller Protoboard and want to try IR object detection. So I wired up two IR LED's with 1k ohm resistors to pins 3 and 4 and a 38 kHz IR detector on pin 2. I verified that the IR detector works and could decode signals from my TV remote. I also verified that if I bring pins 3 and 4 high I can see the IR LED's turn on in my camera.

So now to write some spin code. I noticed that the IR detect code on learn.parallax.com uses the PropBoe's onboard DAC, so I cant use that. So I grabbed the BS2_Functions and ported the PBasic code from "Robotics with Boe Bot" to Spin. But it doesn't work.

I heard the high pulse tone and see the output written to the terminal window, but it is always 5 which means the IR detector isn't receiving any IR no matter how I position my hand. I added additional logging to verify that the frequency select code works, but the duty cycle of the IR LED is too fast for me to see it in the camera. So I'm not sure what's wrong. I'm wondering if there's some limit on the frequencies the BS2_Functions can generate and if I'm outside that range.

Here's the code in case someone can spot something obvious.
{{
Robotics with the Boe-Bot - DisplayBothDistances.bs2
 Test IR detector distance responses of both IR LED/detector pairs to
 frequency sweep.
}}

CON
  _clkmode      = xtal1 + pll16x
  _xinfreq      = 5_000_000

  SPEAKER = 15
  IR_LEFT = 4
  IR_RIGHT = 3
  SENSOR = 2

OBJ
  BS2   : "BS2_Functions"    ' Create BS2 Object

VAR
  long frequencies[5]
  byte irDetectLeft
  byte irDetectRight
  byte distanceLeft
  byte distanceRight

PUB Initialize
{{
-----[ Initialization ]-----------------------------------------------------
}}
  BS2.start (31,30)             ' Initialize BS2 Object timing, Rx and Tx pins for DEBUG

  BS2.FREQOUT(SPEAKER, 1000, 2000) ' Battery Tester
  BS2.Debug_CLS
  BS2.Debug_Str(string("IR OBJECT ZONE",13))
  BS2.Debug_Str(string("Left   Right",13))
  BS2.Debug_Str(string("-----  -----",13))

  frequencies[0] := 37500
  frequencies[1] := 38250
  frequencies[2] := 39500
  frequencies[3] := 40500
  frequencies[4] := 41500

  Main

Pub Main
{{
Main loop
}}
  repeat
    Get_Distances
    Display_Distances

Pri Get_Distances | freqSelect, irFrequency
{{
  Use an IR sweep to estimate distances.
}}
  distanceLeft := 0
  distanceRight := 0
  repeat freqSelect from 0 TO 4
    irFrequency := frequencies[freqSelect]
    BS2.FREQOUT(IR_LEFT, 1, irFrequency)
    irDetectLeft := BS2.IN(SENSOR)
    distanceLeft := distanceLeft + irDetectLeft
    BS2.FREQOUT(IR_RIGHT, 1, irFrequency)
    irDetectRight := BS2.IN(SENSOR)
    distanceRight := distanceRight + irDetectRight
    BS2.PAUSE(50)

Pri Display_Distances
{{
}}
  BS2.Debug_CRSRXY(2,3)
  BS2.Debug_DEC(distanceLeft)
  BS2.Debug_CRSRXY(9,3)
  BS2.Debug_DEC(distanceRight)

Comments

  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-11-27 20:25
    You may want to unleash yourself from an object that turns the Propeller into a lesser processor. Creating a square wave output on a pin is pretty easy -- this is a bit of code I have built into my standard template:
    pub set_freq(ctrx, px, fx)
    
    '' Sets ctrx to frequency fx on pin px (NCO/SE mode)
    '' -- fx in hz
    '' -- use fx of 0 to stop counter that is running
    
      if (fx > 0)                             
        fx := ($8000_0000 / (clkfreq / fx)) << 1            ' convert freq for NCO mode    
        case ctrx
          "a", "A":
            ctra := ((%00100) << 26) | px                   ' configure ctra for NCO on pin
            frqa := fx                                      ' set frequency
            dira[px] := 1                                   ' make pin an output
         
          "b", "B":                         
            ctrb := ((%00100) << 26) | px  
            frqb := fx                    
            dira[px] := 1
    
      else
        case ctrx
          "a", "A":
            ctra := 0                                       ' disable counter
            outa[px] := 0                                   ' clear pin/driver 
            dira[px] := 0                                  
         
          "b", "B":                         
            ctrb := 0 
            outa[px] := 0  
            dira[px] := 0
    


    Here's the trick: the output pin controls the CATHODE side of the circuit. You can then gate the IR LED (without touching the counter) by writing a "1" (modulation off) or "0" to the pin.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-11-28 06:51
    Thanks, I'll give this a try tonight.

    I know the BS2 functions are not optimal, but I use them when porting working code from the basic stamp. Usually they make it quick to get something working and then I fine tune by moving to a more native programming model.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-11-28 10:19
    @Martin_H
    There is really nothing wrong with using the BasicStamp code in the Propeller to get your hardware setup confirmed and working before you switch to something else.

    In fact, it is a smart thing to do if you are learning a new language. Going back and forth with wondering if the hardware is wrong or the code is wrong is a huge waste of time.

    The Basic Stamp code is there for a reason -- to help users migrate over from the BasicStamp. After all, why use a $50 BasicStamp if you can get a Propeller in some setups for less. And of course, you eventually can do more, and learn more.

    IR remote control is a huge favorite of mine.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-11-28 18:41
    Jon, I used your frequency method coupled with waitcnt and that did the trick. I think the problem is that the BS2's FREQOUT command couldn't deal with duration that small and didn't produce any IR pulse.

    Thanks a bunch.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-11-29 00:14
    Glad you got it to work. I've done a lot of work with IR transmission and reception. If you lookup the DEFCON badge project you'll find the objects I wrote for that that allow the badges to "talk" to each other using SIRCS (one of the easier protocols).

    I don't want anyone to think I have anything against the BASIC Stamp; my company (EFX-TEK) uses the BS1 and the BS2 (in our Prop-1 and Prop-2 controllers). But for every design I've done with the Propeller I've coded it in Spin and Assembly; even though I once had a business card when I worked for Parallax that labelled me "King of BASIC Stamps." Don't handcuff yourself; do it right the first time because we rarely do it over.
Sign In or Register to comment.