|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: How is this looking?
Quote:
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);
}
}
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 Last edited by ItzWarty : 13-02-2012 at 02:52. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|