View unanswered posts | View active topics It is currently Thu Apr 17, 2014 9:21 pm






Reply to topic  [ 3 posts ] 
Controlling 1 Motor with 2 Joysticks 
Author Message
Rookie

Joined: Mon Dec 19, 2011 5:06 pm
Posts: 2
Post Controlling 1 Motor with 2 Joysticks
We are trying to control a motor with two joysticks. One joystick turns the motor on at low power when a load is not applied. The second joystick turns the motor on at a high power when a load is applied. We tried programming this, but only one of the joysticks would work, while the other one caused the motor to attempt to move but failed. Here's the code we used:

if (joystick.joy2_y1 > 0) {
motor[arm] = joystick.joy2_y1/2;
}
if (joystick.joy2_y1 < 0)
{
motor[arm] = joystick.joy2_y1/100;
}
if (joystick.joy2_y2 > 0)
{
motor[arm] = joystick.joy2_y2;
}
if (joystick.joy2_y2 < 0)
{
motor[arm] = joystick.joy2_y2/90;
}


Is it possible to control a single motor with 2 joysticks? If so, what's wrong with our code?


Mon Dec 19, 2011 5:22 pm
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post Re: Controlling 1 Motor with 2 Joysticks
Well, one thing I see, is that you are assuming that if you aren't using the joystick, the value will be 0. Due to tolerance issues, this is not true.

A couple more things:
You should give preference to one of the joysticks to prevent further issues. Try rearranging the logic.
Why is it that in one direction you give the motor a lot more power than you do in the other direction? I am guessing this has to do with gravity in the real world.

Try something like this:
Code:
#define THRESHOLD 5

if (joystick.joy2_y1 > THRESHOLD) {
   motor[arm] = joystick.joy2_y1/2;
}
else if (joystick.joy2_y1 < -THRESHOLD)
{
   motor[arm] = joystick.joy2_y1/100;
}
else if (joystick.joy2_y2 > THRESHOLD)
{
   motor[arm] = joystick.joy2_y2;
}
else if (joystick.joy2_y2 < -THRESHOLD)
{
   motor[arm] = joystick.joy2_y2/90;
}


Also note, that dividing the joystick value by 100 will only allow the motor value to be 0 or 1 (same with /90).

_________________
Matt


Mon Dec 19, 2011 6:00 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post 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.


Mon Dec 19, 2011 6:35 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.