Quote:
Originally Posted by joelg236
Thanks for the feedback guys
I just have one question about the whole object oriented stuff. Honestly, I just havnt had to time to even start understanding how java really works. I'm mostly basing my code on what I know already from other languages.
My question is, how would I use separate classes and still pull variables from all of them? Or is that question invalid (public modifier)?
I would also love to hava a constantly updating print function that shows speed, turning, gyro, encoders, etc. But to do that, I think that I would need to either Thread.sleep() every time to keep it from flashing, or find a way to clear the console (which might be a bad idea for debugging other things).
Newbie question, I know, but how do you actually call classes from the main class, and how do I actually run methods from those? In FRC, is it just public static void main(String[] args) to make a main method in separate classes?
All help is appreciated,
Thanks 
|
Here's a simple example of code that I've written for this year's game.
The code obviously isn't a complex part of our program, but it will at least serve as an example of how object oriented programming can make your life really nice.
Note that our method names are UpperCamelCase as opposed to the usual lowerCamelCase. I come from a C# background.
Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.*;
/**
* The LED Rings class manages the state of the LED Rings which we use to light
* up the vision targets of the robot. As has been seen through our real life
* tests, the LED Rings do indeed cause a dramatic lighting change for our target.
*
* The lighted up targets are then detected by image processing.
* @author ItzWarty
*/
public class LEDRingsManager
{
//--------------------------------------------------------------------------
//
// Fields/Properties
//
//--------------------------------------------------------------------------
//Dio 1 - white, Dio 2 - green. Directions are forwards or off
private static Relay m_whiteRelay, m_greenRelay;
private static boolean m_whiteRelayEnabled, m_greenRelayEnabled;
//--------------------------------------------------------------------------
//
// Methods which are invokable by other classes
//
//--------------------------------------------------------------------------
/**
* Initializes the relays which we will use to enable/disable the LED rings
* located at the goal-tracking camera of our robot.
*/
public static void Initialize()
{
m_whiteRelay = RelayManager.GetInstance(2);
m_whiteRelayEnabled = false;
m_greenRelay = RelayManager.GetInstance(1);
m_greenRelayEnabled = false;
}
/**
* Toggles the state of the white LED relay.
*
* If the LED is on, it will be set to off.
* If the LED is off, it will be set to on.
*/
public static void ToggleWhiteLed()
{
SetWhiteLedRingEnabled(!m_whiteRelayEnabled);
}
/**
* Sets whether or not the white LED Ring is on or off
*/
public static void SetWhiteLedRingEnabled(boolean enable)
{
SetLedRingEnabled(m_whiteRelay, m_whiteRelayEnabled = enable);
}
/**
* Gets whether or not the white LED Ring is on or off
*/
public static boolean GetWhiteLedRingEnabled()
{
return m_whiteRelayEnabled;
}
/**
* Toggles the state of the green LED relay
*
* If the LED is on, it will be set to off.
* If the LED is off, it will be set to on.
*/
public static void ToggleGreenLed()
{
SetGreenLedRingEnabled(!m_greenRelayEnabled);
}
/**
* Sets whether or not the green LED Ring is on or off
*/
public static void SetGreenLedRingEnabled(boolean enable)
{
SetLedRingEnabled(m_greenRelay, m_greenRelayEnabled = enable);
}
/**
* Gets whether or not the green LED Ring is on or off
*/
public static boolean GetGreenLedRingEnabled()
{
return m_greenRelayEnabled;
}
/**
* Turns off all LEDs
*/
public static void TurnOffAllLeds()
{
SetWhiteLedRingEnabled(false);
SetGreenLedRingEnabled(false);
}
/**
* Turns on all LEDs
*/
public static void TurnOnAllLeds()
{
SetWhiteLedRingEnabled(true);
SetGreenLedRingEnabled(true);
}
//--------------------------------------------------------------------------
//
// Helper Methods
//
//--------------------------------------------------------------------------
/**
* Sets the state of the given LED Ring relay
* It should be note that this does not set the m_whiteRelayEnabled/m_greenRelayEnabled states
*/
private static void SetLedRingEnabled(Relay relay, boolean enable)
{
if(enable)
relay.set(Relay.Value.kForward);
else
relay.set(Relay.Value.kOff);
}
}
Basically, you'll notice that there are various "getters" and "setters", which do nothing but get or set. When you call get, the state of the robot doesn't change. This makes the code maintainable, as our methods are not "black boxes" - they have a contract, and they meet that contract.
Edit: I should note that RelayManager is one of our own classes; WPILibJ does not have a RelayManager class.
----
Printing stuff through System.out.println should be fine as long as you aren't doing anything completely excessive. Clearing the console will do nothing except free memory on the controlling interface (ex: netbeans). Netconsole is a pretty simple protocol - you send text and the guy on the other side gets that raw text.
----
In our example:
RobotMain calls Initialize on the LED Rings manager: LEDRingsManager.Initialize();
RobotMain's teleop and autonomous methods do so too: SetWhiteLedRingEnabled(true);
----
Best wishes,
ItzWarty
edit: I did end up renaming some methods (the *AllLeds methods) to enable/disable methods for consistency