如何链接2个变量? [英] How to link 2 variables?

查看:100
本文介绍了如何链接2个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在这里遇到问题,我设计的功能可以顺畅地移动1个以上的rc_servo电机,

但我真的有问题,

Hello everyone,
I have a problem here, i designing a function that can move more than 1 rc_servo motor smoothly,
but i really have problem here,

void Poly_servo_goto(int8 index1, int8 index2, float angle1, float angle2)
{
   int8 i;
   const int8 motor_count = 2;
   float max_angle;
   float angles[motor_count];
   
   angles[0] = angle1;
   angles[1] = angle2;
   for(i = 0; i < motor_count; i++)
   {
      if(angles[i] > max_angle){max_angle = angles[i];}
   }
}





函数的输入是index1,index 2,angle1,angle2

索引是选择哪个选择器在我的数组中使用的项目,数组是电机的结构数组。



问题是angle1是伺服的所需输出[index1]

和angle2是伺服的所需输出[index2]



现在在这个函数中我返回2个角度的最大值,但是当值成功返回我怎么知道它属于哪个索引,



提前谢谢,

z3ngew



the input of the function is index1 , index 2, angle1, angle2
the index is a selector to select which item in my array to work with, the array is an array of structures for the motor.

the problem is angle1 is the desired output of servo[index1]
and angle2 is the desired output of servo[index2]

now in this function i am returning the maximum of the 2 angles, but when the value is successfully returned how can i know which index it belongs to,

Thanks in advance,
z3ngew

推荐答案

简单:您必须返回两个值:角度和运动索引。您的代码也有错误,max_angle未初始化。对于两个固定值,使用for循环是一种过度杀伤力。仅当输入角度在数组中时,使用for循环才是合理的。传递马达指数也是一种浪费,我们知道它们只能是零或1,它们是常数。

Simple: You have to return two values: an angle and a motor index. Your code also has a bug, max_angle isn't initialized. For two fixed values its an overkill to use a for loop. Using a for loop would be reasonable only if the input angles were in an array. Passing in the motor indices is also a waste, we know that they can only be zero or 1, they are constants.
void Poly_servo_goto(float angle1, float angle2, uint8* result_index, float* result_angle)
{
    if (angle1 >= angle2)
    {
        *result_index = 0;
        *result_angle = angle1;
    }
    else
    {
        *result_index = 1;
        *result_angle = angle2;
    }
}

// An alternative to the previous solution with "linking" the two result variables.
struct MotorParams
{
    uint8 index;
    float angle;
};

void Poly_servo_goto(float angle1, float angle2, struct MotorParams* motor_params)
{
    if (angle1 >= angle2)
    {
        motor_params->index = 0;
        motor_params->angle = angle1;
    }
    else
    {
        motor_params->index = 1;
        motor_params->angle = angle2;
    }
}


void Poly_servo_goto(float angles[], int num_angles, uint8* result_index)
{
    uint8 index = 0;
    float max_angle = 0;
    for (int i=0; i<num_angles; ++i)
    {
        if (angles[i] >= max_angle)
        {
            max_angle = angles[i];
            index = (uint8)i;
        }
    }
    *result_index = index;
}





其他一些建议:如果你不这样做,请不要使用比int / unsigned更小的整数,如(uint8)我想节省内存空间。平台上最有效的整数通常是一个整数,其宽度与处理器的寄存器相同(在32位系统上为int32)。 int / unsigned通常满足此要求(在16位和32位平台上,而不是在64位平台上)。当您在堆栈上传递参数并将它们存储在局部变量中时,由于多种原因,它通常仍占用机器字的空间(32位系统上的32位),因此使用int / uint通常是一个很好的解决方案并且代码更好。



Some other advices: Don't use smaller integers than int/unsigned like (uint8) if you don't want to save space in memory. The most effective integer on a platform is usually an integer that has to same width as the registers of the processor (on a 32 bit system its int32). int/unsigned usually satisfies this requirement (on 16 and 32 bit platforms, not on 64 bit ones). When you pass parameters on the stack and you store them in local variables it usually still takes the space of a machine-word (32 bit on 32bit system) for many reasons so using an int/uint is often a nice solution and the code nicer.


我找到了一个对我很有用的解决方案,它的想法是统一angle_array索引和motor_output的motor index然后每个角度索引代表它所属的电机驱动

i found a solution which works great with me, its idea is to unify the angle_array index and the maximum_output of motor index and then the each angle index represents its belonging motor to drive
void Poly_servo_goto(int8 index1, int8 index2, float angle1, float angle2)
{
   int8 i;
   int8 max_index;
   float max_angle;
   float angles[max_output];

   angles[index1] = angle1;
   angles[index2] = angle2;
   for(i = 0; i < max_output; i++)
   {
      if(angles[i] > max_angle)
      {
         max_angle = angles[i];
         max_index = i;
      }
   }
}





感谢您的努力pasztorpisti,



z3ngew



thanks for your effort pasztorpisti,

z3ngew


这篇关于如何链接2个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆