Go to Post We are just here to give back to a program that gave us so much as students. - Alex Cormier [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 31-01-2015, 14:02
2386programming 2386programming is offline
Registered User
FRC #2386 (Trojans)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2011
Location: Burlington
Posts: 34
2386programming is an unknown quantity at this point
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.
Reply With Quote
  #2   Spotlight this post!  
Unread 31-01-2015, 17:22
kinganu123 kinganu123 is offline
Registered User
FRC #1747
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Piscataway, NJ
Posts: 243
kinganu123 is on a distinguished road
Re: Inverting Axis

Show us what you have first so we can suggest different approaches on how you can accomplish it.
__________________
Reply With Quote
  #3   Spotlight this post!  
Unread 01-02-2015, 10:36
Oromus's Avatar
Oromus Oromus is offline
Lead Programmer, Community Liaison
AKA: Ryan
FRC #1902 (Exploding Bacon)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Florida
Posts: 83
Oromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to beholdOromus is a splendid one to behold
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.
}
Reply With Quote
  #4   Spotlight this post!  
Unread 02-02-2015, 11:41
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by Oromus View Post
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.
}
Reply With Quote
  #5   Spotlight this post!  
Unread 02-02-2015, 11:46
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by notmattlythgoe View Post

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?


Reply With Quote
  #6   Spotlight this post!  
Unread 02-02-2015, 11:56
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,679
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by notmattlythgoe View Post
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 View Post
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.

Last edited by GeeTwo : 02-02-2015 at 11:56. Reason: fix quote tag
Reply With Quote
  #7   Spotlight this post!  
Unread 02-02-2015, 12:02
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by GeeTwo View Post
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?


Reply With Quote
  #8   Spotlight this post!  
Unread 02-02-2015, 12:06
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by Ether View Post
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.
Reply With Quote
  #9   Spotlight this post!  
Unread 02-02-2015, 12:49
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by notmattlythgoe View Post
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

Reply With Quote
  #10   Spotlight this post!  
Unread 02-02-2015, 13:05
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by Ether View Post
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.
Reply With Quote
  #11   Spotlight this post!  
Unread 02-02-2015, 13:26
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,679
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by Ether View Post
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.
Reply With Quote
  #12   Spotlight this post!  
Unread 02-02-2015, 13:37
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by GeeTwo View Post
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.



Reply With Quote
  #13   Spotlight this post!  
Unread 02-02-2015, 13:51
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,679
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
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?
Reply With Quote
  #14   Spotlight this post!  
Unread 02-02-2015, 13:59
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by GeeTwo View Post
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.




Reply With Quote
  #15   Spotlight this post!  
Unread 02-02-2015, 13:59
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,679
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Inverting Axis

Quote:
Originally Posted by GeeTwo View Post
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..
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 18:07.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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