After making my new smart fabric, I’m successfully turning leather into a touch panel. So right now I can get the raw data and translate them into my point matrix!
According to unknown confidentiality agreement, I better not put any picture here. I would just talk about real-time k-means algorithm for 2 dimension points.
And here is the problem: every time you touch the fabric, it light up 1 to 9 point in my matrix. For one finger it’s totally fine, but for multi-touch, I need to figure out where exactly my fingers are. To do this, I need to assign every point to it’s finger. Here I need clustering analysis, I choose k-means clustering algorithm (wiki).
Here is the quick explanation:
- Randomly assign a position of each finger.
- Assign every point to it’s closest finger position.
- Change position of finger to the centroid of each group.
- Repeat step 2 and 3 until the position stop change.
But K-means is not a high efficiency algorithm, there’s a lot of calculating. In the original algorithm, if there’s any group have not point inside, it’ll get a new randomly position. But here we’re trying to detect fingers, the position is predictable. I set default positions into different corners, so in most cases, the calculating loop would stop within 2 times. (Notice: for real time calculating, if your computer is not fast enough or your matrix is too large, this might be a bad solution.)
Create a function and call it every time I receive new data;
In this function, assign every point into a group.
Here we calculating centroid of each group and loop until stop changing.
Run() function checks to see if the algorithm has stopped. Here Arduino sends data every 100ms, call kmeans() function before last calculating finished would cause error or wrong result. So here I create a boolean variable to prevent that.