Main Site Documentation

Distance US3 Driver - Possible Fix?


#1

I think I figured out what was wrong with the Distance US3 driver and it might be a .NetMF problem (or maybe this is how it works now) which might impact other modules.

I needed a ranger finder for my robot that I’m building for my presentation tomorrow night (like an idiot I promised a robot and some other devices) and so after blowing a day working on drivers for different ranging devices, I might have found out what was wrong with the Distance US3 Driver, but I thought I better check and see if its a known problem and waiting on a fix.

I’ll do a full CodeShare Wednesday if anyone thinks its a good idea, but the guts of it is:


       public double GetDistance()
        {
            long start = 0;
            long end = 0;

            int counter = 0;
            
            this.trigger.Write(false);
            Thread.Sleep(10);

            this.trigger.Write(true);
            Thread.Sleep(1);
            this.trigger.Write(false);

            while (!this.echo.Read())
            {
                if (counter++ > 1000)
                    return -1.0;
                Thread.Sleep(0);
            }

            start = Utility.GetMachineTime().Ticks;

            counter = 0;

            while (echo.Read())
            {
                if (counter++ > 1000)
                    return -2.0;
                Thread.Sleep(0);
            }

            end = Utility.GetMachineTime().Ticks;

            if (counter == 0)
                return -3.0;

             Debug.Print("delta: " + (end - start).ToString());
            double echoTime = (((end - start) / TicksPerMicrosecond)) / 2.0;
            Debug.Print("Echo: " + echoTime.ToString());
            double distance = echoTime * 34.32;
            Debug.Print("Distance: " +distance.ToString());

            return distance;
        }

The issue was I was looking for the read to go true, when in fact when it goes false works, hence why I wonder if its a .NetMF thing.

My code might need some tuning etc, but it seemed to be giving me workable distances which at the moment is good enough for the girls I date.


#2

@ Duke Nukem - I’m not sure I understand the issue you were seeing and how you fixed it.


#3

The line that is different is the second while loop

while (echo.Read())

note it is not a !echo.Read() as it is in the existing driver which doesn’t work

I thought this was surprising as I had a Seeed ultrasonic device that I had written a driver for sometime ago and the echo capture loop on it needed to be changed as well to get it to work with 4.3

I’ll do up a full 4.3 driver and post it in CodeShare tomorrow now that my IoT presentation is finished.


#4

@ Duke Nukem - I looked through the history of 4.2 and 4.3 for the distance sensor and the first loop was always while(!echo) and the second was always while(echo), the same as it is in your post. Where did you see it different?