I am trying to have my two groups of motors run in reverse in a function but it is stuttering back and forth rapidly. However, the forward motion of the motors works very well. Can anyone look over my code and let me know what may be causing the problem and perhaps how I could fix it?
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// Controller1 controller
// LeftMotors motor_group 1, 2
// RightMotors motor_group 9, 10
// BucketArmMotor motor_group 3, 8
// ClawPivotMotor motor 6
// ClawMotor motor 7
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
#include "robot-config.h"
using namespace vex;
bool forwardDrive = true;
// Control drivetrain and drivetrain reversal
void Drivetrain(){
//Declare all variables used in the function here
int speed = 100;
float leftPower = (100 + ((100.0/127)*Controller1.Axis4.value()));
float rightPower = (100 - ((100.0/127)*Controller1.Axis4.value()));
if(Controller1.Axis4.value() == 0){
LeftMotors.setVelocity(speed, percent);
RightMotors.setVelocity(speed, percent);
}
// Increase increase low speed mode factor when R1 pressed
if(Controller1.ButtonR1.pressing()){speed = speed/3;}
else{speed = 100;} // Reset low speed mode factor when R1 is released
if(Controller1.ButtonL2.pressing()){ // Set robot to drive forward
LeftMotors.spin(forward);
RightMotors.spin(forward);
} else if (Controller1.ButtonR2.pressing()) { // Set robot to drive backward
LeftMotors.spin(reverse);
RightMotors.spin(reverse);
} else { // Set robot to to stop
LeftMotors.stop();
RightMotors.stop();
}
// Controll the direction of the robot
if(Controller1.ButtonR2.pressing() || Controller1.ButtonL2.pressing()){
if(Controller1.Axis4.value()>0){ // Set right group of wheels to slow down by the factor of the input
RightMotors.setVelocity(rightPower*(speed/100), percent);
} else if(Controller1.Axis4.value()<0){ // Set left group of wheels to slow down by the factor of the input
LeftMotors.setVelocity(leftPower*(speed/100), percent);
}
} else{
if(Controller1.Axis4.value()>0){ // Set right group of wheels to reverse and left to spin forward
RightMotors.spin(reverse);
LeftMotors.spin(forward);
} else if(Controller1.Axis4.value()<0){ // Set left group of wheels to reverse and right to spin forward
RightMotors.spin(forward);
LeftMotors.spin(reverse);
}
}
}
// Open and close the claw depending on the position of it
bool ControlClaw(bool open) {
// Set the Claw Arm Pivot to hold its position when it is stationary
ClawPivotMotor.setStopping(brakeType::hold);
// Adjust the
if(open == false){
ClawMotor.spinFor(reverse, 0.48,turns);
open = true;
}else if(open == true){
ClawMotor.spinFor(forward, 0.48,turns);
open = false;
}
return open;
}
void ClawArmMotion(){
//Set the claw arm to hold its position when it is not given commands
ClawPivotMotor.setStopping(brakeType::hold);
if(Controller1.ButtonUp.pressing()){
ClawPivotMotor.spin(forward);
} else if(Controller1.ButtonDown.pressing()){
ClawPivotMotor.spin(reverse);
} else {
ClawPivotMotor.stop();
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// All varibles for main function initialised bellow
bool clawOpen = true;
// Set all velocities for motors used in the main function
ClawMotor.setVelocity(40, percent);
ClawPivotMotor.setVelocity(60, percent);
// Infinitly loop processies bellow to operate robot
while(true){
// Call function to accelerate and reverse the robot
Drivetrain();
// Set the bucket arms to be controlled by the left joystick
BucketArmMotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);
// Set the Bucket Arm to hold its position when it is not given commands
BucketArmMotor.setStopping(brakeType::hold);
// Call function to collect ring when Button Y is pressed of the contoller
if(Controller1.ButtonY.pressing()){clawOpen = ControlClaw(clawOpen);}
// Call function to control motion of the claw arm
ClawArmMotion();
}
}