Shop OBEX P1 Docs P2 Docs Learn Events
Is my logic correct? — Parallax Forums

Is my logic correct?

lardomlardom Posts: 1,659
edited 2013-02-16 11:25 in Propeller 1
I'm trying to find a solution for the occasional false reading I get from my Ping sensor. I want two or three values to match out of four returned from the sensor. I decided to try to copy four ping return values to four variables, test all combinations and increment a fifth variable for each match. Is there a better way?
PUB Test_combinations | a, b, c, d, temp

    a := sensordata
    b := sensordata 
    c := sensordata 
    d := sensordata
    
    if a == b
      temp := a
      temp += 1
    if a == c
      temp := a
      temp += 1
    if a == d
      temp := a
      temp += 1
    if b == c
      temp := b
      temp += 1
    if b == d
      temp := b
      temp += 1  
    if c == d
      temp := c
      temp += 1
    if temp => 2
      call_method 

Comments

  • JonnyMacJonnyMac Posts: 9,191
    edited 2013-02-14 10:16
    You may need to give yourself some variance as it's not likely you'll get an exact match from two side-by-side sensors.
  • lardomlardom Posts: 1,659
    edited 2013-02-14 10:25
    I'm using one Ping sensor but I've gotten an occasional return value that looked like a glitch. I don't know, maybe a bypass cap would solve it.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-02-14 10:29
    One way this is commonly done is through a median filter. In a table of 7 successive readings, each new reading replaces the oldest one. The output of the filter is the median of the 7, that is half higher and half lower.
  • lardomlardom Posts: 1,659
    edited 2013-02-14 11:00
    I just did a quick search on "median filter" and it makes sense. I could sort the values high to low and eliminate anything I specify 'out of range'.
  • ManAtWorkManAtWork Posts: 2,178
    edited 2013-02-16 02:21
    Another good method is: take five readings, discard the highest and lowest and calculate the average of the remaining three. (Speedcubers use this to measure solving times to filter out good/bad luck to some degree since the results are always influenced by how "random" the scrambles are) Finding the lowest and highest value is much faster than a full sort.
  • lardomlardom Posts: 1,659
    edited 2013-02-16 05:34
    @ManAtWork, I'll experiment with that approach. I realized I don't need an array at all. I'm also experimenting with letting the Ping sensor warm up so I inserted a "repeat 40" loop before it moves to the main loop. I'm still tweaking it but the result has been better.
    repeat idx from 1 to size
       x := sensordata
       if x > check
         check := x
         Hi_idx := idx
    
    My first attempts filled almost four pages.
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-16 10:39
    @ManAtWork:
    Not sure whether this is a good idea for measuring movement, as the latest measurement will always be smaller than the measurement before if you move towards something. If your move away from something the latest measurement is always bigger. With this kind of filter in both cases you would ignore the latest reading. might be fine if the robot only moves at a constant speed, as then you simply have to bias the value calculated. But if it has variable speed you also need a variable bias.

    What happens if a object crosses the robots track? It decreases the reaction time, as you at least need a number of samples which rapidly change the average.

    @lardom:
    The last code you posted (#7) looks a bit strange. What you need is an array of sensor-readings which is filled up as a ring-buffer. Then you loop over all elements and sum them up, meanwhile find the max- and min-value and subtract them in the end.
  • lardomlardom Posts: 1,659
    edited 2013-02-16 11:25
    I got a case of the 'blues' because I spotted a bug I didn't see before. I have two steppers in separate cogs. Their movements match as long as I call matching variables from within the two cogs. I just noticed a lag between the motors when I call one or two global methods from the "Ping" object, which is a child object. I'm bummed because I thought I had solved that problem. My goal, BTW, is to have the robot find the longest distance after rotating 360° and then move forward in that direction.
Sign In or Register to comment.