Skip to content

Commit

Permalink
First step toward making CAN config data-driven. Parameter grid layou…
Browse files Browse the repository at this point in the history
…t has a bug.
  • Loading branch information
LegacyNsfw committed Dec 4, 2023
1 parent 08c4436 commit 46fc10d
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 75 deletions.
50 changes: 35 additions & 15 deletions Apps/PcmLibrary/Logging/CanLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public override string ToString()

private IPort canPort;
private CanParser parser = new CanParser();
List<UInt32> keySnapshot = new List<UInt32>();

// Note that this is accessed by multiple threads, so it must only be used within "lock(messages)"
Dictionary<UInt32, ParameterValue> messages = new Dictionary<uint, ParameterValue>();

public CanLogger()
{
Expand All @@ -50,7 +54,11 @@ public async Task SetPort(IPort port)

// Remove all known messages
this.keySnapshot.Clear();
this.messages.Clear();

lock (this.messages)
{
this.messages.Clear();
}

if (this.canPort != null)
{
Expand All @@ -60,20 +68,19 @@ public async Task SetPort(IPort port)
await this.canPort.OpenAsync(configuration);

// Discover what messages are available on the bus.
Thread.Sleep(1500);
foreach (UInt32 key in this.messages.Keys)
Thread.Sleep(1500);
lock (this.messages)
{
this.keySnapshot.Add(key);
foreach (UInt32 key in this.messages.Keys)
{
this.keySnapshot.Add(key);
}
}

this.keySnapshot.Sort();
}
}

Dictionary<UInt32, ParameterValue> messages = new Dictionary<uint, ParameterValue>();

List<UInt32> keySnapshot = new List<UInt32>();

public void DataReceived(byte[] buffer, int bytesReceived)
{
for(int i = 0; i < bytesReceived; i++)
Expand All @@ -84,7 +91,10 @@ public void DataReceived(byte[] buffer, int bytesReceived)
ParameterValue pv = this.TranslateValue(message);
if (pv != null)
{
messages[message.MessageId] = pv;
lock (this.messages)
{
this.messages[message.MessageId] = pv;
}
}
}
}
Expand Down Expand Up @@ -117,7 +127,7 @@ private ParameterValue TranslateValue(CanMessage message)
value = value * 14.5037738; // psi
result.Value = ((int)value).ToString("0.00");
result.Units = "F";
result.Name = "Pressue";
result.Name = "AEM Pressue";
return result;

case (uint)0x000a0302:
Expand All @@ -126,7 +136,7 @@ private ParameterValue TranslateValue(CanMessage message)
value = (value * 1.8) + 32.0;
result.Value = ((int)value).ToString("0.00");
result.Units = "F";
result.Name = "Temperature";
result.Name = "AEM Temperature";
return result;

case (uint)0x00000180:
Expand All @@ -135,7 +145,7 @@ private ParameterValue TranslateValue(CanMessage message)
value = (value * 0.0001) * 14.7;
result.Value = value.ToString("0.00");
result.Units = "AFR";
result.Name = "AFR 1";
result.Name = "AEM AFR 1";
return result;

case (uint)0x00000181:
Expand All @@ -144,7 +154,7 @@ private ParameterValue TranslateValue(CanMessage message)
value = (value * 0.0001) * 14.7;
result.Value = value.ToString("0.00");
result.Units = "AFR";
result.Name = "AFR 2";
result.Name = "AEM AFR 2";
return result;

default:
Expand All @@ -160,15 +170,25 @@ public IEnumerable<string> GetParameterNames()
{
foreach (UInt32 key in this.keySnapshot)
{
yield return this.messages[key].Name + "(" + this.messages[key].Units + ")";
string name;
lock(this.messages)
{
name = this.messages[key].Name + "(" + this.messages[key].Units + ")";
}
yield return name;
}
}

public IEnumerable<ParameterValue> GetParameterValues()
{
foreach(UInt32 key in this.keySnapshot)
{
yield return this.messages[key];
ParameterValue value;
lock(this.messages)
{
value = this.messages[key];
}
yield return value;
}
}

Expand Down
27 changes: 27 additions & 0 deletions Apps/PcmLibrary/Logging/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,31 @@ public override bool IsSupported(uint osid)
return this.XColumn.Parameter.IsSupported(osid) && this.YColumn.Parameter.IsSupported(osid);
}
}

public class CanParameter : Parameter
{
public uint MessageId { get; private set; }
public uint ByteIndex { get; private set; }
public uint ByteCount { get; private set; }
public bool HighByteFirst { get; private set; }

/// <summary>
/// This doesn't really make sense in the context of CAN logging, but
/// it's very useful for other parameters so I'm not sure it's worth
/// the trouble to factor it out.
/// </summary>
public override bool IsSupported(uint osid) { return true; }

public CanParameter(uint messageId, uint byteIndex, uint byteCount, bool highByteFirst, string id, string name, string description, IEnumerable<Conversion> conversions)
{
this.MessageId = messageId;
this.ByteIndex = byteIndex;
this.ByteCount = byteCount;
this.HighByteFirst = highByteFirst;
this.Id = id;
this.Name = name;
this.Description = description;
this.Conversions = conversions;
}
}
}
35 changes: 34 additions & 1 deletion Apps/PcmLibrary/Logging/ParameterDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ParameterDatabase
private string pathToXmlDirectory;

private List<Parameter> parameters = new List<Parameter>();
private Dictionary<UInt32, IEnumerable<CanParameter>> canParameters = new Dictionary<UInt32, IEnumerable<CanParameter>>();

/// <summary>
/// Constructor
Expand Down Expand Up @@ -63,7 +64,6 @@ public IEnumerable<Parameter> ListParametersBySupportedOs(uint osId)
return this.parameters.Where(p => p.IsSupported(osId));
}


/// <summary>
/// adds a parameter to the database, will not allow parameters with duplicate IDs, throws exception in that case.
/// </summary>
Expand All @@ -86,6 +86,7 @@ public void LoadDatabase()
this.LoadStandardParameters();
this.LoadRamParameters();
this.LoadMathParameters();
this.LoadCanParameters();
}

/// <summary>
Expand Down Expand Up @@ -202,6 +203,38 @@ private void LoadMathParameters()
}
}

private void LoadCanParameters()
{
string pathToXml = Path.Combine(this.pathToXmlDirectory, "Parameters.CAN.xml");
XDocument xml = XDocument.Load (pathToXml);
foreach (XElement messageElement in xml.Root.Elements("Message"))
{
string messageIdString = messageElement.Attribute("id").Value;
UInt32 messageId = UInt32.Parse(messageIdString);

List<CanParameter> parameters = new List<CanParameter>();

foreach (XElement parameterElement in xml.Root.Elements("CanParameter"))
{
string id = parameterElement.Attribute("id").Value;
string name = parameterElement.Attribute("name").Value;
string description = parameterElement.Attribute("description").Value;

uint firstByte = uint.Parse(parameterElement.Attribute("firstByte").Value);
uint byteCount = uint.Parse(parameterElement.Attribute("byteCount").Value);
bool highByteFirst = bool.Parse(parameterElement.Attribute("highByteFirst").Value);

List<Conversion> conversions = GetConversions(parameterElement);

CanParameter parameter = new CanParameter(messageId, firstByte, byteCount, highByteFirst, id, name, description, conversions);

parameters.Add(parameter);
}

this.canParameters[messageId] = parameters;
}
}

private LogColumn BuildLogColumnForMathParameter(string id, string units, string parameterName)
{
Parameter xParameter = this.parameters.Where(x => (x.Id == id)).FirstOrDefault();
Expand Down
13 changes: 7 additions & 6 deletions Apps/PcmLibraryWindowsForms/DialogBoxes/CanForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Apps/PcmLibraryWindowsForms/DialogBoxes/CanForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="labelRecommendedInterface.Text" xml:space="preserve">
<value>Currently, the only supported CAN interface is this device from Seeed Studio. It will appear in the list as "USB-SERIAL CH340."

You must use the manufacturer's software to configure it to match your CAN bus speed, and a serial speed of 2,000,000.</value>
</data>
</root>
Loading

0 comments on commit 46fc10d

Please sign in to comment.