变换矩阵(翻转?)
Transformation Matrix (roll inversed?)
这是代码的一部分,可视化了我的 arduino 的旋转。
不幸的是,滚动是 "inversed"...它会在每个滴答声中调用一些更新的值。
我所说的反转是指:如果我将 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();
一个选项是反转滚动值:这可以通过从滚动值可以具有的最大值中减去滚动值来完成
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
然后平移偏移回到你想画的位置。
这是代码的一部分,可视化了我的 arduino 的旋转。 不幸的是,滚动是 "inversed"...它会在每个滴答声中调用一些更新的值。
我所说的反转是指:如果我将 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();
一个选项是反转滚动值:这可以通过从滚动值可以具有的最大值中减去滚动值来完成
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 然后平移偏移回到你想画的位置。