Quote:
Originally Posted by John_1102
do you know the FTC code for that?
|
There are many ways to go about it, but I would create a scaling function. Here is an example with the important constants defined within the function. For best performance, I would recommend you calculate the ratio with the constants defined staticaly outside of the function (avoid calculating the ratio on each call).
Code:
int simpleScale(int joyVal) {
const int MAX_JOY_VAL = 127;
const int MAX_MOTOR_VAL = 100;
const int DEADZONE = 5;
//check for deadzone
if(abs(joyVal) < DEADZONE) {
return 0;
}
//calculate ratio based on max motor speed and max analog stick value
float ratio = (MAX_MOTOR_VAL/MAX_JOY_VAL);
//apply ratio to actual joystick value
int result = (joyVal)*ratio;
return result;
}
Note that you should adjust the MAX_MOTOR_VAL constant to whatever max power output you want. Remember that the motors go from -100 to +100, so if you leave it at 100, you get 100% of your power; setting it to 80 gives you 80% and so on.
You can call the function from your main task similar to this:
Code:
//scale output to someMotor based on joystick 1 analog stick 1 y axis
motor[someMotor] = simpleScale(joy1_y1);
If you want to turn the motors on/off with a toggle button instead, you should create an if/else set of statements in your main task to check if the on or off buttons have been pressed and set the motors to either the desired max output (on) or to zero (off).