Centralize definitions of mathematical and physical constants
See original GitHub issueCurrently, I’ve seen π or some approximation to it be defined in 5 files. This can be shown with the output of grep -r '3\.14' $OPENPILOT_TOPLEVEL
:
./selfdrive/car/ford/carcontroller.py: curvature = self.vehicle_model.calc_curvature(actuators.steerAngle*3.1415/180., CS.out.vEgo)
./selfdrive/car/mock/interface.py:LPG = 2 * 3.1415 * YAW_FR * TS / (1 + 2 * 3.1415 * YAW_FR * TS)
./selfdrive/controls/lib/lateral_mpc/generator.cpp:#define PI 3.1415926536
./selfdrive/modeld/thneed/include/msm_kgsl.h: * are cherry picked into other trees out of order so it is frozen as of 3.14.
./selfdrive/locationd/ublox_msg.cc: double gpsPi = 3.1415926535898;
./selfdrive/locationd/test/ephemeris.py: gpsPi = 3.1415926535898
Something similar happens with the gravitational acceleration constant g (often defined as 9.81 or even as 10) and probably with other constants as well. Naturally, this occurs in python source code as much as in c++ code.
I propose that all mathematical and physical constants be defined in a central location. It may be defined in a language-agnostic format and have language-specific files generated at build time for inclusion in source code, e.g. with m4. My proposal is that this file be located in common/constants.txt
and the make procedure can generate therefrom constants.py
and constants.h
.
What are other opinions in regard to this? It’s not a big problem, probably not a problem at all – I don’t think it could be causing any bugs. It can be solved pretty effortlessly though, and I’m willing to implement it.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
If we had tens or hundreds of physical constants it would probably make sense to add something like that. But don’t think it’s worth the complexity.
I would merge something that replaces all the instances of PI with
math.pi
andM_PI
though!Oh, I had a feeling about that one. Thanks for letting me know. I’ve updated my branch, I’ll make a pull request.