|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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. |
|
#2
|
||||
|
||||
|
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..
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. |
|
#3
|
||||
|
||||
|
Re: Running different functions at the same time?
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.
|
|
#4
|
||||
|
||||
|
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
|
|
#5
|
||||
|
||||
|
Re: Running different functions at the same time?
Quote:
But, you can do something like this: Code:
if self.state == 1:
self.piston.fire()
elif state == 2:
something_else
..
self.motor.set(1)
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|