Skip to content

Commit 3349514

Browse files
committed
Final updates before test flight kriswiner#4
1 parent fbffff2 commit 3349514

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

src/QuadCopterCode/controller.cpp

+14-28
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,10 @@ void CalculateControlVector(float *euler_angles, float *g, float *motor_control_
104104
float roll_angle_deg = 0;
105105
float pitch_rate_deg = 0.0;
106106
float roll_rate_deg = 0.0;
107-
float deltaF_pitch_old;
108-
float deltaF_roll_old;
109-
float moment_pitch;
110-
float moment_roll;
107+
float delta_force_pitch;
108+
float delta_force_roll;
111109
float pitch_err_rad = 0.0;
112110
float roll_err_rad = 0.0;
113-
float scale_val = 1.0; // value for scaling controller feedback at low RPM
114-
// float pitch_rate_err = 0.0;
115-
// float roll_rate_err = 0.0;
116111

117112
// Read primary throttle value from left stick
118113
float input_left_throttle = input_radio_values[INDEX_LEFT_STICK];
@@ -147,39 +142,33 @@ void CalculateControlVector(float *euler_angles, float *g, float *motor_control_
147142
// deltaF_roll = (radioRecieverVals[INDEX_RIGHT_STICK_LEFTRIGHT]*Nbar - roll*kP)*DEG2RAD + (0.0f*Nbar - roll_rate*kD)*DEG2RAD;
148143
// ------------------------------
149144

150-
// ------------------------------
151-
152-
// --- PD Controller design ---
153-
// deltaF_pitch_old = pitch_err*kP + (0.0f - pitch_rate)*DEG2RAD*kD;
154-
// deltaF_roll_old = roll_err*kP + (0.0f - roll_rate)*DEG2RAD*kD;
155-
156145
// --- PID Controller design w/ integrator limit ---
157146
// scale_val = min(1.0, (input_left_throttle / 100.0) * (input_left_throttle / 100.0));
158147
pitch_err_rad = (-input_radio_values[INDEX_RIGHT_STICK_UPDOWN] - pitch_angle_deg) * DEG2RAD;
159148
roll_err_rad = (input_radio_values[INDEX_RIGHT_STICK_LEFTRIGHT] - roll_angle_deg) * DEG2RAD;
160149

161150
// Only accrue integrator error once we're flying (so it doesn't ratchet up on the ground)
162-
if (input_left_throttle > 50)
151+
if (input_left_throttle > 70)
163152
{
164153
integrated_pitch_err_rad = constrain(integrated_pitch_err_rad * 0.999 + (pitch_err_rad * delta_time), -0.25, 0.25);
165154
integrated_roll_err_rad = constrain(integrated_roll_err_rad * 0.999 + (roll_err_rad * delta_time), -0.25, 0.25);
166155
}
167-
moment_pitch = (pitch_err_rad * kP) + ((0.0f - pitch_rate_deg) * DEG2RAD * kD) + (integrated_pitch_err_rad * kI);
168-
moment_roll = (roll_err_rad * kP) + ((0.0f - roll_rate_deg) * DEG2RAD) * kD + (integrated_roll_err_rad * kI);
169-
170-
const float force_pitch = moment_pitch / (4 * 0.0995);
171-
const float force_roll = moment_roll / (4 * 0.0995);
156+
delta_force_pitch = (pitch_err_rad * kP) + ((0.0f - pitch_rate_deg) * DEG2RAD * kD) + (integrated_pitch_err_rad * kI);
157+
delta_force_roll = (roll_err_rad * kP) + ((0.0f - roll_rate_deg) * DEG2RAD * kD) + (integrated_roll_err_rad * kI);
172158

173159
// otherwise include pitch and roll force requirements
174-
motor_control_vector[0] = thrustToMotorValNonlinear(-force_pitch - force_roll, input_left_throttle);
175-
motor_control_vector[2] = thrustToMotorValNonlinear(force_pitch + force_roll, input_left_throttle);
176-
motor_control_vector[1] = thrustToMotorValNonlinear(-force_pitch + force_roll, input_left_throttle);
177-
motor_control_vector[3] = thrustToMotorValNonlinear(force_pitch - force_roll, input_left_throttle);
160+
motor_control_vector[0] = thrustToMotorValNonlinear(-delta_force_pitch - delta_force_roll, input_left_throttle);
161+
motor_control_vector[2] = thrustToMotorValNonlinear(delta_force_pitch + delta_force_roll, input_left_throttle);
162+
motor_control_vector[1] = thrustToMotorValNonlinear(-delta_force_pitch + delta_force_roll, input_left_throttle);
163+
motor_control_vector[3] = thrustToMotorValNonlinear(delta_force_pitch - delta_force_roll, input_left_throttle);
178164
}
179165
}
180166
else
181167
{
182-
Serial.println("-------------- DEAD ------------------");
168+
if (DEBUG_MODE)
169+
{
170+
Serial.println("-------------- DEAD ------------------");
171+
}
183172

184173
// if no radio signal, power off the motors
185174
motor_control_vector[0] = 0.0;
@@ -189,12 +178,9 @@ void CalculateControlVector(float *euler_angles, float *g, float *motor_control_
189178
}
190179
}
191180

192-
if (dumb_counter > 15)
181+
if (dumb_counter > 15 && DEBUG_MODE)
193182
{
194183

195-
Serial.print(scale_val);
196-
Serial.print(" | \t");
197-
198184
// Serial.print(pitch);
199185
// Serial.print(F("\t"));
200186
Serial.print(pitch_err_rad);

src/QuadCopterCode/global_junk.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef GLOBAL_JUNK_H
77
#define GLOBAL_JUNK_H
88

9+
#define DEBUG_MODE true
10+
911
extern volatile unsigned long last_rise_time[NUM_INPUTS];
1012
extern volatile float input_radio_values[NUM_INPUTS];
1113

0 commit comments

Comments
 (0)