From 8bc848097caef2c916b1e2aee670b134b13718dc Mon Sep 17 00:00:00 2001 From: Tao Date: Sun, 23 Mar 2025 03:46:13 +0000 Subject: [PATCH 1/2] Battery Profile: add lua support --- src/SCRIPTS/RF2/COMPILE/scripts.lua | 1 + src/SCRIPTS/RF2/MSP/mspSetProfile.lua | 17 +++++- src/SCRIPTS/RF2/MSP/mspStatus.lua | 4 +- src/SCRIPTS/RF2/PAGES/battery.lua | 77 +++++++++++++++++++++++++++ src/SCRIPTS/RF2/pages.lua | 1 + 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/SCRIPTS/RF2/PAGES/battery.lua diff --git a/src/SCRIPTS/RF2/COMPILE/scripts.lua b/src/SCRIPTS/RF2/COMPILE/scripts.lua index 28f80feb..d2b9e457 100644 --- a/src/SCRIPTS/RF2/COMPILE/scripts.lua +++ b/src/SCRIPTS/RF2/COMPILE/scripts.lua @@ -31,6 +31,7 @@ local scripts = { "/SCRIPTS/RF2/PAGES/esc_xdfly.lua", "/SCRIPTS/RF2/PAGES/esc_yge.lua", "/SCRIPTS/RF2/PAGES/experimental.lua", + "/SCRIPTS/RF2/PAGES/battery.lua", "/SCRIPTS/RF2/PAGES/filters.lua", "/SCRIPTS/RF2/PAGES/governor.lua", "/SCRIPTS/RF2/PAGES/mixer.lua", diff --git a/src/SCRIPTS/RF2/MSP/mspSetProfile.lua b/src/SCRIPTS/RF2/MSP/mspSetProfile.lua index 0290a5ee..22db80d8 100644 --- a/src/SCRIPTS/RF2/MSP/mspSetProfile.lua +++ b/src/SCRIPTS/RF2/MSP/mspSetProfile.lua @@ -14,7 +14,20 @@ local function setRateProfile(profileIndex, callback, callbackParam) setPidProfile(profileIndex + 128, callback, callbackParam) end +local function setBatteryProfile(profileIndex, callback, callbackParam) + local message = { + command = 244, -- MSP_SELECT_BATTERY + payload = { profileIndex }, + processReply = function(self, buf) + if callback then callback(callbackParam) end + end, + simulatorResponse = {} + } + rf2.mspQueue:add(message) +end + return { setPidProfile = setPidProfile, - setRateProfile = setRateProfile -} \ No newline at end of file + setRateProfile = setRateProfile, + setBatteryProfile = setBatteryProfile +} diff --git a/src/SCRIPTS/RF2/MSP/mspStatus.lua b/src/SCRIPTS/RF2/MSP/mspStatus.lua index 55d0acb7..040f7fac 100644 --- a/src/SCRIPTS/RF2/MSP/mspStatus.lua +++ b/src/SCRIPTS/RF2/MSP/mspStatus.lua @@ -24,9 +24,11 @@ local function getStatus(callback, callbackParam) --rf2.print("Number of motors: "..tostring(status.motorCount)) --status.servoCount = rf2.mspHelper.readU8(buf) --rf2.print("Number of servos: "..tostring(status.servoCount)) + buf.offset = 31 + status.batteryProfile = rf2.mspHelper.readU8(buf) callback(callbackParam, status) end, - simulatorResponse = { 240, 1, 124, 0, 35, 0, 0, 0, 0, 0, 0, 224, 1, 10, 1, 0, 26, 0, 0, 0, 0, 0, 2, 0, 6, 0, 6, 1, 4, 1 } + simulatorResponse = { 240, 1, 124, 0, 35, 0, 0, 0, 0, 0, 0, 224, 1, 10, 1, 0, 26, 0, 0, 0, 0, 0, 2, 0, 6, 0, 6, 1, 4, 1, 1, 6 } } rf2.mspQueue:add(message) diff --git a/src/SCRIPTS/RF2/PAGES/battery.lua b/src/SCRIPTS/RF2/PAGES/battery.lua new file mode 100644 index 00000000..9cfbefee --- /dev/null +++ b/src/SCRIPTS/RF2/PAGES/battery.lua @@ -0,0 +1,77 @@ +local template = assert(rf2.loadScript(rf2.radio.template))() +local margin = template.margin +local indent = template.indent +local lineSpacing = template.lineSpacing +local tableSpacing = template.tableSpacing +local sp = template.listSpacing.field +local yMinLim = rf2.radio.yMinLimit +local x = margin +local y = yMinLim - lineSpacing +local inc = { x = function(val) x = x + val return x end, y = function(val) y = y + val return y end } +local labels = {} +local fields = {} +local batteries = 6 +local mspSetProfile = assert(rf2.loadScript("MSP/mspSetProfile.lua"))() +local mspStatus = assert(rf2.loadScript("MSP/mspStatus.lua"))() +local editing = false +local profileAdjustmentTS = nil + +local startEditing = function(field, page) + editing = true +end + +local endPidEditing = function(field, page) + mspSetProfile.setBatteryProfile(field.data.value, function() rf2.reloadPage() end, nil) +end + +fields[1] = { t = "Current Profile", x = x, y = inc.y(lineSpacing), sp = x + sp * 1.17, data = { value = nil, min = 0, max = 5, table = { [0] = "1", "2", "3", "4", "5", "6" } }, preEdit = startEditing, postEdit = endPidEditing } + +y = inc.y(lineSpacing) +labels[#labels + 1] = { t = "Battery", x = x, y = inc.y(lineSpacing) } +labels[#labels + 1] = { t = "Capacity", x = x + tableSpacing.col, y = y } +labels[#labels + 1] = { t = "CellCount", x = x + 2 * tableSpacing.col, y = y } + +labels[#labels + 1] = { t = tostring(1), x = x, y = inc.y(lineSpacing) } +fields[#fields + 1] = { x = x + tableSpacing.col, y = y, min = 0, max = 20000, vals = { 1, 2 } } +fields[#fields + 1] = { x = x + 2 * tableSpacing.col, y = y, min = 0, max = 24, vals = { 3 } } +for i=1, batteries - 1 do + labels[#labels + 1] = { t = tostring(i + 1), x = x, y = inc.y(lineSpacing) } + fields[#fields + 1] = { x = x + tableSpacing.col, y = y, min = 0, max = 20000, vals = { 15 + (i - 1) * 3 + 1, 15 + (i - 1) * 3 + 2 } } + fields[#fields + 1] = { x = x + 2 * tableSpacing.col, y = y, min = 0, max = 24, vals = { 15 + (i - 1) * 3 + 3 } } +end + +return { + read = 32, -- MSP_BATTERY_CONFIG + write = 33, -- MSP_SET_BATTERY_CONFIG + title = "Battery Config", + minBytes = 30, + eepromWrite = true, + labels = labels, + fields = fields, + simulatorResponse = { 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 0, 3, 5, 0, 4, 6, 0, 5, 8, 0, 0, 12 }, + + postLoad = function(self) + mspStatus.getStatus(self.onProcessedMspStatus, self) + end, + + timer = function(self) + if profileAdjustmentTS and rf2.clock() - profileAdjustmentTS > 0.35 then + rf2.reloadPage() + elseif rf2.mspQueue:isProcessed() and not editing then + mspStatus.getStatus(self.onProcessedMspStatus, self) + end + end, + + onProcessedMspStatus = function(self, status) + local currentField = self.fields[1] + if currentField.data.value ~= status.batteryProfile and not editing then + if currentField.data.value then + profileAdjustmentTS = rf2.clock() + end + currentField.data.value = status.batteryProfile + end + + rf2.lcdNeedsInvalidate = true + self.isReady = true + end, +} diff --git a/src/SCRIPTS/RF2/pages.lua b/src/SCRIPTS/RF2/pages.lua index 1ae8f95d..18af3344 100644 --- a/src/SCRIPTS/RF2/pages.lua +++ b/src/SCRIPTS/RF2/pages.lua @@ -8,6 +8,7 @@ PageFiles[#PageFiles + 1] = { title = "Profile - PIDs", script = "pids.lua" } PageFiles[#PageFiles + 1] = { title = "Profile - Various", script = "profile.lua" } PageFiles[#PageFiles + 1] = { title = "Profile - Rescue", script = "profile_rescue.lua" } PageFiles[#PageFiles + 1] = { title = "Profile - Governor", script = "profile_governor.lua" } +PageFiles[#PageFiles + 1] = { title = "Battery", script = "battery.lua" } PageFiles[#PageFiles + 1] = { title = "Servos", script = "servos.lua" } PageFiles[#PageFiles + 1] = { title = "Mixer", script = "mixer.lua" } PageFiles[#PageFiles + 1] = { title = "Gyro Filters", script = "filters.lua" } From 2cb8d834769b6bdda8feaf9365dc3ef41072a949 Mon Sep 17 00:00:00 2001 From: Tao Date: Wed, 26 Mar 2025 08:37:22 +0000 Subject: [PATCH 2/2] Battery Profile: add telemetry decoding --- src/SCRIPTS/RF2/rf2tlm.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SCRIPTS/RF2/rf2tlm.lua b/src/SCRIPTS/RF2/rf2tlm.lua index 401bbc37..5fd8683f 100644 --- a/src/SCRIPTS/RF2/rf2tlm.lua +++ b/src/SCRIPTS/RF2/rf2tlm.lua @@ -305,6 +305,8 @@ local RFSensors = { [0x1212] = { name="RTE#", unit=UNIT_RAW, prec=0, dec=decU8 }, -- Current LED profile [0x1213] = { name="LED#", unit=UNIT_RAW, prec=0, dec=decU8 }, + -- Current Battery profile + [0x1214] = { name="BAT#", unit=UNIT_RAW, prec=0, dec=decU8 }, -- Adjustment function [0x1220] = { name="ADJ", unit=UNIT_RAW, prec=0, dec=decAdjFunc },