Last active
September 7, 2018 03:50
-
-
Save dendentang/ca126b4934c8ea9bbd983cb4ec71854f to your computer and use it in GitHub Desktop.
Use multiple points for calibration in most 5 points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, json | |
| class Calibration_Multi: | |
| def __init__(self, filepath=None): | |
| self.dots = { | |
| "origin": [900, 1300, 1700, 2100, 2500], | |
| "adjust": [0, 20, 40, 60 ,80], | |
| "count_in_use": 5 | |
| } | |
| self.filepath = filepath | |
| self.load_setting() | |
| def load_setting(self): | |
| if not self.filepath: | |
| return 0 | |
| if os.path.isfile(filepath): | |
| with open(filepath) as f: | |
| self.dots = json.load(f) | |
| if not 2 <= self.dots["count_in_use"] <= 5: | |
| raise | |
| def set_dot(self, _json): | |
| index = _json["number_of_dot"] - 1 | |
| self.dots["origin"][index] = float(_json["origin_value"]) | |
| self.dots["adjust"][index] = float(_json["adjust_value"]) | |
| if "count" in _json: | |
| self.dots["count_in_use"] = int(_json["count"]) | |
| self.save() | |
| def adjust_value(self, raw_val): | |
| count = self.dots["count_in_use"] | |
| if raw_val < self.dots["origin"][0]: | |
| return self.calculate(raw_val, 0) | |
| if raw_val >= self.dots["origin"][count-1]: | |
| return self.calculate(raw_val, count - 2) | |
| for i in range(count - 1): | |
| if self.dots["origin"][i] <= raw_val < self.dots["origin"][i+1]: | |
| return self.calculate(raw_val, i) | |
| return raw_val | |
| def calculate(self, val, i): | |
| ori = self.dots["origin"] | |
| adj = self.dots["adjust"] | |
| ratio = (val - ori[i]) / (ori[i+1] - ori[i]) | |
| result = adj[i] + ratio * (adj[i+1] - adj[i]) | |
| return result | |
| def save(self): | |
| if not self.filepath: | |
| return 0 | |
| with open(self.filepath, 'w') as f: | |
| json.dump(self.dots, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment