From 89e9e10b6e88742f41c5254251cbca469304d9ca Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 23 Dec 2022 12:53:33 +0100 Subject: [PATCH 1/2] updates in UI, added present counter --- ADIS_Csharp/RobotClientWpf/MainWindow.xaml | 2 +- ADIS_Csharp/RobotClientWpf/MainWindow.xaml.cs | 4 ++-- .../RobotClientWpf/Views/MainView.xaml | 7 ++++-- .../RobotClientWpf/Views/MainView.xaml.cs | 24 ++++++++++++++----- ADIS_Csharp/RobotLib/Battery/DevBattery.cs | 2 +- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ADIS_Csharp/RobotClientWpf/MainWindow.xaml b/ADIS_Csharp/RobotClientWpf/MainWindow.xaml index ae999e5..18f9127 100644 --- a/ADIS_Csharp/RobotClientWpf/MainWindow.xaml +++ b/ADIS_Csharp/RobotClientWpf/MainWindow.xaml @@ -12,7 +12,7 @@ - + diff --git a/ADIS_Csharp/RobotClientWpf/MainWindow.xaml.cs b/ADIS_Csharp/RobotClientWpf/MainWindow.xaml.cs index e1c58d2..edbccd0 100644 --- a/ADIS_Csharp/RobotClientWpf/MainWindow.xaml.cs +++ b/ADIS_Csharp/RobotClientWpf/MainWindow.xaml.cs @@ -61,7 +61,7 @@ namespace RobotClientWpf // set color according to assessed state if(e.Online == false) { - log.Warn("Robot Stationary seems to be offline."); + log.Warn($"Robot Stationary seems to be offline. No response in {e.Seconds}s."); } else { @@ -75,7 +75,7 @@ namespace RobotClientWpf // set color according to assessed state if (e.Online == false) { - log.Warn("Robot Mobile seems to be offline."); + log.Warn($"Robot Mobile seems to be offline. No response in {e.Seconds}s."); } else { diff --git a/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml b/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml index 1aa0a06..584efeb 100644 --- a/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml +++ b/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml @@ -5,7 +5,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:RobotClientWpf.Views" mc:Ignorable="d" - d:DesignHeight="430" d:DesignWidth="1200" FontSize="15" KeyDown="UserControl_KeyDown"> + d:DesignHeight="430" d:DesignWidth="1200" FontSize="15"> @@ -19,7 +19,10 @@ - + + + + diff --git a/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml.cs b/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml.cs index 97d125c..1f731de 100644 --- a/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml.cs +++ b/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml.cs @@ -1,4 +1,5 @@ using RobotClientWpf.Utilities; +using RobotLib.Status; using System.Windows.Controls; using System.Windows.Input; @@ -12,7 +13,7 @@ namespace RobotClientWpf.Views private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); private ChallengeFactory? challenge; private MainWindow? mainWindow; - private const int AMOUNT_SPEED_ADDED_PER_CLICK = 200; + private const int AMOUNT_SPEED_ADDED_PER_CLICK = 350; private const int TURN_ANGLE_PER_CLICK = 30; public MainView() @@ -27,6 +28,22 @@ namespace RobotClientWpf.Views // subscribe to events this.challenge.RobotStationary.SplitFlap.SplitFlapDisplayChanged += this.SplitFlap_SplitFlapDisplayChanged; this.challenge.RobotMobile.Battery.BatteryChanged += Battery_BatteryChanged; + this.challenge.RobotMobile.Status.PresentChanged += Status_PresentChanged; + this.challenge.RobotMobile.Status.StatusChanged += Status_StatusChanged; + } + + private void Status_StatusChanged(object? sender, RobotLib.Status.StatusEventArgs e) + { + // if status manual is set => reset present counter + if(e.Status == RoboStatus.Manual || e.Status == RoboStatus.Auto) + { + UIAccessHelpers.SetTextboxText(tbPresentCount, ""); + } + } + + private void Status_PresentChanged(object? sender, RobotLib.Status.PresentEventArgs e) + { + UIAccessHelpers.SetTextboxText(tbPresentCount, e.Present.ToString()); } private void Battery_BatteryChanged(object? sender, RobotLib.Battery.BatteryEventArgs e) @@ -101,10 +118,5 @@ namespace RobotClientWpf.Views e.Handled = true; } } - - private void UserControl_KeyDown(object sender, KeyEventArgs e) - { - this.HandleKeyDownEvent(sender, e); - } } } diff --git a/ADIS_Csharp/RobotLib/Battery/DevBattery.cs b/ADIS_Csharp/RobotLib/Battery/DevBattery.cs index abe7907..8906b61 100644 --- a/ADIS_Csharp/RobotLib/Battery/DevBattery.cs +++ b/ADIS_Csharp/RobotLib/Battery/DevBattery.cs @@ -11,7 +11,7 @@ namespace RobotLib.Battery { private float voltage; private int lastResponseS = 1000; // seconds since last response - private const int offlineTresholdS = 11; // treshold when to assess a robot as offline (no response for x sec) + private const int offlineTresholdS = 25; // treshold when to assess a robot as offline (no response for x sec) private Stopwatch stopwatchLastResponse = new(); private const string TOPIC_ROBO_REQ_BATTERY = "/both/cmd/battery/get_volt"; From 9c9ab331212aaa7c7df66eb0cb6ab9787568abb9 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 23 Dec 2022 13:46:14 +0100 Subject: [PATCH 2/2] added semaphore to lock mqtt client access on esp32 --- ADIS_ESP32_Eclipse/main/myMqtt.c | 40 ++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/ADIS_ESP32_Eclipse/main/myMqtt.c b/ADIS_ESP32_Eclipse/main/myMqtt.c index b9613db..1ba4ab8 100644 --- a/ADIS_ESP32_Eclipse/main/myMqtt.c +++ b/ADIS_ESP32_Eclipse/main/myMqtt.c @@ -20,6 +20,7 @@ static const char *TAG = "MY_MQTT"; // local cars char _brokerIp[] = "255.255.255.255"; // max length of ip +SemaphoreHandle_t semaphoreMqtt = NULL; // declarations static void log_error_if_nonzero(const char *message, int error_code); @@ -29,8 +30,16 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ esp_mqtt_client_handle_t client; bool MyMqtt_Init(void){ + // initialize semaphore + semaphoreMqtt = xSemaphoreCreateRecursiveMutex(); + if(semaphoreMqtt == NULL){ + ESP_LOGE(TAG, "Failed to initialize semaphore for MyMqtt!"); + }else{ + vQueueAddToRegistry(semaphoreMqtt, "mymqtt_semphr"); + } + + // get mqtt broker IP from NVS char brokerUri[50]; - // get from NVS Challenge_Nvs_GetBrokerIpFromNVS(_brokerIp, sizeof(_brokerIp)); McuUtility_strcpy((unsigned char*)brokerUri, sizeof(brokerUri), (unsigned char*)"mqtt://"); McuUtility_strcat((unsigned char*)brokerUri, sizeof(brokerUri), (unsigned char*)_brokerIp); @@ -54,14 +63,35 @@ void MyMqtt_Deinit(void){ esp_mqtt_client_destroy(client); } +/* HELPERS FOR SEMAPHORE */ +static bool MyMqtt_AquireMutex(void){ + if(xSemaphoreTakeRecursive(semaphoreMqtt, pdMS_TO_TICKS(1000)) != pdTRUE){ + /* timeout */ + ESP_LOGW(TAG, "Timemout while aquiring myMqtt semaphore."); + return false; + } + return true; +} + +static void MyMqtt_ReleaseMutex(void){ + xSemaphoreGive(semaphoreMqtt); +} + + void MyMqtt_Publish(const char *topic, const char *data){ - int msg_id = esp_mqtt_client_publish(client, topic, data, 0, 2, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + if(MyMqtt_AquireMutex()){ + int msg_id = esp_mqtt_client_publish(client, topic, data, 0, 2, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + MyMqtt_ReleaseMutex(); + } } void MyMqtt_Subscribe(const char *topic){ - int msg_id = esp_mqtt_client_subscribe(client, topic, 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d, success=%s", msg_id, msg_id==-1 ? "false" : "true"); + if(MyMqtt_AquireMutex()){ + int msg_id = esp_mqtt_client_subscribe(client, topic, 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d, success=%s", msg_id, msg_id==-1 ? "false" : "true"); + MyMqtt_ReleaseMutex(); + } } char* MyMqtt_GetBrokerIP(void){