From aaeae5fd11fd5b854220003b0098ddc911ea85d9 Mon Sep 17 00:00:00 2001 From: Adam Long Date: Sun, 28 Dec 2014 16:15:51 +0000 Subject: [PATCH] Moved the joystick to lib/controller and changed the existing code to work with it --- src/HHBase.h | 1 - src/HHRobot.cpp | 37 +++++++++-------- src/HHRobot.h | 5 +-- src/Subsystems/Controller.cpp | 62 ----------------------------- src/Subsystems/Controller.h | 14 ------- src/lib/controller/Controller.h | 19 +++++++++ src/lib/controller/Extreme3dPro.cpp | 32 +++++++++++++++ 7 files changed, 73 insertions(+), 97 deletions(-) delete mode 100644 src/Subsystems/Controller.cpp delete mode 100644 src/Subsystems/Controller.h create mode 100644 src/lib/controller/Controller.h create mode 100644 src/lib/controller/Extreme3dPro.cpp diff --git a/src/HHBase.h b/src/HHBase.h index 4bec309..63faedb 100644 --- a/src/HHBase.h +++ b/src/HHBase.h @@ -2,7 +2,6 @@ #define __HH_BASE_H__ #include #include "HHRobot.h" -#include "Subsystems/Controller.h" #include "Subsystems/Shooter.h" #include "Subsystems/Collector.h" #include "Subsystems/Compressor.h" diff --git a/src/HHRobot.cpp b/src/HHRobot.cpp index 9396c3b..bc2b772 100644 --- a/src/HHRobot.cpp +++ b/src/HHRobot.cpp @@ -3,7 +3,8 @@ #include "HHBase.h" HHRobot::HHRobot(): drive(new WCDrive(6,1,1,1,2,1,3,2,1,2,2,2,3)), - controlSystem(new JoystickController()), + driveStick(new Extreme3dPro(1)), + shootStick(new Extreme3dPro(2)), shooter(new HHShooter()), collector(new HHCollector()), compressorSystem(new HHCompressor()), @@ -27,8 +28,8 @@ void HHRobot::Init(){ compressorSystem->StartCompressor(); } bool HHRobot::CheckJoystickValues(){ - float x=controlSystem->GetJoystickAxis(1,"x"); - float y=controlSystem->GetJoystickAxis(1,"y"); + float x=driveStick->GetJoystickAxis("x"); + float y=driveStick->GetJoystickAxis("y"); if((-.1PutBoolValue("Joysticks Valid",true); return true; @@ -38,7 +39,7 @@ bool HHRobot::CheckJoystickValues(){ } } void HHRobot::UpdateDashboard(){ - dashboard->PutFloatValue("Shooting Power",controlSystem->GetThrottle()); + dashboard->PutFloatValue("Shooting Power",shootStick->GetThrottle()); } void HHRobot::RunAuto(){ timer->Reset(); @@ -74,6 +75,8 @@ void HHRobot::Handler(){ bool allowCompressing = true; //Periodic tasks that should be run by every loop shooter->UpdateShooterPosition(targetAngle); + driveStick->handler(); + shootStick->handler(); if(timer->Get()-lastTime>=0.5f){ // Update compressor when current time-last time is more than .5 seconds lastTime=timer->Get(); @@ -81,39 +84,39 @@ void HHRobot::Handler(){ } collector->UpdateCollector(shooter->isShooting,shooter->GetAngle()); //TODO Fix whatever the heck is wrong with this - drive->Update(6,controlSystem->GetJoystickAxis(1,"z")+controlSystem->GetJoystickAxis(1,"x"),controlSystem->GetJoystickAxis(1,"y")); + drive->Update(6,driveStick->GetJoystickAxis("z")+driveStick->GetJoystickAxis("x"),driveStick->GetJoystickAxis("y")); UpdateDashboard(); //Shooting button - if(controlSystem->GetJoystickButton(2,SHOOTER_FIRE)){ - shooter->StartShootingSequence(controlSystem->GetThrottle()); + if(shootStick->ButtonValue[1]){ + shooter->StartShootingSequence(shootStick->GetThrottle()); } //Collector button assignments - if(controlSystem->GetJoystickButton(1,COLLECTOR_INTAKE)) { + if(driveStick->GetJoystickButton(COLLECTOR_INTAKE)) { collector->CollectBall(); collectorTable->PutNumber("Current Collector Speed",1); - }else if(controlSystem->GetJoystickButton(1,COLLECTOR_OUTTAKE)) { + }else if(driveStick->GetJoystickButton(COLLECTOR_OUTTAKE)) { collectorTable->PutNumber("Current Collector Speed",-1); collector->ReleaseBall(); }else{ collectorTable->PutNumber("Current Collector Speed",0); collector->CollectorStop(); } - if(controlSystem->GetJoystickButton(1,COLLECTOR_EXTEND)){ + if(driveStick->GetJoystickButton(COLLECTOR_EXTEND)){ compressorSystem->ExtendCollector(); } - if(controlSystem->GetJoystickButton(1,COLLECTOR_RETRACT)){ + if(driveStick->GetJoystickButton(COLLECTOR_RETRACT)){ compressorSystem->RetractCollector(); } - if(controlSystem->GetJoystickButton(2,SHOOTER_ANGLE_ONE)){ + if(shootStick->GetJoystickButton(SHOOTER_ANGLE_ONE)){ targetAngle=100; } - if(controlSystem->GetJoystickButton(2,SHOOTER_ANGLE_TWO)){ + if(driveStick->GetJoystickButton(SHOOTER_ANGLE_TWO)){ targetAngle=120; } - if(controlSystem->GetJoystickButton(2,SHOOTER_ANGLE_THREE)){ + if(shootStick->GetJoystickButton(SHOOTER_ANGLE_THREE)){ targetAngle=90; } - if(controlSystem->GetJoystickButton(2,SHOOTER_ANGLE_FOUR)){ + if(shootStick->GetJoystickButton(SHOOTER_ANGLE_FOUR)){ targetAngle=130; } shooterTable->PutNumber("Target Shooter Angle",targetAngle); @@ -132,7 +135,7 @@ void HHRobot::Handler(){ // Shooting stuff shooterTable->PutNumber("Current Shooter Angle",shooter->GetAngle()); shooterTable->PutNumber("Current Shooter State",shooter->e_ShooterState); - shooterTable->PutNumber("Current Shooter Power",controlSystem->GetThrottle()*100); - shooterTable->PutNumber("Current Shooter Power (raw)",controlSystem->GetThrottle() * 127.0f + 128); + shooterTable->PutNumber("Current Shooter Power",shootStick->GetThrottle()*100); + shooterTable->PutNumber("Current Shooter Power (raw)",shootStick->GetThrottle() * 127.0f + 128); } // vim: ts=2:sw=2:et diff --git a/src/HHRobot.h b/src/HHRobot.h index f37a870..d4d9c9e 100644 --- a/src/HHRobot.h +++ b/src/HHRobot.h @@ -3,8 +3,8 @@ #include #include "HHBase.h" #include "lib/drive/WCDrive.h" +#include "lib/controller/Controller.h" #include "Definitions.h" -class JoystickController; class HHShooter; class HHCollector; class HHCompressor; @@ -13,9 +13,8 @@ class HHRobot; class HHSonar; class HHRobot{ private: - Joystick *rightStick, *leftStick; WCDrive *drive; - JoystickController *controlSystem; + Extreme3dPro *driveStick, *shootStick; NetworkTable *driveTable, *shooterTable, *collectorTable; HHShooter *shooter; HHCollector *collector; diff --git a/src/Subsystems/Controller.cpp b/src/Subsystems/Controller.cpp deleted file mode 100644 index cb182dd..0000000 --- a/src/Subsystems/Controller.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "Controller.h" -JoystickController::JoystickController(){ - driveJoystick=new Joystick(JOYSTICK_RIGHT); - shootJoystick=new Joystick(JOYSTICK_LEFT); -} -float JoystickController::GetThrottle(){ - return (-GetRawJoystickAxis(2,4)+1)/2; -} -int JoystickController::GetJoystickButton(int joystick, int button){ - switch (joystick){ - case 1: - return driveJoystick->GetRawButton(button); - break; - case 2: - return shootJoystick->GetRawButton(button); - break; - default: - printf("Button from joystick %d does not exist!\n", joystick); - return -1; - break; - } -} -float JoystickController::GetRawJoystickAxis(int joystick, int axis){ - switch (joystick){ - case 1: - return driveJoystick->GetRawAxis(axis); - break; - case 2: - return shootJoystick->GetRawAxis(axis); - break; - default: - printf("Axis from joystick %d does not exist!\n", joystick); - return 999; - break; - } -} - -float JoystickController::GetJoystickAxis(int joystick, std::string axis){ - if (joystick == 1) { - if (axis == "x"){ - return driveJoystick->GetX(); - }else if (axis == "y"){ - return driveJoystick->GetY(); - }else if (axis == "z"){ - return driveJoystick->GetZ(); - }else if(axis == "throttle"){ - return driveJoystick->GetRawAxis(4); - } - }else if(joystick == 2){ - if (axis == "x"){ - return shootJoystick->GetX(); - }else if (axis == "y"){ - return shootJoystick->GetY(); - }else if (axis == "z"){ - return shootJoystick->GetZ(); - }else if(axis == "throttle"){ - return shootJoystick->GetRawAxis(4); - } - } - return 0; -} -// vim: ts=2:sw=2:et diff --git a/src/Subsystems/Controller.h b/src/Subsystems/Controller.h deleted file mode 100644 index 0434df7..0000000 --- a/src/Subsystems/Controller.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "../Definitions.h" -class JoystickController -{ - private: - Joystick *driveJoystick, *shootJoystick; - public: - JoystickController(); - float GetThrottle(); - int GetJoystickButton(int,int); - float GetRawJoystickAxis(int,int); - float GetJoystickAxis(int, std::string axis); -}; -// vim: ts=2:sw=2:et diff --git a/src/lib/controller/Controller.h b/src/lib/controller/Controller.h new file mode 100644 index 0000000..55373ac --- /dev/null +++ b/src/lib/controller/Controller.h @@ -0,0 +1,19 @@ +#include +#include "../Definitions.h" +// Logitech Extreme 3D Pro joysticks +class Extreme3dPro +{ + private: + Joystick *stickofjoy; + public: + //Array to hold joystick button values + int ButtonValue[11]; + //Call periodically to update joystick values + void handler(); + Extreme3dPro(uint8_t); + float GetThrottle(); + int GetJoystickButton(uint8_t); + float GetRawJoystickAxis(uint8_t); + float GetJoystickAxis(std::string axis); +}; +// vim: ts=2:sw=2:et diff --git a/src/lib/controller/Extreme3dPro.cpp b/src/lib/controller/Extreme3dPro.cpp new file mode 100644 index 0000000..7b4aac2 --- /dev/null +++ b/src/lib/controller/Extreme3dPro.cpp @@ -0,0 +1,32 @@ +#include "Controller.h" +Extreme3dPro::Extreme3dPro(uint8_t port){ + stickofjoy = new Joystick(port); +} +float Extreme3dPro::GetThrottle(){ + return (-GetRawJoystickAxis(4)+1)/2; +} +int Extreme3dPro::GetJoystickButton(uint8_t button){ + return stickofjoy->GetRawButton(button); +} +float Extreme3dPro::GetRawJoystickAxis(uint8_t axis){ + return stickofjoy->GetRawAxis(axis); +} +float Extreme3dPro::GetJoystickAxis(std::string axis){ + if (axis == "x"){ + return stickofjoy->GetX(); + }else if (axis == "y"){ + return stickofjoy->GetY(); + }else if (axis == "z"){ + return stickofjoy->GetZ(); + }else if(axis == "throttle"){ + return stickofjoy->GetRawAxis(4); + }else{ + return 0; + } +} +void Extreme3dPro::handler(){ + for(int i=0;i<=11;i++){ + ButtonValue[i] = stickofjoy->GetRawButton(i); + } +} +// vim: ts=2:sw=2:et