Hey Guys,
I’m now finding that, however wonderful programming in C# is, my current application has gone past the limitations of what it can do, at least on an ARM7 chip. I have now gone onto translating key parts of my code into C, and it has improved the efficiency quite dramatically in many areas.
I wanted your opinion on whether the following bits of code would grant a reasonable performance improvement if done in RLP - These are methods that are run very often, but ones that seem to me pretty native to begin with, hence making me doubtful of the value of said translation vs the benefits of netmf.
Code 1: from Spherical to Polar co-ordinates conversion:
static public int[] ToBearing(float[] pos, float wptLat, float wptLng)
{
int _radius = 6371000;
double lat1 = Radians(pos[1]), dLong = Radians(wptLat - pos[0]),
lat2 = Radians(wptLng), bearing, distance;
//Distance
distance = MathEx.Acos(MathEx.Sin(lat1) * MathEx.Sin(lat2) +
MathEx.Cos(lat1) * MathEx.Cos(lat2) * MathEx.Cos(dLong)) * _radius;
//Bearing
bearing = MathEx.Atan2(MathEx.Sin(dLong) * MathEx.Cos(lat2),
MathEx.Cos(lat1) * MathEx.Sin(lat2) - MathEx.Sin(lat1) * MathEx.Cos(lat2) * MathEx.Cos(dLong));
return new int[2] { (int)MathEx.Round(distance), (int)MathEx.Round((Degrees(bearing) + 360) % 360) };
}
Code 2: GPS Interfacing:
public class GpsApi
{
private Thread ReadGps;
private SerialPort gpsUart;
public float[] pos;
public float speed;
public int fix;
public int magVar;
public GpsApi()
{
gpsUart = new SerialPort("COM3", 57600);
fix = 0;
pos = new float[2];
ReadGps = new Thread(UpdateData);
}
public float[] GetPosData()
{
return pos;
}
public void UpdateData()
{
gpsUart.Open();
while (true)
{
gpsUart.DiscardInBuffer();
string[] data;
byte[] rxBytes = new byte[256];
char[] chars;
string[] nmeaStr;
while (gpsUart.BytesToRead != 256)
Thread.Sleep(20);
gpsUart.Read(rxBytes, 0, 256);
try
{
chars = UTF8Encoding.UTF8.GetChars(rxBytes);
string rxData = new string(chars);
nmeaStr = rxData.Split('$', '\r','\n');
}
catch (Exception ex)
{
continue;
}
for (int i = nmeaStr.Length - 4; i >= 3; i--)
{
if (nmeaStr[i] != "")
{
data = nmeaStr[i].Split(',');
switch (data[0])
{
case "GPGLL":
if (GetFixStatus(data[6]))
{
pos[0] = GetDecimalDeg(data[1], 2) * GetSign(data[2]);//lat
pos[1] = GetDecimalDeg(data[3], 3) * GetSign(data[4]);//long
fix = 1;
}
else
fix = 0;
break;
case "GPVTG":
speed = (float)double.Parse(data[7]);
break;
default:
break;
}
}
}
}
}
private float GetDecimalDeg(string degrees, int degLength)
{
return (float)(double.Parse(degrees.Substring(0, degLength)) + (double.Parse(degrees.Substring(2)) / 60));
}
private int GetSign(string sign)
{
if (sign == "W" || sign == "S")
return -1;
else
return 1;
}
private bool GetFixStatus(string fixStatus)
{
if (fixStatus == "A")
return true;
else
return false;
}
}
PS If anyone has any advice on more efficient GPS reading methods, I’d really appreciate it. I never really quite understood what was considered the optimal way of reading serial ports.
Either way, any point in implementing those in RLP?