////计算出正解,即电机要转动到的编码器值并传递给电机驱动===========================
//注意,输入的单位是10um
void solve_motor_f(int x, int y, int z)
{
#define La 11700//234 * 50
#define Lb 5150 //103 * 50
#define La2 136890000
#define Lb2 26522500
int a,b,c, speed_a, speed_b, speed_c;//分别是三个电机的编码器值和速度
int k1,k2,k3, u1,u2,u3, v1,v2,v3;//解算中间值,详见那姐们的论文
float t1,t2,t3;
int angle1, angle2, angle3;
long long mid,mid2,mid4,mid5;//解算优化中间值
long long mid3;
int delta_a, delta_b, delta_c,delta_max;
x /= 2;
y /= 2;
z /= 2; //从这里开始,运算单位都是20um,之前是10um ///3
mid = x*x + y*y + z*z;//mid记录的是三个坐标平方和,因为后面用的频繁 ///9
mid2 = (La2 - Lb2 - mid); ///10
//以下算a编码器值--------------------------------------------------------
k1 = mid2 / Lb + 2*z; ///19
v1 = mid2 / Lb - 2*z; ///20
u1 = (3464*x + 2000*y)/1000;//这里先把sqrt(3)给放大了,最后再缩小 ///18
mid5 = k1*v1/10; ///27
mid5 *=4; //19
mid3 = u1*u1/10 - mid5; ///29
mid4 = sqrt(mid3)*6325; ///373
t1 = (-u1*2000 - mid4)/(2*k1);//至此,t1被放大了2000倍,精度0.025% //46
angle1 = atan_list_1[(int)(t1*2.5)]; ///2568,**
a = -angle1 * 13201 ; ///209,**
a = a /5000;
//算b编码器值------------------------------------------------------------
k2 = k1;//mid2 / Lb + 2*z;
v2 = v1;//mid2 / Lb - 2*z;
u2 = (-3464*x + 2000*y)/1000;
// mid5 = k2*v2/10; //在这里a的mid3没有变化
// mid5 *=4;
mid3 = u2*u2/10 - mid5;
mid4 = sqrt(mid3)*6325;
t2 = (-u2*2000 - mid4)/(2*k2);
angle2 = atan_list_1[(int)(t2*2.5)];
b = -angle2 * 13201 ;
b = b /5000;
//以下算c编码器值--------------------------------------------------------
k3 = k1 / 2;//mid2 / Lb + 2*z;
v3 = v1 / 2;//mid2 / Lb - 2*z;
u3 = -2*y;
mid5 = mid5 / 4;//k1*v1/10;
//mid5 *=4; //19
mid3 = u3*u3/10 - mid5;
mid4 = sqrt(mid3)*6325;
t3 = (-u3*2000 - mid4)/(2*k3);
angle3 = atan_list_1[(int)(t3*2.5)];
c = -angle3 * 13201 ;
c = c /5000;
//解算完鸟!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//然后算编码器速度---------------------------------------------------------
//算出各个电机编码器值的差值,绝对值
delta_a = get_real_encoder(1) - a;
if(delta_a<0)delta_a = -delta_a;
delta_b = get_real_encoder(2) - b;
if(delta_b<0)delta_b = -delta_b;
delta_c = get_real_encoder(3) - c;
if(delta_c<0)delta_c = -delta_c;
//找出最大差值
delta_max = delta_a;
if(delta_max < delta_b)delta_max = delta_b;
if(delta_max < delta_c)delta_max = delta_c;
//算出速度
speed_a = (delta_a * SPEED_BASE)/ delta_max ;
speed_b = (delta_b * SPEED_BASE)/ delta_max ;
speed_c = (delta_c * SPEED_BASE)/ delta_max ;
//算完速度鸟----------------------------------------------------
//电机保护
if(a>84000)a = 84000; if(a<-47000)a=-47000;
if(b>94000)b = 94000; if(b<-40000)b=-40000;
if(c>90000)c = 90000; if(c<-50000)c=-50000;
if(speed_a == 0 )speed_a = 2;
if(speed_b == 0 )speed_b = 2;
if(speed_c == 0 )speed_c = 2;
//赋值一戳,退出
set_motor_distance(1, a, speed_a);
set_motor_distance(2, b, speed_b);
set_motor_distance(3, c, speed_c);
} |