RobotPy trouble - Various issues

Using PyCharm IDE

Connection to robot via WIFI is not an issue but ever since we updated RobotPy to latest software (2020).

Here is what we get after pressing enable on driving station:

Default TeleopInit() method… Override me! 
 19:28:27:344 ERROR : wpilib.ds : Unhandled exception 
 Traceback (most recent call last): 
ERROR  1  Unhandled exception  /home/lvuser/py/robot.py.83:teleopPeriodic 
 File “/usr/local/lib/python3.8/site-packages/wpilib/_impl/start.py”, line 106, in start 
 self.robot.startCompetition() 
 File “/home/lvuser/py/robot.py”, line 83, in teleopPeriodic 
 self.drive.arcadeDrive(self.trig, self.con0.getX(kLeft)) 
 AttributeError: ‘MyRobot’ object has no attribute ‘drive’ 
 
 
 Locals at innermost frame: 
 
 {‘self’: <main.MyRobot object at 0xb411ab40>} 
 
 
 Error at /home/lvuser/py/robot.py.83:teleopPeriodic: Unhandled exception 
 
 Traceback (most recent call last): 
 File “/usr/local/lib/python3.8/site-packages/wpilib/_impl/start.py”, line 106, in start 
 self.robot.startCompetition() 
 File “/home/lvuser/py/robot.py”, line 83, in teleopPeriodic 
 self.drive.arcadeDrive(self.trig, self.con0.getX(kLeft)) 
 File “/usr/local/lib/python3.8/site-packages/wpilib/_impl/start.py”, line 106, in start 
 self.robot.startCompetition() 
 File “/home/lvuser/py/robot.py”, line 83, in teleopPeriodic 
 self.drive.arcadeDrive(self.trig, self.con0.getX(kLeft)) 
 AttributeError: ‘MyRobot’ object has no attribute ‘drive’ 
 
 19:28:27:355 WARNING : wpilib.ds : Robots should not quit, but yours did! 
Warning  1  Robots should not quit, but yours did!  
 Robots should not quit, but yours did! 
Warning  1  Loop time of 0.020000s overrun
 
 Watchdog not fed within 0.020000s 
 Loop time of 0.020000s overrun 
 
 19:28:27:357 ERROR : wpilib.ds : The startCompetition() method (or methods called by it) should have handled the exception above. 
ERROR  1  The startCompetition() method (or methods called by it) should have handled the exception above.  
 The startCompetition() method (or methods called by it) should have handled the exception above. 
 19:28:32:328 INFO : wpilib : WPILib version 2020.2.2.5 
 19:28:32:329 INFO : wpilib : HAL version 2020.2.2.0 
 19:28:32:334 INFO : faulthandler : registered SIGUSR2 for PID 2928 
 Camera Server Library Not Found 
 Default RobotInit() method… Override me! 
 Default DisabledInit() method… Override me! 
 Default DisabledPeriodic() method… Override me! 
 Default RobotPeriodic() method… Override me! 
Warning  44003  FRC: No robot code is currently running.  Driver Station

Have you tried running in simulation? Might be easier to diagnose.

Can you post/link to your code? The error message indicates that there is no attribute ‘drive’… is there?

Getting same series of errors running through sim for both teleop and autonomous.

Here is our code:
(edited spelling)

import wpilib
import wpilib.drive

# class that makes everything work
class MyRobot(wpilib.TimedRobot):

    # Initialize variables, motors, solenoids, controllers, etc
    def __robotInit__(self):
        # Stream the camera - if you aren't vision processing and are just streaming the camera, this is all you need
        wpilib.CameraServer.launch()
    
        # Defines which motors are connected to which sparks
        self.lfMotor = wpilib.Spark(0)
        self.rfMotor = wpilib.Spark(1)
        self.lbMotor = wpilib.Spark(2)
        self.rbMotor = wpilib.Spark(3)
        #  Move arm up and down
        self.upDown = wpilib.Spark(4)
        # Move arm in and out
        self.extend = wpilib.Spark(5)

        # Sets drive motor groups
        self.left = wpilib.SpeedControllerGroup(self.lfMotor, self.lbMotor)
        self.right = wpilib.SpeedControllerGroup(self.rfMotor, self.rbMotor)
        # Tells it to drive with the previously created motor groups
        self.drive = wpilib.drive.DifferentialDrive(self.left, self.right)
        
        # Defines the controllers
        self.con0 = wpilib.XboxController(0)
        # self.con1 = wpilib.XboxController(1)
        
        # Creates variables to store the trigger values
        self.ltrig = self.con0.getTriggerAxis(kLeft)
        self.rtrig = self.con0.getTriggerAxis(kRight)
        # Creates a variable to store which trigger is used to drive, defined by a following function
        def trigDrive(self):
            if self.rtrig > 0:
                return self.rtrig
            elif self.ltrig > 0:
                return self.ltrig
            else:
                return 0

        self.trig = trigDrive

        # Timer for Auto
        self.timer = wpilib.Timer()

        # Defines solenoid
        self.sol = wpilib.DoubleSolenoid(0, 1)
        # Variable for piston toggle
        self.open = true

    # Decides which trigger value to use for driving


     # Auto Example - I don't know what it does
    def __autonomousInit__(self):
        """This function is run once each time the robot enters autonomous mode."""
        self.timer.reset()
        self.timer.start()

    def autonomousPeriodic(self):
        """This function is called periodically during autonomous."""

        # Drive for two seconds
        if self.timer.get() < 2.0:
            self.drive.arcadeDrive(-0.5, 0)  # Drive forwards at half speed
        else:
            self.drive.arcadeDrive(0, 0)  # Stop robot

    # Teleop code
    def __teleopInit__(self):
        pass
    def teleopPeriodic(self):
        
        # Arcade drive using the triggers for forward/backward and the left stick x-axis for left/right
        self.drive.arcadeDrive(self.trig, self.con0.getX(kLeft))
        # Makes the arm motors move - controlled by right stick
        self.upDown.set(getY(kRight))
        self.extend.set(getX(kRight))

        # A button toggles whether piston is open or closed
        if self.con0.getAButton():
            self.open = not self.open
        
        # Open or close the claw based on the toggle
        if self.open:
            self.sol.set("Forward")
        
        else :
            self.sol.set("Reverse")
        

        
# Calls all of the commands and makes it work - needed to make anything work
if __name__ == "__main__":
    wpilib.run(MyRobot)```

Curious, where did you get the idea that robotInit has leading/trailing __? It should be just robotInit, not __robotInit__.

Same goes for the other functions.

We had an issue where the robot wouldn’t even read any of the code. Robot code light was red the entire time. Putting __ on both sides allowed the robot to even receive it. Pycharm has an active debugger that suggested that the Init functions be inside __

I suspect your actual issue was something different. None of the wpilib base class functions have leading/trailing __. While significant portions of our docs are out of date for 2020 still, these look like they’re still mostly right: https://robotpy.readthedocs.io/projects/wpilib/en/latest/wpilib/IterativeRobotBase.html#wpilib.iterativerobotbase.IterativeRobotBase

My recommendation to you is to get your code not crashing in the simulator first, then try to deploy it to the robot.

I wonder if it is pyCharm. Pycharm has the ability to deploy extra files to help with remote debugging. When we were using it (our first Python year), it was okay on the rio, but our microcontrollers (pyboard) struggled. That was a while ago though. If you are near a computer with a good enough graphics card, perhaps try the code below…

import wpilib
from wpilib import drive


class MyRobot(wpilib.TimedRobot):
    
    def robotInit(self):
        self.left_motor = wpilib.Spark(0)
        self.right_motor = wpilib.Spark(1)
        self.drive = wpilib.drive.DifferentialDrive(self.left_motor, self.right_motor)
        self.stick = wpilib.Joystick(0)

        self.drive.setExpiration(0.1)
        self.tm = wpilib.Timer()
        self.tm.start()
    def autonomousInit(self):
         """Executed at the start of teleop mode"""
         self.drive.setSafetyEnabled(True)
         
    def autonomousPeriodic(self):


        #self.MyRobot.setSafetyEnabled(True)
        time = self.tm.get()

        if (time<15):
            self.drive.arcadeDrive(
                .8, 0, True
            )
        else:
            self.drive.arcadeDrive(
                0, 0, True
            )
        

if __name__ == "__main__":
    wpilib.run(MyRobot)

We took out the __ around the functions and ran in the simulator. This is the output:

19:53:07:538 INFO    : wpilib              : WPILib version 2020.2.2.5
19:53:07:539 INFO    : wpilib              : HAL version 2020.2.2.0
19:53:07:540 INFO    : wpilib              : Running with simulated HAL.
19:53:07:542 INFO    : halsim_gui          : WPILib HAL Simulation 2020.2.2.0
HAL Extensions: Attempting to load: halsim_gui
Simulator GUI Initializing.
Simulator GUI Initialized!
HAL Extensions: Successfully loaded extension
Not loading CameraServerShared
19:53:08:158 INFO    : wpilib.cs           : Would launch CameraServer with vision_py=None
19:53:08:167 ERROR   : wpilib.ds           : Unhandled exception
Traceback (most recent call last):
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 38, in robotInit
    self.ltrig = self.con0.getTriggerAxis(kLeft)
NameError: name 'kLeft' is not defined


Locals at innermost frame:

{'self': <__main__.MyRobot object at 0x0000023253DB1040>}


Error at robot.py.38:robotInit: Unhandled exception

Traceback (most recent call last):
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 38, in robotInit
    self.ltrig = self.con0.getTriggerAxis(kLeft)
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 38, in robotInit
    self.ltrig = self.con0.getTriggerAxis(kLeft)
NameError: name 'kLeft' is not defined

19:53:08:222 WARNING : wpilib.ds           : Robots should not quit, but yours did!
Robots should not quit, but yours did!
19:53:08:227 ERROR   : wpilib.ds           : The startCompetition() method (or methods called by it) should have handled the exception above.
The startCompetition() method (or methods called by it) should have handled the exception above.

It seems to have something to do with how the controller determines which side is used. It worked before we updated to robotpy 2020, but stopped working after the updates

Shouldn’t need to specify kLeft – and even if you did it would be wpilib.Joystick.Hand.kLeftHand.

Edit: I missed that you were using an XboxController, this is a change for 2020 XBoxController, wpilib.XboxController.Hand.kLeftHand is what you want.

1 Like

Thank you. I was going to mention that too, but their code did not seem to get that far before crashing. Something else may have hung it up.

Got the simulation program to actually open after fixing the KLeft and Right for xbox controller. Now we are getting this error for when we test teleop:

Simulator GUI Initialized!
HAL Extensions: Successfully loaded extension
Not loading CameraServerShared
20:08:09:722 INFO    : wpilib.cs           : Would launch CameraServer with vision_py=None
Joystick Axis missing, check if all controllers are plugged in
Default frc::IterativeRobotBase::DisabledInit() method... Override me!
Default frc::IterativeRobotBase::DisabledPeriodic() method... Override me!
Default frc::IterativeRobotBase::RobotPeriodic() method... Override me!
20:08:12:730 ERROR   : wpilib.ds           : Unhandled exception
Traceback (most recent call last):
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 84, in teleopPeriodic
    self.drive.arcadeDrive(self.trig, self.con0.getX())
TypeError: getX(): incompatible function arguments. The following argument types are supported:
    1. (self: wpilib._wpilib.XboxController, hand: wpilib.interfaces._interfaces.GenericHID.Hand) -> float

Invoked with: <wpilib._wpilib.XboxController object at 0x0000022286350F30>


Locals at innermost frame:

{'self': <__main__.MyRobot object at 0x000002228634EF40>}


Error at robot.py.84:teleopPeriodic: Unhandled exception

Traceback (most recent call last):
  File "Watchdog not fed within C:\U0.020000sers\first\AppDs
ata\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 84, in teleopPeriodic
    self.drive.arcadeDrive(self.trig, self.con0.getX())
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 84, in teleopPeriodic
    self.drive.arcadeDrive(self.trig, self.con0.getX())
TypeError: getX(): incompatible function arguments. The following argument types are supported:
    1. (self: wpilib._wpilib.XboxController, hand: wpilib.interfaces._interfaces.GenericHID.Hand) -> float

Invoked with: <wpilib._wpilib.XboxController object at 0x0000022286350F30>

Loop time of 0.02020:08:12:865 WARNING : wpilib.ds           : Robots should not quit, but yours did!
000s overrun

Error at frc::MotorSafety::Check [MotorSafety.cpp:103]: A timeout has been exceeded: DifferentialDrive... Output not updated often enough.
Robots should not quit, but yours did!
20:08:12:899 ERROR   : wpilib.ds           : The startCompetition() method (or methods called by it) should have handled the exception above.
The startCompetition() method (or methods called by it) should have handled the exception above.

As the (terrible) error message says, getX requires a hand argument.

Though I feel the Hand argument is better in some ways, an alternative is…

self.stick.getRawAxis(1)

That is for left stick Y axis. The enumeration matches the driver station.
Also, make sure you have an xbox controller plugged into the computer and have dragged the controller from the “System Joysticks” to the Joysticks pane.

Sorry for late replies, had to drive home, will finish testing purely through sims for tonight.

This is the output through teleoperated on simulation, my controller is plugged in.

HAL Extensions: Successfully loaded extension
Not loading CameraServerShared
20:41:41:816 INFO    : wpilib.cs           : Would launch CameraServer with vision_py=None
Joystick Axis missing, check if all controllers are plugged in
Default frc::IterativeRobotBase::DisabledInit() method... Override me!
Default frc::IterativeRobotBase::DisabledPeriodic() method... Override me!
Default frc::IterativeRobotBase::RobotPeriodic() method... Override me!
20:41:43:985 ERROR   : wpilib.ds           : Unhandled exception
Traceback (most recent call last):
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 84, in teleopPeriodic
    self.drive.arcadeDrive(self.trig, self.con0.getX())
TypeError: getX(): incompatible function arguments. The following argument types are supported:
    1. (self: wpilib._wpilib.XboxController, hand: wpilib.interfaces._interfaces.GenericHID.Hand) -> float

Invoked with: <wpilib._wpilib.XboxController object at 0x0000019B2D081BB0>


Locals at innermost frame:

{'self': <__main__.MyRobot object at 0x0000019B2D082040>}


Error at robot.py.84:teleopPeriodic: Unhandled exception

Traceback (most recent call last):
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 84, in teleopPeriodic
    self.drive.arcadeDrive(self.trig, self.con0.getX())
  File "C:\Users\first\AppData\Local\Programs\Python\Python38\lib\site-packages\wpilib\_impl\start.py", line 106, in start
    self.robot.startCompetition()
  File "robot.py", line 84, in teleopPeriodic
    self.drive.arcadeDrive(self.trig, self.con0.getX())
TypeError: getX(): incompatible function arguments. The following argument types are supported:
    1. (self: wpilib._wpilib.XboxWatchdog not fed within Controller, hand: wpilib.interfaces._interfaces.GenericHID.Hand) -> float

Invoked with: <wpilib._wpilib.XboxController object at 0x0000019B2D081BB0>
0.020000
s
Loop time of 0.020000s overrun

20:41:44:013 WARNING : wpilib.ds           : Robots should not quit, but yours did!
Robots should not quit, but yours did!
20:41:44:015 ERROR   : wpilib.ds           : The startCompetition() method (or methods called by it) should have handled the exception above.
The startCompetition() method (or methods called by it) should have handled the exception above.

Same error, need to pass a hand to getX.