add publish for joystick events

main
Simon Frei 4 years ago
parent eaf85084d8
commit c94369679c
  1. 13
      ADIS_Csharp/RaspiControl/MqttConstants.cs
  2. 53
      ADIS_Csharp/RaspiControl/Navigation.cs
  3. 15
      ADIS_Csharp/RaspiControl/NavigationConstants.cs
  4. 21
      ADIS_Csharp/RaspiControl/Program.cs
  5. 4
      ADIS_Csharp/RaspiControl/RaspiControl.csproj

@ -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";
}
}

@ -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;
}
}
}

@ -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;
}
}

@ -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<string,object> data = JsonSerializer.Deserialize<Dictionary<string, object>>(e.Message);
foreach(KeyValuePair<string,object> entry in data) {
Console.WriteLine(entry.Key+ ": " + entry.Value);
}
}
}
}

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -8,7 +8,7 @@
</PropertyGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="cd &quot;$(TargetDir)&quot;&#xD;&#xA;&quot;$(SolutionDir)\SecureUpload.exe&quot; . pi-home:ADIS/$(ProjectName)" />
<Exec Command="cd &quot;$(TargetDir)&quot;&#xD;&#xA;&quot;$(SolutionDir)\SecureUpload.exe&quot; . pi-hslu:ADIS/$(ProjectName)" />
</Target>
<ItemGroup>

Loading…
Cancel
Save