Please help Program Relays in WindRiver....

Hello there, i need some help trying to program relays using windriver. Let’s just say i want to program a relay plugged into relay 1. When its forward, the motor turns one way, when its reversed the motor goes the other way. I want this to be triggered by a joystick buttons. Forward is button 3 and reverse is button 4. This will be for the “leftStick” Here is the code i have, can anyone help? Please, im somewhat confused on how to program relays.

BuiltinDefaultCode.cpp (3.82 KB)


BuiltinDefaultCode.cpp (3.82 KB)

Are you sure you attached the proper file? I don’t see anything in there mentioning the joystick buttons or relay control.

Anyway, you have not described your goal completely. Do you want the motor to run only while a button is pressed? What do you want to happen when both buttons are pressed? Do you want the button to choose the direction and start the motor, but have the motor to continue to run after you release the button, and if so, do you ever want to be able to stop the motor and how do you want to do it? What do you want the motor to be doing when the robot is enabled at the beginning of the match?

The answers to these questions will influence the appropriate program structure for implementing what you want. It will probably be as simple as a pair of if statements, or an if/elseif/else.

ok, let’s just say, if you push button 2, that will turn the motor on relay 1 on forward direction. when they push it again, it will turn it off.

also let’s say button 3 is pushed, when they hold it down, it moves the motor relay 2 in the forward direction. otherwise, it doesnt move at all.

How would i do these two things? Please help, thank you.

Also, that fiel i attached is the current file i have for what ive worked on so far. please feel free to add to it or just post what i can add to make the two thing above work. Thank You again. I know for a button its
if (leftStick->GetRawButton(1)) { , but thats all i know.

ok, let’s just say, if you push button 2, that will turn the motor on relay 1 on forward direction. when they push it again, it will turn it off.

This is a “push-on, push-off” or “toggle” function. Our team uses LabVIEW, so I can’t be certain I’m giving you working code, but the structure should be correct.


// Define and initialize a couple of variables
    static char current_state = 0; // what the button was last time we looked
    static char motor1state = 0; // what the last thing we told the motor to do was
    :
    :
    // Read the button
    current_state = leftStick->GetRawButton(2);
    // Did the button just get pushed?
    if (current_state != previous_state) {
      // Yes, the button wasn't pushed before, but it is now
      if (current_state == 1) {
        // Is the motor already on?
        if (motor1state == 0} {
          // Yes, the motor is on. Turn it off.
          motor1state = 1;
          motor1Relay->Set(Relay::kForward);
        } else {
          // No, the motor is off.  Turn it on.
          motor1state = 0;
          motor1Relay->Set(Relay::kOff);
        }
    }
    // remember the button so you don't do this more than once each press
    previous_state = current_state;

You can get fancy with using the kForward and kOff enumerations in the motor state variable if you like, so the code becomes a little more self-documenting, but that’s not important right now.

also let’s say button 3 is pushed, when they hold it down, it moves the motor relay 2 in the forward direction. otherwise, it doesnt move at all.

This is easy, as it requires no “memory” of past events.


    if (left_stick->GetRawButton(3) == 1) {
      motor2Relay->Set(Relay::kForward);
    } else {
      motor2Relay->Set(Relay::kOff);
    }

I have used more braces than necessary, and my formatting convention might not match what you want to use. This should give you a good start on what you need to do, though.

hello, i seem to get a bunch of error messages about expected ; or { or } after or before the ifs and else’s… for the toggle on and off code… any suggestions?

I have three suggestions. First, notice that I forgot to declare the previous_state variable in my example. (it should be just like the current_state one).

Second, notice that I accidentally used a close brace } instead of a close parenthesis ) character after the test expression on the third if.

Third, just use what I posted as an example of how to do it, not as a complete copy-and-paste solution to your specific problem. Read it, understand it, and then write your own code.

ok thank you very much… good luck this season