How does this model rocket fin Control work?

I'm creating a fin control for a model rocket using Esp32 with 4 servos connected to G33,G25,G26,G27 respectively and a MPU6050 sensor SDA to G21 and SCL to G22. I'm using the esp32 just for a better processing power.

These are the connections

This is the code I'm using

#include <Wire.h>
#include <MPU6050.h>
#include <ESP32Servo.h>

Servo servo1, servo2, servo3, servo4;
int servo1Pin = 33;  // Replace with the actual GPIO pin numbers
int servo2Pin = 25;
int servo3Pin = 26;
int servo4Pin = 27;

MPU6050 sensor;

void setup() {
  
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);
  servo4.attach(servo4Pin);

  Wire.begin();
 
  sensor.initialize();

  
}

void loop() {
  int16_t ax, ay, az, gx, gy, gz;
  sensor.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  // Map acceleration values to servo angles (0-180)
  int servoAngle1 = map(ax, -17000, 17000, 0, 180);
  int servoAngle2 = map(az, -17000, 17000, 0, 180);

  
  servo1.write(servoAngle1);
  servo2.write(servoAngle2);
  servo3.write(180 - servoAngle1);
  servo4.write(180 - servoAngle2);

  delay(10);  // Adjust delay as needed
}

I'm not able to understand how this code works because I got this code from Bing Copilot. To my surprise, this works pretty fine and the servos turn opposite accordingly to sensor's orientation.

My doubt is that why am I using ax and az which is acceleration instead of gx and gz which gives angular speed. The problem that would so appear will be that when rocket accelerates, servos will randomly rotate and totally break the trajectory.

Can somebody tell me what can i improve to be more precise?

It makes no sense to me, either. It will do something, though, and it might be interesting to watch, if you are far enough away from the rocket. For increased safety, all participants should hide behind a barrier, wearing body and head protection. :wink:

FYI an accelerometer can be used to measure pitch and roll tilt angles, as described in the tutorial below. But that only works properly if gravity provides the acceleration.
https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing

I got this code from Bing Copilot.

Ask the bot to explain why it chose such a odd approach to steer a rocket.

"bing" wants your rocket to rotate your fins backwards sideways? Wow.

I moved your topic to a more appropriate forum category @arushdwivedi11.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

so the main motive for the program is to rotate the fins in opposite direction of rocket's rotation so as to bring back the rocket to original path. For example, if the rocket rotates 30 degrees to Z axis then the fins should go -30 degrees with respect to Z axis.

Video of the thing working

Here you can see the working, I had not centred the 2 motors on side thats why they are weirdly positioned, otherwise the programs works pretty well with slow rotation speeds but there might occur problems when rotations are faster.

Your code shows maximum MPU changes (+/-90 degrees) will result in 180 degree servo changes. The video shows that (somewhat).

Only a few degrees of fin direction should be used to steer a rocket traveling at escape velocity. Large MPU changes should translate to small, gradual changes in the fin servos. Sudden or large changes in servo fin direction will break the rocket apart.

The "PID" that will control your servo fins will work like you do when you are navigating a car through a turn... you do not make a 90 degree turns, rather you slowly decrease the turning radius from the tangent until mid-point, then slowly increase the turning radius back to the tangent.