Thread: [FTC]: Robot C
View Single Post
  #10   Spotlight this post!  
Unread 10-11-2009, 09:36
l0jec l0jec is offline
Registered User
no team
 
Join Date: Oct 2009
Rookie Year: 2004
Location: St. Louis, MO
Posts: 52
l0jec has a spectacular aura aboutl0jec has a spectacular aura about
Re: [FTC]: Robot C

That is generally good advice, but I'm going to go ahead and share some extra insight. Remember that the joystick analog sticks have a range of -127 to 128, but your DC motors only have a range of -100 to 100. While you can directly pass the analog stick value to the motor & ROBOTC will not error, you are losing about 20% of the actual range of the analog stick which will not give you the good fine-grained control your robot is capable of.

The basic solution is to apply a linear scale such as: motor_output = joystick_input / max_joystick_value * max_motor_value.

However, we can do even better if we use a parabolic curve so that we have fine control at low speeds for a larger portion of the analog stick's range and then ramp up the scaling for the extremities of the analog stick's range. Below is a function which will do just that (as well as apply a deadzone):

Code:
//scaling function
int scaleForMotor(int joyVal) {

  //constants
  const int DEADZONE = 5;
  const int MAX_MOTOR_VAL = 100;
  const float MAX_JOY_VAL = 127.0;

  //check for deadzone
  if(abs(joyVal) < DEADZONE) {
    return 0;
  }

  //calculate scaled value
  int sign = joyVal / abs(joyVal); // 1 or -1
  float ratio = ((joyVal * joyVal) / (MAX_JOY_VAL * MAX_JOY_VAL));
  int scaledVal = (sign * MAX_MOTOR_VAL) * ratio;

  return scaledVal;
}
I have no issues sharing this to help other teams get going, but please attempt to understand what it is doing if you choose to copy it into your code. Feel free to adjust the constants to set your own deadzone or max motor output.
Just place the function in your code above the main task and call it like below:

Code:
//scales input from joystick 1 left analog stick's y-axis
motor[someMotor] = scaleForMotor(joystick.joy1_y1);  
//scales input from joystick 1 right analog stick's y-axis
motor[someOtherMotor] = scaleForMotor(joystick.joy1_y2);
Best of luck,
l0jec
Reply With Quote