Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   FIRST Tech Challenge (http://www.chiefdelphi.com/forums/forumdisplay.php?f=146)
-   -   [FTC]: Slow Down Max Speed!! (http://www.chiefdelphi.com/forums/showthread.php?t=78958)

JohnFogarty 13-11-2009 18:46

[FTC]: Slow Down Max Speed!!
 
in robotC what is the code you need to put in to make your Max speed decrease?

NickE 13-11-2009 18:55

Re: [FTC]: Slow Down Max Speed!!
 
Divide the input by a constant before sending the value to the motors. I'm not too familiar with FTC, but in RobotC for Vex, it would look like something like this:
Code:

motor[port1] = vexRT[Ch3] / 1.5;
A better solution would probably be to re-gear the robot for the desired speed. In changing the maximum speed in code, you cannot use the maximum power of the motors.

JohnFogarty 13-11-2009 21:06

Re: [FTC]: Slow Down Max Speed!!
 
We need to slow the drive motors because we are using a 4:1 ratio and it's still too fast. i want to make the joysticks max be a certain speed.

NickE 13-11-2009 21:10

Re: [FTC]: Slow Down Max Speed!!
 
The code i put above would scale the input to the output. To limit it without scaling, do something like this:
Code:

if(vexRT[Ch1]>80){
  motor[port1] = 80;
}else if(vexRT[Ch1]<-80){
  motor[port1] = -80;
}else{
  motor[port1] = vexRT[Ch1];
}


JohnFogarty 13-11-2009 21:18

Re: [FTC]: Slow Down Max Speed!!
 
Quote:

Originally Posted by NickE (Post 882438)
The code i put above would scale the input to the output. To limit it without scaling, do something like this:
Code:

if(vexRT[Ch1]>80){
  motor[port1] = 80;
}else if(vexRT[Ch1]<-80){
  motor[port1] = -80;
}else{
  motor[port1] = vexRT[Ch1];
}


i will try and see if i can make this into FTC code thanks.

l0jec 14-11-2009 07:49

Re: [FTC]: Slow Down Max Speed!!
 
Quote:

Originally Posted by John_1102 (Post 882439)
i will try and see if i can make this into FTC code thanks.

I would recommend you take the scaling route (NickE's first example) as the second example will limit the output to the motor by only listening to 2/3 of the range of your analog stick (less control).
That or use a predefined speed constant with a simplified on/off control.

JohnFogarty 14-11-2009 09:08

Re: [FTC]: Slow Down Max Speed!!
 
Quote:

Originally Posted by l0jec (Post 882493)
I would recommend you take the scaling route (NickE's first example) as the second example will limit the output to the motor by only listening to 2/3 of the range of your analog stick (less control).
That or use a predefined speed constant with a simplified on/off control.

do you know the FTC code for that?

l0jec 14-11-2009 21:31

Re: [FTC]: Slow Down Max Speed!!
 
Quote:

Originally Posted by John_1102 (Post 882499)
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).

NickE 16-11-2009 02:35

Re: [FTC]: Slow Down Max Speed!!
 
Instead of using variables as in l0jec's example, you could use #define statements to reduce memory usage and potentially speed up the program execution some. With a #define statement, the compiler in RobotC will replace all instances of 'MAX_JOY_VAL', for instance, with the value defined in the statement. The robot will see the defined values and not the names for the values defined in the statements. Be sure not to use either equals signs or semicolons in these lines.

Here's the same code he posted, except with #define statments:
Code:

int simpleScale(int joyVal) {
 
#define MAX_JOY_VAL 127
#define MAX_MOTOR_VAL 100
#define 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; 
 
}


JohnFogarty 16-11-2009 10:46

Re: [FTC]: Slow Down Max Speed!!
 
Quote:

Originally Posted by NickE (Post 882720)
Instead of using variables as in l0jec's example, you could use #define statements to reduce memory usage and potentially speed up the program execution some. With a #define statement, the compiler in RobotC will replace all instances of 'MAX_JOY_VAL', for instance, with the value defined in the statement. The robot will see the defined values and not the names for the values defined in the statements. Be sure not to use either equals signs or semicolons in these lines.

Here's the same code he posted, except with #define statments:
Code:

int simpleScale(int joyVal) {
 
#define MAX_JOY_VAL 127
#define MAX_MOTOR_VAL 100
#define 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; 
 
}


Thanks i shall try these :D.

l0jec 16-11-2009 13:24

Re: [FTC]: Slow Down Max Speed!!
 
Quote:

Originally Posted by NickE (Post 882720)
Instead of using variables as in l0jec's example, you could use #define statements to reduce memory usage and potentially speed up the program execution some. With a #define statement, the compiler in RobotC will replace all instances of 'MAX_JOY_VAL', for instance, with the value defined in the statement. The robot will see the defined values and not the names for the values defined in the statements. Be sure not to use either equals signs or semicolons in these lines.

That is true of normal C, but ROBOTC claims that their scalar constants work like true constants and will not allocate additional memory/etc. Notice that I used the "const" keyword when defining those variables.
If we're not changing the MAX_MOTOR_VAL or MAX_JOY_VAL, then we're still wasting CPU on a floating point calculation on each call though. The ratio variable could also be pulled out of the function and made a constant if we want to be crazy with optimization.

Another more flexible option for such a method would be to take a joystick analog stick value as well as a max motor speed as arguments and calculate a custom scale on the fly. Would be useful if you want to provide multiple scales (based on max output) for different motors from a single function.


All times are GMT -5. The time now is 18:29.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi