Go to Post Brandon, your amazing! CD media will rock my socks off more then DS&K! :p Ok, maybe I won't go that far. - Kyle Love [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 24-01-2016, 12:43
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 217
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Running different functions at the same time?

Hi,

So we are planning on making a shooter to throw the boulders into the high goal. I need to be able to spin the shooter motor while the pneumatics are being retracted.

My thought would be to have another thread using Python's threading module and just starting a new thread for the motor but I don't know if that is the recommended way of accomplishing it. Here is an example of what I have. I need that for loop to be going while the pneumatics are being set to false.
Code:
        if self.arm1.get()==True:

            for i in range(100): #I need this to be on while the solenoids retract
                self.shooter.set(1)

            self.arm1.set(False)
            self.arm2.set(False)
        else:
            self.arm1.set(True)
            self.arm2.set(True)

            for i in range(100): #I need this to be on while the solenoids retract
                self.shooter.set(1)

            self.arm1.set(False)
            self.arm2.set(False)

Thanks!

Last edited by team-4480 : 24-01-2016 at 13:20.
Reply With Quote
  #2   Spotlight this post!  
Unread 24-01-2016, 18:08
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,043
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Running different functions at the same time?

There are a number of ways of dealing with this in python. Threads are one of them -- but I generally recommend avoiding threads in robot programming, as it's too easy to get it wrong and there are other ways of doing it that work just as well for many (but not all) problems.

The traditional way of dealing with this sort of thing is through logical constructs referred to as state machines. The idea is to execute the same piece over and over again (but not in a loop),

Code:
if self.state == 0:
    do_something()
    if condition_is_true:
        self.state = 1 # this advances to the next stage

elif self.state == 1:
    do_something_else..
Logically, this is the same as your for loop if the "condition_is_true" is checking to see if some count is 100 or not, and you incremented the count each time.

Another way of dealing with this that's unique to python is by using generators (the Yeti framework that team 4819 has does this), but it's more of an advanced topic.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #3   Spotlight this post!  
Unread 24-01-2016, 18:30
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 217
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Re: Running different functions at the same time?

Quote:
Originally Posted by virtuald View Post
The traditional way of dealing with this sort of thing is through logical constructs referred to as state machines. The idea is to execute the same piece over and over again (but not in a loop),
With this method, could I run a motor at full power and at the exact same time be able to control a piston? I feel like the motor would stop spinning while I control the piston.
Reply With Quote
  #4   Spotlight this post!  
Unread 24-01-2016, 18:37
TimTheGreat's Avatar
TimTheGreat TimTheGreat is offline
ArchdukeTim
FRC #1418 (Vae Victis)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Falls Church
Posts: 234
TimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura about
Re: Running different functions at the same time?

The good (and bad) thing about motors is if you don't tell them what to do, they'll do the last thing they were told. This means if you forget to set a motor to 0 when you're done with it it will stay turned on. You can use this to your advantage

Code:
if state == 1:
    motor.set(1)
    state = 2
if state == 2:
    do_pneumatic_stuff()
    #because the motor hasn't been set to 0, it's still set to 1
    if pneumatic_stuff_done:
        state == 3
if state == 3
    motor.set(0) #Now we set it to 0 to stop it
__________________
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.



2012 - Gracious Professionalism - Greater DC
2014 - Regional Finalist - Virginia | Industrial Design - Virginia | Regional Finalist - Greater DC
2015 - Innovation in Control - Greater DC
2016 - District Event Winner - VAHAY | Innovation in Control - VAHAY | District Event Winner - MDBET | Industrial Design - MDBET | District Champion - CHCMP | Innovation in Control - CHCMP
Reply With Quote
  #5   Spotlight this post!  
Unread 24-01-2016, 18:55
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,043
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Running different functions at the same time?

Quote:
Originally Posted by TimTheGreat View Post
The good (and bad) thing about motors is if you don't tell them what to do, they'll do the last thing they were told. This means if you forget to set a motor to 0 when you're done with it it will stay turned on. You can use this to your advantage

Code:
if state == 1:
    motor.set(1)
    state = 2
if state == 2:
    do_pneumatic_stuff()
    #because the motor hasn't been set to 0, it's still set to 1
    if pneumatic_stuff_done:
        state == 3
if state == 3
    motor.set(0) #Now we set it to 0 to stop it
While you're right that this technique works, I highly recommend against it. Instead, I would explicitly set the motor in each branch, so you can be absolutely sure that the motor is doing what you want.

But, you can do something like this:

Code:
if self.state == 1:
    self.piston.fire()
elif state == 2:
    something_else

.. 

self.motor.set(1)
As you can see, as long as the motor setting is *always* happening and isn't excluded by some if statement, then you're fine.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 06:25.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi