Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Inverting Axis (http://www.chiefdelphi.com/forums/showthread.php?t=133776)

2386programming 31-01-2015 14:02

Inverting Axis
 
Hello, I am trying to find a way for when our driver starts reversing with our mecanum drive robot, the rotation axis gets inverted.

kinganu123 31-01-2015 17:22

Re: Inverting Axis
 
Show us what you have first so we can suggest different approaches on how you can accomplish it.

Oromus 01-02-2015 10:36

Re: Inverting Axis
 
I'd suggest multiplying the axis you want to invert by -1. Example code:

Code:

if (reversing) { //Put whatever code you use to detect if you're reversing here
    myMotor.set(joystick.getYAxis() * -1); //Set a motor to the inverse of the Y axis.
}


notmattlythgoe 02-02-2015 11:41

Re: Inverting Axis
 
Quote:

Originally Posted by Oromus (Post 1436909)
I'd suggest multiplying the axis you want to invert by -1. Example code:

Code:

if (reversing) { //Put whatever code you use to detect if you're reversing here
    myMotor.set(joystick.getYAxis() * -1); //Set a motor to the inverse of the Y axis.
}


You can also just place a - in front of it.

Code:

if (reversing) { //Put whatever code you use to detect if you're reversing here
    myMotor.set(-joystick.getYAxis()); //Set a motor to the inverse of the Y axis.
}


Ether 02-02-2015 11:46

Re: Inverting Axis
 
Quote:

Originally Posted by notmattlythgoe (Post 1437258)

You can also just place a - in front of it.

Code:

if (reversing) { //Put whatever code you use to detect if you're reversing here
    myMotor.set(-joystick.getYAxis()); //Set a motor to the inverse of the Y axis.
}


Is this just a matter of aesthetics, or does it actually generate more efficient code?



GeeTwo 02-02-2015 11:56

Re: Inverting Axis
 
Quote:

Originally Posted by notmattlythgoe (Post 1437258)
You can also just place a - in front of it.

Code:

if (reversing) { //Put whatever code you use to detect if you're reversing here
    myMotor.set(-joystick.getYAxis()); //Set a motor to the inverse of the Y axis.
}


Quote:

Originally Posted by Ether (Post 1437260)
Is this just a matter of aesthetics, or does it actually generate more efficient code?

I'm not sure about Java, but I do know that with traditional compilers, it is more efficient - changing the sign on a number is a much simpler operation, usually done in a single clock cycle. Unless the compiler is doing serious optimization, multiplying by -1 is a full floating-point multiply.

Ether 02-02-2015 12:02

Re: Inverting Axis
 
Quote:

Originally Posted by GeeTwo (Post 1437264)
Unless the compiler is doing serious optimization, multiplying by -1 is a full floating-point multiply.

Why would you consider that to be a serious -- rather than a simple and obvious -- optimization?



notmattlythgoe 02-02-2015 12:06

Re: Inverting Axis
 
Quote:

Originally Posted by Ether (Post 1437260)
Is this just a matter of aesthetics, or does it actually generate more efficient code?



That is a question I do not know the answer to. I'm assuming that it is just aesthetic. You are technically using the unary minus operator in both situations because the - in front of the 1 is technically inverting the value of 1.

It is however better practice to place the - in front of the variable instead of multiplying by -1.

Ether 02-02-2015 12:49

Re: Inverting Axis
 
Quote:

Originally Posted by notmattlythgoe (Post 1437269)
It is however better practice to place the - in front of the variable instead of multiplying by -1.

I share your opinion on this matter1, but to move out of the realm of individual opinion: Is there a widely accepted code formatting standard that states this explicitly?

1for aesthetic reasons and to discourage the compiler from generating a floating-point multiply


notmattlythgoe 02-02-2015 13:05

Re: Inverting Axis
 
Quote:

Originally Posted by Ether (Post 1437290)
I share your opinion on this matter1, but to move out of the realm of individual opinion: Is there a widely accepted code formatting standard that states this explicitly?

1for aesthetic reasons and to discourage the compiler from generating a floating-point multiply


It is known.

I do not know of any source that explicitly states it, and I haven't found one in a quick Google search. It's just the way I learned and it is the way I do it at work.

GeeTwo 02-02-2015 13:26

Re: Inverting Axis
 
Quote:

Originally Posted by Ether (Post 1437266)
Why would you consider that to be a serious -- rather than a simple and obvious -- optimization?

It's simple and obvious to a human, of course. If things were obvious to computers, programming them would be much easier.

OBTW, you pass the Turing test.

Ether 02-02-2015 13:37

Re: Inverting Axis
 
Quote:

Originally Posted by GeeTwo (Post 1437302)
It's simple and obvious to a human, of course. If things were obvious to computers...

You are claiming that particular optimization isn't obvious to a compiler. In fact, you implied it would be difficult (serious optimization). Please explain your reasoning for that.




GeeTwo 02-02-2015 13:51

Re: Inverting Axis
 
I did a bit of Googling, and it does seem that several optimizers will generate the same code for both cases. Optimizers appear to have come a ways since I last paid close attention.

It still makes sense to me to ask for -x (or 0-x) rather than -1 * x. Provided that the code is equally (or arguably better) readable by people, why not give the computer the simplest directions?

Ether 02-02-2015 13:59

Re: Inverting Axis
 
Quote:

Originally Posted by GeeTwo (Post 1437319)
It still makes sense to me to ask for -x...rather than -1 * x

Nobody is arguing otherwise.

Quote:

..why not give the computer the simplest directions?
Because "simple" is subjective and a matter of personal preference.

That's why I asked if this (very reasonable, in my opinion) use of the unary operator in this case might be part of a widely-accepted Java coding standard that could be referenced in support of the recommendation.





GeeTwo 02-02-2015 13:59

Re: Inverting Axis
 
Quote:

Originally Posted by GeeTwo (Post 1437319)
Provided that the code is equally (or arguably better) readable by people, why not give the computer the simplest directions?

For an example where the simpler code is not easier to read:

Logically, you want to do a conditional:

if (c is between a and b, and you don't know whether a is larger or smaller than b) then...

Human simpler (use this!):

if ((a<b) && (b<c) || (a>b) && (b>c)) then..

Probably machine quicker, but don't use:

if ((a-b)*(b-c) > 0) then..

Ether 02-02-2015 14:09

Re: Inverting Axis
 
Quote:

Originally Posted by GeeTwo (Post 1437326)
For an example where the simpler code is not easier to read:

Logically, you want to do a conditional:

if (c is between a and b, and you don't know whether a is larger or smaller than b) then...

Human simpler (use this!):

if ((a<b) && (b<c) || (a>b) && (b>c)) then..

Probably machine quicker, but don't use:

if ((a-b)*(b-c) > 0) then..

Either your code is wrong, or your prose description is.

I guess neither is simpler.



GeeTwo 02-02-2015 16:01

Re: Inverting Axis
 
I swapped b and c between the two. Let's correct the prose. I meant to say that b was between a and c, and you didn't know whether a or c were larger.

2386programming 03-02-2015 16:44

Re: Inverting Axis
 
Code:

double strafe = mainStick.getRawAxis(3);
        double forward = mainStick.getRawAxis(1);
        double rotation = mainStick.getRawAxis(2);


//mecanum algotrithms
        double mFL = strafe + forward + rotation;
        double mFR = strafe + forward - rotation;
        double mRL = strafe - forward + rotation;
        double mRR = strafe - forward - rotation;

        FL.set(mFL);
        FR.set(mFR);
        RL.set(mRL);
        RR.set(mRR);


This is my code for regular driving for mecanum but I feel that if I inverted the motot not everything else would be the same.

Ether 03-02-2015 17:31

Re: Inverting Axis
 
Quote:

Originally Posted by 2386programming (Post 1437809)

Code:



        double mFL = strafe + forward + rotation;
        double mFR = strafe + forward - rotation;
        double mRL = strafe - forward + rotation;
        double mRR = strafe - forward - rotation;


This is my code for regular driving for mecanum but I feel that if I inverted the motor not everything else would be the same.

The correct inverse kinematics for mecanum is:
Code:



double mFL = forward - strafeRight  - rotateClockwise;
double mFR = forward + strafeRight  + rotateClockwise;
double mRL = forward - strafeRight  + rotateClockwise;
double mRR = forward + strafeRight  - rotateClockwise;

If the above doesn't work, it's better to do the necessary motor inverting/swapping to make it work, rather than changing the signs of the kinematics.




All times are GMT -5. The time now is 13:03.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi