55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class TorqueMap
|
|
{
|
|
public Dictionary<int, double> points;
|
|
|
|
public TorqueMap()
|
|
{
|
|
points = new Dictionary<int, double>();
|
|
}
|
|
|
|
public void AddPoint(int rpm, double torque)
|
|
{
|
|
points.Add(rpm, torque);
|
|
}
|
|
|
|
public double GetTorque(int rpm)
|
|
{
|
|
if (points == null || points.Count == 0)
|
|
throw new System.InvalidOperationException("Torque map has no points.");
|
|
|
|
var sortedRpms = points.Keys.OrderBy(r => r).ToList();
|
|
int firstRpm = sortedRpms[0];
|
|
int lastRpm = sortedRpms[sortedRpms.Count - 1];
|
|
|
|
if (rpm <= firstRpm)
|
|
return points[firstRpm];
|
|
if (rpm >= lastRpm)
|
|
return points[lastRpm];
|
|
|
|
for (int i = 0; i < sortedRpms.Count - 1; i++)
|
|
{
|
|
int r1 = sortedRpms[i];
|
|
int r2 = sortedRpms[i + 1];
|
|
if (rpm >= r1 && rpm <= r2)
|
|
{
|
|
double t1 = points[r1];
|
|
double t2 = points[r2];
|
|
return t1 + (rpm - r1) * (t2 - t1) / (r2 - r1);
|
|
}
|
|
}
|
|
|
|
throw new System.InvalidOperationException("Interpolation failed.");
|
|
}
|
|
|
|
public double GetTorque(double angularVelocityRadPerSec)
|
|
{
|
|
// Convert rad/s to RPM: 1 rad/s = 60/(2π) RPM
|
|
double rpmDouble = angularVelocityRadPerSec * 60.0 / (2.0 * Math.PI);
|
|
int rpm = (int)rpmDouble; // or use rounding if preferred: (int)Math.Round(rpmDouble)
|
|
return GetTorque(rpm);
|
|
}
|
|
} |