Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
C++
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];}
   }
}


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
Posted

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.
C++
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;
}


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.
 
Share this answer
 
v2
Comments
z3ngew 16-Aug-13 17:14pm    
i have done this because this same function is duplicated but with 3 and 4 angles/index
pasztorpisti 16-Aug-13 17:25pm    
Then its time to learn more about arrays. :-) An important note related to passing arrays as function parameters: declaring an array parameter is equivalent to declaring that parameter as a pointer. So float angles[] is equivalent to float* angles as a function parameter so if you modify the array from the function then you modify the array you passed as a parameter. This is common mistake of beginners.
Sergey Alexandrovich Kryukov 17-Aug-13 1:09am    
Good points, a 5.
—SA
pasztorpisti 17-Aug-13 7:33am    
Thank you! I felt this example code necessary in this case. I'm afraid it wasn't enough...
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
C++
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;
      }
   }
}


thanks for your effort pasztorpisti,

z3ngew
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900