Thank you i will test this when I can get it moving. I looked through my code again though and didn’t see anything that would stop it from moving. Its also especially confusing because I still have my other two pids in the code and they work fine. would you be willing to look at my code and see if you can help?
here is all of the code
#region VEXcode Generated Robot Configuration
from vex import *
import urandom
# Brain should be defined by default
brain=Brain()
# Robot configuration code
LeftDrive1111_motor_a = Motor(Ports.PORT20, GearSetting.RATIO_6_1, True)
LeftDrive1111_motor_b = Motor(Ports.PORT6, GearSetting.RATIO_6_1, True)
LeftDrive1111 = MotorGroup(LeftDrive1111_motor_a, LeftDrive1111_motor_b)
LeftDrive1155_motor_a = Motor(Ports.PORT3, GearSetting.RATIO_6_1, True)
LeftDrive1155_motor_b = Motor(Ports.PORT9, GearSetting.RATIO_6_1, False)
LeftDrive1155 = MotorGroup(LeftDrive1155_motor_a, LeftDrive1155_motor_b)
RightDrive1111_motor_a = Motor(Ports.PORT5, GearSetting.RATIO_6_1, False)
RightDrive1111_motor_b = Motor(Ports.PORT11, GearSetting.RATIO_6_1, False)
RightDrive1111 = MotorGroup(RightDrive1111_motor_a, RightDrive1111_motor_b)
RightDrive1155_motor_a = Motor(Ports.PORT7, GearSetting.RATIO_6_1, False)
RightDrive1155_motor_b = Motor(Ports.PORT2, GearSetting.RATIO_6_1, True)
RightDrive1155 = MotorGroup(RightDrive1155_motor_a, RightDrive1155_motor_b)
CataIntake1 = Motor(Ports.PORT13, GearSetting.RATIO_18_1, False)
inertial_10 = Inertial(Ports.PORT10)
controller_1 = Controller(PRIMARY)
IntakeACT = DigitalOut(brain.three_wire_port.b)
Wings = DigitalOut(brain.three_wire_port.c)
CataIntake_motor_a = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
CataIntake_motor_b = Motor(Ports.PORT4, GearSetting.RATIO_18_1, True)
CataIntake = MotorGroup(CataIntake_motor_a, CataIntake_motor_b)
# wait for rotation sensor to fully initialize
wait(30, MSEC)
def play_vexcode_sound(sound_name):
# Helper to make playing sounds from the V5 in VEXcode easier and
# keeps the code cleaner by making it clear what is happening.
print("VEXPlaySound:" + sound_name)
wait(5, MSEC)
# add a small delay to make sure we don't print in the middle of the REPL header
wait(200, MSEC)
# clear the console to make sure we don't have the REPL in the console
print("\033[2J")
#endregion VEXcode Generated Robot Configuration
vexcode_brain_precision = 0
vexcode_console_precision = 0
vexcode_controller_1_precision = 0
message1 = Event()
CataShoot = Event()
intakeACT = Event()
WingsOpen = Event()
WingsClose = Event()
BlockerUp = Event()
Intake2 = Event()
Test = [[0 for y in range(10)] for x in range(10)]
DriverControl = False
Autonomous = False
Started = False
IntakeAUCT = False
wings = False
Intake = False
Cata = False
IntakeR = False
Auton = False
ButtonUp = False
Button_Up = False
myVariable = 0
RightDrive = 0
LeftDrive = 0
ErrorDS = 0
OutputDS = 0
Auton1 = 0
CataStop = 0
armupdown = 0
LeftRot = 0
RightRot = 0
TurnOSC = 0
Heading = 0
consecutive_stable_iterations = 0
inertial_10.set_heading(0, DEGREES)
Wheel_Diameter = 3.25
Drive_Gear_Ratio = (36/48)
max_speed = 100
min_speed = 50
error = 0
integral = 0
derivative = 0
previous_error = 0
kp = .564
ki = .000013
kd = 0
kp2 = .762
ki2 = .0006
kd2 = 4.4
def drive_distance(target_distance, Time, TimeStop):
global error, integral, derivative, previous_error
# Set initial rotation to 0 degrees
LeftDrive1111.set_position(0, DEGREES)
# Reset PID variables
error = 0
integral = 0
derivative = 0
previous_error = 0
consecutive_stable_iterations = 0
# Main PID loop for turning
while True:
current_distance = ((3.14159265 * Wheel_Diameter) / 360) * (LeftDrive1111.position(DEGREES)) * (Drive_Gear_Ratio)
error = (target_distance * 1) - current_distance
integral = integral + error
if error < 0 :
intergral = 0
derivative = error - previous_error
previous_error = error
output = kp * error + ki * integral + kd * derivative
speed = abs(output)
if error > 1:
left_speed = 1 * speed
right_speed = 1 * speed
if error < -1:
left_speed = -1 * speed
right_speed = -1 * speed
if abs(error) < 1.5:
consecutive_stable_iterations += 1
else:
consecutive_stable_iterations = 0\
# Check for stability
if consecutive_stable_iterations >=Time:
LeftDrive1111.stop()
LeftDrive1155.stop()
RightDrive1111.stop()
RightDrive1155.stop()
break
if TimeStop == 1:
wait(3, SECONDS)
break
# Set motor velocities
LeftDrive1111.spin(FORWARD, left_speed, VOLT)
LeftDrive1155.spin(FORWARD, left_speed, VOLT)
RightDrive1111.spin(FORWARD, right_speed, VOLT)
RightDrive1155.spin(FORWARD, right_speed, VOLT)
# Sleep for a short time to avoid excessive loop execution
wait(20, MSEC)
def drive_turn(target_distance, target_angle, Time):
global error, integral, derivative, previous_error, error2, integral2, derivative2, previous_error2
# Set sensor rotation to 0 degrees
LeftDrive1111.set_position(0, DEGREES)
inertial_10.set_rotation(0, DEGREES)
# Reset PID variables
error = 0
integral = 0
derivative = 0
previous_error = 0
consecutive_stable_iterations = 0
error2 = 0
integral2 = 0
derivative2 = 0
previous_error2 = 0
TimeStop = 50
# Main PID loop
while True:
current_distance = ((3.14159265 * Wheel_Diameter) / 360) * (LeftDrive1111.position(DEGREES)) * (Drive_Gear_Ratio)
error = (target_distance * 1) - current_distance
integral = integral + error
if error < 0 :
integral = 0
derivative = error - previous_error
previous_error = error
output = kp * error + ki * integral + kd * derivative
speed = abs(output)
current_angle = inertial_10.rotation(DEGREES)
error2 = target_angle - current_angle
integral2 = integral2 + error2
if error2 < 0 :
integral2 = 0
derivative2 = error2 - previous_error2
previous_error2 = error2
output2 = kp2 * error2 + ki2 * integral2 + kd2 * derivative2
speed2 = abs(output2)
error3 = error + error2
if error > 1:
left_speed3 = (1 * speed)
right_speed3 = (1 * speed)
if error < -1:
left_speed3 = (-1 * speed)
right_speed3 = (-1 * speed)
if error2 > 1:
left_speed4 = 1.25 * speed2 * 2 + 10
right_speed4 = -1.1 * speed2 * 2 + 10
if error2 < -1:
left_speed4 = -1.35 * speed2 * 2
right_speed4 = 1.1 * speed2 * 2
left_speed5 = left_speed4 + left_speed3
right_speed5 = right_speed4 + right_speed3
if abs(error3) < 1.5:
consecutive_stable_iterations += 1
else:
consecutive_stable_iterations = 0\
# Check for stability
if consecutive_stable_iterations >=Time:
LeftDrive1111.stop()
LeftDrive1155.stop()
RightDrive1111.stop()
RightDrive1155.stop()
break
if TimeStop == 1:
wait(3, SECONDS)
break
# Set motor velocities
LeftDrive1111.spin(FORWARD, left_speed5, VOLT)
LeftDrive1155.spin(FORWARD, left_speed5, VOLT)
RightDrive1111.spin(FORWARD, right_speed5, VOLT)
RightDrive1155.spin(FORWARD, right_speed5, VOLT)
# Sleep for a short time to avoid excessive loop execution
wait(20, MSEC)
def turn_to_angle(target_angle, Time, TimeStop):
global error, integral, derivative, previous_error
inertial_10.set_rotation(0, DEGREES)
# Reset PID variables
error = 0
integral = 0
derivative = 0
previous_error = 0
consecutive_stable_iterations = 0
# Main PID loop for turning
while True:
current_angle = inertial_10.rotation(DEGREES)
error = target_angle - current_angle
integral = integral + error
if error < 0 :
intergral = 0
derivative = error - previous_error
previous_error = error
output = kp2 * error + ki2 * integral + kd2 * derivative
speed = abs(output)
if error > 1:
left_speed2 = 1.25 * speed * 2
right_speed2 = -1.1 * speed * 2
if error < -1:
left_speed2 = -1.35 * speed * 2
right_speed2 = 1.1 * speed * 2
if abs(error) < 1.5:
consecutive_stable_iterations += 1
else:
consecutive_stable_iterations = 0\
# Check for stability
if consecutive_stable_iterations >=Time:
LeftDrive1111.stop()
LeftDrive1155.stop()
RightDrive1111.stop()
RightDrive1155.stop()
break
if TimeStop == 1:
wait(3, SECONDS)
break
# Set motor velocities
LeftDrive1111.spin(FORWARD, left_speed2, VOLT)
LeftDrive1155.spin(FORWARD, left_speed2, VOLT)
RightDrive1111.spin(FORWARD, right_speed2, VOLT)
RightDrive1155.spin(FORWARD, right_speed2, VOLT)
# Sleep for a short time to avoid excessive loop execution
wait(20, MSEC)
def Intake_Time_Direction(Intake_Time_Direction__Time, Intake_Time_Direction__Direction):
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
CataIntake.spin(FORWARD)
CataIntake.set_velocity((Intake_Time_Direction__Direction * 100), PERCENT)
wait(Intake_Time_Direction__Time, SECONDS)
CataIntake.stop()
CataIntake.set_velocity(100, PERCENT)
def ondriver_drivercontrol_0():
global Button_Up
controller_1.buttonUp.pressed(Button_Up)
Buttonup = False
CataIntake.set_position(0, DEGREES)
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision, ButtonUp
if Button_Up:
if ButtonUp:
CataIntake.spin_for(FORWARD, 360,DEGREES)
wait(.2, SECONDS)
CataIntake.spin_for(FORWARD, 360,DEGREES)
wait(1,SECONDS)
ButtonUp = False
else:
CataIntake.spin_to_position(0,DEGREES)
wait(5,MSEC)
ButtonUp = True
def onauton_autonomous_0():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
Autonomous = True
DriverControl = False
Started = False
while True:
controller_1.screen.set_cursor(1,1)
controller_1.screen.print(inertial_10.heading(DEGREES))
controller_1.screen.clear_row(1)
controller_1.screen.set_cursor(2,1)
controller_1.screen.print(((3.14159265 * Wheel_Diameter) / 360) * (LeftDrive1111.position(DEGREES)) * (Drive_Gear_Ratio))
controller_1.screen.clear_row(2)
controller_1.screen.set_cursor(3,1)
controller_1.screen.print()
controller_1.screen.clear_row(3)
def when_started1():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
# Devices setup
LeftDrive1111.set_position(0, DEGREES)
LeftDrive1111.set_stopping(COAST)
LeftDrive1155.set_stopping(COAST)
RightDrive1111.set_stopping(COAST)
RightDrive1155.set_stopping(COAST)
LeftDrive1111.set_max_torque(100, PERCENT)
RightDrive1111.set_max_torque(100, PERCENT)
LeftDrive1155.set_max_torque(100, PERCENT)
RightDrive1155.set_max_torque(100, PERCENT)
CataIntake.set_max_torque(100, PERCENT)
CataIntake.set_velocity(100, PERCENT)
inertial_10.set_heading(0, DEGREES)
inertial_10.calibrate()
while inertial_10.is_calibrating():
sleep(50)
CataIntake.set_stopping(HOLD)
while True:
if DriverControl or Started:
LeftDrive1111.set_velocity(LeftDrive, PERCENT)
LeftDrive1155.set_velocity(LeftDrive, PERCENT)
RightDrive1111.set_velocity(RightDrive, PERCENT)
RightDrive1155.set_velocity(RightDrive, PERCENT)
LeftDrive1111.spin(FORWARD)
LeftDrive1155.spin(FORWARD)
RightDrive1111.spin(FORWARD)
RightDrive1155.spin(FORWARD)
wait(5, MSEC)
wait(5, MSEC)
def when_started2():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
Started = True
DriverControl = False
Autonomous = False
def ondriver_drivercontrol_1():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
# Controller Controls
while True:
if DriverControl or Started:
LeftDrive = controller_1.axis3.position() + 0.01 * (controller_1.axis1.position() * math.fabs(controller_1.axis1.position()))
RightDrive = controller_1.axis3.position() + -0.01 * (controller_1.axis1.position() * math.fabs(controller_1.axis1.position()))
if controller_1.buttonL2.pressing():
if IntakeAUCT:
IntakeACT.set(True)
IntakeAUCT = False
wait(0.5, SECONDS)
else:
IntakeACT.set(False)
IntakeAUCT = True
wait(0.5, SECONDS)
intakeACT.broadcast()
if controller_1.buttonL1.pressing():
if wings:
Wings.set(True)
wings = False
wait(0.5, SECONDS)
else:
Wings.set(False)
wings = True
wait(0.5, SECONDS)
BlockerUp.broadcast()
wait(5, MSEC)
def ondriver_drivercontrol_2():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
while True:
for repeat_count in range(10):
if IntakeAUCT:
brain.screen.set_cursor(1, 1)
controller_1.screen.print("Arm UP")
else:
controller_1.screen.print("Arm DOWN")
wait(0.5, SECONDS)
controller_1.screen.clear_row(1)
controller_1.screen.set_cursor(controller_1.screen.row(), 1)
wait(5, MSEC)
wait(5, MSEC)
def when_started3():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
while True:
if DriverControl or Started:
for repeat_count2 in range(10):
if controller_1.buttonR2.pressing():
CataIntake.set_velocity(100, PERCENT)
CataIntake.spin(FORWARD)
else:
if not (Cata and controller_1.buttonR1.pressing()):
wait(0.01, SECONDS)
CataIntake.stop()
if controller_1.buttonR1.pressing():
CataIntake.set_velocity(50, PERCENT)
CataIntake.spin(REVERSE)
else:
if not (controller_1.buttonR2.pressing() and Cata):
wait(0.01, SECONDS)
CataIntake.stop()
wait(5, MSEC)
wait(5, MSEC)
def onauton_autonomous_1():
global message1, CataShoot, intakeACT, WingsOpen, WingsClose, BlockerUp, Intake2, Test, DriverControl, Autonomous, Started, IntakeAUCT, wings, Intake, Cata, IntakeR, Auton, myVariable, RightDrive, LeftDrive, ErrorDS, OutputDS, Auton1, CataStop, armupdown, LeftRot, RightRot, TurnOSC, Heading, vexcode_brain_precision, vexcode_console_precision, vexcode_controller_1_precision
inertial_10.set_heading(0, DEGREES)
inertial_10.calibrate()
while inertial_10.is_calibrating():
sleep(65)
drive_turn(24, 1, 50)
# create a function for handling the starting and stopping of all autonomous tasks
def vexcode_auton_function():
# Start the autonomous control tasks
auton_task_0 = Thread( onauton_autonomous_0 )
auton_task_1 = Thread( onauton_autonomous_1 )
# wait for the driver control period to end
while( competition.is_autonomous() and competition.is_enabled() ):
# wait 10 milliseconds before checking again
wait( 10, MSEC )
# Stop the autonomous control tasks
auton_task_0.stop()
auton_task_1.stop()
def vexcode_driver_function():
# Start the driver control tasks
driver_control_task_0 = Thread( ondriver_drivercontrol_0 )
driver_control_task_1 = Thread( ondriver_drivercontrol_1 )
driver_control_task_2 = Thread( ondriver_drivercontrol_2 )
# wait for the driver control period to end
while( competition.is_driver_control() and competition.is_enabled() ):
# wait 10 milliseconds before checking again
wait( 10, MSEC )
# Stop the driver control tasks
driver_control_task_0.stop()
driver_control_task_1.stop()
driver_control_task_2.stop()
# register the competition functions
competition = Competition( vexcode_driver_function, vexcode_auton_function )
ws2 = Thread( when_started2 )
ws3 = Thread( when_started3 )
when_started1()
[details="Summary"]
[spoiler]This text will be hidden[/spoiler]
[/details]