From c94369679c3a719efc51e6998a23ecf54141b541 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Fri, 9 Dec 2022 12:30:15 +0100 Subject: [PATCH] add publish for joystick events --- ADIS_Csharp/RaspiControl/MqttConstants.cs | 13 +++++ ADIS_Csharp/RaspiControl/Navigation.cs | 53 ------------------- .../RaspiControl/NavigationConstants.cs | 15 ++++++ ADIS_Csharp/RaspiControl/Program.cs | 21 ++++---- ADIS_Csharp/RaspiControl/RaspiControl.csproj | 4 +- 5 files changed, 42 insertions(+), 64 deletions(-) create mode 100644 ADIS_Csharp/RaspiControl/MqttConstants.cs delete mode 100644 ADIS_Csharp/RaspiControl/Navigation.cs create mode 100644 ADIS_Csharp/RaspiControl/NavigationConstants.cs diff --git a/ADIS_Csharp/RaspiControl/MqttConstants.cs b/ADIS_Csharp/RaspiControl/MqttConstants.cs new file mode 100644 index 0000000..c84cac2 --- /dev/null +++ b/ADIS_Csharp/RaspiControl/MqttConstants.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RaspiControl { + internal static class MqttConstants { + public static string MOBILE_NAV_TURN_TOPIC = "mobile/cmd/nav/turn"; + public static string MOBILE_NAV_MOVE_TOPIC = "mobile/cmd/nav/move"; + public static string MOBILE_NAV_STOP_TOPIC = "mobile/cmd/nav/stop"; + } +} diff --git a/ADIS_Csharp/RaspiControl/Navigation.cs b/ADIS_Csharp/RaspiControl/Navigation.cs deleted file mode 100644 index 1510584..0000000 --- a/ADIS_Csharp/RaspiControl/Navigation.cs +++ /dev/null @@ -1,53 +0,0 @@ -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; - } - } -} diff --git a/ADIS_Csharp/RaspiControl/NavigationConstants.cs b/ADIS_Csharp/RaspiControl/NavigationConstants.cs new file mode 100644 index 0000000..90ad6d4 --- /dev/null +++ b/ADIS_Csharp/RaspiControl/NavigationConstants.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RaspiControl { + internal static class NavigationConstants { + public static int TURN_ANGLE_LEFT = -10; + public static int TURN_ANGLE_RIGHT = 10; + public static int SPEED_FORWARD = 100; + public static int SPEED_BACKWARD = -100; + public static bool STOP = true; + } +} diff --git a/ADIS_Csharp/RaspiControl/Program.cs b/ADIS_Csharp/RaspiControl/Program.cs index 262d227..cc1d4ba 100644 --- a/ADIS_Csharp/RaspiControl/Program.cs +++ b/ADIS_Csharp/RaspiControl/Program.cs @@ -2,13 +2,12 @@ using System.Text; using M2Mqtt; using M2Mqtt.Messages; -using Swan.Formatters; +using System.Text.Json; 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"); @@ -16,8 +15,7 @@ namespace RaspiControl { 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 joystick = new Joystick(); joystick.JoystickChanged += Joystick_JoystickChanged; } catch (Exception ex) { @@ -30,18 +28,19 @@ namespace RaspiControl { 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); + client.Publish(MqttConstants.MOBILE_NAV_TURN_TOPIC, Encoding.UTF8.GetBytes($"{NavigationConstants.TURN_ANGLE_LEFT}"), 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); + client.Publish(MqttConstants.MOBILE_NAV_TURN_TOPIC, Encoding.UTF8.GetBytes($"{NavigationConstants.TURN_ANGLE_RIGHT}"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false); break; case JoystickButton.Up: + client.Publish(MqttConstants.MOBILE_NAV_MOVE_TOPIC, Encoding.UTF8.GetBytes($"{NavigationConstants.SPEED_FORWARD}"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false); break; case JoystickButton.Down: + client.Publish(MqttConstants.MOBILE_NAV_MOVE_TOPIC, Encoding.UTF8.GetBytes($"{NavigationConstants.SPEED_BACKWARD}"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false); break; case JoystickButton.Center: - navigation.ResetAngle(); - client.Publish("mobile/cmd/nav/drive", Encoding.UTF8.GetBytes("true"), MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false); + client.Publish(MqttConstants.MOBILE_NAV_STOP_TOPIC, Encoding.UTF8.GetBytes($"{NavigationConstants.STOP}"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); break; } } @@ -49,6 +48,10 @@ namespace RaspiControl { private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { Console.Write(e.Topic + "\n"); Console.Write(Encoding.UTF8.GetString(e.Message) + "\n"); + Dictionary data = JsonSerializer.Deserialize>(e.Message); + foreach(KeyValuePair entry in data) { + Console.WriteLine(entry.Key+ ": " + entry.Value); + } } } } \ No newline at end of file diff --git a/ADIS_Csharp/RaspiControl/RaspiControl.csproj b/ADIS_Csharp/RaspiControl/RaspiControl.csproj index b90fa41..f1edd23 100644 --- a/ADIS_Csharp/RaspiControl/RaspiControl.csproj +++ b/ADIS_Csharp/RaspiControl/RaspiControl.csproj @@ -1,4 +1,4 @@ - + Exe @@ -8,7 +8,7 @@ - +