OCCRA
Go to Post Here is more data, Because we all love data. - Bochek [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-23-2018, 06:05 PM
rustyraptor rustyraptor is offline
Registered User
FRC #5613
 
Join Date: Nov 2017
Location: United States
Posts: 8
rustyraptor is an unknown quantity at this point
Help with teleop code.

Here is my code. I have been experimenting with the Toggle class and the plain getRawButton methods however I noticed that when I have a lot of buttons to handle like this some problems start to arise. Sometimes buttons won't work while I am moving the robot or I can only do one button at a time. I understand this is likely a problem with using all the if statements. (I tried if/elif) but I still get a lot of problems like that. My question is: Is there a better way to structure all this? Is this the point where I have to start using the MagicBot framework? We just want to be able to switch between "slow" and "fast" and "reverse" and also be able to control the motors that handle the cube launcher. I will be looking for some other repos on github to see how other teams have done it.

Code:
#!/usr/bin/env python3

import wpilib
from wpilib.drive import DifferentialDrive
from robotpy_ext.control.toggle import Toggle


class MyRobot(wpilib.IterativeRobot):

    def robotInit(self):
        self.frontLeft = wpilib.Spark(0)
        self.rearLeft  = wpilib.Spark(1)
        self.left      = wpilib.SpeedControllerGroup(self.frontLeft, self.rearLeft)

        self.frontRight = wpilib.Spark(2)
        self.rearRight  = wpilib.Spark(3)
        self.right      = wpilib.SpeedControllerGroup(self.frontRight, self.rearRight)

        self.drive  = DifferentialDrive(self.left, self.right)

        self.stick1 = wpilib.Joystick(0)
        self.stick2 = wpilib.Joystick(1)
        self.toggle7 = Toggle(self.stick1, 7)
        self.toggle1 = Toggle(self.stick1, 1)
        self.toggle6 = Toggle(self.stick1, 6)
        self.toggle4 = Toggle(self.stick1, 4)
        self.toggle5 = Toggle(self.stick1, 5)
        self.toggle3 = Toggle(self.stick1, 3)
        self.toggle2 = Toggle(self.stick1, 2)

        self.intake = wpilib.Spark(4)
        self.Lshoot = wpilib.Spark(5)
        self.Rshoot = wpilib.Spark(6)
        self.shoot  = wpilib.SpeedControllerGroup(self.Lshoot, self.Rshoot)

    def disabled(self):
        wpilib.Timer.delay(0.01)
    def teleopInit(self):
        timer = wpilib.Timer()
        timer.start()
        self.a = -1
        self.b = 0
        self.c = 0
        self.d = 1
    def teleopPeriodic(self):
        self.drive.arcadeDrive(self.stick1.getY() * self.a * self.d , self.stick1.getX() * 0.8)
        self.intake.set(self.b)
        self.shoot.set(self.c)
        if self.toggle7.off:
            self.a = -0.7
        if self.toggle7.on:
            self.a = -1
        if self.toggle1.on:
            self.d = -1
        if self.toggle1.off:
            self.d = 1
        if self.toggle4:
            self.b = 1
        if self.toggle6:
            self.b = -1
        if self.toggle5:
            self.c = 1
        if self.toggle3:
            self.c = -1
        if self.toggle2:
            self.b = 1
            self.c = 1
        else:
            self.a = -1
            self.b = 0
            self.c = 0
    def autonomousInit(self):
        timer = wpilib.Timer()
        timer.start()
        if wpilib.DriverStation.getInstance().getGameSpecificMessage() == "RRR" or "RLR":
            self.speed = 0
        elif wpilib.DriverStation.getInstance().getGameSpecificMessage() == "LLL" or "LRL":
            self.speed = 0
        else:
            self.speed = 0
    def autonomousPeriodic(self):
        self.drive.arcadeDrive(self.speed, 0)


if __name__ == '__main__':
    wpilib.run(MyRobot)
Reply With Quote
  #2   Spotlight this post!  
Unread 02-23-2018, 10:16 PM
nickbrickmaster nickbrickmaster is offline
Registered User
AKA: Nick Schatz
no team (3184 Alum)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2014
Location: Eagan MN
Posts: 453
nickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond reputenickbrickmaster has a reputation beyond repute
Re: Help with teleop code.

It might do to give your code some more descriptive variable names, as a start.

Are you sure you want toggle actions? We generally tend to have hold-to-activate or single-press actions because it's easier for the drivers to determine whether something should be active (are they holding the button vs. did they press it sometime in the past?)

Thirdly which I think may be the cause of some of your problems, is that you are checking
Code:
if toggleX
which, while valid syntax, probably won't do what you want. This will always return True for an initialized object as those are "truthy." You instead want to check toggleX.on, which I see you doing in some places.

And you definitely want to use if statements and DO NOT conjoin them together in if/elif chains. You only want to use those for disjoint actions (things that will never occur at the same time.)
__________________
Proceed as if success is inevitable.
Reply With Quote
  #3   Spotlight this post!  
Unread 02-24-2018, 05:03 PM
TimTheGreat's Avatar
TimTheGreat TimTheGreat is offline
ArchdukeTim
FRC #1418 (Vae Victis)
Team Role: Mentor
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Falls Church
Posts: 299
TimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud of
Re: Help with teleop code.

Hey rustyraptor,

I'm the creator of this feature of robotpy so hopefully I can help you out

Let's take a look at that last if/else statement.

Code:
if self.toggle2:
    self.b = 1
    self.c = 1
else:
    self.a = -1
    self.b = 0
    self.c = 0
This is why things (everything but self.d) seem to not be changing. When you call self.toggle2, it checks to see if you have repressed the button. This will return true once and only once for each button press.

So lets say you press and release button 2. The first time it detects this, self.toggle2 will be true, and then b and c will both get set to 1. The next loop. self.toggle2 won't be true, so the code will go into the else statement. Can you see the problem? a,b, and c are all (pretty much) always getting reset to -1, 0, and 0 respectively.

Remember, a toggle only has 2 values, true and false. So you can only really use a single toggle to toggle between two values. You're trying to switch between three, so you might have to rethink your logic.

If you need more help, please feel free to join the robotpy gitter at https://gitter.im/robotpy/robotpy-wpilib
__________________
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.



2012 - Gracious Professionalism - Greater DC
2014 - Regional Finalist - Virginia | Industrial Design - Virginia | Regional Finalist - Greater DC
2015 - Innovation in Control - Greater DC
2016 - District Event Winner - VAHAY | Innovation in Control - VAHAY | District Event Winner - MDBET | Industrial Design - MDBET | District Champion - CHCMP | Innovation in Control - CHCMP
Reply With Quote
  #4   Spotlight this post!  
Unread 02-24-2018, 06:29 PM
Hitchhiker 42's Avatar
Hitchhiker 42 Hitchhiker 42 is offline
Roboter
AKA: Mark Lavrentyev
no team (FullMetal Falcons)
Team Role: Programmer
 
Join Date: Oct 2015
Rookie Year: 2015
Location: Providence, RI
Posts: 992
Hitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond reputeHitchhiker 42 has a reputation beyond repute
Re: Help with teleop code.

You also have an issue here:

Code:
if wpilib.DriverStation.getInstance().getGameSpecificMessage() == "RRR" or "RLR":
    self.speed = 0
elif wpilib.DriverStation.getInstance().getGameSpecificMessage() == "LLL" or "LRL":
    self.speed = 0
else:
    self.speed = 0
That if statement is always going to be true, because "RLR" is a truthy statement, so the whole expression is always true. You want instead:

Code:
if wpilib.DriverStation.getInstance().getGameSpecificMessage() == "RRR" or wpilib.DriverStation.getInstance().getGameSpecificMessage() == "RLR":
    self.speed = 0
elif wpilib.DriverStation.getInstance().getGameSpecificMessage() == "LLL" or wpilib.DriverStation.getInstance().getGameSpecificMessage() == "LRL":
    self.speed = 0
else:
    self.speed = 0
__________________
Don't forget your towel!

Reply With Quote
  #5   Spotlight this post!  
Unread 02-25-2018, 01:27 PM
TimTheGreat's Avatar
TimTheGreat TimTheGreat is offline
ArchdukeTim
FRC #1418 (Vae Victis)
Team Role: Mentor
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Falls Church
Posts: 299
TimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud ofTimTheGreat has much to be proud of
Re: Help with teleop code.

The pythonic way would be to say

Code:
if wpilib.DriverStation.getInstance().getGameSpecificMessage() in ['RRR', 'RLR']
    self.speed = 0
elif ... in ['LLL', 'LRL']
    self.speed = 0
else:
    speed = 0
but then you should ask, if you're always setting the speed to 0, what is the purpose of these statements?
__________________
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.



2012 - Gracious Professionalism - Greater DC
2014 - Regional Finalist - Virginia | Industrial Design - Virginia | Regional Finalist - Greater DC
2015 - Innovation in Control - Greater DC
2016 - District Event Winner - VAHAY | Innovation in Control - VAHAY | District Event Winner - MDBET | Industrial Design - MDBET | District Champion - CHCMP | Innovation in Control - CHCMP
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:04 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi