Hey everyone,
I’ve been trying to code a PID autonomous with the aid of a youtube video, made by @Connor. The code on there is meant for a four motor drive, when my team’s is a six motor. I attempted to edit it myself, but all it does is spin around infinitely. (Actually I’m not sure I haven’t had infinite time to test it out) Here is the code:
//Auton PID Program
//Based from Connor 1814D's PID Program
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port]
// LeftFrontDrive 18_1 3
// LeftMiddleDrive 18_1 1
// LeftBackDrive 18_1 7
// RightFrontDrive 18_1 6
// RightMiddleDrive 18_1 4
// RightBackDrive 18_1 8
// ConveyorBelt 18_1 9
// MobileLift 18_1 10
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
#include <cmath>
using namespace vex;
competition Competition;
void wait(float mSec){
vex::task::sleep(mSec);
}
//-------------------------
//PID Coding
double kP = 0.1;
double kI = 0.1;
double kD = 0.1;
double turnkP = 0.1;
double turnkI = 0.1;
double turnkD = 0.1;
int desiredValue = 200;
int desiredTurnValue = 0;
int error;
int prevError = 0;
int derivative;
int totalError;
int turnError;
int turnPrevError = 0;
int turnDerivative;
int turnTotalError;
bool resetDriveSensors = false;
bool enableDrivePID = true;
int drivePID(){
while(enableDrivePID){
if (resetDriveSensors){
resetDriveSensors = false;
LeftFrontDrive.setPosition(0, degrees);
LeftMiddleDrive.setPosition(0, degrees);
LeftBackDrive.setPosition(0, degrees);
RightFrontDrive.setPosition(0, degrees);
RightMiddleDrive.setPosition(0, degrees);
RightBackDrive.setPosition(0, degrees);
}
int leftFrontPosition = LeftFrontDrive.position(degrees);
int leftMiddlePosition = LeftMiddleDrive.position(degrees);
int leftBackPosition = LeftBackDrive.position(degrees);
int rightFrontPosition = RightFrontDrive.position(degrees);
int rightMiddlePosition = RightMiddleDrive.position(degrees);
int rightBackPosition = RightBackDrive.position(degrees);
int averagePosition = (leftFrontPosition + rightFrontPosition + leftMiddlePosition + rightMiddlePosition + leftBackPosition + rightBackPosition)/6;
error = averagePosition - desiredValue;
derivative = error - prevError;
totalError += error;
double lateralMotorPower = error * kP + derivative * kD + totalError * kI;
int turnDifference = leftFrontPosition - rightFrontPosition;
turnError = turnDifference - desiredTurnValue;
turnDerivative = turnError - turnPrevError;
turnTotalError += turnError;
double turnMotorPower = turnError * turnkP + turnDerivative * turnkD + turnTotalError * turnkI;
LeftFrontDrive.spin(forward, lateralMotorPower + turnMotorPower, voltageUnits::volt);
LeftMiddleDrive.spin(forward, lateralMotorPower + turnMotorPower, voltageUnits::volt);
LeftBackDrive.spin(forward, lateralMotorPower + turnMotorPower, voltageUnits::volt);
RightFrontDrive.spin(forward, lateralMotorPower - turnMotorPower, voltageUnits::volt);
RightMiddleDrive.spin(forward, lateralMotorPower - turnMotorPower, voltageUnits::volt);
RightBackDrive.spin(forward, lateralMotorPower - turnMotorPower, voltageUnits::volt);
prevError = error;
turnPrevError = turnError;
vex::task::sleep(20);
}
return 1;
}
//Autonomous
void autonomous(void){
vex::task wooooo(drivePID);
resetDriveSensors = true;
desiredValue = 300;
desiredTurnValue = 600;
vex::task::sleep(1000);
resetDriveSensors = true;
desiredValue = 300;
desiredTurnValue = 600;
vex::task::sleep(1000);
}
//User Control
void usercontrol(void) {
Controller1.rumble(rumbleShort);
enableDrivePID = false;
while(Competition.isDriverControl()){
LeftFrontDrive.spin(fwd, Controller1.Axis3.value() + (Controller1.Axis1.value()*.45), pct);
LeftMiddleDrive.spin(fwd, Controller1.Axis3.value() + (Controller1.Axis1.value()*.45), pct);
LeftBackDrive.spin(fwd, Controller1.Axis3.value() + (Controller1.Axis1.value()*.45), pct);
RightFrontDrive.spin(fwd, Controller1.Axis3.value() - (Controller1.Axis1.value()*.45), pct);
RightMiddleDrive.spin(fwd, Controller1.Axis3.value() - (Controller1.Axis1.value()*.45), pct);
RightBackDrive.spin(fwd, Controller1.Axis3.value() - (Controller1.Axis1.value()*.45), pct);
bool R1Button = Controller1.ButtonR1.pressing();
bool R2Button = Controller1.ButtonR2.pressing();
bool L1Button = Controller1.ButtonL1.pressing();
bool L2Button = Controller1.ButtonL2.pressing();
bool UpButton = Controller1.ButtonUp.pressing();
bool DownButton = Controller1.ButtonDown.pressing();
//Front Mobile Goal Lift (Up/Down)
if(UpButton){
MobileLift.spin(fwd, 12, volt);
}
else if(DownButton){
MobileLift.spin(fwd, -12, volt);
}
else {
MobileLift.stop(hold);
}
//Back Mobile Goal Lift (R1/R2)
if(R1Button){
LeftFrontDrive.spin(fwd, 12, volt),
LeftMiddleDrive.spin(fwd, -12, volt);
RightFrontDrive.spin(fwd, 12, volt);
RightMiddleDrive.spin(fwd, -12, volt);
}
else if(R2Button){
LeftFrontDrive.spin(fwd, -12, volt);
LeftMiddleDrive.spin(fwd, 12, volt);
RightFrontDrive.spin(fwd, -12, volt);
RightMiddleDrive.spin(fwd, 12, volt);
}
//Conveyor Belt (L1/L2)
if(L1Button){
ConveyorBelt.spin(fwd, 12, volt);
}
else if (L2Button){
ConveyorBelt.spin(fwd, -12, volt);
}
else{
ConveyorBelt.stop(hold);
}
}
}
//Main
int main() {
Competition.drivercontrol(usercontrol);
while (true) {
wait(1, msec);
}
}
//-------------------------
I would appreciate any and all feedback you have for me.
-Omnom