View unanswered posts | View active topics It is currently Wed Nov 26, 2014 4:07 am






Reply to topic  [ 4 posts ] 
Using: TopHat 
Author Message
Rookie

Joined: Thu Dec 18, 2008 4:52 pm
Posts: 5
Post Using: TopHat
The information given on 'TopHat' is:
Code:
joystick.joy2_TopHat; // -1 = not pressed, otherwise 0 to 7 for pressed ‘octant’


How do I write an 'if-else' code for this variable?

i.e., what I want to do is:

Code:
if (joystick.joy2_TopHat = 0) //meaning the top button of the D-pad is pressed
{
motor[motorD] = 50; //motor runs at 50% power
}
else
{
motor[motorD] = 0; //motor does not run
}


How would I go about this?


Thu Dec 18, 2008 9:50 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Using: TopHat
Here is an example from the program we wrote for the showcase tournament:

Code:
const int TopHatIdle           = -1;  // Tophat value when no button is pressed.
const int TopHatNorth          =  0;  // Tophat value when the north button is pushed.
const int TopHatNorthEast      =  1;  // Tophat value when the northeast button is pushed.
const int TopHatEast           =  2;  // Tophat value when the east button is pushed.
const int TopHatSouthEast      =  3;  // Tophat value when the southeast button is pushed.
const int TopHatSouth          =  4;  // Tophat value when the south button is pushed.
const int TopHatSouthWest      =  5;  // Tophat value when the southwest button is pushed.
const int TopHatWest           =  6;  // Tophat value when the west button is pushed.
const int TopHatNorthWest      =  7;  // Tophat value when the northwest button is pushed.

/***************************************************************************
* HandleTopHat()
*
* Takes the current value of the tophat control (passed in) and performs the
* correct action.
*
* In general, all tophat buttons change the behavior of the other buttons.
*
* The North and South positions adjust the gear (speed range) of the drive motors.
*
* The East and West positions are shift states for the 8 pushbuttons. That
* means that when the tophat is pressed to the East or West no immediate
* action needs to be performed. Instead, the HandleButton() code switches
* on the state of the tophat to decide what mode we are in and what the
* individual buttons should do.
*
* For the purposes of this robot, if the the tophat is in the East position,
* The 8 regular buttons move the sensor arm to different positions instead of
* moving the manipulator arm which is the default behavior of the buttons.
*
* Holding the tophat in the West postion is reserved for higher level behaviors
* like scanning for colors, parallel parking and ordering take-out curry.
*
****************************************************************************/

void HandleTopHat(int nTopHat)
{
    switch (nTopHat)
    {
    case TopHatNorth:  // Increase the drive speed
        gearup();
        break;

    case TopHatSouth:  // Decrease the drive speed
        geardown();
        break;
    }
}

/***************************************************************************
* HandleButton()
*
* Based on the position of the tophat this function calls the corresponding
* handle___Button function.
*
* Supports 24 buttons. See the comment block for HandleTopHat for details.
*
***************************************************************************/
void HandleButton(short nButtons, short nTopHat)
{
    if (nLastButtons == nButtons)  // If the buttons are exactly the same as they were before we're done.
        return;

    nLastButtons = nButtons;       // remember what buttons were pressed so we don't repeat ourselves

    switch (nTopHat)
    {
    case TopHatEast:
        HandleSensorButton(nButtons);  // Shifted button state affects sensor arm
        break;

    case TopHatWest:
        HandleOtherButton(nButtons);   // Shifted button state does other functions
        break;

    default:
        HandleClawButton(nButtons);    // Normal button state affects the claw arm
        break;
    }
}


_________________
Jeff McBride
Benson Robotics Club


Fri Dec 19, 2008 12:04 am
Profile
Rookie

Joined: Thu Dec 18, 2008 4:52 pm
Posts: 5
Post Re: Using: TopHat
So in case of this code:

Code:
void HandleTopHat(int nTopHat)
{
    switch (nTopHat)
    {
    case TopHatNorth:  // Increase the drive speed
        gearup();
        break;

    case TopHatSouth:  // Decrease the drive speed
        geardown();
        break;
    }
}


Am I able to simply switch one of its effects with turning on a motor at some power level for instance?

i.e., instead of having 'gearup();', have like a executive command for a motor, servo, etc.

The goal is to press up and have a servo move something and and press down and have a servo move down.
I understand it would be much easier to use the other push buttons but I just want to know the possibilities available.

btw, thanks for the quick reply.


Fri Dec 19, 2008 12:19 am
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Using: TopHat
Certainly. You can make it do pretty much anything you want. I suggest that you start by writing a paragraph or two describing the behavior that you would like the tophat to have.

For example:

When the tophat is not being pressed I want the servo to stay where it is.
When the tophat is pushed north I want the servo to begin moving clockwise. It should rotate 10 degrees per second that the tophat is in the north position.
When you release the button the servo should stay put.
When the tophat is pushed south I want the servo to begin moving counter-clockwise. It should rotate 10 degrees per second that the tophat is in the south position.
When you release the button the servo should stay put.
If you press the tophat to the west the servo should immediately move all the way in the clockwise direction.
If you press the tophat to the east the servo should immediately move all the way in the counter-clockwise direction.


Once you have described your desired behavior to that level of detail then you can start creating code to implement it. You can start by turning your procedure into a big comment block at the beginning of your code.

If you can't get your code to match the behavior then you should post your code and we can give you some hints.

_________________
Jeff McBride
Benson Robotics Club


Fri Dec 19, 2008 1:30 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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:  
cron



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