Skip to content

Commit aa04c98

Browse files
committed
Routing and HDrive fixes
1 parent e761f51 commit aa04c98

File tree

9 files changed

+96
-62
lines changed

9 files changed

+96
-62
lines changed

include/WPID/Chassis/Chassis.h

-12
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ class Chassis {
4242
float straight_offset = 0;
4343
float turn_offset = 0;
4444

45-
/**
46-
* Maximum acceleration for ramp
47-
*/
48-
float max_acceleration = 1;
49-
5045
/**
5146
* The units used for distances
5247
*/
@@ -133,13 +128,6 @@ class Chassis {
133128
*/
134129
virtual void setBrakeType(vex::brakeType type) = 0;
135130

136-
/**
137-
* @brief Set the max acceleration of the chassis.
138-
* The value is arbitrary, between 0 and 1.
139-
* @param max_accel an arbitrary value to increment to ramp the speed up
140-
*/
141-
virtual void setMaxAcceleration(float max_accel) = 0;
142-
143131
/**
144132
* @brief Set the measurement units for chassis values.
145133
* @param preferred_units the user's measurement system

include/WPID/Chassis/HDrive.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class HDrive : public wpid::Tank {
209209
* Increase the value if the robot is ramping too slow, or decrease if it causes too much jerk.
210210
* @param max_accel a value to increment to ramp the speed up in velocityUnits::pct
211211
*/
212-
void setMaxAcceleration(float max_accel);
212+
void setMaxAcceleration(float straight_max_accel, float c_max_accel);
213213

214214
/**
215215
* @brief Set the offset for the straight and turn functions.

include/WPID/Chassis/Tank.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Tank : public wpid::Chassis{
130130
* Increase the value if the robot is ramping too slow, or decrease if it causes too much jerk.
131131
* @param max_accel a value to increment to ramp the speed up in velocityUnits::pct
132132
*/
133-
void setMaxAcceleration(float max_accel) override;
133+
void setMaxAcceleration(float max_accel);
134134

135135
/**
136136
* @brief Set the offset for the straight and turn functions.

include/init.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace vex;
1010
extern brain Brain;
1111
extern controller* con;
1212

13-
extern Tank* chassis;
13+
extern HDrive* chassis;
1414
extern Mechanism* fourbar;
1515

1616
void init(void);

src/WPID/Chassis/HDrive.cpp

+32-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void HDrive::setTurnPID(PID pid){
3434

3535
void HDrive::setStrafePID(PID pid){
3636
pidStrafe = pid;
37+
center->setPID(pidStrafe);
3738
}
3839

3940
void HDrive::spin(int left_velocity, int right_velocity, int center_velocity){
@@ -53,9 +54,14 @@ void HDrive::straight(float distance, int max_speed){
5354

5455
void HDrive::straightAsync(float distance, int max_speed){
5556
distance = Conversion::standardize(distance, this->measure_units);
56-
float target = ((distance + straight_offset) / wheel_circumference) * 360.0;
57-
left->setPID(pidStraight);
58-
right->setPID(pidStraight);
57+
if(distance > 0){
58+
distance += straight_offset;
59+
} else {
60+
distance -= straight_offset;
61+
}
62+
float target = ((distance) / wheel_circumference) * 360.0;
63+
left->setPID(pidStraight.copy());
64+
right->setPID(pidStraight.copy());
5965
this->spinToTarget(target, target, 0, max_speed, max_speed, 0);
6066
}
6167

@@ -65,9 +71,14 @@ void HDrive::turn(int target_angle, int max_speed){
6571
}
6672

6773
void HDrive::turnAsync(float target_angle, int max_speed){
68-
float target = ((track_width/2)*((float)(target_angle+turn_offset)*M_PI/180)/wheel_circumference)*360;
69-
left->setPID(pidTurn);
70-
right->setPID(pidTurn);
74+
if(target_angle > 0){
75+
target_angle += turn_offset;
76+
} else {
77+
target_angle -= turn_offset;
78+
}
79+
float target = ((track_width/2)*((float)(target_angle)*M_PI/180)/wheel_circumference)*360;
80+
left->setPID(pidTurn.copy());
81+
right->setPID(pidTurn.copy());
7182
this->spinToTarget(target, -target, 0, max_speed, max_speed, 0);
7283
}
7384

@@ -78,7 +89,12 @@ void HDrive::strafe(float distance, int max_speed){
7889

7990
void HDrive::strafeAsync(float distance, int max_speed){
8091
distance = Conversion::standardize(distance, this->measure_units);
81-
float target = ((distance + strafe_offset) / center_wheel_circumference) * 360.0;
92+
if(distance > 0){
93+
distance += strafe_offset;
94+
} else {
95+
distance -= strafe_offset;
96+
}
97+
float target = ((distance) / center_wheel_circumference) * 360.0;
8298
this->spinToTarget(0, 0, target, 0, 0, max_speed);
8399
}
84100

@@ -99,9 +115,9 @@ void HDrive::diagonalAsync(float straight_distance, float strafe_distance, int s
99115
}
100116

101117
void HDrive::spinToTarget(float left_target, float right_target, float center_target, int l_max_spd, int r_max_spd, int c_max_spd){
102-
left->moveRelative(left_target, l_max_spd);
103-
right->moveRelative(right_target, r_max_spd);
104-
center->moveRelative(center_target, c_max_spd);
118+
left->moveRelativeAsync(left_target, l_max_spd);
119+
right->moveRelativeAsync(right_target, r_max_spd);
120+
center->moveRelativeAsync(center_target, c_max_spd);
105121
}
106122

107123
void HDrive::stop(){
@@ -146,8 +162,12 @@ void HDrive::setOffset(float straight, float turn, float center){
146162
strafe_offset = center;
147163
}
148164

149-
void HDrive::setMaxAcceleration(float max_accel){
150-
this->max_acceleration = max_accel;
165+
void HDrive::setMaxAcceleration(float straight_max_accel, float c_max_accel){
166+
if(straight_max_accel < 0 || c_max_accel < 0)
167+
LOG(WARN) << "Negative accelerations not allowed";
168+
this->left->setMaxAcceleration(straight_max_accel);
169+
this->right->setMaxAcceleration(straight_max_accel);
170+
this->center->setMaxAcceleration(c_max_accel);
151171
}
152172

153173
void HDrive::setTimeout(int timeout){

src/WPID/Chassis/Tank.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ void Tank::setOffset(float straight, float turn){
111111
void Tank::setMaxAcceleration(float max_accel){
112112
if(max_accel < 0)
113113
LOG(WARN) << "Negative accelerations not allowed";
114-
this->max_acceleration = max_accel;
115114
this->left->setMaxAcceleration(max_accel);
116115
this->right->setMaxAcceleration(max_accel);
117116
}

src/auton.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ int printAngle(){
55
while(1){
66
Brain.Screen.clearScreen();
77
Brain.Screen.setCursor(1, 1);
8-
Brain.Screen.print("Angle: %f", fourbar->getPosition(deg));
8+
//Brain.Screen.print("Angle: %f", fourbar->getPosition(deg));
99
this_thread::sleep_for(25);
1010
}
1111
return 0;
1212
}
1313

1414
void auton(){
15-
thread printAngleThread = thread(printAngle);
16-
// chassis->straight(24, 75);
17-
// chassis->straight(-24, 75);
18-
// chassis->turn(90, 25);
19-
// chassis->turn(-90, 25);
20-
fourbar->moveAbsolute(60, 75);
21-
fourbar->moveAbsolute(90, 75);
22-
fourbar->moveAbsolute(0, 75);
15+
//thread printAngleThread = thread(printAngle);
16+
// fourbar->moveAbsolute(60, 70);
17+
// fourbar->moveAbsolute(110, 60);
18+
// fourbar->moveAbsolute(0, 70);
19+
20+
chassis->strafe(24,40);
21+
chassis->turn(90,35);
22+
chassis->diagonal(-24, -24, 40);
23+
chassis->turn(-90,35);
24+
chassis->straight(-24,40);
2325
}

src/init.cpp

+26-14
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33
brain Brain;
44
controller* con = new controller(controllerType::primary);
55

6-
Tank* chassis;
7-
Mechanism* fourbar;
6+
HDrive* chassis;
7+
//Mechanism* fourbar;
88

99
motor leftFront = motor(PORT17, ratio18_1, false);
1010
motor leftBack = motor(PORT18, ratio18_1, false);
11+
motor center = motor(PORT16, ratio18_1, true);
1112

1213
motor rightFront = motor(PORT19, ratio18_1, true);
1314
motor rightBack = motor(PORT20, ratio18_1, true);
1415

1516
motor_group leftGroup = motor_group(leftFront, leftBack);
1617
motor_group rightGroup = motor_group(rightFront, rightBack);
18+
motor_group centerGroup = motor_group(center);
1719

18-
motor mech = motor(PORT11, ratio18_1, false);
19-
motor_group mechGroup = motor_group(mech);
20+
//motor mech = motor(PORT11, ratio18_1, false);
21+
//motor_group mechGroup = motor_group(mech);
2022

2123
void init(void) {
2224
Brain.Screen.clearScreen();
23-
chassis = new Tank(12.5, 1.625, &leftGroup, &rightGroup, 1);
24-
chassis->setOffset(0, 5);
25+
chassis = new HDrive(12.5, 1.625, 1.625, &leftGroup, &rightGroup, &centerGroup, 1);
26+
chassis->setOffset(0, 3, 0);
2527
chassis->setBrakeType(brakeType::brake);
26-
chassis->setMaxAcceleration(2);
28+
chassis->setMaxAcceleration(2, 2);
2729

2830
PID straight = PID(0.2, 0.65, 0.02);
2931
straight.setBias(0);
@@ -32,7 +34,7 @@ void init(void) {
3234
straight.setDelayTime(20);
3335
straight.setErrorRange(2);
3436

35-
PID turn = PID(0.15, 0.65, 0.02);
37+
PID turn = PID(0.15, 0.7, 0.01);
3638
turn.setBias(0);
3739
turn.setMaxIntegral(8);
3840
turn.setLowSpeedThreshold(3);
@@ -41,15 +43,25 @@ void init(void) {
4143

4244
chassis->setStraightPID(straight);
4345
chassis->setTurnPID(turn);
46+
chassis->setMeasurementUnits(Conversion::measurement::in);
47+
48+
PID strafe = PID(0.3, 0.67, 0.02);
49+
strafe.setBias(0);
50+
strafe.setMaxIntegral(10);
51+
strafe.setLowSpeedThreshold(5);
52+
strafe.setDelayTime(20);
53+
strafe.setErrorRange(2);
54+
55+
chassis->setStrafePID(strafe);
4456

4557
// Fourbar setup
46-
fourbar = new Mechanism(&mechGroup, 0.25);
47-
fourbar->setBrakeType(brakeType::hold);
48-
fourbar->setMaxAcceleration(5);
49-
fourbar->setBounds(0, 110);
58+
// fourbar = new Mechanism(&mechGroup, 0.25);
59+
// fourbar->setBrakeType(brakeType::hold);
60+
// fourbar->setMaxAcceleration(5);
61+
// fourbar->setBounds(0, 110);
5062

51-
PID lift = PID(1.3, 0.7, 0.02);
52-
fourbar->setPID(lift);
63+
// PID lift = PID(1.5, 0.7, 0.02);
64+
// fourbar->setPID(lift);
5365

5466
LOG().setBaseLevel(DEBUG);
5567
LOG(INFO) << "Robot Initialized";

src/usercontrol.cpp

+24-11
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,40 @@ double scale(double x) {
77
return (x > 0 ? 100 : -100) * ((x/127) * (x/127));
88
}
99

10+
int printInDriverControl(){
11+
while(1){
12+
Brain.Screen.clearScreen();
13+
Brain.Screen.setCursor(30, 30);
14+
Brain.Screen.print("In DRIVER CONTROL mode");
15+
this_thread::sleep_for(25);
16+
}
17+
return 0;
18+
}
19+
1020
void usercontrol(){
1121
auton();
12-
Brain.Screen.printAt(1, 1, "IN DRIVER CONTROL");
22+
thread printAngleThread = thread(printInDriverControl);
23+
//Brain.Screen.printAt(1, 1, "IN DRIVER CONTROL");
1324
// axis values on controller
14-
double axis3, axis4 = 0;
25+
double axis1, axis3, axis4= 0;
1526
while (1) {
1627
axis3 = scale(con->Axis3.value());
1728
axis4 = scale(con->Axis4.value());
29+
axis1 = scale(con->Axis1.value());
30+
1831

19-
if(con->ButtonR1.pressing()){
20-
fourbar->spin(20);
21-
} else if (con->ButtonR2.pressing()) {
22-
fourbar->spin(-20);
23-
} else {
24-
fourbar->stop();
25-
}
32+
// if(con->ButtonR1.pressing()){
33+
// fourbar->spin(20);
34+
// } else if (con->ButtonR2.pressing()) {
35+
// fourbar->spin(-20);
36+
// } else {
37+
// fourbar->stop();
38+
// }
2639

2740
if(con->ButtonDown.pressing()){
28-
chassis->spin(-20);
41+
chassis->spin(-20,0);
2942
} else {
30-
chassis->spin(axis3 + axis4, axis3 - axis4);
43+
chassis->spin(axis3 + axis4, axis3 - axis4, axis1);
3144
}
3245

3346
wait(20, msec);

0 commit comments

Comments
 (0)