Quote:
Originally Posted by NickE
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.