Skip to content

Commit cf5a63a

Browse files
committed
Added PID Balancing
1 parent 48718c1 commit cf5a63a

File tree

4 files changed

+49
-50
lines changed

4 files changed

+49
-50
lines changed

MATE_2019/Arduino/mainBoard/mainBoard.ino

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,13 @@
66
#include <SPI.h>
77
#include <SparkFunLSM9DS1.h>
88

9-
#include <Servo.h>
10-
119
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
1210
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
1311

1412
#define SerialConnection Serial
1513

1614
LSM9DS1 imu;
1715

18-
Servo FR;
19-
Servo BR;
20-
Servo BL;
21-
Servo FL;
22-
23-
Servo UL;
24-
Servo UR;
25-
Servo UB;
26-
27-
Servo shoulderTilt;
28-
Servo wristTilt;
29-
Servo wristTwist;
30-
3116
const int buzzer = 17;
3217
const int water1 = 20;
3318
const int water2 = 21;
@@ -54,19 +39,6 @@ void setup()
5439
imu.settings.device.mAddress = LSM9DS1_M;
5540
imu.settings.device.agAddress = LSM9DS1_AG;
5641

57-
FR.attach(27);
58-
BR.attach(14);
59-
BL.attach(15);
60-
FL.attach(16);
61-
62-
UL.attach(25);
63-
UR.attach(24);
64-
UB.attach(26);
65-
66-
// shoulderTilt.attach(9);
67-
// wristTilt.attach(10);
68-
// wristTwist.attach(11);
69-
7042
pinMode(buzzer, OUTPUT);
7143
pinMode(water4, INPUT);
7244

@@ -75,7 +47,7 @@ void setup()
7547

7648
while (!imu.begin())
7749
{
78-
delay(500);
50+
delay(100);
7951
}
8052

8153
digitalWrite(buzzer, 0);
@@ -171,7 +143,18 @@ void loop()
171143
info.toCharArray(driveCommands, COMMAND_SIZE - 1);
172144
drive(driveCommands);
173145

174-
writeString(info);
146+
writeString(":");
147+
if (roll < 10.0 && roll > 0.0)
148+
{
149+
writeString("0");
150+
}
151+
writeString(String(roll, 3));
152+
writeString(";");
153+
if (pitch < 10.0 && pitch > 0.0)
154+
{
155+
writeString("0");
156+
}
157+
writeString(String(pitch, 3));
175158

176159
digitalWrite(buzzer, 1);
177160

@@ -184,12 +167,12 @@ void loop()
184167
}
185168
}
186169

187-
// Only run if a command has been received within 25 ticks
188-
if (timer > 25)
170+
// Only run if a command has been received within 100 ticks
171+
/*if (timer > 100)
189172
{
190173
disabledCommand.toCharArray(driveCommands, COMMAND_SIZE - 1);
191174
drive(driveCommands);
192-
}
175+
}*/
193176
//Rough timer counting
194177
delay(1);
195178
digitalWrite(buzzer, 0);
@@ -247,4 +230,5 @@ void drive(char array[])
247230
Wire.write(commands[7]);
248231
Wire.write(':');
249232
Wire.endTransmission();
233+
250234
}

MATE_2019/Main/Headers/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
class Utils
77
{
88
public:
9+
static std::string charToString(const char* val, int start, int stop);
10+
911
// Converts a number from range a-b to range c-d
1012
static double convertRange(double oldMin, double oldMax, double newMin,
1113
double newMax, double oldValue);

MATE_2019/Main/Sources/Utils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#include "..\Headers\Utils.h"
22

3+
std::string Utils::charToString(const char* val, int start, int stop)
4+
{
5+
std::string s = "";
6+
for (int i = start; i <= stop; ++i)
7+
{
8+
s += val[i];
9+
}
10+
return s;
11+
}
12+
313
double Utils::convertRange(double oldMin, double oldMax, double newMin,
414
double newMax, double oldValue)
515
{

MATE_2019/Main/main.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ bool waterLeak = false;
1515
char output[MAX_DATA_LENGTH];
1616
string imu;
1717
string prevIMU;
18-
int pitch = 0; // Heading up and down
19-
int roll = 0; // Angle side to side relative to ground
20-
18+
float pitch = 0; // Angle forward to back
19+
float roll = 0; // Angle side to side
2120
double pitchSetpoint = 0.0;
2221
double rollSetpoint = 0.0;
2322

@@ -45,6 +44,10 @@ void transferData(string data)
4544

4645
bool checked = false;
4746
int i = 0;
47+
48+
pitch = atof(Utils::charToString(output, 1, 6).c_str());
49+
roll = atof(Utils::charToString(output, 8, 13).c_str());
50+
4851
for (char c : output)
4952
{
5053
cout << c;
@@ -90,15 +93,15 @@ void teleop(double FWD, double STR, double RCW)
9093
// : is verification character for arduino
9194
string data = ":";
9295

93-
// PID pitchPID(0.007, 0.0, 0.0);
94-
// pitchPID.setContinuous(false);
95-
// pitchPID.setOutputLimits(-1.0, 1.0);
96-
// pitchPID.setSetpoint(pitchSetpoint);
96+
PID pitchPID(0.07, 0.0, 0.0);
97+
pitchPID.setContinuous(false);
98+
pitchPID.setOutputLimits(-1.0, 1.0);
99+
pitchPID.setSetpoint(pitchSetpoint);
97100

98-
// PID rollPID(0.007, 0.0, 0.0);
99-
// rollPID.setContinuous(false);
100-
// rollPID.setOutputLimits(-1.0, 1.0);
101-
// rollPID.setSetpoint(rollSetpoint);
101+
PID rollPID(0.07, 0.0, 0.0);
102+
rollPID.setContinuous(false);
103+
rollPID.setOutputLimits(-1.0, 1.0);
104+
rollPID.setSetpoint(rollSetpoint);
102105

103106
// Let driver adjust angle of robot if necessary
104107
if (gamepad1.getButtonPressed(xButtons.A))
@@ -126,17 +129,17 @@ void teleop(double FWD, double STR, double RCW)
126129
// heading adjusts where front is
127130
double heading = -rad45;
128131

129-
// FR AND FL ARE SWAPPED
130-
// BR IS INVERTED?
131-
// WORKING VALUES 2/13/2020 W/ EXTENSION CORDS
132132
double FR = (-STR * sin(heading) - FWD * sin(heading) + RCW); // A
133133
double BR = (STR * cos(heading) + FWD * sin(heading) - RCW); // B
134134
double BL = (-STR * sin(heading) + FWD * cos(heading) - RCW); // C
135135
double FL = (-STR * cos(heading) + FWD * cos(heading) - RCW); // D
136136

137-
double UL = -(gamepad1.rightTrigger() - gamepad1.leftTrigger());
138-
double UR = -(gamepad1.rightTrigger() - gamepad1.leftTrigger());
139-
double UB = -(gamepad1.rightTrigger() - gamepad1.leftTrigger()) * 0.4;
137+
double UL = -(gamepad1.rightTrigger() - gamepad1.leftTrigger()) +
138+
pitchPID.getOutput(pitch) + rollPID.getOutput(roll);
139+
double UR = -(gamepad1.rightTrigger() - gamepad1.leftTrigger()) +
140+
pitchPID.getOutput(pitch) - rollPID.getOutput(roll);
141+
double UB = 0.4 * (-(gamepad1.rightTrigger() - gamepad1.leftTrigger()) -
142+
pitchPID.getOutput(pitch));
140143

141144
double* vals[] = {&FR, &BR, &BL, &FL, &UL, &UR, &UB};
142145

0 commit comments

Comments
 (0)