Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create Mac desktop app that plays sound when it detects sudden movement of MacBook.
Just I saw the article that when I use SMSLib (Sudden Motion Sensor), the app can detect accelerometer.

Can someone please help me to know how to detect sudden movement of MacBook?
Is SMSLib library helpful for this?

What I have tried:

Just I am trying to use SMSLib, but I don't know this can be helpful.
Posted
Updated 7-Oct-16 23:18pm

1 solution

It should be possible by polling the sensor periodically and compare the sensor data with the data from the previous poll. However, polling requires a significant amount of system resources when peformed in short intervals which are necessary to detect short sudden movements.

Most accelerometers have options to generate interrupts on specific conditions. But using these requires that the device (the MacBook) supports them and the accelerometer can be programmed using the library (the library sources should show if this is possible).

If the MacBook is laying on the table the sensor data should be near zero for the X and Y axis and 1.0 for the Z axis (or -1.0). When it is moved, the data will change accordingly.

Because the data are noisy it might be necessary to filter them when slow and small movements should be detected. A simple filter uses a factor below 1.0 (for each axis):
filtered = actual_value * factor - prev_value * (1 - factor)
prev_value = filtered_value


You can then optionally calculate the pitch and roll:
x_norm = x / sqrt(x^2 + y^2 + z^2)
y_norm = y / sqrt(x^2 + y^2 + z^2)
pitch = asin(x_norm)
roll = asin(y_norm / cos(pitch))


I suggest to write a simple application that polls the sensor and prints out the raw and calculated values. Then you can play with it to find out the required limits when moving.
 
Share this answer
 
Comments
Taiming J 8-Oct-16 5:28am    
Can you please provide example code or samples to do it?
I am very thankful if you provide it.
Thank you
Jochen Arndt 8-Oct-16 5:34am    
https://github.com/liangqi/SMSLib:

"Call smsGetData() to fetch calibrated SMS data."

"See smslib.m and SMSTester.m for an example of this."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900