ねこでじ(Nekodigi)

Nekodigi’s diary

学習中の気づきをまとめています。応援よろしくお願いします

【Arduino×Processing】Calculate rotation with MPU9250 and visualize with Processing

Abstract

www.youtube.com
I had used MPU9250 with Raspberry PI before, so I did same thing with Arduino. I implemented formula to calculate yaw, but I won't use because I want to make it accurate and simple.

How it works

Arduino

At first, get acceleration and angular velocity with this library.
github.com
Then add (angular velocity * delta time) to the rotation.
 \begin{array}{l}
\mathsf{rot\ is\ angular\ velocity}\\
\mathsf{dt\ is\ delta\ time}\\
roll\ -=\ rot_{x} *dt\\
pitch\ -=\ rot_{y} *dt
\end{array}
Calculate rotation from acceleration with below formula to calibrate.
  \begin{array}{l}
\mathsf{acc\ is\ acceleration}\\
troll=\mathrm{atan2}( acc_{y} ,acc_{z})\\
temp=\sqrt{acc^{2}_{z} +acc^{2}_{y}}\\
tpitch=\mathrm{atan2}( acc_{x} ,temp)
\end{array}
Blend them like this.
※I used pointer to make function of this process.
 \begin{array}{l}
\mathsf{fac\ is\ factor}\\
roll=( 1-fac) *roll+fac*troll
\end{array}
Finally serial print them.

Processing

Use readStringUntil and convert to number. Then, just visualize it.

Source code

gist.github.com