'How to make calculation while receiving new data?
I try to make a realtime classification desktop app using C#. Classification based on KNN algorithm. I got the data from sensor, I read it using timer_tick (i got 100 data every 1 second or read the data every 10 ms).
I want to start the classification by taking every 280 data i have one at a time. (means I run classification code every 0 - 279, 280 - 559, ...)
I do have alot of problem :
For now i dont implement any thread or task, so the UI freeze whenever i called the doKNNClassification() which also means while the doKNNClassification() running i am not receiving any new data
it takes alot of time to do KNN (estimation, minimum time I get to do KNN on 280 data is 4 minutes) meanwhile it just take 2.8 seconds to get new 280 data which means it is time to run the classification function again. (What happened if call the same function again while it is still running)
How to make the program to keep receiving new data while also do another task/function whenever i got enough data ?
*I hope i write this one clear enough and really appreciate any suggestion
This some code I write so far
Sensor varSensorHelper = new Sensor(); // Custom class
List<Sensor> listSensorMeasurement = new List<Sensor>();
private void timer2_Tick(object sender, EventArgs e)
{
// 2520 = 9 x 280
string text = ""; // Use this var for displaying the sensor measurement on the UI
// if i have 2520, i will stop receiving data sensor
if (arraySensor.Count < 2520)
{
if (arraySensor.Count % 280 != 0)
{
// this part is to separate which data belong to which sensor
// I use 3 sensor, identified by the sensor ID which is CC2, C42, C62
foreach (KeyValuePair<uint, ConnectedMtData> data in _connectedMtwData)
{
if (data.Value._orientation != null)
{
if (Regex.IsMatch(data.Key.ToString(), patternCC2))
{
varSensorHelper.Roll1 = data.Value._orientation.x();
varSensorHelper.Pitch1 = data.Value._orientation.y();
varSensorHelper.Yaw1 = data.Value._orientation.z();
}
else if (Regex.IsMatch(data.Key.ToString(), patternC42))
{
varSensorHelper.Roll2 = data.Value._orientation.x();
varSensorHelper.Pitch2 = data.Value._orientation.y();
varSensorHelper.Yaw2 = data.Value._orientation.z();
}
else if (Regex.IsMatch(data.Key.ToString(), patternC62))
{
//MASUKKAN DATA SENSOR KE ROW BARU
varSensorHelper.Roll3 = data.Value._orientation.x();
varSensorHelper.Pitch3 = data.Value._orientation.y();
varSensorHelper.Yaw3 = data.Value._orientation.z();
}
text += string.Format("{3:X8} {0,-5:f2}, {1,-5:f2}, {2,-5:f2} [°]\n",
data.Value._orientation.x(),
data.Value._orientation.y(),
data.Value._orientation.z(), data.Key);
listSensorMeasurement.add(varSensorHelper);
}
}
}
else
{
doKNNClassification();
}
}
else
{
//I have enough data, I need to stop this timer_tick function
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
