Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test program for distance sensor HcSr04 #80

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Raspberry.IO.Components/Controllers/Pca9685/PwmChannel.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ public enum PwmChannel
C9 = 9,
C10 = 10,
C11 = 11,
C12 = 12,
C13 = 13,
C14 = 14,
C15 = 15
}
}
2 changes: 1 addition & 1 deletion Raspberry.IO/Raspberry.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
9 changes: 8 additions & 1 deletion RaspberrySharp.IO.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raspberry.IO.GeneralPurpose", "Raspberry.IO.GeneralPurpose\Raspberry.IO.GeneralPurpose.csproj", "{281C71ED-C36D-408E-8BAA-75C381DC17E7}"
EndProject
Expand Down Expand Up @@ -66,6 +66,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raspberry.IO.Interop", "Ras
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Gpio.Ds1307", "Tests\Test.Gpio.Ds1307\Test.Gpio.Ds1307.csproj", "{E9412139-F9EA-4E39-AB7C-CE775ED06C82}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Components.Sensors.Distance.HcSr04", "Tests\Test.Components.Sensors.Distance.HcSr04\Test.Components.Sensors.Distance.HcSr04.csproj", "{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -172,6 +174,10 @@ Global
{E9412139-F9EA-4E39-AB7C-CE775ED06C82}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9412139-F9EA-4E39-AB7C-CE775ED06C82}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9412139-F9EA-4E39-AB7C-CE775ED06C82}.Release|Any CPU.Build.0 = Release|Any CPU
{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -198,6 +204,7 @@ Global
{99EB3D1A-F0B7-454E-BB50-9B2F5349BC5B} = {C39D0CC3-C0F2-4B58-8779-2CCB5B52534C}
{CAF876A0-0FCB-44F7-96F1-53704CB6015F} = {C39D0CC3-C0F2-4B58-8779-2CCB5B52534C}
{E9412139-F9EA-4E39-AB7C-CE775ED06C82} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5} = {15ECD485-B3FD-4B6F-8491-7489DFFC89BC}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
Expand Down
6 changes: 6 additions & 0 deletions Tests/Test.Components.Sensors.Distance.HcSr04/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
157 changes: 157 additions & 0 deletions Tests/Test.Components.Sensors.Distance.HcSr04/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System;
using Raspberry.IO.Components.Sensors.Distance.HcSr04;
using Raspberry.IO.GeneralPurpose;
using UnitsNet;
using System.Threading;

namespace Test.Components.Sensors.Distance.HcSr04
{
class Program
{
private static GpioConnectionDriver driver;
private static HcSr04Connection sensor;
private static GpioOutputBinaryPin triggerGpioPin;
private static GpioInputBinaryPin echoGpioPin;

private static int triggerGpioPinSetting = 0;
private static int echoGpioPinSetting = 0;
private static int delay = 500;
private static int readError = 0;

static void Main(string[] args)
{
//Configure GPIO pins bellow or provide in console
//triggerGpioPinSetting = 20;
//echoGpioPinSetting = 26;

//Reads GPIO pins from console
if (triggerGpioPinSetting == 0)
triggerGpioPinSetting = GetGpioPin("trigger");
if (echoGpioPinSetting == 0)
echoGpioPinSetting = GetGpioPin("echo");

Console.CursorVisible = false;

//Creating sensor
driver = new GpioConnectionDriver();
triggerGpioPin = new GpioOutputBinaryPin(driver, (ProcessorPin)triggerGpioPinSetting);
echoGpioPin = new GpioInputBinaryPin(driver, (ProcessorPin)echoGpioPinSetting);
sensor = new HcSr04Connection(triggerGpioPin, echoGpioPin);

while(true)
{
if (Console.KeyAvailable)
KeyPressed();

try
{
//Get data from sensor and print them
Length distance = sensor.GetDistance();
PrintHeader();
PrintData(distance);
PrintFooter();
}
catch
{
readError++;
}

Thread.Sleep(delay);
}
}

/// <summary>
/// Console read key support
/// </summary>
private static void KeyPressed()
{
ConsoleKeyInfo cki = Console.ReadKey();
switch (cki.Key)
{
case ConsoleKey.UpArrow:
delay = delay > 50 ? delay - 50 : delay;
break;
case ConsoleKey.DownArrow:
delay = delay < 1000 ? delay + 50 : delay;
break;
case ConsoleKey.Escape:
Environment.Exit(0);
break;
}

if (Console.KeyAvailable)
KeyPressed();
}

/// <summary>
/// Get GPIO pins from console
/// </summary>
/// <param name="pinName">GPIO pin usage</param>
/// <param name="message">Error message</param>
/// <returns></returns>
private static int GetGpioPin(string pinName, string message = "")
{
PrintHeader();
Console.WriteLine();

if (!string.IsNullOrEmpty(message))
{
Console.WriteLine(message);
Console.WriteLine();
}

Console.Write(string.Format(" Please enter GPIO pin number for {0} pin: ", pinName));

string answer = Console.ReadLine();
int pin;
bool result = int.TryParse(answer, out pin);
if(result)
{
if (pin > 32)
return GetGpioPin(pinName, string.Format(" Error. Value {0} is to big", pin));
return pin;
}
else
{
return GetGpioPin(string.Format(pinName, "Error. Unable parse string {0}", answer));
}
}

#region Print Methods

private static void PrintHeader()
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" #################################");
Console.WriteLine(" # Demo pogram for sensor HCSR04 #");
Console.WriteLine(" #################################");
Console.WriteLine();
if(triggerGpioPinSetting != 0)
Console.WriteLine(" Trigger pin GPIO{0}", triggerGpioPinSetting);
if(echoGpioPinSetting != 0)
Console.WriteLine(" Echo pin GPIO{0}", echoGpioPinSetting);
}

private static void PrintData(Length length)
{
Console.WriteLine(" Reads {0:0.0} per secound [Hz]", 1000.0 / delay);
Console.WriteLine();
Console.WriteLine(" Read errors {0}", readError);
Console.WriteLine(" Length {0,5:0.0} cm", length.Centimeters);
Console.WriteLine(" Length {0,5:0.0} inch", length.Inches);
}

private static void PrintFooter()
{
if (Console.WindowHeight < 17)
return;
Console.SetCursorPosition(0, Console.WindowHeight - 4);
Console.WriteLine(" Press arrow up to increase read sppead");
Console.WriteLine(" Press arrow down to decrease read sppead");
Console.WriteLine(" Press escape key to exit");
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Test.Components.Sensors.Distance.HcSr04")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Test.Components.Sensors.Distance.HcSr04")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a2d800af-749c-4b8f-8cc6-7b5e99f3eac5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A2D800AF-749C-4B8F-8CC6-7B5E99F3EAC5}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Test.Components.Sensors.Distance.HcSr04</RootNamespace>
<AssemblyName>Test.Components.Sensors.Distance.HcSr04</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnitsNet, Version=3.49.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\UnitsNet.3.49.0\lib\net35\UnitsNet.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Raspberry.IO.Components\Raspberry.IO.Components.csproj">
<Project>{8388cfca-e3db-43f7-b049-2cb195211ce8}</Project>
<Name>Raspberry.IO.Components</Name>
</ProjectReference>
<ProjectReference Include="..\..\Raspberry.IO.GeneralPurpose\Raspberry.IO.GeneralPurpose.csproj">
<Project>{281c71ed-c36d-408e-8baa-75c381dc17e7}</Project>
<Name>Raspberry.IO.GeneralPurpose</Name>
</ProjectReference>
<ProjectReference Include="..\..\Raspberry.IO\Raspberry.IO.csproj">
<Project>{ace64f17-87e5-43e7-97a0-bdde19059c61}</Project>
<Name>Raspberry.IO</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
4 changes: 4 additions & 0 deletions Tests/Test.Components.Sensors.Distance.HcSr04/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="UnitsNet" version="3.49.0" targetFramework="net461" />
</packages>