You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.9 KiB
52 lines
1.9 KiB
using RobotLib.Communication;
|
|
using RobotLib.SplitFlap;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
|
|
namespace RobotLib.Movement
|
|
{
|
|
public class DevLineSensor : DevBase
|
|
{
|
|
private const string TOPIC_ROBO_CALIB_CMD = "/mobile/cmd/line_sens/calib";
|
|
private const string TOPIC_ROBO_GET_CALIB = "/mobile/cmd/line_sens/get_calib";
|
|
private const string TOPIC_ROBO_CALIB_STATE = "/mobile/state/line_sens/calib_data";
|
|
|
|
public event EventHandler<LineSensorCalibrationDataEventArgs> NewCalibrationDataArrived;
|
|
|
|
public DevLineSensor(IPublisherSubscriber com) : base(com, new List<string>() { TOPIC_ROBO_CALIB_STATE }) { }
|
|
|
|
public void GetCalibrationData()
|
|
{
|
|
base.SendMessage(TOPIC_ROBO_GET_CALIB, true.ToString());
|
|
}
|
|
|
|
public void StartCalibration(bool start)
|
|
{
|
|
string payload = JsonSerializer.Serialize(new Dictionary<string, bool>() { { "start", start } });
|
|
base.SendMessage(TOPIC_ROBO_CALIB_CMD, payload);
|
|
}
|
|
|
|
protected override void ParseMessage(string fromTopic, string message)
|
|
{
|
|
if (fromTopic == TOPIC_ROBO_CALIB_STATE)
|
|
{
|
|
var parsedState = GetValueFromMesage<string>("state", message);
|
|
var parsedValues = GetValueFromMesage<string>("data", message);
|
|
|
|
if (parsedState == null) parsedState = "?";
|
|
if (parsedValues == null) parsedValues = "?";
|
|
|
|
LineSensorCalibrationDataEventArgs eventArgs = new(parsedState, parsedValues);
|
|
NewCalibrationDataArrived?.Invoke(this, eventArgs);
|
|
}
|
|
}
|
|
|
|
private T GetValueFromMesage<T>(string parameter, string message)
|
|
{
|
|
var data = JsonSerializer.Deserialize<Dictionary<string, T>>(message);
|
|
data.TryGetValue(parameter, out T value);
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
|