Skip to content

Commit

Permalink
Refactor control, remake controlManual to controlTorque
Browse files Browse the repository at this point in the history
  • Loading branch information
okalachev committed Dec 13, 2023
1 parent 5af8a68 commit 5cd7713
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
27 changes: 10 additions & 17 deletions flix/control.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ void control()
if (mode == STAB) {
controlAttitude();
controlRate();
controlTorque();
} else if (mode == ACRO) {
controlRate();
controlTorque();
} else if (mode == MANUAL) {
controlManual();
controlTorque();
}
}

Expand Down Expand Up @@ -95,6 +97,11 @@ void interpretRC()
-controls[RC_CHANNEL_PITCH] * MAX_TILT,
attitudeTarget.getYaw());
ratesTarget.z = controls[RC_CHANNEL_YAW] * YAWRATE_MAX;

} else if (mode == MANUAL) {
// passthrough mode
yawMode = YAW_RATE;
torqueTarget = Vector(controls[RC_CHANNEL_ROLL], -controls[RC_CHANNEL_PITCH], controls[RC_CHANNEL_YAW]) * 0.01;
}

if (yawMode == YAW_RATE || !motorsActive()) {
Expand Down Expand Up @@ -129,7 +136,6 @@ void controlAttitude()
void controlRate()
{
if (!armed) {
memset(motors, 0, sizeof(motors));
rollRatePID.reset();
pitchRatePID.reset();
yawRatePID.reset();
Expand All @@ -141,28 +147,15 @@ void controlRate()
torqueTarget.x = rollRatePID.update(ratesTarget.x - ratesFiltered.x, dt); // un-normalized "torque"
torqueTarget.y = pitchRatePID.update(ratesTarget.y - ratesFiltered.y, dt);
torqueTarget.z = yawRatePID.update(ratesTarget.z - ratesFiltered.z, dt);

motors[MOTOR_FRONT_LEFT] = thrustTarget + torqueTarget.y + torqueTarget.x - torqueTarget.z;
motors[MOTOR_FRONT_RIGHT] = thrustTarget + torqueTarget.y - torqueTarget.x + torqueTarget.z;
motors[MOTOR_REAR_LEFT] = thrustTarget - torqueTarget.y + torqueTarget.x + torqueTarget.z;
motors[MOTOR_REAR_RIGHT] = thrustTarget - torqueTarget.y - torqueTarget.x - torqueTarget.z;

motors[0] = constrain(motors[0], 0, 1);
motors[1] = constrain(motors[1], 0, 1);
motors[2] = constrain(motors[2], 0, 1);
motors[3] = constrain(motors[3], 0, 1);
}

// passthrough mode
void controlManual()
void controlTorque()
{
if (controls[RC_CHANNEL_THROTTLE] < 0.1) {
if (!armed) {
memset(motors, 0, sizeof(motors));
return;
}

torqueTarget = ratesTarget * 0.01;

motors[MOTOR_FRONT_LEFT] = thrustTarget + torqueTarget.y + torqueTarget.x - torqueTarget.z;
motors[MOTOR_FRONT_RIGHT] = thrustTarget + torqueTarget.y - torqueTarget.x + torqueTarget.z;
motors[MOTOR_REAR_LEFT] = thrustTarget - torqueTarget.y + torqueTarget.x + torqueTarget.z;
Expand Down
2 changes: 1 addition & 1 deletion gazebo/flix.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void signalizeHorizontality();
void control();
void interpretRC();
void controlAttitude();
void controlManual();
void controlRate();
void controlTorque();
void showTable();
bool motorsActive();
void cliTestMotor(uint8_t n);
Expand Down

0 comments on commit 5cd7713

Please sign in to comment.