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