这是代码的一部分,可视化我的arduino的旋转。 不幸的是,滚动是“反转的”......每次打勾都会调用一些更新的值。
反转我的意思是:如果我将arduino向右滚动,可视化将其向左滚动,反之亦然。
pushMatrix(); // begin object
translate(width/4, height/4); // set position
float c1 = cos(radians(roll));
float s1 = sin(radians(roll));
float c2 = cos(radians(pitch));
float s2 = sin(radians(pitch));
float c3 = cos(radians(yaw));
float s3 = sin(radians(yaw));
applyMatrix( c2*c3, s1*s3+c1*c3*s2, c3*s1*s2-c1*s3, 0,
-s2 , c1*c2 , c2*s1 , 0,
c2*s3, c1*s2*s3-c3*s1, c1*c3+s1*s2*s3, 0,
0 , 0 , 0 , 1);
drawArduino();
popMatrix(); // end of object
有人看到我犯的错误吗?
这就是我计算传递给矩阵的值的方法:
int aix, aiy, aiz, tax, tay, taz;
int gix, giy, giz;
float ax, ay, az;
float gx, gy, gz;
float roll, pitch, heading;
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
// convert from raw data to gravity and degrees/second units
ax = convertRawAcceleration(aix);
ay = convertRawAcceleration(aiy);
az = convertRawAcceleration(aiz); //acceleration for 2g
gx = convertRawGyro(gix);
gy = convertRawGyro(giy);
gz = convertRawGyro(giz);
// update the filter, which computes orientation
filter.updateIMU(gx, gy, gz, ax, ay, az);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
答案 0 :(得分:0)
一个选项是反转滚动值:这可以通过从滚动值中减去滚动值来实现
inverted = max - current
我看到你正在使用学位,所以这里的演示你可以运行如何反转这个值,假设你的最大值是360:
var roll, pitch, yaw;
var maxRoll = 360;
var invertRoll = false;
function setup(){
createCanvas(300,300);
rectMode(CENTER);
}
function draw(){
var rollValue = roll;
if(invertRoll){
rollValue = maxRoll - roll;
background(0);
}else{
background(255);
}
background(invertRoll ? color(0) : color(255));
translate(width * 0.5, height * 0.5);
rotate(radians(rollValue));
rect(0,0,100,100);
}
function mouseDragged(){
roll = map(mouseX,0,width,0,360);
}
function keyPressed(){
invertRoll = !invertRoll;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/p5.min.js"></script>
如果有帮助,这是基于上面代码段的处理测试草图:
float roll, pitch, yaw;
float maxRoll = 360;
boolean invertRoll;
void setup() {
size(400,400,P3D);
}
void draw() {
background(255);
pushMatrix(); // begin object
translate(width/4, height/4); // set position
float rollValue = roll;
if(invertRoll) rollValue = maxRoll - roll;
float c1 = cos(radians(rollValue));
float s1 = sin(radians(rollValue));
float c2 = cos(radians(pitch));
float s2 = sin(radians(pitch));
float c3 = cos(radians(yaw));
float s3 = sin(radians(yaw));
applyMatrix( c2*c3, s1*s3+c1*c3*s2, c3*s1*s2-c1*s3, 0,
-s2, c1*c2, c2*s1, 0,
c2*s3, c1*s2*s3-c3*s1, c1*c3+s1*s2*s3, 0,
0, 0, 0, 1);
drawArduino();
popMatrix(); // end of object
}
void drawArduino() {
box(100);
}
void mouseDragged(){
roll = map(mouseX,0,width,0,360);
}
void keyPressed(){
invertRoll = !invertRoll;
}
另一种选择是将想要反转的轴缩放-1 然后转换为偏移回你想要绘制的位置。