add raspicontrol (c#)

main
Simon Frei 4 years ago
parent 7ffd5b9d7f
commit 408d1fee4c
  1. 10
      ADIS_Csharp/ADIS_Csharp.sln
  2. 66
      ADIS_Csharp/RaspiControl/Joystick.cs
  3. 18
      ADIS_Csharp/RaspiControl/JoystickButton.cs
  4. 15
      ADIS_Csharp/RaspiControl/JoystickEventArgs.cs
  5. 53
      ADIS_Csharp/RaspiControl/Navigation.cs
  6. 54
      ADIS_Csharp/RaspiControl/Program.cs
  7. 20
      ADIS_Csharp/RaspiControl/RaspiControl.csproj

@ -19,9 +19,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MqttTest", "MqttTest\MqttTe
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RobotLib", "RobotLib\RobotLib.csproj", "{915E2889-F10D-4D02-8313-308F642EC64F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RobotLib", "RobotLib\RobotLib.csproj", "{915E2889-F10D-4D02-8313-308F642EC64F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RobotConsoleClient", "RobotConsoleClient\RobotConsoleClient.csproj", "{7FFC4B71-FFC2-45A1-941D-4B0A78C20B89}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RobotConsoleClient", "RobotConsoleClient\RobotConsoleClient.csproj", "{7FFC4B71-FFC2-45A1-941D-4B0A78C20B89}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RobotClientWpf", "RobotClientWpf\RobotClientWpf.csproj", "{1D715C65-A8D8-45D3-AE63-D88C60337505}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RobotClientWpf", "RobotClientWpf\RobotClientWpf.csproj", "{1D715C65-A8D8-45D3-AE63-D88C60337505}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RaspiControl", "RaspiControl\RaspiControl.csproj", "{CE8FA2F2-2DE4-40B6-A933-988E135C5039}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -69,6 +71,10 @@ Global
{1D715C65-A8D8-45D3-AE63-D88C60337505}.Debug|Any CPU.Build.0 = Debug|Any CPU {1D715C65-A8D8-45D3-AE63-D88C60337505}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D715C65-A8D8-45D3-AE63-D88C60337505}.Release|Any CPU.ActiveCfg = Release|Any CPU {1D715C65-A8D8-45D3-AE63-D88C60337505}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D715C65-A8D8-45D3-AE63-D88C60337505}.Release|Any CPU.Build.0 = Release|Any CPU {1D715C65-A8D8-45D3-AE63-D88C60337505}.Release|Any CPU.Build.0 = Release|Any CPU
{CE8FA2F2-2DE4-40B6-A933-988E135C5039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE8FA2F2-2DE4-40B6-A933-988E135C5039}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE8FA2F2-2DE4-40B6-A933-988E135C5039}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE8FA2F2-2DE4-40B6-A933-988E135C5039}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi;
namespace RaspiControl {
public class Joystick {
private static readonly IGpioPin joystickUpPin = Pi.Gpio[BcmPin.Gpio19];
private static readonly IGpioPin joysticDownPin = Pi.Gpio[BcmPin.Gpio13];
private static readonly IGpioPin joystickLeftPin = Pi.Gpio[BcmPin.Gpio06];
private static readonly IGpioPin joystickRightPin = Pi.Gpio[BcmPin.Gpio05];
private static readonly IGpioPin joystickPushPin = Pi.Gpio[BcmPin.Gpio26];
private static readonly List<IGpioPin> inputPins = new List<IGpioPin> { joystickUpPin, joysticDownPin,
joystickLeftPin, joystickRightPin, joystickPushPin };
#region Properties
public JoystickButton State {
get {
JoystickButton state = JoystickButton.None;
if (!joystickLeftPin.Read()) state |= JoystickButton.Left;
if (!joystickRightPin.Read()) state |= JoystickButton.Right;
if (!joystickUpPin.Read()) state |= JoystickButton.Up;
if (!joysticDownPin.Read()) state |= JoystickButton.Down;
if (!joystickPushPin.Read()) state |= JoystickButton.Center;
return state;
}
}
#endregion
#region Events
public event EventHandler<JoystickEventArgs>? JoystickChanged;
private void OnJoystickChanged(JoystickButton state) {
Console.WriteLine($"Joystick was pushed: {state}");
this.JoystickChanged?.Invoke(this, new JoystickEventArgs(state));
}
#endregion
public Joystick() {
Pi.Init<BootstrapWiringPi>();
foreach (var pin in inputPins) {
pin.PinMode = GpioPinDriveMode.Input;
}
Thread thread = new Thread(Run);
thread.IsBackground = true;
thread.Start();
}
private void Run() {
JoystickButton oldState = State;
while (true) {
JoystickButton newState = State;
if (oldState != newState) {
oldState = newState;
OnJoystickChanged(newState);
}
Thread.Sleep(50);
}
}
}
}

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RaspiControl {
public enum JoystickButton {
None = 0,
Left = 1,
Right = 2,
Up = 4,
Down = 8,
Center = 16
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RaspiControl {
public class JoystickEventArgs : EventArgs {
public JoystickButton Button { get; }
public JoystickEventArgs(JoystickButton button) {
Button = button;
}
}
}

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RaspiControl {
internal class Navigation {
private int angleRight;
private int angleLeft;
public Navigation() {
this.angleRight = 0;
this.angleLeft = 0;
}
public void ResetAngle() {
this.angleRight = 0;
this.angleRight= 0;
}
public int GetAngleLeftStep() {
this.angleLeft -= 10;
if (this.angleLeft < -180) {
this.angleLeft = -180;
}
return -10;
}
public int GetAngleRightStep() {
this.angleRight += 10;
if (this.angleRight > 180) {
this.angleRight = 180;
}
return 10;
}
public int IncreaseAngleLeft() {
this.angleLeft -= 10;
if(this.angleLeft < -180) {
this.angleLeft = -180;
}
return this.angleLeft;
}
public int IncreaseAngleRight() {
this.angleRight += 10;
if (this.angleRight > 180) {
this.angleRight = 180;
}
return this.angleRight;
}
}
}

@ -0,0 +1,54 @@
using System;
using System.Text;
using M2Mqtt;
using M2Mqtt.Messages;
using Swan.Formatters;
namespace RaspiControl {
class Programm {
private static MqttClient client;
private static Navigation navigation;
private static Joystick joystick;
static void Main(string[] args) {
try {
client = new MqttClient("localhost");
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
client.Subscribe(new string[] { "APROG/REQUEST" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
navigation= new Navigation();
joystick= new Joystick();
joystick.JoystickChanged += Joystick_JoystickChanged;
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
private static void Joystick_JoystickChanged(object sender, JoystickEventArgs e) {
switch (e.Button) {
case JoystickButton.None:
break;
case JoystickButton.Left:
client.Publish("mobile/cmd/nav/left", Encoding.UTF8.GetBytes($"{navigation.GetAngleLeftStep()}"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
break;
case JoystickButton.Right:
client.Publish("mobile/cmd/nav/right", Encoding.UTF8.GetBytes($"{navigation.GetAngleRightStep()}"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
break;
case JoystickButton.Up:
break;
case JoystickButton.Down:
break;
case JoystickButton.Center:
navigation.ResetAngle();
client.Publish("mobile/cmd/nav/drive", Encoding.UTF8.GetBytes("true"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
break;
}
}
private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) {
Console.Write(e.Topic + "\n");
Console.Write(Encoding.UTF8.GetString(e.Message) + "\n");
}
}
}

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="cd &quot;$(TargetDir)&quot;&#xD;&#xA;&quot;$(SolutionDir)\SecureUpload.exe&quot; . pi-home:ADIS/$(ProjectName)" />
</Target>
<ItemGroup>
<PackageReference Include="M2MqttClientDotnetCore" Version="1.0.1" />
<PackageReference Include="Unosquare.Raspberry.IO" Version="0.27.1" />
<PackageReference Include="Unosquare.WiringPi" Version="0.5.1" />
</ItemGroup>
</Project>
Loading…
Cancel
Save