Log in

View Full Version : Enhanced I/O through Cypress


Skyehawk
24-11-2012, 17:09
Our team is new to the Enhanced I/O through Cypress. I have done everything to spec in setting it up except one thing; when I tried to load the 2012.hex onto the module it said fail in the lower right hand corner (where it says connected) so I used the 2009.hex and it loaded fine. The DS reconized it and I have full suport on the DS of all the functions that are native.

When I loaded my code onto the robot I am using Cypress to get a button value. Here's where I need help:
Initilized:
public class RobotClass extends IterativeRobot {
Relay Feed = new Relay(2);
DriverStationEnhancedIO Cypress = DriverStation.getInstance().getEnhancedIO();
Teleop:
if (Cypress.getButton(2) ) {
Feed.setDirection(Relay.Direction.kForward);
} else if (Cypress.getButton(3)) {
Feed.setDirection(Relay.Direction.kReverse);
} else {
Feed.set(Relay.Value.kOff);

Netbeans wants me to sorround the teleop portion of code with a try - catch statement:
try {
if(Cypress.getButton(2) ) {
Feed.setDirection(Relay.Direction.kForward);
} else if (Cypress.getButton(3)) {
Feed.setDirection(Relay.Direction.kReverse);
} else {
Feed.set(Relay.Value.kOff);
}
} catch (EnhancedIOException ex) {
ex.printStackTrace();
}

Is the try - catch the correct way to do this? It doesnt seem right. If it isn't, what is the correct way to do this?
Thanks,
Skyehawk

Joe Ross
24-11-2012, 20:31
Most of the ways that EnhancedIOException gets thrown is because the enhanced IO module isn't plugged in to the driver station computer. You should think about what you want the code to do in that case and put that in the catch block. You probably want to turn the relay off, but you may want to do something else like send status back to the driver station.

If you're not familiar with exceptions, there is a java tutorial about them. http://docs.oracle.com/javase/tutorial/essential/exceptions/

Skyehawk
25-11-2012, 01:03
I read that page. Interesting stuff, though a little hard to grasp at parts. The I/O was plugged in and getting readings from the button I had wired in.

This is my new code:
try {
if (Cypress.getButton(2)) {
Feed.setDirection(Relay.Direction.kForward);
} else if(Cypress.getButton(3)) {
Feed.setDirection(Relay.Direction.kReverse);
}
} catch (EnhancedIOException ex) {
ex.printStackTrace();
Feed.set(Relay.Value.kOff);
}
I have not tried this code yet so I will update the thread once I do test it. Is this what you were getting at? Also, do I need the "ex.printStackTrace();" line, I know it helps in debuging, but will the program run more efficently without it?

joelg236
25-11-2012, 02:51
"ex.printStackTrace();" Does exactly what it says: prints a stack trace to the default console (in your case, netbeans console used to deploy). It would not add anything significant to keep that inside of the catch block. (And it would end up being helpful when debugging)

As for whether what you did will work, it completely depends on what you want your code to do when that exception is thrown, so if that is setting the Feed to Relay.Value.kOff, sure.

Something that bugged me a bit in your code is this:
DriverStationEnhancedIO Cypress = DriverStation.getInstance().getEnhancedIO();

In Java, camel case is standard (see coding conventions - http://www.oracle.com/technetwork/java/codeconv-138413.html). This is because accessing static members only requires the class name, which should start with a capital letter. When your fields are named starting with capital letters, it can be hard to distinguish between fields and classes. It's definitely worth starting a habit of camel casing.

Skyehawk
25-11-2012, 16:44
I managed to test the program. I got this in the run box:
[cRIO] edu.wpi.first.wpilibj.DriverStationEnhancedIO$Enha ncedIOException: Enhanced IO Missing
[cRIO] at edu.wpi.first.wpilibj.DriverStationEnhancedIO.getD igitals(DriverStationEnhancedIO.java:629)
[cRIO] at edu.wpi.first.wpilibj.DriverStationEnhancedIO.getD igital(DriverStationEnhancedIO.java:619)
[cRIO] at edu.wpi.first.wpilibj.templates.RobotClass.teleopP eriodic(RobotClass.java:70)
[cRIO] at edu.wpi.first.wpilibj.IterativeRobot.startCompetit ion(IterativeRobot.java:145)
[cRIO] at edu.wpi.first.wpilibj.RobotBase.startApp(RobotBase .java:156)
[cRIO] in virtual method #10 of javax.microedition.midlet.MIDlet(bci=17)
[cRIO] at javax.microedition.midlet.MIDletTunnelImpl.callSta rtApp(64)
[cRIO] at com.sun.squawk.imp.MIDletMainWrapper.main(110)
[cRIO] in virtual method #95 of com.sun.squawk.Klass(bci=25)
[cRIO] at com.sun.squawk.Isolate.run(1506)
[cRIO] at java.lang.Thread.run(231)
[cRIO] in virtual method #47 of com.sun.squawk.VMThread(bci=42)
[cRIO] in static method #3 of com.sun.squawk.VM(bci=6)
[cRIO] Robot Drive... Output not updated often enough.

This is a single loop of the teleop portion of the code. It says near the top that it can not find the Enhanced I/O. Is this what was being refered to previously? I also made a few changes to my code:
try {
if (dseio.getDigital(1)) {
feed.setDirection(Relay.Direction.kForward);
} else if (dseio.getDigital(3)) {
feed.setDirection(Relay.Direction.kReverse);
}
} catch (EnhancedIOException ex) {
feed.set(Relay.Value.kOff);
ex.printStackTrace();
}
I changed the .getButton to .getDigital based on how things are labeled on the DS.
I reimaged the cRIO (to change IP) and my relay was not responding to my command from the joystick (it worked before I reimaged it). I added it to autonomous and the relay worked. (the joystick was responding in the dionostics tab so I have no Idea what caused it to quit working, I will restore the cRIO to the IP that it had previously) Once I get the relay going again on user command I will have a definate answer on whether or not this code worked, but in the meantime feel free to critique.