0

我正在尝试在 arduino 控制的四足动物上实现逆运动学,但我得到了一些不准确的计算结果。为了检查出了什么问题,我将算法移植到 C++ 并在 PC 上运行。我的问题是我会假设坐标变化会产生相同的结果,无论我改变坐标的哪个方向。但是 x 和 y 轴上的过渡会产生几度差异,具体取决于方向。我的数学知识有点尘土飞扬,所以也许我在检查算法时错过了一些东西。腿的配置是这样的http://te.unib.ac.id/lecturer/indraagustian/wp-content/uploads/2014/05/逆运动学.jpg

什么可能导致结果的差异?

#include <cmath>
#include <iostream>

#define CONNECTED_SERVOS 12
#define PI 3.1415

#define coxa 55
#define femur 40
#define tibia 47

int nogo_buffer[CONNECTED_SERVOS];
int end_position[CONNECTED_SERVOS];
int cur_pw[CONNECTED_SERVOS];

int calibrate[] = { 1500, 1520, 1500, 1560, 1400, 1410, 1520, 1550, 1500, 1560, 1510, 1620 }; //calibrate servos to simmetrical positions

int floatToInt(double num) {
    //Rounding a number avoiding truncation:

    return (int)(num < 0 ? (num - 0.5) : (num + 0.5));
}

int radToMicro(double rad, int ref) {

    //Make sure rad isn't negative:


    if (rad < 0) {
        while (rad < -PI) {
            rad += PI;
        }
    }
    else {
        while (rad > PI) {
            rad -= PI;
        }
    }

    //edit 2400
    return ref - floatToInt(572.958 * rad);
}



void IkLeg(int x, int y, int z, int nLeg)
{

    double L1 = sqrt(pow(x,2) + pow(z,2));
    double L = sqrt(pow(L1 - coxa,2) + pow(y,2));
    double t = acos((pow(tibia,2) + pow(femur,2) - pow(L,2)) / (2 * tibia * femur)) / PI * 180;
    double f1 = acos(y / L) / PI * 180;
    double f2 = acos((pow(femur,2) + pow(L,2) - pow(tibia,2)) / (2 * femur * L)) / PI * 180;
    double f = f1 + f2;
    double c = atan2(z, x) / PI * 180;
    std::cout << c << std::endl;
    std::cout << f-90 << std::endl;
    std::cout << t-90 << std::endl;

    //coxa angle
    nogo_buffer[3 * nLeg - 1] = radToMicro(c/180*PI, calibrate[3 * nLeg - 1]);

    std::cout << nogo_buffer[3 * nLeg - 1] << std::endl;

    //femur angle
    nogo_buffer[3 * nLeg - 2] = radToMicro(f / 180 * PI, calibrate[3 * nLeg - 2]+900);

    std::cout << nogo_buffer[3 * nLeg - 2] << std::endl;

    //tibia angle
    nogo_buffer[3 * nLeg - 3] = radToMicro(t / 180 * PI, calibrate[3 * nLeg - 3]+900);

    std::cout << nogo_buffer[3 * nLeg - 3] << std::endl;
}

int main() {
    IkLeg(95,47,0,1);
    std::cout << "x transition" << std::endl;
    IkLeg(105, 47, 0, 1);
    IkLeg(85, 47, 0, 1);
    std::cout << "y transition" << std::endl;
    IkLeg(95, 57, 0, 1);
    IkLeg(95, 37, 0, 1);
    std::cout << "z transition" << std::endl;
    IkLeg(95, 47, 30, 1);
    IkLeg(95, 47, -30, 1);

    return 0;
}
4

0 回答 0