I used the NXP FRDM-KL25Z board. Since the 3-axis accelerometer MMA8451Q was listed, I took up the information by serial communication from here and plotted it in real time with Python. Saw.
(Accelerometer) FRDM-KL25Z --- (Serial) ---> PC (Real-time plot)
FRDM-KL25Z has the configuration shown below.
I used mbed. Now, in order to include the MMA8451Q.h
file, go to here to add the MMA8451Q library to the mbed compiler. To import.
accel_serial.cpp
#include "mbed.h"
#include "MMA8451Q.h"
#define MMA8451_I2C_ADDRESS (0x1d<<1)
DigitalOut myled(LED_GREEN);
Serial pc(USBTX, USBRX);
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
PwmOut rled(LED_RED);
PwmOut gled(LED_GREEN);
PwmOut bled(LED_BLUE);
int main()
{
pc.printf("Hello World!\n");
while (true) {
pc.printf("%f,", acc.getAccX());
pc.printf("%f,", acc.getAccY());
pc.printf("%f\n", acc.getAccZ());
rled = 1.0 - abs(acc.getAccX());
gled = 1.0 - abs(acc.getAccY());
bled = 1.0 - abs(acc.getAccZ());
wait(0.2);
}
}
After writing the program, I compiled it, downloaded the binary file, and dragged and dropped the FRDM-KL25Z to the folder recognized by USB.
I used Python. Install the library with pip
$ pip install numpy matplotlib seaborn pyserial
Next, connect FRDM-KL25Z via USB and check the allocation destination.
$ ls /dev/tty*
In my environment it was assigned to / dev / ttyACM1
.
If they are different, rewrite the / dev / ttyACM1
part in the program.
Create the following python file and execute it to complete. (Please execute with sudo authority)
When you tilt the FRDM-KL25Z, the acceleration information will be reflected in the graph.
plot_accel.py
from __future__ import unicode_literals, print_function
import numpy as np
import matplotlib.pyplot as plt
import serial
import seaborn as sns
sns.set(font_scale=2)
s = serial.Serial('/dev/ttyACM1')
fig, ax = plt.subplots(3, 1)
t = np.arange(0,10,0.1)
list_x = np.zeros(100).tolist()
list_y = np.zeros(100).tolist()
list_z = np.zeros(100).tolist()
lines_x, = ax[0].plot(t, list_x)
lines_y, = ax[1].plot(t, list_y)
lines_z, = ax[2].plot(t, list_z)
ax[0].set_ylim((-90,90))
ax[1].set_ylim((-90,90))
ax[2].set_ylim((-1,1))
ax[0].set_ylabel("Rot_X", size=30)
ax[1].set_ylabel("Rot_Y", size=30)
ax[2].set_ylabel("Z", size=30)
acc = s.readline().split(",") # just for warming up
while True:
t += 0.1
acc = s.readline().split(",")
acc_x = float(acc[0])*90
acc_y = float(acc[1])*90
acc_z = float(acc[2])
list_x.pop(0)
list_x.append(acc_x)
list_y.pop(0)
list_y.append(acc_y)
list_z.pop(0)
list_z.append(acc_z)
plt.draw()
lines_x.set_data(t, list_x)
lines_y.set_data(t, list_y)
lines_z.set_data(t, list_z)
ax[0].set_xlim((t.min(), t.max()))
ax[1].set_xlim((t.min(), t.max()))
ax[2].set_xlim((t.min(), t.max()))
plt.pause(.0001)
Supplementary comment
Getting the serial signal is the following part:
s = serial.Serial('/dev/ttyACM1')
acc = s.readline().split(",")
Real-time plot updates could be achieved by using the following instead of plt.show ()
.
plt.draw()
# ... matplotlib.Update lines element...
plt.pause(.0001)
Reference: http://qiita.com/hausen6/items/b1b54f7325745ae43e47