DeepSpace  2019
Robot.cpp
Go to the documentation of this file.
1 #include "Robot.h"
2 
3 #include <frc/commands/Scheduler.h>
4 #include <frc/smartdashboard/SmartDashboard.h>
5 #include <iostream>
6 #include <string>
7 #include <fstream>
8 
9 #include "Utils/EdgeLight.h"
10 
11 // /home/lvuser/FRC_UserProgram.log
12 
13 // Subsystems
25 
26 // Logging
27 std::vector<std::string> logBuffer;
28 
29 void Log(std::string message){
30  logBuffer.push_back(message);
31 }
32 
33 void vector_print(std::vector<std::string> const &input)
34 {
35  for (auto const& i: input) {
36  std::cout << i << std::endl;
37  }
38 }
39 
40 void Display(clock_t start, clock_t current){
41  double duration = (double)(current - start) / CLOCKS_PER_SEC;
42  std::cout << "-- " << duration << " since boot --" << std::endl;
44  logBuffer.clear();
45 }
46 
47 
48 // Robot
50  // Start logging timer
51  this->logStart = clock();
52 
53  // Print out a banner to the shell
54  std::cout << " Welcome 5024!" << std::endl;
55  std::cout << "-------------------" << std::endl;
56  std::cout << "Robot Starting.." << std::endl;
57 
58  // Subsystems
59  Header("Creating Subsystems.. ");
60  this->m_DriveTrain = new DriveTrain();
61  this->m_CrawlDrive = new CrawlDrive();
62  this->m_Arm = new Arm();
63  this->m_Leg = new Leg();
64  this->m_Slider = new Slider();
65  this->m_Piston = new Piston();
66  this->m_oi = new OI();
67  this->m_cCompressor = new cCompressor();
68  this->m_HatchGripper = new HatchGripper();
69  this->m_Flap = new Flap();
70  this->m_Light = new Light();
71  EndHeader();
72 
73  // Init commands
74  Header("Creating Commands.. ");
75  this->pTriggerDrive = new TriggerDrive();
76  this->pPullArm = new PullArm();
77  this->pPullLeg = new PullLeg();
78  this->pControlSlider = new ControlSlider();
81  this->pControlCargo = new ControlCargo();
82  this->pControlLight = new ControlLight();
83  this->pClimbManager = new ClimbManager();
84  this->pAutoClimbHigh = new AutoClimbHigh();
85  this->pClimb = new Climb();
86  EndHeader();
87 
88  // Init camera
89  Header("Starting CameraServer.. ");
90  this->frontCam = frc::CameraServer::GetInstance()->StartAutomaticCapture("Back", CAMERASERVER_DRIVER_CAMERA);
91  this->visionCam = frc::CameraServer::GetInstance()->StartAutomaticCapture("Front", CAMERASERVER_VISION_CAMERA);
92  EndHeader();
93 
94  // Set vision cam settings
95  Header("Setting camera config.. ");
96  std::ifstream visionSettingsFile("/home/lvuser/deploy/config.json");
97  std::string visionSettings((std::istreambuf_iterator<char>(visionSettingsFile)), (std::istreambuf_iterator<char>()));
98  this->visionCam.SetConfigJson(visionSettings);
99  this->frontCam.SetConfigJson(visionSettings);
100  EndHeader();
101 
102  // Init Gyro
103  Header("Resetting Gyro.. ");
104  this->pGyro = new AHRS(frc::SPI::kMXP);
105  this->pGyro->Reset();
106  EndHeader();
107 
108  // Create Telemetry table
109  Header("Connecting to telemetry table.. ");
110  this->ntTelemetry = NetworkTable::GetTable("SmartDashboard/Telemetry");
111  EndHeader();
112 
113  // create ds and pdp objects
114  // std::cout << "Creating Driverstation and PDP objects" << std::endl;
115  // this->pdp = new frc::PowerDistributionPanel(10);
116 }
117 
127  // Send information about the robot over NetworkTables
128 
129  bool dsAttached = this->driverStation.IsDSAttached();
130  bool fmsAttached = this->driverStation.IsFMSAttached();
131  float gyroAngle = this->pGyro->GetAngle();
132 
133  this->ntTelemetry->PutBoolean("DSconn", dsAttached);
134  this->ntTelemetry->PutBoolean("FMSconn", fmsAttached);
135  this->ntTelemetry->PutNumber("Angle", gyroAngle);
136 
137  // Push logs to network
138  this->logCurrent = clock();
139  if(logBuffer.size() > 0){
140  Display(this->logStart, this->logCurrent);
141  }
142 }
143 
150  Header("Robot disabling.. ");
151  Robot::m_Flap->Release();
152 
153  // Stop controller vibration once match ends
154  if(this->pClimbManager != nullptr){
155  this->pClimbManager->pJoyOp->SetRumble(frc::GenericHID::RumbleType::kRightRumble, 0.0);
156  }
157  EndHeader();
158 }
159 
160 void Robot::DisabledPeriodic() {frc::Scheduler::GetInstance()->Run();}
161 
174  // std::string autoSelected = frc::SmartDashboard::GetString(
175  // "Auto Selector", "Default");
176  // if (autoSelected == "My Auto") {
177  // m_autonomousCommand = &m_myAuto;
178  // } else {
179  // m_autonomousCommand = &m_defaultAuto;
180  // }
181 
182  // m_autonomousCommand = m_chooser.GetSelected();
183 
184  // if (m_autonomousCommand != nullptr) {
185  // m_autonomousCommand->Start();
186  // }
187  Header("AutonomousInit starting.. ");
188  this->SharedInit();
189  EndHeader();
190 
191  Header("Resetting gyro.. ");
192  this->pGyro->Reset();
193  EndHeader();
194 }
195 
197  this->SharedPeriodic();
198  frc::Scheduler::GetInstance()->Run();
200 }
201 
203  // This makes sure that the autonomous stops running when
204  // teleop starts running. If you want the autonomous to
205  // continue until interrupted by another command, remove
206  // this line or comment it out.
207  // if (m_autonomousCommand != nullptr) {
208  // m_autonomousCommand->Cancel();
209  // m_autonomousCommand = nullptr;
210  // }
211  Header("Teleop commands starting.. ");
212  this->SharedInit();
213  EndHeader();
214 }
215 
217  this->SharedPeriodic();
218  frc::Scheduler::GetInstance()->Run();
220 }
221 
223  if (this->pTriggerDrive != nullptr) { this->pTriggerDrive->Start(); }
224  if (this->pPullArm != nullptr) { this->pPullArm->Start(); }
225  if (this->pPullLeg != nullptr) { this->pPullLeg->Start(); }
226  if (this->pControlSlider != nullptr) { this->pControlSlider->Start(); }
227  if (this->pControlCompressor != nullptr) { this->pControlCompressor->Start(); }
228  if (this->pControlHatchGripper != nullptr) { this->pControlHatchGripper->Start(); }
229  if (this->pControlCargo != nullptr) { this->pControlCargo->Start(); }
230  if (this->pControlLight != nullptr) { this->pControlLight->Start(); }
231  if (this->pClimbManager != nullptr) { this->pClimbManager->Start(); }
232 }
233 
235 
236  // Active certain commands if we want to start climbing
238  case ClimbManager::ClimbState::kInactive:
239  if (this->pTriggerDrive != nullptr)
240  this->pTriggerDrive->Start(); // Let driver drive
241  this->pClimbManager->UpdateRumble();
242  break;
243  case ClimbManager::ClimbState::kActive:
244  if (this->pTriggerDrive != nullptr) {
245  this->pTriggerDrive->Start(); // Let driver drive
246  }
247  if (this->pClimb != nullptr) {
248  this->pClimb->Start(); // Start manual climb commands
249  }
250  this->pClimbManager->UpdateRumble(); // rumble controller
251  break;
252  case ClimbManager::ClimbState::kAuto:
253  if ( ! this->pAutoClimbHigh->IsClimbing())
254  if (this->pAutoClimbHigh != nullptr) {
255  this->pAutoClimbHigh->Start(); // Start auto climb command
256  this->pClimbManager->UpdateRumble(); // rumble controller
257  }
258  break;
259  default:
260  Log("ClimbManager's climb state was invalid, setting to Inactive");
261  ClimbManager::CurrentClimbState = ClimbManager::ClimbState::kInactive;
262  break;
263  }
264 
265  if(this->driverStation.GetAlliance() == frc::DriverStation::Alliance::kBlue){
266  if(this->driverStation.IsOperatorControl()){
268  }else{
270  }
271 
272  }else if(this->driverStation.GetAlliance() == frc::DriverStation::Alliance::kRed){
273  if(this->driverStation.IsOperatorControl()){
275  }else{
277  }
278 
279  }else{
281  }
282 }
283 
285 
286 
287 #ifndef RUNNING_FRC_TESTS
288 int main(){
289  // Start the robot
290  WinGame(Robot);
291  Log("FATAL!!! Robot program finished!!!");
292  return 1;
293 }
294 #endif
bool start
Definition: __main__.py:1094
Definition: Flap.h:9
static CrawlDrive * m_CrawlDrive
Pointer for the DriveTrain.
Definition: Robot.h:65
Definition: Light.h:9
#define WinGame(_class_)
Definition: RobotMap.h:119
#define Header(x)
Definition: RobotMap.h:121
TriggerDrive * pTriggerDrive
Pointer for the TriggerDrive command.
Definition: Robot.h:75
void SharedInit()
Used to initalize commands for both auto and teleop. (2019 only)
Definition: Robot.cpp:222
ClimbManager * pClimbManager
Pointer for the climb management command.
Definition: Robot.h:83
An interface command for toggleing the gripper.
Definition: ControlLight.h:11
Definition: Robot.h:57
void Release()
Release pressure.
Definition: Flap.cpp:19
void RobotInit() override
Runs once on robot boot.
Definition: Robot.cpp:49
A subsystem that interfaces with the robot&#39;s cCompressor.
Definition: Compressor.h:10
static Arm * m_Arm
Pointer for the Arm.
Definition: Robot.h:66
An interface command for driving the robot with an xbox controller.
Definition: TriggerDrive.h:12
PullLeg * pPullLeg
Pointer for the leg debugging command.
Definition: Robot.h:77
An interface command for activating the Pull Arm on the robot.
Definition: PullLeg.h:11
static cCompressor * m_cCompressor
Pointer for the cCompressor.
Definition: Robot.h:62
static Flap * m_Flap
Pointer for the Cargo Flap.
Definition: Robot.h:71
An interface command for activating the Pull Arm on the robot.
Definition: PullArm.h:11
A subsystem that interfaces with the drivebase on the robot.
Definition: DriveTrain.h:13
A subsytem that interfaces with the climberbase on the robot.
Definition: CrawlDrive.h:11
void DisabledPeriodic() override
Runs in a loop while the robot is disabled.
Definition: Robot.cpp:160
Definition: Climb.h:11
void TestPeriodic() override
Runs in a loop during test mode.
Definition: Robot.cpp:284
AHRS * pGyro
Definition: Robot.h:115
Operator Interface allows various commands to access the xbox controllers through the DriverStation...
Definition: OI.h:7
frc::DriverStation & driverStation
DriverStation instance.
Definition: Robot.h:110
An interface command for driving the robot with an xbox controller.
Climb * pClimb
Pointer for the climb control command.
Definition: Robot.h:85
ControlHatchGripper * pControlHatchGripper
Pointer for the finger control command.
Definition: Robot.h:78
void AutonomousPeriodic() override
Runs in a loop during auto.
Definition: Robot.cpp:196
void vector_print(std::vector< std::string > const &input)
Definition: Robot.cpp:33
cs::UsbCamera frontCam
Variable for the front camera.
Definition: Robot.h:102
void RobotPeriodic() override
Runs in a loop while the robot is turned on in any mode.
Definition: Robot.cpp:126
ControlCargo * pControlCargo
Pointer for the cargo control command.
Definition: Robot.h:81
Definition: Leg.h:10
void UpdateRumble(void)
Updates whether the controller should be rumbleing or not.
AutoClimbHigh * pAutoClimbHigh
Pointer for the auto high climb command.
Definition: Robot.h:84
static Slider * m_Slider
Pointer for the Slider.
Definition: Robot.h:69
std::shared_ptr< NetworkTable > ntTelemetry
A pointer to the /SmartDashboard/Telemetry table.
Definition: Robot.h:113
bool IsClimbing()
Returns whether the robot is in the middle of climbing.
frc::XboxController * pJoyOp
Definition: ClimbManager.h:23
An interface command for driving the robot with an xbox controller.
Definition: ControlCargo.h:12
static OI * m_oi
Pointer for the Operator Interface (OI)
Definition: Robot.h:68
std::vector< std::string > logBuffer
Definition: Robot.cpp:27
PullArm * pPullArm
Pointer for the arm debugging command.
Definition: Robot.h:76
Definition: Piston.h:9
static Piston * m_Piston
Pointer for the Piston.
Definition: Robot.h:64
static HatchGripper * m_HatchGripper
Pointer for the Hatch Gripper.
Definition: Robot.h:70
void Display(clock_t start, clock_t current)
Definition: Robot.cpp:40
void AutonomousInit() override
Runs once at the start of auto. Usually while the announcer starts counting down from 3...
Definition: Robot.cpp:173
static DriveTrain * m_DriveTrain
Pointer for the DriveTrain.
Definition: Robot.h:63
void SharedPeriodic()
Runs during teleop and auto.
Definition: Robot.cpp:234
static ClimbState CurrentClimbState
Definition: ClimbManager.h:15
cs::UsbCamera visionCam
Variable for the Vision camera.
Definition: Robot.h:103
A subsystem that interfaces with the drivebase on the robot.
Definition: Slider.h:13
#define CAMERASERVER_VISION_CAMERA
Definition: RobotMap.h:27
int main()
Definition: Robot.cpp:288
void Log(std::string message)
Main robot class that is called by wpilib.
Definition: Robot.cpp:29
void DisabledInit() override
Runs once every time the robot is disabled.
Definition: Robot.cpp:149
An interface command for driving the robot with an xbox controller.
Definition: ControlSlider.h:12
void Append(LedColour colour)
Definition: EdgeLight.cpp:7
ControlSlider * pControlSlider
Pointer for the ControlSlider command.
Definition: Robot.h:79
static Leg * m_Leg
Pointer for the Leg.
Definition: Robot.h:67
static Light * m_Light
Pointer for the Light.
Definition: Robot.h:72
clock_t logStart
Definition: Robot.h:117
void TeleopInit() override
Runs once at the start of teleop when the bell sounds.
Definition: Robot.cpp:202
#define EndHeader()
Definition: RobotMap.h:122
ControlCompressor * pControlCompressor
Pointer for the ControlCompressor command.
Definition: Robot.h:80
clock_t logCurrent
Definition: Robot.h:117
void TeleopPeriodic() override
Runs in a loop during teleop.
Definition: Robot.cpp:216
An interface command for toggleing the gripper.
#define CAMERASERVER_DRIVER_CAMERA
Definition: RobotMap.h:26
ControlLight * pControlLight
Pointer for the vision light control command.
Definition: Robot.h:82
A subsystem that controls the Arm of the robot.
Definition: Arm.h:15