Issues with python and my strafe part of my teams code

This is more visual studio code, but trying to get their strafing to work in autonomous, it works in usercontrol no issues.

def slideL (distance, speed):
    strafe_left.spin_for(REVERSE, distance, DEGREES, speed, VelocityUnits.PERCENT, False)
    strafe_right.spin_for(FORWARD, distance, DEGREES, speed, VelocityUnits.PERCENT)

Above is our function for strafe:

left_drive_smart.set_velocity((controller_1.axis3.position() + controller_1.axis4.position() + controller_1.axis1.position()), PERCENT)
        left_drive_rear.set_velocity((controller_1.axis3.position() + controller_1.axis4.position() - controller_1.axis1.position()), PERCENT)
        right_drive_smart.set_velocity((controller_1.axis3.position() - controller_1.axis4.position() - controller_1.axis1.position()), PERCENT)
        right_drive_rear.set_velocity((controller_1.axis3.position() - controller_1.axis4.position() + controller_1.axis1.position()), PERCENT)
        left_drive_smart.spin(FORWARD)
        left_drive_rear.spin(FORWARD)
        right_drive_smart.spin(FORWARD)
        right_drive_rear.spin(FORWARD)

Above is our user control, Axis 4 is our strafe.

1 Like

Are you using VEXcode Pro V5? My guess is that your motor groups are set up incorrectly, or it could be the way you are running it, but it is hard to tell without the full code.

1 Like

Actually using Visual studio Code and using the VEX extention, I guess I could of put this under the PROS section, but figured its Python so it should mimic VEXCode V5, sorry about the confussion.

# ---------------------------------------------------------------------------- #
#                                                                              #
# 	Module:       main.py                                                      #
# 	Author:       KaldenPrime03                                                #
# 	Created:      Wed Aug 24 2022                                              #
# 	Description:  V5 project                                                   #
#                                                                              #
# ---------------------------------------------------------------------------- #

# Library imports
#from turtle import right
#from turtle import forward
from vex import *

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

# Robot Configuration Code
left_drive_smart = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_drive_smart = Motor(Ports.PORT10, GearSetting.RATIO_18_1, True)
left_drive_rear = Motor (Ports.PORT11, GearSetting.RATIO_18_1, False)
right_drive_rear = Motor(Ports.PORT20, GearSetting.RATIO_18_1, True)
flywheel_run = Motor(Ports.PORT4, GearSetting.RATIO_18_1, True)
convayer_belt = Motor(Ports.PORT5, GearSetting.RATIO_18_1, False)
rollerw = Motor(Ports.PORT7, GearSetting.RATIO_18_1, True)
inertial_sensor = Inertial(Ports.PORT12)
colorsense = Optical(Ports.PORT4)

# Motor groups
left_group = MotorGroup(left_drive_smart, left_drive_rear)
right_group = MotorGroup(right_drive_smart, right_drive_rear)
strafe_left = MotorGroup(left_drive_smart, right_drive_rear)
strafe_right = MotorGroup(left_drive_rear, right_drive_smart)

# drivetrain and Controller
drivetrain = SmartDrive(left_group, right_group, inertial_sensor, 319.19, 295, 40, INCHES, 1) #TrackWidth, WheelBase, Wheel Travel
controller_1 = Controller(PRIMARY)

#Functions
def driveF(distance, speed, direction):
        drivetrain.drive_for(direction, distance, INCHES, speed, VelocityUnits.PERCENT)

def turningT(angle, speed, turn):
    drivetrain.turn_for(turn, angle, DEGREES, speed)

def slideL (distance, speed):
    strafe_left.spin_for(REVERSE, distance, DEGREES, speed, VelocityUnits.PERCENT, False)
    strafe_right.spin_for(FORWARD, distance, DEGREES, speed, VelocityUnits.PERCENT)





#Competition Code
def autonomous():
    count = 0
    brain.screen.clear_screen()
    while True:
        brain.screen.print_at("Auton.... %6d" %(count), x=10, y=40)
        count = count + 1
        driveF(100, 100, FORWARD)
        slideL(20, 100)
        turningT(90, 100, RIGHT)
        wait(20, MSEC)

        

def usercontrol():
    count = 0
    brain.screen.clear_screen()
    while True:
        # controller with joy sticks
        left_drive_smart.set_velocity((controller_1.axis3.position() + controller_1.axis4.position() + controller_1.axis1.position()), PERCENT)
        left_drive_rear.set_velocity((controller_1.axis3.position() + controller_1.axis4.position() - controller_1.axis1.position()), PERCENT)
        right_drive_smart.set_velocity((controller_1.axis3.position() - controller_1.axis4.position() - controller_1.axis1.position()), PERCENT)
        right_drive_rear.set_velocity((controller_1.axis3.position() - controller_1.axis4.position() + controller_1.axis1.position()), PERCENT)
        left_drive_smart.spin(FORWARD)
        left_drive_rear.spin(FORWARD)
        right_drive_smart.spin(FORWARD)
        right_drive_rear.spin(FORWARD)

        


        #left_group.set_velocity((controller_1.axis3.position() + controller_1.axis4.position()), PERCENT)  Forward Backward, Left turn right turn
        #right_group.set_velocity((controller_1.axis3.position() - controller_1.axis4.position()), PERCENT)  
        #left_group.spin(FORWARD)
        #right_group.spin(FORWARD)


        
        # Buttons
        if controller_1.buttonA.pressing():
            rollerw.spin(FORWARD)
        elif controller_1.buttonB.pressing():
            rollerw.spin(REVERSE)
        elif controller_1.buttonL1.pressing():
            rollerw.stop(HOLD)
        
        

        brain.screen.print_at("driver... %6d" %(count), x=10, y=40)
        count = count + 1
        wait(20, MSEC)

comp = Competition(usercontrol, autonomous)
1 Like

Please do not put it under the PROS section. PROS is something completely different from VEXCode Pro V5 and the VEX VSCode Extension.

9 Likes

One issue with your code is this:
Unlike your driveF function which drives your robot in the unit of inches:

your sideL function drives your robot in terms of degrees (per motor):

As such, when you call sideL with a distance of 20:
sideLcall

your robot will hardly move. (each motor only turning 20 degrees)

To fix this you could make another function that converts inches into degrees.

I have made this mistake before. I don’t know if there is another problem with your code, but I can tell that this could be one potential issue.

3 Likes

Ok ya I was looking at that with my kids and figured that be a red flag, I tried just changing degrees to inches and got red lines galore.

Thank you for the post for the code, is that under python or C++? Just wondering if I need to re-write it or not.

Thanks again

1 Like

No problem,
And yes, changing DEGREES to INCHES would give you red lines galore :laughing: The reason for this, is because the member function spin_for only operates in terms of the motor(s) it is being called on, unlike a SmartDrive object which you supply with necessary information about your robot in order for you to be able to use the INCHES unit.

the code I put in my post is the same code you used (Python). You shouldn’t have to re-write your code completely, you may just need to add a function that converts inches to degrees… which (assuming you have an x-drive, is slightly less intuitive than writing such a function for a tank drive due to the fact that all of the wheels are rotated ±45 degrees)

2 Likes

There are probably a few existing threads discussing how to do this.

1 Like

I found the Autonomous Conversions but its in C++, not sure how to start off that kind of equation unless its the same as just writing

def DEGREES = INCHES / wheel_circumfrance * 360:

but when I do this, I have a red line under the =

Sorry still new to learning Python and where everything goes, but also still new to C++ too…

1 Like

That’s ok, when making a function in python (in general) there are a few rules:

  1. the function name can’t (shouldn’t) be taken by another variable or constant like DEGREES or INCHES
  2. each function needs a parameter list (or empty parameter list)
  3. function body (indented portion after colon)
  4. if your function is supposed to return a value, use the return keyword

Taking these ‘rules’ into consideration, here is a valid function for python:

def inches_to_degrees(inches):
    return inches / wheel_circumference * 360

wheel_circumference can be a global variable or you can include it in the parameter list, doing both at the same time would probably be the best choice because it reduces the amount of ‘magic numbers’ in your code, and your function can be reused to work with different configurations.

keep in mind your equation only works if your drivetrain is direct drive, and Not a holonomic drive.

3 Likes

Thank you, you are amazing, I really appreciate the help

1 Like

keep in mind your equation only works if your drivetrain is direct drive, and Not a holonomic drive.

Ok does that mean like mecanum wheels would be an issue then? This is kinda the application we’re putting it into thats why.

1 Like

sorry, I should have clarified, that function won’t work with a holonomic x-drive… I am not sure about mecanum.
I’m not saying your code won’t run (it should), I am just saying that the robot may not drive as far as you thought it would.

1 Like

So I got this in my code, and got it with no lines:

# drivetrain and Controller
drivetrain = SmartDrive(left_group, right_group, inertial_sensor, 319.19, 295, 40, INCHES, 1) #TrackWidth, WheelBase, Wheel Travel
controller_1 = Controller(PRIMARY)
WheeL_circumfrance = 4

#Functions
def inches_degress(INCHES):
    return INCHES / WheeL_circumfrance * 360
def driveF(distance, speed, direction):
        drivetrain.drive_for(direction, distance, INCHES, speed, VelocityUnits.PERCENT)

def turningT(angle, speed, turn):
    drivetrain.turn_for(turn, angle, DEGREES, speed)

def slideL (distance, speed):
    strafe_left.spin_for(REVERSE, distance, inches_degress, speed, VelocityUnits.PERCENT, False)
    strafe_right.spin_for(FORWARD, distance, inches_degress, speed, VelocityUnits.PERCENT)

but my inches_degress is coming up with red lines… Should it be ok?

1 Like

First of all, you have to pipe the distance argument from sideL into inches_degrees, then from that function into the spin_for member function.

Here is the Vex Command Help for spin_for

The third argument of spin_for is a unit of measuring how far the motor should spin.
The second argument of spin_for is how many of those units the motor will spin for.

Because we can’t make our own units for spin for we have to convert a unit we can’t use to a unit we can use; hence, the inches_degrees function you made.

like this:

# drivetrain and Controller
drivetrain = SmartDrive(left_group, right_group, inertial_sensor, 319.19, 295, 40, INCHES, 1) #TrackWidth, WheelBase, Wheel Travel
controller_1 = Controller(PRIMARY)
WheeL_circumfrance = 4

#Functions
def inches_degress(inches):
    return inches/ WheeL_circumfrance * 360

def driveF(distance, speed, direction):
    drivetrain.drive_for(direction, distance, INCHES, speed, VelocityUnits.PERCENT)

def turningT(angle, speed, turn):
    drivetrain.turn_for(turn, angle, DEGREES, speed)

def slideL (distance, speed):
    converted_speed = inches_degrees(distance)
    strafe_left.spin_for(REVERSE, converted_speed , DEGREES, speed, VelocityUnits.PERCENT, False)
    strafe_right.spin_for(FORWARD, converted_speed , DEGREES, speed, VelocityUnits.PERCENT)

Second of all, the reason inches_degrees is coming up with errors is because spin_for is expecting you to give it either DEGREES or INCHES (see the Vex Command Help), but you gave it a function instead.

3 Likes