
Re: Controlling 1 Motor with 2 Joysticks
First, you did not post the whole code. I assume your code is inside a forever while-loop. Secondly, your code is fighting to move the motor. If both joysticks are active, one will try to program the arm motor one way and the other will try to program it the other way. Since you did not use deadband, both of your joysticks are likely active at the same time (non-zero). I am not sure if I understand what you want to do exactly, but it sounds like you want to control the arm with the joystick but may want to slow it down if there is no load. If that's the case, you don't really need 2 joysticks. How about using just one joystick to control the arm at lower speed but when a button is pressed and held, then it will apply more power.
 |  |  |
 | Code: // // Joystick input macros. // #ifndef DEADBAND_INPUT_THRESHOLD #define DEADBAND_INPUT_THRESHOLD 20 #endif
/** * These macros ignore input value (n) that is within the DEADBAND_THRESHOLD. * This is necessary because analog joysticks do not always centered at zero. * So if the joystick is at the rest position, we will consider it zero even * though the value is non-zero but within DEADBAND_THRESHOLD. */ #define DEADBAND(n,t) ((abs(n) > (t))? (n): 0) #define DEADBAND_INPUT(n) DEADBAND(n, DEADBAND_INPUT_THRESHOLD)
/** * The NORMALIZE macro transforms a value (n) in the range between (sl) and * (sh) to the range between (tl) and (th). */ #define MOTOR_MIN_VALUE -100 #define MOTOR_MAX_VALUE 100
#define NORMALIZE(n,sl,sh,tl,th) (int)(((long)(n) - (sl))*((th) - (tl))/((sh) - (sl)) + (tl)) #define NORMALIZE_DRIVE(x,m,n) NORMALIZE(x, m, n, \ MOTOR_MIN_VALUE, MOTOR_MAX_VALUE) #define JOYSTICK_POWER(n) NORMALIZE_DRIVE(DEADBAND_INPUT(n), -128, 127)
int armPower;
while (true) { getJoystickSettings(joystick); armPower = JOYSTICK_POWER(joystick.joy2_y1);
if (armPower < 0) { // // It seems your code want to use less power when running in reverse, // but I am not sure how your formula works. Divide by 90 or 100 doesn't // seem right. // armPower /=2; }
if (joy2Btn(1) == 0) { // // Button 1 is not pressed, so we only need half power. // armPower /= 2; }
motor[arm] = armPower;
wait1Msec(100); }
|  |
 |  |  |
Since I don't know what power you want under which circumstances, the above code is just an example. Feel free to change the scale factor. Basically, if button 1 is pressed, the above code will run the arm "forward" full speed and half speed reverse. If button 1 is released, it will run the arm "forward" half speed and quarter speed in reverse.