ART v1.0-alpha
A Robot Template that raises the floor for VRC teams
Loading...
Searching...
No Matches
SmartDrive.cpp
Go to the documentation of this file.
1
16#include "ART/SmartDrive.h"
17
18namespace art
19{
20
21 SmartDrive::SmartDrive(TankDrive drive, vex::inertial inert) : TankDrive(drive), m_inert(inert)
22 {
23 m_inert.calibrate();
24 }
25
27 {
28 m_wheelSize = size;
29 return *this;
30 }
31
33 {
34 m_gearRatio = ratio;
35 return *this;
36 }
37
38 SmartDrive &SmartDrive::withHorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio)
39 {
40 m_tracker = HorizontalTracker(rotation, wheelSize, gearRatio);
41 return *this;
42 }
43
44 SmartDrive &SmartDrive::withHorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio, Length wheelOffset)
45 {
46 m_tracker = HorizontalTracker(rotation, wheelSize, gearRatio, wheelOffset);
47 return *this;
48 }
49
52 m_tracker.m_rotation->setPosition(0, vex::deg);
53 }
54 while (m_inert.isCalibrating())
55 {
56 vex::wait(5, vex::msec);
57 }
58
59 while (true)
60 {
61 static Angle prevDir;
62 if (m_dir != prevDir)
63 {
64 m_inert.setHeading(m_dir.degrees(), vex::deg);
65 }
66 m_dir = Degrees(m_inert.heading(vex::degrees));
67 prevDir = m_dir;
68
69 Angle wheelTravel = Angle((getLeftTravel() + getRightTravel()) / 2.0);
70 Length travel = Length(wheelTravel.revolutions() * getWheelTravel());
71
72 // Distance travel = Inches(change*((3.25 * 3.1415)/360.f) / (72.f/48.f));
73 Vec2 posChange = Vec2::dirAndMag(m_dir, travel);
74
75 if (m_tracker.m_rotation != nullptr)
76 {
77 Length hTravel = m_tracker.getTravel();
78
79 Vec2 trackerTravel = Vec2::dirAndMag(m_dir + Degrees(90), hTravel);
80
81 posChange = posChange + trackerTravel;
82 }
83
84 m_pos = m_pos + posChange;
86
87 vex::wait(20, vex::msec);
88 }
89 }
90
91 void SmartDrive::driveFor(Length target, double speed){
92
93 Angle offset = Degrees((m_left.position(vex::degrees) + m_right.position(vex::degrees)) / 2.f);
94 Angle targetRot = Revolutions(target / getWheelTravel()) + offset;
95
96 int dir = std::abs(target)/target;
97
98 Angle pos = offset;
99
100 while ((targetRot - pos) * dir > 0)
101 {
102 pos = Degrees((m_left.position(vex::degrees) + m_right.position(vex::degrees)) / 2.f);
103
104 arcade(speed, 0);
105
106 wait(20, vex::msec);
107 }
108
109 }
111
112 double out= 0;
113
114 Angle offset = Degrees((m_left.position(vex::degrees) + m_right.position(vex::degrees)) / 2.f);
115 Angle targetRot = Revolutions(target / getWheelTravel()) + offset;
116
118 while (!m_driveForPID.isCompleted())
119 {
120 Angle pos = Degrees((m_left.position(vex::degrees) + m_right.position(vex::degrees)) / 2.f);
121 out = m_driveForPID.calculate(targetRot - pos);
122
123 arcade(out, 0);
124
125 wait(20, vex::msec);
126 }
127 arcade(0, 0, 0);
128 }
130 m_driveForPID = pid;
131 return *this;
132 }
133
134 void SmartDrive::turnFor(Angle target, double speed){
135
136 Angle targetAngle = Degrees(target.degrees() + m_inert.rotation(vex::degrees));
137
138 int dir = double(std::abs(target) / target);
139
140 while (Degrees(targetAngle.degrees() - m_inert.rotation(vex::degrees)) * dir > 0)
141 {
142 Angle error = Degrees(targetAngle.degrees() - m_inert.rotation(vex::degrees));
143
144 arcade(0, speed);
145
146 wait(20, vex::msec);
147 }
148 arcade(0, 0, 0);
149 }
151
152 double out = 0;
153
154 Angle targetAngle = Degrees(m_inert.rotation(vex::degrees) + target.degrees());
155
157 while (!m_turnForPID.isCompleted())
158 {
159 Angle error = Degrees(targetAngle.degrees() - m_inert.rotation(vex::degrees));
160
161 out = m_turnForPID.calculate(error);
162
163 arcade(0, out);
164
165 wait(20, vex::msec);
166 }
167 arcade(0, 0, 0);
168 }
170 m_turnForPID = pid;
171 return *this;
172 }
173
174 void SmartDrive::turnTo(Angle target, double speed){
175
176 Angle error = shortestTurnPath(Degrees(target.degrees() - m_inert.heading(vex::degrees)));
177 int errorSign = std::abs(error)/error;
178
179 while (!(std::abs(error.degrees()) < 10) || errorSign == std::abs(error)/error)
180 {
181 error = shortestTurnPath(Degrees(target.degrees() - m_inert.heading(vex::degrees)));
182
183 arcade(0, speed);
184
185 if((std::abs(error.degrees()) < 5)){
186 arcade(0, 0, 0);
187 return;
188 }
189
190 errorSign = std::abs(error)/error;
191 wait(20, vex::msec);
192 }
193 arcade(0, 0, 0);
194 }
196
197 double out = 0;
198
200 while (!m_turnToPID.isCompleted())
201 {
202 Angle error = shortestTurnPath(Degrees(target.degrees() - m_inert.heading(vex::degrees)));
203
204 out = m_turnToPID.calculate(error);
205
206 arcade(0, out);
207
208 wait(20, vex::msec);
209 }
210 arcade(0, 0, 0);
211 }
213 m_turnToPID = pid;
214 return *this;
215 }
216
220
222 SmartDrive::HorizontalTracker::HorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio) : m_wheelSize(wheelSize), m_gearRatio(gearRatio){
223 m_rotation = std::make_shared<vex::rotation>(rotation);
224 }
225 SmartDrive::HorizontalTracker::HorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio, Length wheelOffset) : m_wheelSize(wheelSize), m_gearRatio(gearRatio), m_offset(wheelOffset){
226 m_rotation = std::make_shared<vex::rotation>(rotation);
227 }
229 Angle currentAngle = Degrees(m_rotation->position(vex::degrees));
230
231 m_travelAngle = Angle(currentAngle - m_lastAngle);
232 m_lastAngle = currentAngle;
233
234 m_travelDistance = Length(m_travelAngle * (m_wheelSize / 2.0) * m_gearRatio);
235
236 return m_travelDistance;
237 }
238
240 Angle currentAngle = Degrees(m_left.position(vex::degrees));
241
242 Angle tempAngle = Angle(currentAngle - m_LastLeftPos);
243 m_LastLeftPos = currentAngle;
244
245 return tempAngle;
246 }
248 Angle currentAngle = Degrees(m_right.position(vex::degrees));
249
250 Angle tempAngle = Angle(currentAngle - m_LastRightPos);
251 m_LastRightPos = currentAngle;
252
253 return tempAngle;
254 }
255
256} // namespace art
Header containing the SmartDrive class.
A Utility Unit class for Angles.
Definition Units.h:275
double revolutions()
Returns the Angle in revolutions.
Definition Units.cpp:182
double degrees()
Returns the Angle in degrees.
Definition Units.cpp:170
A Utility Unit class for Length.
Definition Units.h:37
A general use PID class.
Definition PID.h:59
double calculate(double error)
Runs the PID by providing an input, then calculates and returns an output.
Definition PID.cpp:94
bool isCompleted()
Gets the current state of the PID.
Definition PID.cpp:127
void reset()
Resets the PID object.
Definition PID.cpp:25
A Smarter Drive Class.
Definition SmartDrive.h:48
void turnForPID(Angle target)
Turns for a specified angle (and stops)
void turnToPID(Angle target)
Turns tp a specified angle (and stops)
SmartDrive & withTurnForPID(PID pid)
Sets the PID object for the turnForPID Method.
Angle m_LastLeftPos
The last angle read from the Left motors.
Definition SmartDrive.h:675
Angle m_dir
Stores the current heading of the robot.
Definition SmartDrive.h:621
PID m_driveForPID
The PID object for the driveFor Method.
Definition SmartDrive.h:246
HorizontalTracker m_tracker
Stores the Horizontal Tracker.
Definition SmartDrive.h:592
Angle getRightTravel()
Gets the Angle traveled by the left motors.
SmartDrive & withTurnToPID(PID pid)
Sets the PID object for the turnToPID Method.
Vec2 m_pos
Stores the Position of the robot.
Definition SmartDrive.h:602
double m_gearRatio
A double representing the gear ratio of the drivetrain.
Definition SmartDrive.h:658
SmartDrive(TankDrive drive, vex::inertial inert)
Construct a new Smart Drive object.
Angle getLeftTravel()
Gets the Angle traveled by the left motors.
Angle m_LastRightPos
The last angle read from the Right motors.
Definition SmartDrive.h:692
Length getWheelTravel()
Get the Travel of the wheels for one revolution of the motor.
SmartDrive & withGearRatio(double ratio)
Sets the gear ratio of the drivetrain.
SmartDrive & withWheelSize(Length size)
Sets the wheel size of the Smart Drive.
int track()
Continuously tracks and updates the position of the SmartDrive.
vex::inertial m_inert
Stores the inertial sensor object.
Definition SmartDrive.h:631
Length m_wheelSize
A Length representing the diameter of the wheel.
Definition SmartDrive.h:645
PID m_turnForPID
The PID object for the turnForPID Method.
Definition SmartDrive.h:326
SmartDrive & withHorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio)
Add Horizontal Tracker to the SmartDrive.
Vec2 m_centerPos
Stores the Center Position of the robot.
Definition SmartDrive.h:610
void turnFor(Angle target, double speed)
Turns for a specified Angle.
SmartDrive & withDriveForPID(PID pid)
Sets the PID object for the driveForPID Method.
PID m_turnToPID
The PID object for the turnToPID Method.
Definition SmartDrive.h:412
void turnTo(Angle target, double speed)
Turns to a specified field-centric Angle.
void driveFor(Length target, double speed)
Drives for a specified distance.
void driveForPID(Length target)
Drives for a specified distance (and stops)
A Simple TankDrive class.
Definition TankDrive.h:55
SimpleMotorGroup m_left
a SimpleMotorGroup for the left side
Definition TankDrive.h:164
void arcade(double x, double y, double rot)
Commands the TankDrive using arcade inputs.
Definition TankDrive.cpp:22
SimpleMotorGroup m_right
a SimpleMotorGroup for the right side
Definition TankDrive.h:174
Definition PID.h:20
Angle Degrees(double degrees)
Constructs an Angle from Degrees.
Definition Units.cpp:120
Angle shortestTurnPath(const Angle target)
Returns the shortest turn path to reach the target angle.
Definition Units.cpp:188
Angle Revolutions(double revolutions)
Constructs an Angle from Revolutions.
Definition Units.cpp:134
A struct for tracking the robot's lateral displacement.
Definition SmartDrive.h:463
HorizontalTracker()
Construct an empty Horizontal Tracker object.
Length getTravel()
Get the distance traveled by the Horizontal Tracker.
Length m_offset
Stores a Length representing the vertical distance from horizontal tracker to the tracking center (po...
Definition SmartDrive.h:583
std::shared_ptr< vex::rotation > m_rotation
A Pointer to a rotation sensor object.
Definition SmartDrive.h:529
A Utility 2D Vector class.
Definition Vec2.h:39
static Vec2 dirAndMag(double dir, double mag)
Constructs a vector with the specified direction and magnitude.
Definition Vec2.cpp:104