View unanswered posts | View active topics It is currently Fri Jul 25, 2014 3:02 am






Reply to topic  [ 9 posts ] 
!!HELP!! Teleop/Joystick Buttons ??? 
Author Message
Rookie

Joined: Fri Feb 04, 2011 2:13 pm
Posts: 4
Post !!HELP!! Teleop/Joystick Buttons ???
Problem:
I am not good with writing code.
We need the motor[claw] to operate with the joystick buttons (6 & 8 would be good).
However, every attempt to get that to happen fails.
We could put the [claw] control on a joystick and it works, but that is not optimal.
Last year the buttons worked just fine when we programed our robot.
If it helps, when we push joystick buttons 6 & 8, the debug window shows 07 & 05,..?
I have tried every possible combination 5, 05, 06, 6 ,.. etc.
Sometimes the claw opens a bit but doesnt close when a button is pushed.
At wits end.

Yes, even tried replacing joysticks
Code:
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Motor, motorA, claw, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorB, wristbend, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorC, wristtwist, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_1, Armbend, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, Rightdrive, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, Leftdrive, tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "JoystickDriver.c"

void initializeRobot()
{
return;
}

task main()
{
initializeRobot();

waitForStart();

while(true)
{
getJoystickSettings(joystick);
motor[Rightdrive] = joystick.joy1_y2;
motor[Leftdrive] = joystick.joy1_y1;
motor[wristbend] = joystick.joy2_y2;
motor[wristtwist] = joystick.joy2_x2;
motor[Armbend] = joystick.joy2_y1;
motor[claw] = joystick.joy2_Buttons;
}
while (true)
{
if(joystick.joy1_y1 < 15 && joystick.joy1_y1 > -15)
{
motor[Leftdrive] = 0;
}
else
{
motor[Leftdrive] = 100;
}

if(joystick.joy1_y2 < 15 && joystick.joy1_y2 > -15)
{
motor[Rightdrive] = 0;
}
else
{
motor[Rightdrive] = 100;
}

if(joystick.joy2_x1 < 15 &&joystick.joy2_x1 > -15)
{
motor[wristtwist] = 0;
}
else
{
motor[wristtwist] = 80;
}

if(joystick.joy2_y2 < 15 && joystick.joy2_y2 > -15)
{
motor[wristbend] = 0;
}
else
{
motor[wristbend] = 80;
}

if(joystick.joy2_y1 <15 && joystick.joy2_y1 > -15)
{
motor[Armbend] = 0;
}
else
{
motor[Armbend] = 40;
}

if(joy2Btn(6) == 1)
{
motor[claw] = 80;
}
else if(joy2Btn(8) == 1)
{
motor[claw] = -80;
}
else
{
motor[claw] = 0;

}
}
}


Fri Feb 04, 2011 3:04 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: !!HELP!! Teleop/Joystick Buttons ???
I have reformated your code for better readability and added comments to it.
Code:
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Motor, motorA, claw, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorB, wristbend, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorC, wristtwist, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_1, Armbend, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, Rightdrive, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, Leftdrive, tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "JoystickDriver.c"

void initializeRobot()
{
    return;
}

task main()
{
    initializeRobot();

    waitForStart();

    while(true)
    {
        getJoystickSettings(joystick);

        //
        // The following lines allow you to drive your robot with joystick 1.
        //
        motor[Rightdrive] = joystick.joy1_y2;
        motor[Leftdrive] = joystick.joy1_y1;
        //
        // The following lines allow you to drive your arm attachment with joystick 2.
        //
        motor[wristbend] = joystick.joy2_y2;
        motor[wristtwist] = joystick.joy2_x2;
        motor[Armbend] = joystick.joy2_y1;

        //What is the intention of the following line? This is not doing anything useful.
        motor[claw] = joystick.joy2_Buttons;
    }

    //
    // All the following lines are irrelevant because they won't execute.
    // The loop above is a never ending loop so it will never exit.
    //
    while (true)
    {
        if(joystick.joy1_y1 < 15 && joystick.joy1_y1 > -15)
        {
            motor[Leftdrive] = 0;
        }
        else
        {
            motor[Leftdrive] = 100;
        }

        if(joystick.joy1_y2 < 15 && joystick.joy1_y2 > -15)
        {
            motor[Rightdrive] = 0;
        }
        else
        {
            motor[Rightdrive] = 100;
        }

        if(joystick.joy2_x1 < 15 &&joystick.joy2_x1 > -15)
        {
            motor[wristtwist] = 0;
        }
        else
        {
            motor[wristtwist] = 80;
        }

        if(joystick.joy2_y2 < 15 && joystick.joy2_y2 > -15)
        {
            motor[wristbend] = 0;
        }
        else
        {
            motor[wristbend] = 80;
        }

        if(joystick.joy2_y1 <15 && joystick.joy2_y1 > -15)
        {
            motor[Armbend] = 0;
        }
        else
        {
            motor[Armbend] = 40;
        }

        if(joy2Btn(6) == 1)
        {
            motor[claw] = 80;
        }
        else if(joy2Btn(8) == 1)
        {
            motor[claw] = -80;
        }
        else
        {
            motor[claw] = 0;
        }
    }
}


Fri Feb 04, 2011 3:58 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: !!HELP!! Teleop/Joystick Buttons ???
Here is my guess of your intention. However, your original code seemed to suggest you don't want the joystick movement to control the power of the motors (i.e. you always set the power to a constant if it is outside of the deadband). This may make sense for the arm attachment but not for drive motors. If you want to set a constant power of a motor on user input, you should use a button instead of the joystick.
Code:
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Motor, motorA, claw, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, motorB, wristbend, tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor, motorC, wristtwist, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_1, Armbend, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, Rightdrive, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, Leftdrive, tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "JoystickDriver.c"

//
// The DEAD_BAND macro makes sure non-zero-centered joystick won't move the robot.
//
#define DEAD_BAND(n, t) ((abs(n) < (t))? 0: (n))
//
// The MOTOR_POWER macro translates the joystick range of (-128 to 127) to the motor
// power range of (-100 to 100) after applying DEAD_BAND to the joystick value.
//
#define MOTOR_POWER(n)  (DEAD_BAND(n, 15)*100/128)

void initializeRobot()
{
    return;
}

task main()
{
    initializeRobot();

    waitForStart();

    while(true)
    {
        getJoystickSettings(joystick);

        //
        // The following lines allow you to drive your robot with joystick 1.
        //
        motor[Rightdrive] = MOTOR_POWER(joystick.joy1_y2);
        motor[Leftdrive] = MOTOR_POWER(joystick.joy1_y1);
        //
        // The following lines allow you to drive your arm attachment with joystick 2.
        //
        motor[wristbend] = MOTOR_POWER(joystick.joy2_y2);
        motor[wristtwist] = MOTOR_POWER(joystick.joy2_x2);
        motor[Armbend] = MOTOR_POWER(joystick.joy2_y1);

        if (joy2Btn(5))
        {
            motor[claw] = 80;
        }
        else if (joy2Btn(7))
        {
            motor[claw] = -80;
        }
        else
        {
            motor[claw] = 0;
        }

        wait1Msec(10);
    }
}


Fri Feb 04, 2011 4:16 pm
Profile
Rookie

Joined: Fri Feb 04, 2011 2:13 pm
Posts: 4
Post Re: !!HELP!! Teleop/Joystick Buttons ???
See, I told you I wasnt very good at writing code. :)

You guessed our intentions correct.
The code changes worked GREAT!
(Even cleared up a "twitching" issue the motors had when paused)

THANK YOU , THANK YOU, THANK YOU !


Fri Feb 04, 2011 4:45 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: !!HELP!! Teleop/Joystick Buttons ???
I am glad it works out for you. Make sure you understand all the changes and how they work. :)


Fri Feb 04, 2011 4:52 pm
Profile
Rookie

Joined: Fri Feb 04, 2011 2:13 pm
Posts: 4
Post Re: !!HELP!! Teleop/Joystick Buttons ???
New Topic, but posting here because the applicable code is on this thread.

The "Twitching" motors are becoming a BIG problem.
(I thought that the new code suggestions had solved the problem, but I was mistaken).

Problem: The (3) NXT motors in the above code sometimes seem to have a life of their own, mostly [motor B wristbend].
During Teleop the motors will start to move even with no joystick input and sometimes wont move with joystick input.
We have replaced the joysticks and batteries in the NXT.
The 12v Tetrix motors do not seem to be affected.
I dont know if it is a problem with the code, NXT Brick, WiFi or the motors themselves.

Any Suggestions?


Wed Feb 09, 2011 11:14 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: !!HELP!! Teleop/Joystick Buttons ???
The above code is very simple. As you can see, the only lines that dealt with wristbend and writsttwist are:
Code:
        motor[wristbend] = MOTOR_POWER(joystick.joy2_y2);
        motor[wristtwist] = MOTOR_POWER(joystick.joy2_x2);

So if it ever moves, it means the joy2_x2 and joy2_y2 are not zero. If you are sure you were hands off joy2 and it was still moving, it means your joystick was returning non-zero at its center position. Having said that, the above code does have the DEAD_BAND macro to eliminate that problem. However, it may mean the deadband is not big enough (i.e. the tolerance 15 is too small). To test this theory, you don't even need to run code. In the RobotC IDE, open the joystick dialog and move the joystick randomly then let go. Note the value of the joy2_x2 and joy2_y2 values. They will not be zero but if they are greater than 15, that's your problem. What joysticks are you using? If you have confirmed this is your problem, you should move the joystick and let go many times and note the maximum "center" values you have ever seen, add a little margin and set that as your new deadband tolerance instead of 15. Just FYI, we use 20 as our deadband tolerance.


Wed Feb 09, 2011 1:41 pm
Profile
Rookie

Joined: Fri Feb 04, 2011 2:13 pm
Posts: 4
Post Re: !!HELP!! Teleop/Joystick Buttons ???
Thx.
I will set the DB to 20 or above to see if that fixes it.
However, I feel like its something else.
The joystick shows no value in the debug window when its not moved.
Also, the motor "twitches", that is moves sporatically off, on, off, on,..etc. like it has a pulse.
There was also an instance where it wouldnt move or lag when a control input was issued.
Could the motors/s be failing.?
A possible signal interference?

We are shutting down now because of snow. I hope to try your fix tomorrow, (if we are here).

Thanks for the quick reply!


Wed Feb 09, 2011 2:00 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: !!HELP!! Teleop/Joystick Buttons ???
What do you mean by "The joystick shows no value in the debug window when its not moved"? It must show some values in the joystick dialog. Even if the joystick is perfectly centered, it should show "0". But in any case, if the problem is not the code, then anything is possible, wiring, electronics malfunction (e.g. motor controllers) etc. Good luck in locating the root of the problem.


Wed Feb 09, 2011 2:09 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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.