-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New naming convention of variables linked to a parameter #11686
Conversation
Here is the current script: #!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import fileinput
import sys
filenames = []
for filename in sys.stdin:
filenames.append(filename.rstrip())
pattern = r'^.*<px4::params::([A-Za-z0-9_]+)>\)\s*([A-Za-z0-9_]+)[,\s]?.*$'
regex = re.compile(pattern, re.MULTILINE)
replace_dict = dict()
print("Searching for parameters...")
for filename in filenames:
print("In {} :".format(filename))
with open(filename, 'r') as f:
text = f.read()
for match in regex.finditer(text):
#print("Line: " + match.group(0) + '\t' + match.group(1) + " and " + match.group(2).lower())
replace_dict[match.group(2)] = "_param_" + match.group(1).strip().lower()
print("The following variables will be changed")
for old_var in replace_dict:
print("{} -> {}".format(old_var, replace_dict[old_var]))
for filename in filenames:
print("In {} :".format(filename))
for old_var in replace_dict:
for line in fileinput.input(filename, inplace=1):
line = re.sub(r'(>\)\s*)\b' + old_var + r'\b', r'\1' + replace_dict[old_var], line.rstrip()) # replace the declaration
line = re.sub(r'(^\s*)\b' + old_var + r'\b', r'\1' + replace_dict[old_var], line.rstrip()) # replace the 2 liner declaration
line = re.sub(r'\b' + old_var + r'\b(\.[gs]et\()',replace_dict[old_var] + r'\1' , line.rstrip()) # replace the usages (with get/set)
print(line) usage |
@bresch Recommend that you update the docs after this merges. Possibly in https://dev.px4.io/en/advanced/parameters_and_configurations.html#parameter-names , but definitely with a note near the code example in https://dev.px4.io/en/advanced/parameters_and_configurations.html#c-api |
Cool! Forcing consistency is even better. Convention also sounds good. The only downside is no direct copy-paste because it needs to be lowercased and prepended but that's a good compromise.
is in my eyes the most important advantage and will improve efficiency when scanning code. |
This is really nice. Would it also be possible to have a naming convention for when we use topics? I.e. vehicle_local_position is sometimes called |
@CarlOlsson Good idea, I consistently switched to |
595d405
to
cb8d598
Compare
@dagar Now that I modified all the parameters, if I run the script from Firmware/src, I still have 3 weird changes: ---------- src/lib/FlightTasks/tasks/AutoLine/FlightTaskAutoLine.cpp ----------
index 31c6016..8dab82f 100644
@@ -267,4 +267,4 @@ void FlightTaskAutoLine::_generateAltitudeSetpoints()
_velocity_setpoint(2) = 0.0f;
_position_setpoint(2) = _target(2);
}
-}
\ No newline at end of file
+}
---------- src/lib/FlightTasks/tasks/Failsafe/FlightTaskFailsafe.cpp ----------
index db3604e..9a84ab2 100644
@@ -74,4 +74,4 @@ bool FlightTaskFailsafe::update()
return true;
-}
\ No newline at end of file
+}
------------------ src/lib/parameters/px4params/srcparser.py ------------------
index 5098545..3d22fac 100644
@@ -320,7 +320,7 @@ class SourceParser(object):
self.param_groups[group].AddParameter(param)
state = None
return True
-
+
def IsNumber(self, numberString):
try:
float(numberString) |
@bresch you're losing the last newline. Without digging into the specifics of your script you could try finishing by inserting a final newline. For the |
2a47f76
to
51c6974
Compare
@dagar Can we merge that now? |
The first two it finishes the files with a newline and the third one it removes trailing whitespaces. Even though I'm not sure if the changes are indtended they look correct to me. Not specific to this PR but isn't PX4 consistently indented with tabs? The scripts in the Tools folder have inconsistent indentations... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the parameters I manually scanned looked like expected. I also did some simulation tests with switching parameters just to be sure and it was all fine. It's all random testing and not extensive.
…BlockParam variables with the new strict naming convention
using parameter_update.py script
using paramter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py script
using parameter_update.py followed by a make format
@MaEtUgR Thanks for the review! I converted the spaces into tabs in the python script and rebased on master |
Nono, please Python with spaces! I strongly vote that we use PEP8 for Python scripts which recommends spaces: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces |
@julianoes Ok, I removed the last commit :D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same again and I already reviewed it.
Description
After a discussion with @MaEtUgR , @bkueng and @Stifael we came to the conclusion that all the variables that are directly linked to a parameter (declared using
(ParamType<px4::params::PARAMETER_NAME>) variable
)should have the same naming convention to be easy to grep for.
The following convention was used in the flight tasks:
(ParamType<px4::params::PARAMETER_NAME>) PARAMETER_NAME
The problem is that ALLCAPS is usually only used for
#defines
in C/C++ and should not used to name a variable.We then decided that the following naming makes more sense:
(ParamType<px4::params::PARAMETER_NAME>) _param_parameter_name
The reasons are:
_param
After trying with
sed
andperl
in a bash script, I finally decided to use python since it's easier to handle the corner cases.Status:
I'm converting the modules one by one to keep the commits small.
- [ ] pass/fail script for CIWill be in a separate PR@dagar for the CI script, can it be in Python?
Replaces #11582
FYI: @BazookaJoe1900 @mcsauder @mhkabir