Is center of mass calculated automatically in ardupilot or is there some code for it?
Not calculated at all, you have to balance the craft yourself and it’s assumed the flight controller is as close to the pitch/roll centre as possible.
The are some offset values you can set but that’s getting into advanced tuning (kind of…) or at least advanced building, and by that stage you probably would have found the appropriate parameters in the documentation yourself.
In the early stages it’d be advised to keep everything as symetrical and balanced as possible.
I was thinking about you question and wondering if you meant auto-levelling.
The autopilot will try to auto-level in appropriate flight modes (all except Acro) such as Stabilse, Loiter and so on.
But as I mentioned you do need to balance your craft first as best as possible by shifting the battery and camera weight, or some motors will work harder than others potentially leading to instability or even loss of control.
During set up it’s important to get the Level Horizon calibration correct. I dont set this by leveling the flight controller itself, or by assuming the craft is sitting level on your kitchen table - I use a very light weight spirit level across the tops of the motors and chock the landing gear to get the spirit level reading correctly, check several times in multiple directions before hitting the Level button in MissionPlanner. After all it is the props and motors that need to be level in flight, even if the FC is not mounted perfectly level.