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.


        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!

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),


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.

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.

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

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:


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.