Creating Raspberry pi 2 driver for distanceUS3

Hi all,

I am trying to create a driver for my US3 distance sensors. But it does not seem to work so far this is what i have but no luck in producing non negative numbers


   public class DistanceUs3 : Module
    {

        private const int MIN_DISTANCE = 2;
        private const int MAX_DISTANCE = 400;
        private const int MIN_FLAG = -2;
        private const int MAX_FLAG = -1;
        private DigitalIO trigger;
        private DigitalIO echo;

        protected async override Task Initialize(ISocket parentSocket)
        {
            echo = await parentSocket.CreateDigitalIOAsync(SocketPinNumber.Three);
            trigger = await parentSocket.CreateDigitalIOAsync(SocketPinNumber.Four, false);

        }

        /// <summary>The value that will be returned when the sensor failed to take an accurate reading.</summary>
        public static int SensorError { get { return -1; } }

        /// <summary>The number of errors to encounter before returning SENSOR_ERROR.</summary>
        public int AcceptableErrors { get; set; }

        public override string Name => "Distance";
        public override string Manufacturer => "GHI Electronics, LLC";

        /// <summary>Takes a number of measurements and returns the average in centimeters.</summary>
		/// <returns>The averaged distance or SensorError.</returns>
		public double GetDistance()
        {
            return this.GetDistance(1);
        }

        /// <summary>Takes a number of measurements and returns the average in centimeters.</summary>
        /// <param name="measurements">The number of measurements to take and average.</param>
        /// <returns>The averaged distance or SensorError.</returns>
        public double GetDistance(int measurements)
        {
            var sum = 0.0;
            var errorCount = 0;

            for (var i = 0; i < measurements; i++)
            {
                var value = this.GetDistanceHelper();

                if (value >= MIN_DISTANCE && value <= MAX_DISTANCE)
                {
                    sum += value;
                }
                else
                {
                    errorCount++;
                    i--;

                    if (errorCount > this.AcceptableErrors)
                        return SensorError;
                }
            }

            return sum / measurements;
        }

        private double GetDistanceHelper()
        {
            trigger.Write(false);


            this.trigger.Write(true);


            this.trigger.Write(false);

            var error = 0;
            while (!this.echo.Read())
                if (error++ > 1000)
                    return SensorError;

            var start = DateTime.Now.Ticks;

            error = 0;
            while (this.echo.Read())
                if (error++ > 1000)
                    return SensorError;

            var end = DateTime.Now.Ticks;

            return (end - start) / (TimeSpan.TicksPerMillisecond / 1000.0) / 58.0;
        }
    }

i think it has to do with this piece

this.echo = GTI.DigitalInputFactory.Create(socket, Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, this);

this does not seem to be available for the pi

The module require some very specific timing that I do not think it will work with Windows or Linux.

@ Gus -

Could you maybe explain what you mean by timing ?

Things need to happen in a very short time, faster than what the operating system can normally handle.

I’m gathering that this is why it was discontinued, even for GHI products?

It seems like there should be some sort of atomic RLP function to get data from the module that would solve any timing issues, but this is from a naive programmer who has yet to do RLP.

I just know that I was able to use those ping modules with great success on a much slower Parallax “Stamp” processor.

It’s such a useful module for robotics that I would think that GHI would have made it a priority to get working with all boards…

@ mtylerjr - correct but we now have the new pulse feedback feature that does many things, including handling distance sensors.

@ Gus -

So there is another way to do distance sensing on the raspberry pi 2 windows iot ? Could you indicate what solution to use ?

Microsoft just announced the new fast gpio drivers, called lightning! Maybe this news driver can do the trick.

Post with all the links https://www.ghielectronics.com/community/forum/topic?id=21905&page=1#msg204942

1 Like

Hi,

With the arival of the lighting drivers i wanted to give them a go with the fez cream but i don’t seem to be lucky with this. I can’t seem to get the lighting code to work. Has anybody had any luck of running it ?

This is what the code looks like it is from the git page. I have added both Microsoft.Iot.Lighting and Microsoft.Iot.SdkFromArduino and are using the windows sdk version 10.0.10563.0 But the Microsoft.Iot namespace cannot be found. What is it that i forgot to do?


       if (Microsoft.Iot.Lightning.Providers.Provider.IsLightningEnabled)
            {
                Windows.Devices.LowLevelDevicesController.DefaultProvider = new Microsoft.Iot.Lightning.Providers.Provider();
            }

@ WE - I’ve got a number of break out boards that I’m planning on releasing drivers for like I did for the AS3935 Lightning Detector, but once I get caught up, I might take a look at creating Windows IoT modifications for them, its all just a matter of how much time I have.

2 Likes

Hi Duke Nukem thank you for your reply can i take it that you allready made the lighting drivers work on windows 10 iot and the cream board ?

Regards,

Wim