I think that the most important aspect of that code is to understand that the multiplication by 4000 is really a division.
What ! but it says _builtin_mulss(y_rate, 4000); Surely you say that is multiplication ?? Yes it is.
But look what happens in the next line .....
Note the use of ._.W1
._.W1 means that only the top 16 bits of a 32 bit word is going to be used. The bottom 16 bits of the 32 bit word are dropped.
The net result is that we have really multiplied by the fraction of (4000 / 65536) which is 0.061 .
As to the original logic for using exactly 4000 (or 0.061), that I cannot relate. But it is going to be a ratio which takes either the accelerometer value,
or the gyro value, makes it smaller, and when it added onto 3000, probably (I say probably because I have not done the detail calculations), will give a good value of between +1000 to -1000 for expected values of accel and gyros.
3000+1000 = 4000 which is maximum Servo deflection in UDB Units. UDB units are twice the microsecond pulse length. (4000 / 2 which is 2000 for maximum servo deflection)(3000 / 2 being 1500 micro seconds)..
Bill may remember the original reason for using 4000 in more detail.
I hope that helps.
Leonardo, it is super to see you making progress. Thanks for the updates.
Best wishes, Pete