C++ Relay Code?

Hey!

I seem to have a constant stream of programming problems, but here’s my problem.

I can get a relay to work in a basic code (two drive motors, left and right, one joystick plus a single relay.) However whenever I try to move the code over to my FULL bot code (with many motors and two Joysticks) the code just fails.

What I’m using is the following (only parts of it)


#include <WPILib.h>

class RobotDemo public IterativeRobot
{
Relay *hRelay;
Joystick *stickDrive;

public:
RobotDemo()
{
     hRelay = new Relay(1);
     stickDrive = new Joystick(1);
}
void TeleopPeriodic()
{
     if(stickDrive->GetTrigger())
            hRelay->Set(Relay::kForward);
     else
            hRelay->Set(Relay::kOff);
}
};
START_ROBOT_CLASS(RobotDemo);

This is the parts of the code that included any Relay related stuff. If that runs alone (or almost alone) it works, if that runs in the full program, it doesn’t. Any suggestions?

EDIT : Perhaps I should rephrase. What could possibly be in my code to cause this particular part of the code not to work?

Your logic seems good, but here’s a question…Are you trying to have the relay fire in teleop or autonomous?

If you’re trying to do teleop, your logic needs to be in the TeleopPeriodic function.

If you’re trying to do autonomous, you have to keep in mind that the inputs are zero’d out during autonomous and plan your logic around that. As is, the button will always be false and therefore the relay will always be in the off state.

My bad, I wrote the wrong function down when copying it to the program. That was NOT the problem.

Did you make sure that there isn’t something else instantiated as Relay(1)? If there’s something somewhere else overwriting your value you could never see your output.

I would suggest adding printfs to make sure that the code is being reached like you think it is.

This type of problem is usually something silly that is overlooked, most likely in the place that you least expect.

You said that it worked in a smaller test bed. I’m assuming it was with the same physical hardware setup. It’s worth checking :slight_smile:

Nope, there is no other Relay present, I manually cleared out every reference to relays in the code then manually re-entered them. I’m going to scan over the code a few more times tonight, if I find anything I’ll let you know, I may make changes and test them tomorrow morning.

I will echo what Dave said: Put in printf’s to make sure that your code goes where you think it should. This would reveal if you were using the wrong joystick port, for example.

Ok, I will have another 5 hours or so with the Robot tomorrow, so I’ll try getting it up and running then. I actually have a feeling that its a problem with my relay declaration, the reason I’m thinking this is because when I move the line

hRelay->Set(Relay::kForward);

to the TeleopPeriodic() function (essentially eliminating the need to press the trigger) the relay still remains inactive (however it is initialized I believe, the light is not flashing but solid orangeyred.)

What could cause this? Is there a limit on the number of variables etc. that can be declared in the robot? Anyway, once I’m back with the robot tomorrow I’ll comment out mostly all of the code until it gets working, then slowly re-intigrate aspects of the code until it stops working and troubleshoot from there. If the schools internet is up I’ll keep this updated, if its not I’ll post an update tomorrow night. I need this working by Friday night so I really hope this can be resolved quickly

FYI relays are solid orange whether or not they have a signal so that’s not really a good indication. It just means you’re getting power.

Your approach seems like a good plan about backing things out and reintegrating. Maybe before you do that, throw some prints into the periodic function to make sure it’s being called properly. If it isn’t then you may be chasing a ghost.

Yeah, I realize that, I’ll try to figure out how to use printf’s in robot code. I never have before because usually my code works (or comes very close to working) and I’ve never had a need.

Problem solved.

After about an hour of work I got desperate and started looking in the most obscure places. I then realized what was wrong. We have two projects on the laptop, both nearly identical, they both actually do the exact same thing for the robot. The only difference is that one is uploaded and the other is not. I was editing/building the one that IS NOT uploading, and was constantly re-loading the one that was never changed. My initial code (see post 1) worked when put into the RIGHT project.

Time to have some fun and add in a custom dashboard!

Thanks everyone for the help!
Nick

Glad to hear that it worked. Your comments about the dashboard reminded me that I wanted to publish our dashboard framework. It sat on top of the WPILib interface and made things really easy to interface with. I need to clean it up a bit to make it less intimidating to read (we had over 100 pieces of data that we were sending to the dashboard). Look for something in the next day or so.