Shop OBEX P1 Docs P2 Docs Learn Events
Ping my LED — Parallax Forums

Ping my LED

crazyrobotgirlcrazyrobotgirl Posts: 32
edited 2013-11-16 12:14 in Propeller 1
Hello all. I am alittle stuck trying to get my LED on pin 7 to turn on when my ping senser is reading < than '90, and turn off when => '90.


{{ 
&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9516;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9516;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9516;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
&#9474; dualPing Demo                       &#9474; BR             &#9474; (C)2008             &#9474;  13Sept09     &#9474;
&#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9524;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9524;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9524;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
&#9474;                                                                                            &#9474;
&#9474; Update/extension of the original Savage/Martin object.                                     &#9474;
&#9474; This object is a dual-channel driver for Ping))) ultrasonic ranging units (Channels A & B).&#9474;
&#9474; A single cog is used to monitor both ping units (via counters A & B).                      &#9474;
&#9474;                                                                                            &#9474;
&#9474; Features: &#8226;counters used for time-of-flight measurement (fire & forget)                    &#9474;
&#9474;           &#8226;dual ping driver capable of 2 simultaneous measurements                         &#9474;
&#9474;           &#8226;simple ambient temperature compensation functionality                           &#9474;
&#9474;           &#8226;drop-in replacement for original object                                         &#9474;
&#9474;                                                                                            &#9474;
&#9474; Miscellaneous notes:                                                                       &#9474;
&#9474; &#8226;Although this object can drive 2 Ping))) units, it works perfectly fine for single        &#9474;
&#9474;  channel applications, too...just leave the AorB selector set to A and use channel A.      &#9474;
&#9474; &#8226;If you are using two Ping))) units simultaneously, there is significant possibility       &#9474;
&#9474;  for mutual interference.  To avoid this, it is recommended that the two Ping))) units     &#9474;
&#9474;  be placed AT LEAST a foot apart and that each be angled at least 15-20 deg out from each  &#9474;
&#9474;  other.                                                                                    &#9474;
&#9474; &#8226;It is strongly recommended that the Ping))) units be mounted inside small open-ended      &#9474;
&#9474;  boxes (like project boxes, for example) to help isolate them from noise.  Why?            &#9474;
&#9474;  A room is typically a confined space filled with many hard surfaces and lots of good      &#9474;
&#9474;  sound reflectors that tend to accentuate noise. In addition, the Ping))) unit apparently  &#9474;
&#9474;  has significant ringdown and/or sidelobes that can add to this interference. As such,     &#9474;
&#9474;  it can sometimes be difficult to get consistent/reliable range measurements. A box helps  &#9474;
&#9474;  to: a) block extraneous ambeint noise b) channel energy coming from sidelobes out toward  &#9474;
&#9474;  the front of the sensor where it does no harm, and c) adds directivity to the reciever.   &#9474;
&#9474;  For example: the back wall of my garage is made of cinder blocks. If the bare Ping)))     &#9474;
&#9474;  unit is placed against this wall facing out into the garage, I cannot get a correct range &#9474;
&#9474;  measurement as long as the Ping))) unit is within about 8" or less of this block wall,    &#9474;
&#9474;  presumably because energy from sidelobes bounces off the many scatterers in the rough     &#9474;
&#9474;  surface of the cinder blocks.  If the Ping))) unit is placed in a small box, the unit     &#9474;
&#9474;  can be mounted against the wall without trouble.  Chris' installation appears to be       &#9474;
&#9474;  optimal: [URL]http://forums.parallax.com/forums/default.aspx?f=21&m=176721[/URL]                     &#9474;
&#9474; &#8226;A note on temperature compensation...a temperature variation of 0 deg F to 100 deg F will &#9474;
&#9474;  change the resultant distance measurement by &#8730;(560/460) or ~10%...not a big deal in most  &#9474;
&#9474;  applications, but perhaps worth considering for those cases where a considerable range    &#9474;
&#9474;  of ambient temperature is expected.                                                       &#9474;
&#9474;                                                                                            &#9474;
&#9474; See end of file for terms of use.                                                          &#9474;
&#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
SCHEMATIC                                                                                            
&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;  
2X: &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;        
    &#9474;&#9484;&#9472;&#9472;&#9472;&#9488;         &#9484;&#9472;&#9472;&#9472;&#9488;&#9474;        
    &#9474;&#9474; &#8227; &#9474; PING))) &#9474; &#8227; &#9474;&#9474;            
    &#9474;&#9492;&#9472;&#9472;&#9472;&#9496;         &#9492;&#9472;&#9472;&#9472;&#9496;&#9474;        
    &#9474;    GND +5V SIG    &#9474;        
    &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9516;&#9472;&#9472;&#9472;&#9516;&#9472;&#9472;&#9472;&#9516;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;        
          &#9474;  &#61463;&#9474;   &#61628; 1K           
          &#61464;  &#9492;&#9496;   &#9492;&#61620; to Prop     
&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;  
                                                                           
}}                                                                         
                                                                           
CON                                                                            
  _clkmode        = xtal1 + pll16x    ' System clock &#8594; 80 MHz                  
  _xinfreq        = 5_000_000
'hardware constants
  pingA           = 9         'pin-- ping))) A
  pingB           = 8         'pin-- ping))) B
'software constants
  updaterate      = 8        'range update rate when active (measurement cycles per second) ~15 max
'machine states
  #0, a, b
  
VAR

OBJ   
  debug: "SerialMirror"        'Same as fullDuplexSerial, but can also call from subroutines
  ping: "dualPing"             'Updated ping object (dual channel fire & forget)
  
PUB Init
  waitcnt(clkfreq * 5 + cnt)                      'Start FullDuplexSerial
  Debug.start(31, 30, 0, 57600)
  Debug.Str(String("MSG,Initializing...",13))     'Note: debug formatting is set up for use with plx-daq
  ping.calibrate(68)                              'calibrate ping for ambient temperature=68 F
  main

Pub Main| mark, distA, distB
  mark := cnt                                       'process initialization time
   dira[1] :=1
 
  repeat
    ping.selectAB(a)                                'Select channel A
    distA := ping.ReadPingIn                        'read Ping))) channel A
'    distA := ping.ReadPingTenths                     
    ping.fireping(pingA)                            'restart measurement cycle on channel A
    ping.selectAB(b)                                'Select channel B
    distB := ping.ReadPingIn                        'read Ping))) channel B
'   distB := ping.ReadPingTenths
    ping.fireping(pingB)                            'restart measurement cycle on channel B
'Now have two new measurement values and have restarted the Ping))) measurement cycle...
'Cog is free to do other things while the next measurement cycle is runnning...
    distA := kalman1c(distA,0)                      'Mild filtering to supress msmnt jitter
    distB := kalman1c(distB,b)                      'Mild filtering to supress msmnt jitter
 
    Debug.Str(String("DATA, TIME, TIMER, "))
    Debug.Str(String("  distA, "))                  'kick out measurement data for display
    Debug.Dec(distA)
    Debug.Str(String("  distB, "))
    Debug.dec(distB)
    Debug.Str(String(13))
   
    mark += clkfreq/updaterate
    waitcnt(mark)

                                      'wait for next measurement frame
   
[COLOR=#FF0000]Pub ReadPing| mark, distA, distB
   dira[7]:=1                             'LED on pin 1
 
    repeat
     if distB => 90  
       outa[7] :=0
     if distB < 90
       outa[7] :=1 
     mark += clkfreq/updaterate
    waitcnt(mark)
[/COLOR]
               
pri kalman1c(x_meas,AorB):x_ret
''Simple 1-D kalman filter for scalar random constant with simplified covariance model.
''Uses a constant covariance (and therefore hard-coded kalman gain). Simplifies/speeds
''up calculations. User must manually select gain, where gain is given by:
''gain = const / k_scale (high gain&#61627;believe msmnts)
''Max filter update rate ~9000 samples/sec for 1 cog @ clkfreq=80
'Note: as implemented here, this is actually 2 filters, one for channel A and one for B
'Can easily delete the AorB functionality if only a single channel is desired
  x_prev[AorB] := x_cur[AorB]                        
  x_cur[AorB] := (x_prev[AorB] * k_scale + (k * (x_meas - x_prev[AorB]))) / k_scale
'   Debug.Str(String("  x_meas, "))
'   Debug.dec(x_meas)
'   Debug.Str(String("  x_prev, "))
'   Debug.Dec(x_prev[AorB])
'   Debug.Str(String("  x_cur, "))
'   Debug.dec(x_cur[AorB])
  return x_cur[AorB]

dat
'-----------[ Predefined variables and constants ]-----------------------------
k_scale        long      65_536            '2^16 (scaling varible on K, needed because K is of order 1)                      
k              long      32_768            'Kalman filter gain, in this case 32_768 / 65_536 = 0.5
x_prev         long      0,0               'Kalman filter estimate of x at previous timestep
x_cur          long      0,0               'Kalman filter estimate of x at current timestep

DAT
{{
&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
&#9474;                                     TERMS OF USE: MIT License                                       &#9474;                                                            
&#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
&#9474;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and    &#9474;
&#9474;associated documentation files (the "Software"), to deal in the Software without restriction,        &#9474;
&#9474;including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,&#9474;
&#9474;and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,&#9474;
&#9474;subject to the following conditions:                                                                 &#9474;
&#9474;                                                                                                     &#9474;                        &#9474;
&#9474;The above copyright notice and this permission notice shall be included in all copies or substantial &#9474;
&#9474;portions of the Software.                                                                            &#9474;
&#9474;                                                                                                     &#9474;                        &#9474;
&#9474;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT&#9474;
&#9474;LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  &#9474;
&#9474;IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         &#9474;
&#9474;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION&#9474;
&#9474;WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                      &#9474;
&#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
}}

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-11-16 12:02
    I did not look to hard at your code but I notice you have a ping object in use there, "ping: "dualPing", and you are calling its "ReadPingIn" and "ReadPingTenths" methods.

    You also have a local "ReadPing" method that you are not calling from anywhere. So your tests against 90 are never happening.
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2013-11-16 12:14
    Yhank you , but I figured it out
Sign In or Register to comment.