Model switcher

Added model selector to swap between several different models on the fly.
This commit is contained in:
FrogAi
2024-01-12 22:39:30 -07:00
parent 46b0b80f47
commit ae99774cf3
14 changed files with 111 additions and 2 deletions

View File

@@ -0,0 +1,63 @@
import hashlib
import os
import shutil
from openpilot.common.params import Params
from openpilot.system.hardware import HARDWARE
OPENPILOT_PATH = "/data/openpilot"
DESTINATION_PATH = os.path.join(OPENPILOT_PATH, "selfdrive/modeld/models")
MODELS_SOURCE = os.path.join(DESTINATION_PATH, "models")
THNEED_FILE = os.path.join(DESTINATION_PATH, "supercombo.thneed")
MODEL_NAME = {
0: "new-delhi",
1: "blue-diamond-v1",
2: "blue-diamond-v2",
3: "farmville",
4: "new-lemon-pie",
}
def set_model_list_parameter(params):
"""Create a string of all the model names for future comparisons."""
# Retrieve the previous model list
previous_model_list = params.get("ModelList", encoding='utf-8')
# Create a new model list
model_list = "".join(MODEL_NAME.values())
if previous_model_list != model_list:
# Reset the selected model if the model list changed
params.put_int("Model", 0)
params.put("ModelList", model_list)
params.remove("CalibrationParams");
params.remove("LiveTorqueParameters");
def onnx_already_set(path1, path2):
"""Check if the two files are identical by comparing their SHA-256 hashes."""
with open(path1, 'rb') as f1, open(path2, 'rb') as f2:
return hashlib.sha256(f1.read()).hexdigest() == hashlib.sha256(f2.read()).hexdigest()
def copy_model_variant(params):
# Get the corresponding supercombo variant name
variant = MODEL_NAME.get(params.get_int("Model"), MODEL_NAME[0])
# Copy the variant .onnx file to supercombo.onnx in the destination models folder
onnx_path = os.path.join(MODELS_SOURCE, f"{variant}.onnx")
destination = os.path.join(DESTINATION_PATH, "supercombo.onnx")
if not onnx_already_set(onnx_path, destination):
# Delete the thneed file
if os.path.exists(THNEED_FILE):
os.remove(THNEED_FILE)
# Copy over the onnx file
shutil.copy(onnx_path, destination)
# Reboot
HARDWARE.reboot()
if __name__ == "__main__":
params = Params()
set_model_list_parameter(params)
copy_model_variant(params)

View File

@@ -31,6 +31,7 @@ FrogPilotControlsPanel::FrogPilotControlsPanel(SettingsWindow *parent) : FrogPil
{"AggressiveAcceleration", "Aggressive Acceleration With Lead", "Increase acceleration aggressiveness when following a lead vehicle from a stop.", ""},
{"StoppingDistance", "Increased Stopping Distance", "Increase the stopping distance for a more comfortable stop.", ""},
{"Model", "Model Selector", "Choose your preferred openpilot model.", "../assets/offroad/icon_calibration.png"},
{"MTSCEnabled", "Map Turn Speed Control", "Slow down for anticipated curves detected by your downloaded maps.", "../frogpilot/assets/toggle_icons/icon_speed_map.png"},
};
@@ -176,6 +177,28 @@ FrogPilotControlsPanel::FrogPilotControlsPanel(SettingsWindow *parent) : FrogPil
} else if (param == "StoppingDistance") {
toggle = new FrogPilotParamValueControl(param, title, desc, icon, 0, 10, std::map<int, QString>(), this, false, " feet");
} else if (param == "Model") {
modelSelectorButton = new FrogPilotButtonIconControl(title, tr("SELECT"), desc, icon);
QStringList models = {"New Delhi (Default)", "Blue Diamond V1", "Blue Diamond V2", "Farmville", "New Lemon Pie"};
QObject::connect(modelSelectorButton, &FrogPilotButtonIconControl::clicked, this, [this, models]() {
int currentModel = params.getInt("Model");
QString currentModelLabel = models[currentModel];
QString selection = MultiOptionDialog::getSelection(tr("Select a driving model"), models, currentModelLabel, this);
if (!selection.isEmpty()) {
int selectedModel = models.indexOf(selection);
params.putInt("Model", selectedModel);
params.remove("CalibrationParams");
params.remove("LiveTorqueParameters");
modelSelectorButton->setValue(selection);
if (FrogPilotConfirmationDialog::toggle("Reboot required to take effect.", "Reboot Now", this)) {
Hardware::reboot();
}
}
});
modelSelectorButton->setValue(models[params.getInt("Model")]);
addItem(modelSelectorButton);
} else {
toggle = new ParamControl(param, title, desc, icon, this);
}
@@ -262,6 +285,7 @@ void FrogPilotControlsPanel::parentToggleClicked() {
aggressiveProfile->setVisible(false);
conditionalSpeedsImperial->setVisible(false);
conditionalSpeedsMetric->setVisible(false);
modelSelectorButton->setVisible(false);
standardProfile->setVisible(false);
relaxedProfile->setVisible(false);
@@ -272,6 +296,7 @@ void FrogPilotControlsPanel::hideSubToggles() {
aggressiveProfile->setVisible(false);
conditionalSpeedsImperial->setVisible(false);
conditionalSpeedsMetric->setVisible(false);
modelSelectorButton->setVisible(true);
standardProfile->setVisible(false);
relaxedProfile->setVisible(false);

View File

@@ -22,6 +22,8 @@ private:
void updateMetric();
void updateToggles();
FrogPilotButtonIconControl *modelSelectorButton;
FrogPilotDualParamControl *aggressiveProfile;
FrogPilotDualParamControl *conditionalSpeedsImperial;
FrogPilotDualParamControl *conditionalSpeedsMetric;