Help with RobotPy Initializing a system in Periodic startup

Using RobotPy, testing code that will retract an arm system back to zero position at the enablement of the Teleop() in the driver station. We are aware of the watchdog thread 100ms vs the Periodic 20ms. We are not using CommandBase and wondering if it’s possible on generic RobotPy program. Pseudo Code/Use Case:

RobotInit()

  1. Set retract_arm_flag = True

TeleopInit()

  1. If retract_arm_flag = True
  2. Get Arm Retract Limit Switch Position from limit switch value
  3. If = Off/False (Limit Switch is not triggered) Then start motor at 50% to retract arm
  4. Keep checking Arm Retract Limit Switch Position value
  5. When Arm Retract Limit Switch Position = True set the motor to 0% arm is fully retracted, set retract_arm_flag = False

When we implement this, the motor jolts, stop and starts at random period cycles until it triggers the switch and turns off.

We’re not able to go to CommandBase right now, just exploring if there re any options to keep the motor running smoothly?

That’s correct, you don’t need to use commands to move a motor smoothly.

There’s a lot of different incorrect ways you could have implemented that pseudo code. Without the real code it’s hard to say what’s wrong.

My guess is that you have a loop in Keep checking Arm Retract Limit Switch Position value? If you do, that’s probably your problem, you should avoid using loops in robot code.

Thanks Dustin,

Here is the last version we tried. We don’t use While Loops but did try tracking elapsed time and no joy either.

python

def robotInit(self):
# Initialize the elevator
self.elevator_initializing = True
self.elevator_motor.set(0.0) # Ensure motor is initially off

def robotPeriodic(self):
# Elevator initialization process
if self.elevator_initializing:
if not self.bottom_elevator_limit_switch.get(): # If NOT at bottom position
self.elevator_motor.set(0.5) # Move elevator down
else:
self.elevator_motor.set(0.0) # Stop motor
self.elevator_encoder.reset() # Reset encoder position
self.elevator_initializing = False # Initialization complete

We put the old code back and set the motor speed as a Periodic instance variable rather than Periodic cycle scope. It all works now as expected.

def robotInit(self):

//Setup state flags, no initialization logic here

self.elevator_initializing = False

self.elevator_initialized = False # Track if initialization is complete

def teleopInit(self):

Initialize the motor speed and start the initialization process in teleop mode

self.elevator_motor_speed = 0.0 # Track the motor speed as an instance variable

self.elevator_motor.set(self.elevator_motor_speed) # Ensure motor is initially stopped

self.elevator_initializing = True # Start the initialization process

def robotPeriodic(self):

//Separate Initialization Logic

if self.elevator_initializing:

//Check if the elevator is already at the bottom position (limit switch triggered)

if not self.bottom_elevator_limit_switch.get(): # Limit switch is triggered

print(“Elevator at bottom position. Completing initialization.”)

self.elevator_motor_speed = 0.0 # Stop motor

self.elevator_motor.set(self.elevator_motor_speed) # Apply stop

self.elevator_encoder.reset() # Reset encoder to zero

self.elevator_initializing = False # Stop initialization

self.elevator_initialized = True # Mark initialization as complete

elif self.elevator_motor_speed != 0.5: # Only set speed if not already set

print(“Starting elevator motor to move down.”)

self.elevator_motor_speed = 0.5 # Set motor to move down

self.elevator_motor.set(self.elevator_motor_speed)

//Separate Operator Control Logic

if self.elevator_initialized:

//Simulate operator control using a joystick input (replace with actual controller logic)

joystick_input = self.joystick.getY() # Assume joystick Y-axis controls elevator

//Set motor speed based on joystick input for operator control

self.elevator_motor_speed = joystick_input

self.elevator_motor.set(self.elevator_motor_speed)