From decb1f3b177767eccc447573d7c5ebc0eb88acc6 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 16 Dec 2022 16:22:06 +0100 Subject: [PATCH 1/2] fixed battery voltage display, ignoring firstline when parsing response message --- .../RobotClientWpf/Views/MainView.xaml | 2 +- ADIS_Csharp/RobotLib/Battery/DevBattery.cs | 12 ++++++++---- ADIS_ESP32_Eclipse/main/robo_wrapper.c | 19 ++++++++++--------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml b/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml index c0eddde..405d037 100644 --- a/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml +++ b/ADIS_Csharp/RobotClientWpf/Views/MainView.xaml @@ -23,7 +23,7 @@ - + NaN diff --git a/ADIS_Csharp/RobotLib/Battery/DevBattery.cs b/ADIS_Csharp/RobotLib/Battery/DevBattery.cs index 06caaa4..80aa885 100644 --- a/ADIS_Csharp/RobotLib/Battery/DevBattery.cs +++ b/ADIS_Csharp/RobotLib/Battery/DevBattery.cs @@ -45,14 +45,18 @@ namespace RobotLib.Battery { if (fromTopic == TOPIC_ROBO_RESP_BATTERY) { - var parsedVoltageString = GetValueFromMesage("voltage", message); - if (parsedVoltageString == null) parsedVoltageString = "?"; + var parsedString = GetValueFromMesage("voltage", message); + if (parsedString == null) parsedString = "?"; // example message = "Battery: 1.25 V" - var valueUnit = message.Trim().Split(' '); + var valueUnit = parsedString.Trim().Split(' '); string voltage = valueUnit[0].Trim(); + float fVoltage = float.NaN; - this.Voltage = float.Parse(voltage); + if(float.TryParse(voltage, out fVoltage)) + { + this.Voltage = fVoltage; + } } } diff --git a/ADIS_ESP32_Eclipse/main/robo_wrapper.c b/ADIS_ESP32_Eclipse/main/robo_wrapper.c index 2062442..2a78d8e 100644 --- a/ADIS_ESP32_Eclipse/main/robo_wrapper.c +++ b/ADIS_ESP32_Eclipse/main/robo_wrapper.c @@ -114,16 +114,8 @@ bool Robo_Wrapper_GetBatteryVoltage(unsigned char *voltage){ } static bool getValueOfStatusResponse(unsigned char* response, const unsigned char* key, unsigned char* value, size_t valueStringLen){ - int16_t pos = McuUtility_strFind(response, (unsigned char*)key); - unsigned char extractedString[50] = ""; - - if(pos == -1){ // error string not found = -1 - ESP_LOGE(TAG, "Could not find key %s in response.", key); - return false; - } - unsigned char *p; - p = (unsigned char*)response + pos; + p = (unsigned char*)response; // skip first line (if the keyword would also appear in the headline, this is an issue) while(*p!='\n'){ @@ -131,6 +123,14 @@ static bool getValueOfStatusResponse(unsigned char* response, const unsigned cha } p+=1; // skip newline + // find key + int16_t pos = McuUtility_strFind(p, (unsigned char*)key); + if(pos == -1){ // error string not found = -1 + ESP_LOGE(TAG, "Could not find key %s in response.", key); + return false; + } + p+=pos; // move pointer to found key + // skip until colon while(true){ if(*p==':'){ @@ -147,6 +147,7 @@ static bool getValueOfStatusResponse(unsigned char* response, const unsigned cha } // extract value + unsigned char extractedString[50] = ""; uint8_t i = 0; while(true){ if(*p == '\n'){ From 75c0788d95ced8c7b13eefd830370d4e83450fef Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 16 Dec 2022 16:49:03 +0100 Subject: [PATCH 2/2] added logging to file for UI app, introduced robot types in RobotLib, publishing with qos = 0 and retain = false --- .../RobotClientWpf/ChallengeFactory.cs | 4 +- ADIS_Csharp/RobotClientWpf/NLog.config | 6 +++ ADIS_Csharp/RobotLib/Battery/DevBattery.cs | 5 --- .../Communication/MqttPublisherSubscriber.cs | 2 +- ADIS_Csharp/RobotLib/Robot.cs | 42 +++++++++++++------ ADIS_Csharp/RobotLib/RobotTypes.cs | 9 ++++ 6 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 ADIS_Csharp/RobotLib/RobotTypes.cs diff --git a/ADIS_Csharp/RobotClientWpf/ChallengeFactory.cs b/ADIS_Csharp/RobotClientWpf/ChallengeFactory.cs index 004b29b..a1675a6 100644 --- a/ADIS_Csharp/RobotClientWpf/ChallengeFactory.cs +++ b/ADIS_Csharp/RobotClientWpf/ChallengeFactory.cs @@ -12,8 +12,8 @@ namespace RobotClientWpf public ChallengeFactory() { this.PublisherSubscriber = MqttPublisherSubscriber.Instance; - this.RobotStationary = new Robot(this.PublisherSubscriber); - this.RobotMobile = new Robot(this.PublisherSubscriber); + this.RobotStationary = new Robot(this.PublisherSubscriber, RobotTypes.Stationary); + this.RobotMobile = new Robot(this.PublisherSubscriber, RobotTypes.Mobile); } } } diff --git a/ADIS_Csharp/RobotClientWpf/NLog.config b/ADIS_Csharp/RobotClientWpf/NLog.config index 7483624..6cef1f2 100644 --- a/ADIS_Csharp/RobotClientWpf/NLog.config +++ b/ADIS_Csharp/RobotClientWpf/NLog.config @@ -10,9 +10,15 @@ + + \ No newline at end of file diff --git a/ADIS_Csharp/RobotLib/Battery/DevBattery.cs b/ADIS_Csharp/RobotLib/Battery/DevBattery.cs index 80aa885..cceecdf 100644 --- a/ADIS_Csharp/RobotLib/Battery/DevBattery.cs +++ b/ADIS_Csharp/RobotLib/Battery/DevBattery.cs @@ -31,11 +31,6 @@ namespace RobotLib.Battery public DevBattery(IPublisherSubscriber com) : base(com, new List() { TOPIC_ROBO_RESP_BATTERY }) { } - public override void Refresh() - { - this.RequestBatteryVoltage(); - } - public void RequestBatteryVoltage() { base.SendMessage(TOPIC_ROBO_REQ_BATTERY, true.ToString()); diff --git a/ADIS_Csharp/RobotLib/Communication/MqttPublisherSubscriber.cs b/ADIS_Csharp/RobotLib/Communication/MqttPublisherSubscriber.cs index e0246fa..5e00ad8 100644 --- a/ADIS_Csharp/RobotLib/Communication/MqttPublisherSubscriber.cs +++ b/ADIS_Csharp/RobotLib/Communication/MqttPublisherSubscriber.cs @@ -159,7 +159,7 @@ namespace RobotLib.Communication } lock (clientLock) { - var msgId = client.Publish(topic, Encoding.ASCII.GetBytes(message)); + var msgId = client.Publish(topic, Encoding.ASCII.GetBytes(message), 0, false); log.Trace($"Published to topic '{topic}'. MessageId = {msgId}"); } } diff --git a/ADIS_Csharp/RobotLib/Robot.cs b/ADIS_Csharp/RobotLib/Robot.cs index 1ebe3b3..1cf4fc0 100644 --- a/ADIS_Csharp/RobotLib/Robot.cs +++ b/ADIS_Csharp/RobotLib/Robot.cs @@ -2,26 +2,43 @@ using RobotLib.SplitFlap; using RobotLib.Movement; using RobotLib.Battery; -using System.Threading; +using System.Timers; namespace RobotLib { public class Robot { - public Robot(IPublisherSubscriber com) + public Robot(IPublisherSubscriber com, RobotTypes type) { Com = com; - //Buzzer = new DevBuzzer(Com); - Battery = new DevBattery(Com); - SplitFlap = new DevSplitFlap(com); - LineSensor = new DevLineSensor(com); - Movement = new DevMovement(com); - - Timer timer = new Timer(TimerCallback); - timer.Change(2000, 10000); + Type = type; + if(type == RobotTypes.Undefined) + { + throw new System.ArgumentException("Undefined robot type, must define type!"); + } + + if(type == RobotTypes.Mobile) + { + //Buzzer = new DevBuzzer(Com); + Battery = new DevBattery(Com); + LineSensor = new DevLineSensor(com); + Movement = new DevMovement(com); + } + else if(type == RobotTypes.Stationary) + { + SplitFlap = new DevSplitFlap(com); + } + + Timer timer = new Timer + { + Interval = 10000 + }; + timer.Enabled= true; + timer.Elapsed += Timer_Elapsed; } public IPublisherSubscriber Com { get; } + public RobotTypes Type { get; } //public DevBuzzer Buzzer { get; } public DevBattery Battery { get; } @@ -39,13 +56,12 @@ namespace RobotLib //Com.Disconnect(); } - private void TimerCallback(object state) + private void Timer_Elapsed(object sender, ElapsedEventArgs e) { if (Com.IsConnected) { - Battery.Refresh(); + Battery?.RequestBatteryVoltage(); } } - } } \ No newline at end of file diff --git a/ADIS_Csharp/RobotLib/RobotTypes.cs b/ADIS_Csharp/RobotLib/RobotTypes.cs new file mode 100644 index 0000000..2f8aa8a --- /dev/null +++ b/ADIS_Csharp/RobotLib/RobotTypes.cs @@ -0,0 +1,9 @@ +namespace RobotLib +{ + public enum RobotTypes + { + Undefined = 0, + Mobile = 1, + Stationary + } +}