FIRST Robotics Competition Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

When a button is pressed move motor for x milliseconds?

Solved!
Go to solution

Hello! I hope we can get some help.

 

We've been able to program our joystick to move independent motors (using http://wpilib.screenstepslive.com/s/currentCS/m/labview/l/696735-how-to-use-joystick-buttons-to-cont... guidance), but now we need to make it so that when  button X is pressed it moves a motor for a few seconds and then stops, and when button Y is pressed it moves the motor the opposite way for a few seconds and then stops.

 

To put it another way in case I haven't been clear, instead of the motor stopping when the button is no longer pressed, we want it to move for a predetermined amount of time and then stop.

 

Really hope I'm making sense!

Thank you!

0 Kudos
Message 1 of 7
(7,147 Views)

Hello schmit_phs,

 

I would suggest you try programming a state machine for this behavior. I'm attaching a simple VI explaining this concept in the FRC concept (where the auton and teleop VIs are enclosed in an outside while loop).

 

More resources to check out:

 

The best of luck at the competition!

0 Kudos
Message 2 of 7
(7,138 Views)

Hey schmit_phs,

 

The code will be the same as when you're driving from a Joystick except you'll have a static value for your motors.

 

The best way we can think about this is like running code very similar to our autonomous but in the teleop and instead of triggered by a sensor it is triggered by a button push.

 

Here's a quick refresher on the autonomous code. Referencing that I'd use a case structure to decide if we need to the "turn left for 1 second code" etc.

 

[FRC 2017] Autonomous Timed Movement Tutorial -
https://forums.ni.com/t5/FIRST-Robotics-Competition/FRC-2017-Autonomous-Timed-Movement-Tutorial/ta-p...

 

Hope this helps!

JY
Application Engineer, RF and Communications
National Instruments
0 Kudos
Message 3 of 7
(7,137 Views)

The answers you're getting show that there's a bit of choice here.

 

It'd really depend on what project template you're starting from:

 

Command and Control - 

Create an action called "Drive for Time" (believe this is created for you in the template).  With a button press, call that action with the time desired inserted as an input.

 

Arcade Drive - 

Look at this: https://wpilib.screenstepslive.com/s/currentCS/m/labview/l/696733-making-a-one-shot-button-press  Create that to read the button input (so it only reads once).

Use the Elaped Time Express VI: https://www.ni.com/docs/en-US/bundle/labview-api-ref/page/vi-lib/express/express-execution-control/e... Reset it when you press the button.  Feed the "Time has Elapsed?" value out and into a Select.  If True, use your normal motor inputs.  If False, use the set value.  That way, it'll use the value you care about for the duration of the Elapsed Time configuration until time has elapsed.  Once it has, it'll go back to reading whatever comes from the joysticks.

 

Please, please, please... do NOT replicate Autonomous code.  This will halt your teleop code until your loops have finished.  That's a bad idea as it means you lose control of your robot until this is finished.

0 Kudos
Message 4 of 7
(7,131 Views)
Solution
Accepted by topic author robocoachpaul

@robocoachpaul wrote:

Hello! I hope we can get some help.

 

We've been able to program our joystick to move independent motors (using http://wpilib.screenstepslive.com/s/currentCS/m/labview/l/696735-how-to-use-joystick-buttons-to-cont... guidance), but now we need to make it so that when  button X is pressed it moves a motor for a few seconds and then stops, and when button Y is pressed it moves the motor the opposite way for a few seconds and then stops.

 

To put it another way in case I haven't been clear, instead of the motor stopping when the button is no longer pressed, we want it to move for a predetermined amount of time and then stop.

 

Really hope I'm making sense!

Thank you!


So here is a possible solution to your request (Code attached).

I coded this up to show that it is not a trivial solution to what may seem like a simple problem as there is quite a few things to think about.

 

There are 4 inputs to monitor

5 data items that need to be recalled for each iteration (previous button states, Start time, motor value and Am i waiting for the time state)

There is also many logic decisions. (ignore button presses if timing, when do I start timer, when is timer complete, What motor value do I set....)

 

As you can see trying to code without state machines can get complicated and confusing quickly.

By using a state machine recommended by the other poster it helps break down all of these issues into smaller problems to solve. 

I will post a possible state machine solution at a later time so you can see the difference.

Motor timer 3 of 3.png

 

 

Motor timer 1 of 3.pngMotor timer 2 of 3.png

Champion CLA

Making senseless computers do
intelligent real world things
is NOT easy. SO MAKE IT FUN!
Message 5 of 7
(7,102 Views)

Adding on to what Mark gave you here, I'd strongly encourage taking a look at: https://forums.ni.com/t5/FIRST-Robotics-Competition/Command-and-Control-Tutorial/ta-p/3534946

 

The one line "build a command" bit becomes easier to work through once you spend an evening or two understanding the tutorial.  Then, you can avoid all of the various thoughts required to get Mark's example working for you.

 

Trying to work in a state machine that doesn't break the FRC architecture, keeps your code readable, and does what you want is likely more work than understanding this tutorial.  Keep in mind, similar to the advice against autonomous copy/paste, you'll need to ensure this state machine doesn't hold up your Teleop code at all.  Otherwise, you get into that position where your robot is no longer responsive while the command finishes.  Don't do that to yourself.

0 Kudos
Message 6 of 7
(7,097 Views)

MarkBalla, I cannot say thank you enough! It works perfectly. We're amazed--there's no way we would have figured this out on our own. 

 

Thank you so, so much for taking your time to write that code with such detailed annotations. Not only does it work, but we're fairly certain that we actually UNDERSTAND how it's working!

0 Kudos
Message 7 of 7
(7,091 Views)