Pressed command Python

Hi there,

So my teams are trying to figure out how to toggle their punchers / flywheels / other mechanisms so they dont have to hold the button down during driver control and help their team mates out during match loads.

Heres a code we’re trying to figure out, I thought I got the function correct but I am guessing not cause its not working for us:

# ---------------------------------------------------------------------------- #
#                                                                              #
# 	Module:       main.py                                                      #
# 	Author:       Kamalei10                                                    #
# 	Created:      Thu Aug 31 2023                                              #
# 	Description:  VEX Competition Template WIS                                 #
#                                                                              #
# ---------------------------------------------------------------------------- #

# Library imports
from vex import *

# Brain should be defined by default
brain=Brain()

# Robot Configuration Code

Lef = Motor(Ports.PORT20,GearSetting.RATIO_18_1, False)
Leb = Motor(Ports.PORT2,GearSetting.RATIO_18_1, False)
Rif = Motor(Ports.PORT11,GearSetting.RATIO_18_1, True)
Rib = Motor(Ports.PORT1,GearSetting.RATIO_18_1, True)
PnCh = Motor(Ports.PORT3,GearSetting.RATIO_36_1)
SeA = Inertial(Ports.PORT4)

# Motor groups

LSide = MotorGroup(Lef, Leb)
RSide = MotorGroup(Rif, Rib)


# drivetrain and Controller

Liam = SmartDrive(Lef, Rif, SeA, 300, 320, 320, MM)
Sear = Controller(PRIMARY)

#Functions

def DriveF(direction, distance, speed):
    Liam.set_drive_velocity(speed, PERCENT)
    Liam.drive_for(direction, distance, INCHES)

def TurningT(angle, speed):
    Liam.set_turn_velocity(speed, PERCENT)
    Liam.turn_to_heading(angle,DEGREES,speed)

def PunchG(direction, time, speed):
    PnCh.set_velocity(speed)
    PnCh.spin(direction)
    wait(time, SECONDS)
    PnCh.stop(COAST)

def DripuN():
    PnCh.set_velocity(100)
    PnCh.spin(FORWARD)
    while not not Sear.buttonUp.pressing():
        wait(5, MSEC)
    PnCh.stop()


#Competition Code
def pre_autonomous():
    while True:
        SeA.calibrate()

def autonomous():
    while True:
        PunchG(FORWARD, 30, 100)

        wait(60000)
def usercontrol():
    Sear.buttonUp.pressed(DripuN)
    while True:

        # controller with joy sticks
     
        LSide.set_velocity((Sear.axis3.position() + Sear.axis4.position()), PERCENT)
        RSide.set_velocity((Sear.axis3.position() - Sear.axis4.position()), PERCENT)
        LSide.spin(FORWARD)
        RSide.spin(FORWARD)
        
        #Buttons
    
        if Sear.buttonDown.pressing():
            PnCh.set_velocity(85, PERCENT)
            PnCh.spin(FORWARD)
        elif Sear.buttonB.pressing():
            PnCh.set_velocity(85, PERCENT)
            PnCh.spin(REVERSE)
        else:
            PnCh.stop(HOLD)

comp = Competition(usercontrol, autonomous) #do not touch this

So DripuN is the function my student made from pulling it from vex code blocks to python translate. We also have the pressed command before the while true. Also did it after the while true and the brain gave me an error.

Any ideas? Sorry for dumb questions

To the top if any one can chime in, that be greatly appreciated.

Currently the code builds and uploads but there is no action coming from the controller when we push buttonUp

It’s because you are trying to control the motor in more than one place.

        if Sear.buttonDown.pressing():
            PnCh.set_velocity(85, PERCENT)
            PnCh.spin(FORWARD)
        elif Sear.buttonB.pressing():
            PnCh.set_velocity(85, PERCENT)
            PnCh.spin(REVERSE)
        else:
            PnCh.stop(HO

this code is sending stop to the motor when other buttons are not being used. best not to mix using pressing() in a loop with pressed() events.

1 Like

looks like all they need is this.

        if Sear.buttonDown.pressing():
            PnCh.spin(FORWARD, 85, PERCENT)
        elif Sear.buttonB.pressing():
            PnCh.spin(REVERSE, 85, PERCENT)
        elif Sear.buttonUp.pressing():
            PnCh.spin(FORWARD, 100, PERCENT)
        else:
            PnCh.stop(HOLD)

remove the call to buttonUp.pressed

(and no need to use set_velocity, just combine it all into one line of code,)

1 Like

Ahh ok, didnt think that would be the issue, I’ll have my kids has tag their pressing commands out and try again.

Thank you again Jpear, sorry you gotta keep troubleshooting my mess ups :-p

So will this make it so the motors will continue to run if the button is released? This is more so for one of my teams who is a single person now, the rest of her team ditched and she needs to match load by herself.

no, but nor did this.

def DripuN():
    PnCh.set_velocity(100)
    PnCh.spin(FORWARD)
    while not not Sear.buttonUp.pressing():
        wait(5, MSEC)
    PnCh.stop()

due to the double negative “not not”

if that’s the intent, I would move all the PnCh control into its own thread as the logic will get more complicated. perhaps start by writing out what the motor should do for each button state as you have three buttons controlling the single motor in different ways.

1 Like

Sorry the not not was a translation from blocks to python, just was trying to see if that would of worked.

We took out the 2nd not, and she just wants to use one button to activate her puncher, we removed the if else statement from the while loop, also removed the set velocity, put it into the spin command, the command still doesnt work unfortunately.

We’ll see what else we can do, sorry to bug you. Thank u.

explain clearly what the desired behavior is.

push buttonUp and the PnCh motor start to spin continuously, is that what you want ?

how should the PnCh motor be stopped ?

1 Like

Sorry, here we go

She wants to start the motor with one button,
Forward at full power,
let it run while not holding it,
and stopping it either on the same button or a different button.

This way she can match load during skills driver control by herself since she has no other team mates to help her.

ok, then all you need is something like this.

def PnChStart():
    PnCh.spin(FORWARD, 100, PERCENT)

def PnChStop():
    PnCh.stop()

Sear.buttonUp.pressed(PnChStart)
Sear.buttonDown.pressed(PnChStop)

def usercontrol():
    while True:
        # controller with joy sticks
        LSide.set_velocity((Sear.axis3.position() + Sear.axis4.position()), PERCENT)
        RSide.set_velocity((Sear.axis3.position() - Sear.axis4.position()), PERCENT)
        LSide.spin(FORWARD)
        RSide.spin(FORWARD)
        
comp = Competition(usercontrol, autonomous) #do not touch this

start the motor with buttonUp, stop the motor with buttonDown

1 Like

Bruh seriously? Its that simple? :expressionless: Thank you now I feel even more dumb.

If you just want to toggle PnCh on/off with a single button, one approach might be something like this:

def TogglePnCh():
   if PnCh.isSpinning():
      PnCh.stop()
   else:
      PnCh.spin(FORWARD, 85, PERCENT)
      

and

Sear.buttonUp.pressed(TogglePnCh)

Then you can remove all that unnecessary button press logic in the usercontrol() function’s while loop.

Sidenote: I’d suggest a wait(100) or something in that forever while loop. A controller loop without a wait() feels too tight to me. However, I’m sure @jpearman can speak to the sensibility of a wait() in this case; I suppose the registered event handlers are fired on a separate thread so won’t care that there’s a tight while loop running on the “main” thread. Nevertheless, it feels resource intensive to be updating velocity faster than human visual reaction time (on the order of 200 ms).

1 Like

unfortunately the is_spining function doesn’t work like that, it means that a spin_for or spin_to is still being executed.

yes, adding sleep(20) in the while loop would be reasonable., however, Python handles scheduler switching a little differently from C++ in that we automatically yield to the scheduler each time around a loop in addition to the forced yield after 2mS that’s used in C++.

2 Likes