-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathShooter.cpp
More file actions
195 lines (173 loc) · 5.51 KB
/
Shooter.cpp
File metadata and controls
195 lines (173 loc) · 5.51 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Shooter.cpp
*
* Created on: Feb 13, 2016
* Author: Noah
*/
#include "Shooter.h"
#include "math.h"
#define PI 3.14159265
Shooter::Shooter(uint32_t leftTalon, uint32_t rightTalon, uint32_t angleTalon, uint32_t kickerTalon, Position *position_) :
left(leftTalon),
right(rightTalon),
aim(angleTalon),
ballSensor(Constants::shooterIRPin),
servo(Constants::servoPin),
kicker(kickerTalon),
position(position_),
accel(I2C::Port::kOnboard)
{
left.SetControlMode(CANTalon::ControlMode::kPercentVbus);
right.SetControlMode(CANTalon::ControlMode::kPercentVbus);
aim.SetControlMode(CANTalon::ControlMode::kPosition);
aim.SetFeedbackDevice(CANTalon::FeedbackDevice::AnalogPot);
aim.SetClosedLoopOutputDirection(true);
kicker.SetControlMode(CANTalon::ControlMode::kPercentVbus);
}
void Shooter::Enable()
{
left.Enable();
right.Enable();
aim.Enable();
kicker.Enable();
}
void Shooter::Disable()
{
left.Disable();
right.Disable();
aim.Disable();
kicker.Disable();
}
void Shooter::SetSpeed(float leftSpeed, float rightSpeed) //speeds are a percentage of the maximum possible speed
{
left.Set(leftSpeed);
right.Set(rightSpeed);
}
void Shooter::SetSpeed(float speed) {
left.Set(-speed);
right.Set(speed);
}
/*void Shooter::SetAngle(float angle) { //degrees
if (angle < Constants::shooterMinAngle || angle > Constants::shooterMaxAngle) {
return;
}
aim.SetControlMode(CANTalon::ControlMode::kPosition);
int potValue = (int)(Constants::potMinValue - (angle * Constants::aimDegreesToPotFactor));
aim.Set(potValue);
if (angle < Constants::shooterMinAngle || angle > Constants::shooterMaxAngle) {
return;
}
int potValue = (int)(Constants::potMinValue - (angle * Constants::aimDegreesToPotFactor));
aim.SetControlMode(CANTalon::ControlMode::kPercentVbus);
float failsafe = 0.0;
float delta_t = 0.05;
while (abs(aim.GetAnalogInRaw() - potValue) > 2.0 && failsafe < 2.0/delta_t)
{
aim.Set()
}
}*/
void Shooter::SetPotValue(int potValue) {
aim.SetControlMode(CANTalon::ControlMode::kPosition);
aim.Set(potValue);
}
void Shooter::SetAngle(float angle) { //degrees
if (angle < 32 || angle > 44) {
return;
}
aim.SetControlMode(CANTalon::ControlMode::kPosition);
int position = aim.GetAnalogInRaw();
int failsafe = 0;
int potValue = (int)(Constants::potMinValue - (angle * Constants::aimDegreesToPotFactor));
if (aim.GetAnalogInRaw() < potValue) {
while (aim.GetAnalogInRaw() < potValue && failsafe < 200) {
aim.Set(position);
position--; //depending on how the talon is wired this may need to be ++ and the other one may need to be --
failsafe++;
Wait(.01);
}
} else {
while (aim.GetAnalogInRaw() > potValue && failsafe < 200) {
aim.Set(position);
failsafe++;
position++; //depending on how the talon is wired this may need to be -- and the other one may need to be ++
Wait(.01);
}
}
//aim.SetAnalogPosition(potValue);
aim.SetControlMode(CANTalon::ControlMode::kPercentVbus);
}
/*decide if you're going to use the accelerometer or the pot.
Then make changes in Robot.cpp accordingly.
If you use the pot, you will have to recalibrate the code to accomodate for it and account for slippage.
If you use the accelerometer you will have to change things in Robot.cpp to account for method changes.
Lastly, you may need to change the value you're setting the motor to, depending on how fast it moves.
Test it out and see which one you'd rather use*/
void Shooter::SetAngleAccelerometer(float angle) {
if (angle < Constants::shooterMinAngle || angle > Constants::shooterMaxAngle) {
return;
}
aim.SetControlMode(CANTalon::ControlMode::kPercentVbus);
int position = Angle();
int failsafe = 0;
if (position < angle) {
while (position < angle && failsafe < 250) {
if (position < Constants::shooterMinAngle || position > Constants::shooterMaxAngle) {
aim.Set(0.0);
return;
}
aim.Set(.2); //may need to be adjusted && negative may be flipped
position = Angle();
failsafe++;
Wait(.01);
}
} else {
while (position > angle && failsafe < 250) {
if (position < Constants::shooterMinAngle || position > Constants::shooterMaxAngle) {
aim.Set(0.0);
return;
}
aim.Set(-.2); //may need to be adjusted && negative may need to be flipped
position = Angle();
failsafe++;
Wait(.01);
}
}
}
void Shooter::Move(float speed) {
aim.SetControlMode(CANTalon::ControlMode::kPercentVbus);
aim.Set(speed);
}
void Shooter::PrepareShooter(float angle, float speed) {
SetAngle(angle);
SetSpeed(speed);
}
void Shooter::LoadBall() {
SetSpeed(-.6);
}
void Shooter::Shoot() {
kicker.Set(-.5);
Wait(0.45);
kicker.Set(.5);
Wait(0.45);
kicker.Set(0);
}
bool Shooter::HasBall() {
return !ballSensor.Get();
}
float Shooter::WheelSpeed() {
return left.Get();
}
float Shooter::Angle() {
//return (Constants::potMinValue - aim.GetAnalogInRaw()) / Constants::aimDegreesToPotFactor; //wired backwards
//return (Constants::potMaxValue - aim.GetAnalogInRaw()) / Constants::aimDegreesToPotFactor; //wired forwards
return Roll();
}
float Shooter::Roll() {
return -(atan2(accel.GetX(),sqrt(accel.GetY()*accel.GetY()+accel.GetZ()*accel.GetZ())) * 180.0) / PI;
}
float Shooter::Pitch(){
return (atan2(accel.GetY(),sqrt(accel.GetX()*accel.GetX()+accel.GetZ()*accel.GetZ())) * 180.0) / PI;
}
float Shooter::ReadPot() {
return aim.GetAnalogInRaw();
}