Tracking Motion Level using Raspberry PI and MATLAB/SIMULINK

Some days ago I have already confessed to be somehow fascinated of the Netflix Socks. Every evening I have to search for the episode (actually I look “The Man in the High Castle” based on the novel from Philip K. Dick) and the time I fell asleep. So something like the Netflix Socks was ideal but I have some major doubts.

First of all I have no problem with cold feet. So I rather take my socks off. Then there is the issue with the battery. I really don’t want wo have another wearable I have to take care about. And last but not least I often watch movies with my wife. And to be honest I’m not even a Netflix customer. For me is Amazon Prime in combination with a Fire TV Stick the better choice but that is another story. So I had the idea to make something video based. I’m not sure if this will work, because if it would be so easy, I’m sure my Samsung TV would have such  function. But I want to give it a try and start with a little feasibility study.

The idea of all the sleep tracking is to measure movements. Wearables to this with acceleration sensors. I want to do this with a Raspberry PI and the Raspberry PI Cam. I will use MATLAB/SIMULINK for programming. MATLAB/SIMULINK is a rather more a platform than a programming language and in my opinion predestined for engineers. They follow a common marketing  approach and sell it cheap for students and make expensive (really expensive) for professionals. Fortunately there is a Home Edition since some years. When I found it I was a bit disappointed because it is still rather expensive for private persons (for the basis and some toolboxes you pay already more than 200$) But I really like the system and as they support Raspberry, Arduino, Lego Mindstorms, and other platforms and therefore it is a nice playground for makers like me.

After following the installation procedure of the development software itself you have to get the Hardware Support Package for Raspberry. A wizard will lead you through the installation process and at the end you will have SD card with Raspbian and the MATLAB software. In my case the network setup afterwards didn’t work so I had to connect a monitor, keyboard and mouse and make the network setting manually. For programming you have to use SIMULINK to run your software permanently on the Raspberry. I will not explain how to use the software. They have tons of tutorials on their website and in the meanwhile you find a lot of resources in the internet. Before you can use the Raspberry PI Cam you have to follow a few steps.

simulink model
SIMULINK model for motion measurement

The SIMULINK model is really easy. I just connect the camera with  “Sum of absolute metrics” directly and through a  delay block. Then make a small type conversion and put it to a UDP socket. Additionally I connect an Image Viewer to align the camera. For testing I use a socket server I’ve downloaded from the internet.

Matlab
Testing the model

For data acquisition I use the server of my irrigation project. I create a table tblmotion to store my measuring values.

Beside the motion measurment value I also store the raw bytes coming from my model because it was late in the evening when I did this and so I was not sure if my type conversion was working. I didn’t want to loose the data from the trial and with raw data I could have corrected the type conversion afterwards if it would have failed.

As socket server I use a PHP script. To run a PHP script in the Raspberry shell you have to install the PHP-CLI.

After having a nap in front of the TV I did the data analysis the next day with MATLAB. I did not invest in the database toolbox so I just query the data, export it to CSV and import it in MATLAB. I’ wearing a Garmin Vivosmart so I knew I felt asleep approx. 01:00. Looking at the data there seems to be a trend in the motion value.

Motion value
Motion value

So I do a bit of data analytics. I build some statistic values (STDDEV, AVG, VARIANCE) over different periods (1min, 5min, 10min, 20min) in SQL and import the data in MATLAB

The result makes me quite confident.

aggregate data

The data show a significant change in the movement value between 00:30 and 01:00. So the challenge will be to put this information in a reliable algorithm. Most properly I will have to get the neuronal network toolbox soon to make some trials.

Stay tuned!

Leave a Reply