I’ve looked at a lot of topics but haven’t found a clear answer. How do you move 2 motors at the same time in python?
they both use
leftMotor.spin(FORWARD, controller.axis3.position(), PERCENT)
rightMotor.spin(FORWARD, controller.axis2.position(), PERCENT)
already, but I don’t understand how to make them move at the same time autonomously then back to manual.
you have to use multithreading. it is a process in code where you start two threads like in vex forums and then they operate at the same time
If this is for something like a drivetrain, I would have a while loop constantly in the op control to move the motors. Then, in Auton, you would run different code to move the motors set distances.
I don’t quite understand what you mean when you say “make them move at the same time autonomously then back to manual”. Could you expand on what this means further?
Hope this helps
The spin method is non-waiting, meaning other commands can run too. I don’t know why it’s not working.
The OP’s code incorporates ‘controller.axis3.position()’, meaning that the code is meant for driver control. But it’s not gonna work like the OP probably expected…
Research the Competition Template.
To make the 2 motors spin at the same time during autonomous, you would use motor.spinFor();
leftDrive.spinFor(forward, 10, degrees);
rightDrive.spinFor(forward, 10, degrees);
however, this alone would make the left motor spin 10 degrees, then after it finishes, the right motor would spin 10 degrees. To make them run at the same time, add , false after the degrees of the first motor only
leftDrive.spinFor(forward, 10, degrees, false);
rightDrive.spinFor(forward, 10, degrees);
if you wanted to control 3 motors, you would put false after each one except the last like this:
motor1.spinFor(forward, 10, degrees, false);
motor2.spinFor(forward, 10, degrees, false);
motor3.spinFor(forward, 10, degrees);
This would make all motors run at the same time for their set rotation in degrees.
Another helpful thing my team uses is code that calculates exactly how many degrees to rotate for inches, here is what you need for that:
variables to add (set replace the 4 in wheelDiameter with your wheel diameters):
double wheelDiameter = 4;
double oneInch = (360)/(wheelDiameter*M_PI);
How to use with .spinFor
motor1.spinFor(forward, oneInch*[INCHES TO MOVE], degrees, false);
motor2.spinFor(forward, oneInch*[INCHES TO MOVE], degrees, false);
motor3.spinFor(forward, oneInch*[INCHES TO MOVE], degrees);
Replace “[INCHES TO MOVE]” with how many inches to move if you want to move one tile, do 24
I hope this helps!
So what I want the robot to do is, when it sees red, the auton takes over, grabs the ball, lifts it up, and drives back. The problem is that I have the code in a while loop(the LM, RM, AM, and CM), so every time it sees red, even when I don’t want it to, it lifts the arm and moves around. Does that make sense?
Can you send the code you have? I have done turning to objects before, I should be able to help!
This is my code right now, it works, but it’s wonky
while True:
#Create Drivetrain
leftMotor.spin(FORWARD, controller.axis3.position(), PERCENT)
rightMotor.spin(FORWARD, controller.axis2.position(), PERCENT)
#If/Elif/Else for the Arm
if controller.buttonL1.pressing() and armMotor.position(DEGREES) < 350:
armMotor.spin(FORWARD, 40, PERCENT)
elif controller.buttonL2.pressing():
armMotor.spin(REVERSE, 40, PERCENT)
else:
armMotor.stop(HOLD)
#If/Elif/Else for the Claw
if controller.buttonR1.pressing():
clawMotor.spin(FORWARD, 30, PERCENT)
elif controller.buttonR2.pressing():
clawMotor.spin(REVERSE, 30, PERCENT)
else:
clawMotor.stop(HOLD)
wait(5,MSEC)
#Optical eye Code
if eyeBall.color() = Color(RED):
clawMotor.spin(FORWARD)
armMotor.set_timeout(2, SECONDS)
armMotor.set_position(300, DEGREES)
My code wasn’t saved, so it’s for the most part right, I think. Now, the next step I want is for the robot to go forward by itself(left and right motor at the same time), and THEN pick up the ball and lift it up(when it sees red). So, would I need to get object distance and drive that distance?
the problem is that because it sees red while it is in the claw, it thinks it needs to go to 300, degrees all the time
The way I would solve this is to code the program to only trigger when the eyeball detects a new red. You store a boolean representing whether a red was detected last loop. If a red is detected and no red was detected last loop, then the red has just been detected. This means the robot will only detect a red again once the current object has been dropped.
something like this
#Optical eye Code
if eyeBall.color() = Color(RED): #detect a red
if (redLast = false): #trigger if its a new red
clawMotor.spin(FORWARD)
armMotor.set_timeout(2, SECONDS)
armMotor.set_position(300, DEGREES)
redLast = true #store red detected for next loop
else:
redLast = false #store no red detected for next loop
Hope this helps
It shows an error for some reason. It says, “cannot assign to function call here. Maybe you meant ‘==’ instead of ‘=’? (, line 67) (67,9)” It then highlights the eyeBall. Any suggestions on how to fix it? when I add another = sign it then tells me all of the = signs need 2
I’m not a Python programmer, so the code above was simply to give an idea of how the logic would work. Do you understand the logic in the broken code enough to replicate a functional version?
I can help explain the logic as but you need to contact someone else for Python help. There are always non-Vex Python forums that can help.
Okay, thank you for your help