forked from FRC1296/RHSRobot2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drivetrain.cpp
97 lines (78 loc) · 1.98 KB
/
Drivetrain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* The Drivetrain component class handles driving related functionality.
*/
//Local
#include "Drivetrain.h" //For the local header file
//Robot
#include "ComponentBase.h"
#include "RobotParams.h"
//Built-In
#include <math.h>
#include <assert.h>
#include <string>
#include <iostream>
using namespace std;
Drivetrain::Drivetrain() :
ComponentBase(DRIVETRAIN_TASKNAME, DRIVETRAIN_QUEUE,
DRIVETRAIN_PRIORITY) {
leftMotor = new CANTalon(CAN_DRIVETRAIN_LEFT_MOTOR);
rightMotor = new CANTalon(CAN_DRIVETRAIN_RIGHT_MOTOR);
wpi_assert(leftMotor && rightMotor);
leftMotor->SetControlMode(CANSpeedController::kPercentVbus);
rightMotor->SetControlMode(CANSpeedController::kPercentVbus);
leftMotor->SetVoltageRampRate(120.0);
rightMotor->SetVoltageRampRate(120.0);
gyro = new ADXRS453Z;
wpi_assert(gyro);
gyro->Start();
pTask = new Task(DRIVETRAIN_TASKNAME, (FUNCPTR) &Drivetrain::StartTask,
DRIVETRAIN_PRIORITY, DRIVETRAIN_STACKSIZE);
wpi_assert(pTask);
pTask->Start((int)this);
}
Drivetrain::~Drivetrain() //Destructor
{
delete(pTask);
delete leftMotor;
delete rightMotor;
}
void Drivetrain::OnStateChange() //Handles state changes
{
switch (localMessage.command) {
case COMMAND_ROBOT_STATE_AUTONOMOUS:
leftMotor->Set(0.0);
rightMotor->Set(0.0);
break;
case COMMAND_ROBOT_STATE_TEST:
leftMotor->Set(0.0);
rightMotor->Set(0.0);
break;
case COMMAND_ROBOT_STATE_TELEOPERATED:
leftMotor->Set(0.0);
rightMotor->Set(0.0);
break;
case COMMAND_ROBOT_STATE_DISABLED:
leftMotor->Set(0.0);
rightMotor->Set(0.0);
break;
case COMMAND_ROBOT_STATE_UNKNOWN:
leftMotor->Set(0.0);
rightMotor->Set(0.0);
break;
default:
leftMotor->Set(0.0);
rightMotor->Set(0.0);
break;
}
}
void Drivetrain::Run() {
switch (localMessage.command) {
case COMMAND_DRIVETRAIN_DRIVE_TANK:
leftMotor->Set(pow(localMessage.params.tankDrive.left, 3));
rightMotor->Set(-pow(localMessage.params.tankDrive.right, 3));
break;
case COMMAND_SYSTEM_MSGTIMEOUT:
default:
break;
}
}