首页 > 解决方案 > 围绕固定轴旋转立方体

问题描述

所以我有一个代码,它从 Raspberry Pi 中的 IMU 传感器获取实时角度信息,并使用 mqtt 代理传输到我的计算机。该角度基于 IMU 在直立位置(即固定坐标系)期间的方向。现在我想通过获取两个相应传感器读数之间的差异来为 IMU 设置动画,这将指示该特定轴所需的旋转量。但是,我得到的动画没有正确显示方向。我的问题是,在

obj.rotate(angle=a, axis=vec(x,y,z),origin=vector(x0,y0,z0))

vpython的功能,我怎么说旋转是围绕固定坐标系,而不是围绕IMU的轴旋转?我的代码如下:

from vpython import *
import math
import paho.mqtt.client as mqtt
import time

scene.title = "VPython: Draw a rotating cube"

scene.range = 2
scene.autocenter = True

def on_connect(client, userdata, flags, rc):
    global callback_on_connect
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() - if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("CoreElectronics/test")#these two are topics; thus the client here listens to these two topics
    client.subscribe("CoreElectronics/topic")
    callback_on_connect=1

# The callback for when a PUBLISH message is received from the server.
#  ONLY WORKS IF YOU HAVE A MESSAGE FROM RASPBERRY PI SINCE IT IS A CALLBACK!
def on_message(client, userdata, msg):
    global yaw
    global pitch
    global roll
    global callback_on_message
    callback_on_message=1
    #print(msg.topic+" "+str(msg.payload))
    #print(float(msg.payload))
    #print(3)
    f=msg.payload
    angles = [float(i) for i in f.split()]
    #print(3)
    #type(angles)

    yaw=angles[0]
    pitch=angles[1]
    roll=angles[2]
    print(angles)
    #x = [float(i) for i in f.split()]
    #print(x[0])
    #print(x[1])
        # Do something else

print("Drag with right mousebutton to rotate view.")
print("Drag up+down with middle mousebutton to zoom.")

cube = box(pos=vec(0,0,0),length=1,height=0.1,width=1)    # using defaults, see http://www.vpython.org/contents/docs/defaults.html

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

print("Connecting to server...")
client.connect("mqtt.eclipse.org", 1883, 60)
print("Reached loop function...")
client.loop_start()

yaw=''
pitch=''
roll=''
yaw2=0
pitch2=0
roll2=0
#callback_on_connect=0
callback_on_message=0;

while callback_on_message==0:
    print("waiting for callback as a result of successful message receive", callback_on_message)
#now we are connected, can start taking in data
while True:      # Animation-loop
    try:
        #print("animating")
        #arrow(pos=vec(0,0,0),axis=vec(1,0,0))
        #arrow(pos=vec(0,0,0),axis=vec(0,1,0))
        #arrow(pos=vec(0,0,0),axis=vec(0,0,1))
        cube.rotate(angle=radians(yaw-yaw2), axis=vec(1,0,0),origin=vector(0,0,0))
        cube.rotate(angle=radians(pitch-pitch2), axis=vec(0,1,0),origin=vector(0,0,0))
        cube.rotate(angle=radians(roll-roll2), axis=vec(0,0,1),origin=vector(0,0,0))
        yaw2=yaw
        pitch2=pitch
        roll2=roll

    except KeyboardInterrupt:
        client.loop_stop()

    #cube.rotate( angle=yaw, axis=vec(1,0,0) )
    #cube.rotate( angle=pitch, axis=vec(0,1,0) )
    #cube.rotate( angle=roll, axis=vec(0,0,1) )

标签: python3drotational-matricesvpython

解决方案


推荐阅读