I am trying to build a python code that can rotate a 3d cube based on the values from an external magnetometer
i have written below code to simulate motion of a 3d cube along x axis..how i can edit to rotate as per 3 axis values and axis details coming from the sensor? I am using an I2C based magnetometer with my raspberry pi that sends me heading details
import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
import threading
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.axis('off')
# Define the vertices of the cube
p1 = np.array([0, 0, 0])
p2 = np.array([1, 0, 0])
p3 = np.array([1, 1, 0])
p4 = np.array([0, 1, 0])
p5 = np.array([0, 0, 1])
p6 = np.array([1, 0, 1])
p7 = np.array([1, 1, 1])
p8 = np.array([0, 1, 1])
# Define the rotation angle in degrees
theta_x = 45
ax.plot([p1[0], p2[0]], [p1[1], p2[1]], [p1[2], p2[2]], 'k-')
ax.plot([p2[0], p3[0]], [p2[1], p3[1]], [p2[2], p3[2]], 'k-')
ax.plot([p3[0], p4[0]], [p3[1], p4[1]], [p3[2], p4[2]], 'k-')
ax.plot([p4[0], p1[0]], [p4[1], p1[1]], [p4[2], p1[2]], 'k-')
ax.plot([p5[0], p6[0]], [p5[1], p6[1]], [p5[2], p6[2]], 'k-')
ax.plot([p6[0], p7[0]], [p6[1], p7[1]], [p6[2], p7[2]], 'k-')
ax.plot([p7[0], p8[0]], [p7[1], p8[1]], [p7[2], p8[2]], 'k-')
ax.plot([p8[0], p5[0]], [p8[1], p5[1]], [p8[2], p5[2]], 'k-')
ax.plot([p5[0], p1[0]], [p5[1], p1[1]], [p5[2], p1[2]], 'k-')
ax.plot([p6[0], p2[0]], [p6[1], p2[1]], [p6[2], p2[2]], 'k-')
ax.plot([p7[0], p3[0]], [p7[1], p3[1]], [p7[2], p3[2]], 'k-')
ax.plot([p8[0], p4[0]], [p8[1], p4[1]], [p8[2], p4[2]], 'k-')
def animate(i):
ax.clear()
plt.axis('off')
global theta_x
ax.view_init(elev=10., azim=i)
# Define the rotation matrix
rotation_matrix_x = np.array([[1, 0, 0],
[0, np.cos(np.deg2rad(theta_x)), -np.sin(np.deg2rad(theta_x))],
[0, np.sin(np.deg2rad(theta_x)), np.cos(np.deg2rad(theta_x))]])
# Rotate the vertices of the cube using the rotation matrix
global p1,p2,p3,p4,p5,p6,p7,p8
p1 = rotation_matrix_x.dot(p1)
p2 = rotation_matrix_x.dot(p2)
p3 = rotation_matrix_x.dot(p3)
p4 = rotation_matrix_x.dot(p4)
p5 = rotation_matrix_x.dot(p5)
p6 = rotation_matrix_x.dot(p6)
p7 = rotation_matrix_x.dot(p7)
p8 = rotation_matrix_x.dot(p8)
# Plot the rotated cube
ax.plot([p1[0], p2[0]], [p1[1], p2[1]], [p1[2], p2[2]], 'k-')
ax.plot([p2[0], p3[0]], [p2[1], p3[1]], [p2[2], p3[2]], 'k-')
ax.plot([p3[0], p4[0]], [p3[1], p4[1]], [p3[2], p4[2]], 'k-')
ax.plot([p4[0], p1[0]], [p4[1], p1[1]], [p4[2], p1[2]], 'k-')
ax.plot([p5[0], p6[0]], [p5[1], p6[1]], [p5[2], p6[2]], 'k-')
ax.plot([p6[0], p7[0]], [p6[1], p7[1]], [p6[2], p7[2]], 'k-')
ax.plot([p7[0], p8[0]], [p7[1], p8[1]], [p7[2], p8[2]], 'k-')
ax.plot([p8[0], p5[0]], [p8[1], p5[1]], [p8[2], p5[2]], 'k-')
ax.plot([p5[0], p1[0]], [p5[1], p1[1]], [p5[2], p1[2]], 'k-')
ax.plot([p6[0], p2[0]], [p6[1], p2[1]], [p6[2], p2[2]], 'k-')
ax.plot([p7[0], p3[0]], [p7[1], p3[1]], [p7[2], p3[2]], 'k-')
ax.plot([p8[0], p4[0]], [p8[1], p4[1]], [p8[2], p4[2]], 'k-')
theta_x=theta_x +20
ani = animation.FuncAnimation(fig, animate, frames=760, interval=1000)
plt.show()