From d9818ec71507c8568f30fa6a25db9741d88d6574 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 28 Feb 2015 11:05:04 -0500 Subject: [PATCH] Started working on Xbox contoller code (untested) --- Commands/Collector/RollOut.cpp | 7 ++- Commands/Drivetrain/Drive.cpp | 6 +- OI.cpp | 107 ++++++++++++++++++--------------- OI.h | 11 ++-- Subsystems/Drivetrain.cpp | 3 +- 5 files changed, 74 insertions(+), 60 deletions(-) diff --git a/Commands/Collector/RollOut.cpp b/Commands/Collector/RollOut.cpp index d3f5150..79b4705 100644 --- a/Commands/Collector/RollOut.cpp +++ b/Commands/Collector/RollOut.cpp @@ -6,9 +6,10 @@ void RollOut::Initialize(){ SetTimeout(2.0); } void RollOut::Execute(){ - //TODO check this value to move the motors in the right direction - // Devide by 2 twice because this speed should be half the collector speed - DentRobot::collector->MoveRollers(-DentRobot::oi->GetLeftThrottle() * 0.8); + //TODO: figure out how to implement this with an Xbox controller + //DentRobot::collector->MoveRollers(-DentRobot::oi->GetLeftThrottle() * 0.8); + //Will just move at .8 power for now + DentRobot::collector->MoveRollers(-0.8); } bool RollOut::IsFinished(){ return IsTimedOut(); diff --git a/Commands/Drivetrain/Drive.cpp b/Commands/Drivetrain/Drive.cpp index 553302f..33fa9fd 100644 --- a/Commands/Drivetrain/Drive.cpp +++ b/Commands/Drivetrain/Drive.cpp @@ -7,9 +7,9 @@ void Drive::Initialize(){ } void Drive::Execute(){ double x, y, z; - x = DentRobot::oi->GetLeftStick()->GetRawAxis(0); - y = -DentRobot::oi->GetLeftStick()->GetRawAxis(1); - z = DentRobot::oi->GetLeftStick()->GetRawAxis(2); + x = DentRobot::oi->GetLeftAxis("left", "x"); + y = DentRobot::oi->GetLeftAxis("left", "y"); + z = DentRobot::oi->GetLeftAxis("right", "x"); //Code to lock the x axis when not holding button 1 //if (DentRobot::oi->GetLeftStick()->GetRawButton(1)){ // x=0; diff --git a/OI.cpp b/OI.cpp index 3dcfaa2..5306d1d 100644 --- a/OI.cpp +++ b/OI.cpp @@ -12,56 +12,67 @@ #include "Commands/Test/CheckRobot.h" OI::OI() { // Joysticks - leftStick=new Joystick(0); + leftController=new Joystick(0); rightStick=new Joystick(1); - // Collector - JoystickButton *left1=new JoystickButton(leftStick, 1); - JoystickButton *left2=new JoystickButton(leftStick, 2); - left1->WhileHeld(new RollIn(GetLeftThrottle())); - left2->WhileHeld(new RollOut()); - // Elevator - raise=new Raise(); - lower=new Lower(); - JoystickButton *right4=new JoystickButton(rightStick, 4); - JoystickButton *right6=new JoystickButton(rightStick, 6); - right4->WhenPressed(lower); - right4->CancelWhenPressed(raise); - right6->WhenPressed(raise); - right6->CancelWhenPressed(lower); - // BinElevator - JoystickButton *right3=new JoystickButton(rightStick, 3); - JoystickButton *right5=new JoystickButton(rightStick, 5); - //JoystickButton *right7=new JoystickButton(rightStick, 7); - //JoystickButton *right8=new JoystickButton(rightStick, 8); - //right7->WhenPressed(new BinOpenArms()); - //right8->WhenPressed(new BinCloseArms()); - binRaise=new BinRaise(3.0); - binLower=new BinLower(2.0); - right3->WhenPressed(binLower); - right3->CancelWhenPressed(binRaise); - right5->WhenPressed(binRaise); - right5->CancelWhenPressed(binLower); - // Cancel - JoystickButton *right12=new JoystickButton(rightStick, 12); - right12->CancelWhenPressed(raise); - right12->CancelWhenPressed(lower); - right12->CancelWhenPressed(binRaise); - right12->CancelWhenPressed(binLower); - // Basic motor test - CheckRobot* checkRobot=new CheckRobot(); - JoystickButton *left7=new JoystickButton(leftStick, 7); - left7->WhenPressed(checkRobot); + // Main buttons + leftA = new JoystickButton(leftController, 1); + leftB = new JoystickButton(leftController, 2); + leftX = new JoystickButton(leftController, 3); + leftY = new JoystickButton(leftController, 4); + // Left and right uttons + leftLB = new JoystickButton(leftController, 5); + leftRB = new JoystickButton(leftController, 6); + leftBack = new JoystickButton(leftController, 7); + leftStart = new JoystickButton(leftController, 8); + // Left, right stick press + leftLPress = new JoystickButton(leftController, 9); + leftRPress = new JoystickButton(leftController, 10); } -Joystick* OI::GetRightStick(){ - return rightStick; +float OI::GetLeftAxis(std::string stick, std::string axis){ + if(stick=="left"){ + if(axis=="x"){ + return leftController->GetX(); + }else if(axis=="y"){ + return -leftController->GetY(); + }else if(axis=="trigger"){ + //TODO: Figure out what axis this is + return -4; + } + }else if(stick=="right"){ + if(axis=="x"){ + return leftController->GetTwist(); + }else if(axis=="y"){ + return -leftController->GetRawAxis(4); + }else if(axis=="trigger"){ + //TODO: Figure out what axis this is + return -4; + } + } + //TODO: Fix this placeholder for NULL + return -5; } -Joystick* OI::GetLeftStick(){ - return leftStick; -} -double OI::GetRightThrottle(){ - return (-rightStick->GetRawAxis(3)+1.0)/2; -} -double OI::GetLeftThrottle(){ - return (-leftStick->GetRawAxis(3)+1.0)/2; +bool OI::GetLeftButton(std::string button){ + if(button=="a"){ + return leftA->Get(); + }else if(button=="b"){ + return leftB->Get(); + }else if(button=="x"){ + return leftX->Get(); + }else if(button=="y"){ + return leftY->Get(); + }else if(button=="lb"){ + return leftLB->Get(); + }else if(button=="rb"){ + return leftRB->Get(); + }else if(button=="back"){ + return leftBack->Get(); + }else if(button=="start"){ + return leftStart->Get(); + }else if(button=="lpress"){ + return leftLPress->Get(); + }else if(button=="rpress"){ + return leftRPress->Get(); + } + return false; } // vim: ts=2:sw=2:et diff --git a/OI.h b/OI.h index 1214378..ab00683 100644 --- a/OI.h +++ b/OI.h @@ -2,19 +2,20 @@ #define OI_H #include "WPILib.h" +#include "string" #include "Commands/Command.h" class OI { private: - Joystick *leftStick, *rightStick; + Joystick *leftController, *rightStick; + // Named Xbox buttons + JoystickButton *leftA, *leftB, *leftX, *leftY, *leftLB, *leftRB, *leftBack, *leftStart, *leftLPress, *leftRPress; public: OI(); Command *raise, *lower, *binLower, *binRaise; - Joystick* GetRightStick(); - Joystick* GetLeftStick(); - double GetLeftThrottle(); - double GetRightThrottle(); + float GetLeftAxis(std::string, std::string); + bool GetLeftButton(std::string); }; #endif // vim: ts=2:sw=2:et diff --git a/Subsystems/Drivetrain.cpp b/Subsystems/Drivetrain.cpp index c56b463..67950bd 100644 --- a/Subsystems/Drivetrain.cpp +++ b/Subsystems/Drivetrain.cpp @@ -15,7 +15,8 @@ void Drivetrain::DriveMecanum(float x, float y, float z, float sensitivity, floa double correctX = -(sensitivity*(pow(x, 3))+(1-sensitivity)*x); double correctY = -(sensitivity*(pow(y, 3))+(1-sensitivity)*y); double correctZ = -z * 0.5; - if (DentRobot::oi->GetLeftStick()->GetRawButton(9)){ + // If they're holding the right button, slow down + if (DentRobot::oi->GetLeftButton("rb")){ correctY /= SmartDashboard::GetNumber("DriveSpeedReductionThresh"); } rightFront->Set((-correctX + correctY - correctZ));