From c822bda7799be8a0bd25759351c8f15d616e80b0 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 7 Nov 2026 14:42:44 -0500 Subject: [PATCH] CG envelope can be entered as moment (lb-in/1000) As POH moment-envelope charts and Garmin Pilot give it. Stored as CG inches (moment x 1000 / weight), shown back as entered with the CG next to each point; switching the units and saving re-reads the numbers. Co-Authored-By: Claude Opus 5.5 --- app/controllers/loading_setups_controller.rb | 4 +-- app/models/loading_setup.rb | 30 +++++++++++++++++--- app/views/loading_setups/edit.html.erb | 14 ++++++--- docs/guide/major-records.md | 2 +- test/integration/group_four_test.rb | 16 +++++++++++ 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/app/controllers/loading_setups_controller.rb b/app/controllers/loading_setups_controller.rb index 642816c..707ef83 100644 --- a/app/controllers/loading_setups_controller.rb +++ b/app/controllers/loading_setups_controller.rb @@ -9,10 +9,10 @@ class LoadingSetupsController < ApplicationController end def update - permitted = params.fetch(:setup, {}).permit(:fuel_lb_per_gal, stations: %i[name arm max fuel], envelope: %i[weight cg]) + permitted = params.fetch(:setup, {}).permit(:fuel_lb_per_gal, :envelope_unit, stations: %i[name arm max fuel], envelope: %i[weight cg]) @setup = LoadingSetup.from_params(permitted.to_h) if @setup.valid? - @aircraft.update!(loading_stations: @setup.stations_json, cg_envelope: @setup.envelope) + @aircraft.update!(loading_stations: @setup.stations_json, cg_envelope: @setup.envelope.map { |w, cg| [ w.to_f, cg.to_f ] }) log_event("aircraft", "Loading stations and CG envelope updated") redirect_to aircraft_weight_balance_records_path(@aircraft), notice: "Loading setup saved." else diff --git a/app/models/loading_setup.rb b/app/models/loading_setup.rb index ec0c59c..0b332b1 100644 --- a/app/models/loading_setup.rb +++ b/app/models/loading_setup.rb @@ -1,31 +1,52 @@ # Loading stations (name, arm, maximum, fuel or not) and the CG envelope # (weight/CG points in order around the boundary), as entered from the POH. +# The envelope is stored as CG in inches; it can be entered as moment in +# lb-in/1000 (how many POH charts and Garmin Pilot give it) and is shown +# back the way it was entered. class LoadingSetup include ActiveModel::Model Station = Data.define(:name, :arm, :max, :fuel) + ENVELOPE_UNITS = { "cg" => "CG (inches aft of datum)", "moment" => "Moment (lb-in / 1000)" }.freeze + attr_accessor :stations, :envelope, :fuel_lb_per_gal + attr_writer :envelope_unit + + def envelope_unit = ENVELOPE_UNITS.key?(@envelope_unit) ? @envelope_unit : "cg" + def moment? = envelope_unit == "moment" + + # The envelope as entered: [weight, CG in] or [weight, moment / 1000]. + def entered_envelope + return envelope unless moment? + envelope.map { |w, cg| [ w, (w * cg / 1000).round(3) ] } + end def self.from_aircraft(aircraft) data = aircraft.loading_stations.is_a?(Hash) ? aircraft.loading_stations : {} new(stations: Array(data["stations"]).map { Station.new(name: _1["name"], arm: _1["arm"].to_d, max: _1["max"]&.to_d, fuel: _1["fuel"] == true) }, envelope: Array(aircraft.cg_envelope).map { [ _1[0].to_d, _1[1].to_d ] }, - fuel_lb_per_gal: (data["fuel_lb_per_gal"] || 6).to_d) + fuel_lb_per_gal: (data["fuel_lb_per_gal"] || 6).to_d, envelope_unit: data["envelope_unit"]) end # From the edit form: blank rows are ignored. def self.from_params(p) rows = Array(p["stations"]&.values).reject { _1["name"].to_s.strip.empty? && _1["arm"].to_s.strip.empty? } points = Array(p["envelope"]&.values).reject { _1["weight"].to_s.strip.empty? && _1["cg"].to_s.strip.empty? } - setup = new(fuel_lb_per_gal: p["fuel_lb_per_gal"].presence&.to_d || 6, stations: [], envelope: []) + setup = new(fuel_lb_per_gal: p["fuel_lb_per_gal"].presence&.to_d || 6, stations: [], envelope: [], envelope_unit: p["envelope_unit"]) setup.instance_variable_set(:@raw_stations, rows) setup.instance_variable_set(:@raw_envelope, points) setup.stations = rows.filter_map do |r| next unless numeric?(r["arm"]) Station.new(name: r["name"].to_s.strip.first(40), arm: r["arm"].to_d, max: numeric?(r["max"]) ? r["max"].to_d : nil, fuel: r["fuel"] == "1") end - setup.envelope = points.filter_map { numeric?(_1["weight"]) && numeric?(_1["cg"]) ? [ _1["weight"].to_d, _1["cg"].to_d ] : nil } + setup.envelope = points.filter_map do |pt| + next unless numeric?(pt["weight"]) && numeric?(pt["cg"]) + weight = pt["weight"].to_d + value = pt["cg"].to_d + next [ weight, value ] unless setup.moment? + [ weight, weight.positive? ? (value * 1000 / weight).round(4) : value ] + end setup end @@ -37,11 +58,12 @@ class LoadingSetup errors.add(:base, "Every station needs a name and a numeric arm (inches)") if raw_s.size != stations.size || stations.any? { _1.name.blank? } errors.add(:base, "Every envelope point needs a numeric weight and CG") if raw_e.size != envelope.size errors.add(:base, "The CG envelope needs at least 3 points, or none") if envelope.size.between?(1, 2) + errors.add(:base, "Envelope weights must be more than 0") if envelope.any? { |w, _| !w.positive? } errors.add(:base, "Fuel weight per gallon must be between 4 and 9 lb") unless fuel_lb_per_gal.to_d.between?(4, 9) end def stations_json - { "fuel_lb_per_gal" => fuel_lb_per_gal.to_f, + { "fuel_lb_per_gal" => fuel_lb_per_gal.to_f, "envelope_unit" => envelope_unit, "stations" => stations.map { { "name" => _1.name, "arm" => _1.arm.to_f, "max" => _1.max&.to_f, "fuel" => _1.fuel } } } end diff --git a/app/views/loading_setups/edit.html.erb b/app/views/loading_setups/edit.html.erb index 46d92e0..184afd9 100644 --- a/app/views/loading_setups/edit.html.erb +++ b/app/views/loading_setups/edit.html.erb @@ -1,7 +1,7 @@ <% content_for :title, "#{@aircraft.tail_number} Loading setup" %> <%= render "shared/aircraft_header", aircraft: @aircraft %> <% stations = @setup.stations + Array.new([ 8 - @setup.stations.size, 2 ].max) { nil } %> -<% points = @setup.envelope + Array.new([ 10 - @setup.envelope.size, 2 ].max) { nil } %> +<% points = @setup.entered_envelope + Array.new([ 10 - @setup.envelope.size, 2 ].max) { nil } %> <%= form_with url: aircraft_loading_setup_path(@aircraft), method: :patch, class: "card card-body max-w-3xl space-y-5" do %>

Loading setup

@@ -31,11 +31,17 @@
CG envelope

The corners of the normal-category envelope, in order around it (e.g. forward limit at low weight, forward limit at gross, aft limit at gross, aft limit at low weight).

-
Weight (lb)CG (in)
+
+ <%= label_tag "setup[envelope_unit]", "Envelope values are", class: "label" %> + <%= select_tag "setup[envelope_unit]", options_for_select(LoadingSetup::ENVELOPE_UNITS.invert, @setup.envelope_unit), class: "input" %> +

Use the same units as your source: a POH “moment envelope” chart or Garmin Pilot give moment in lb-in/1000 (e.g. 2300 lb at 104.5). Moments are converted to CG (moment × 1000 ÷ weight) when saved. Changing this and saving reads the numbers below in the new units, so it also fixes moments typed in as CG.

+
+
Weight (lb)<%= @setup.moment? ? "Moment (lb-in/1000)" : "CG (in)" %><%= "= CG (in)" if @setup.moment? %>
<% points.each_with_index do |pt, i| %> -
+
<%= text_field_tag "setup[envelope][#{i}][weight]", pt&.first&.to_s&.sub(/\.0\z/, ""), class: "input", inputmode: "decimal", "aria-label": "Envelope point #{i + 1} weight" %> - <%= text_field_tag "setup[envelope][#{i}][cg]", pt&.last&.to_s&.sub(/\.0\z/, ""), class: "input", inputmode: "decimal", "aria-label": "Envelope point #{i + 1} CG" %> + <%= text_field_tag "setup[envelope][#{i}][cg]", pt&.last&.to_s&.sub(/\.0\z/, ""), class: "input", inputmode: "decimal", "aria-label": "Envelope point #{i + 1} #{@setup.moment? ? 'moment' : 'CG'}" %> + <%= "#{@setup.envelope[i].last.round(2)} in" if @setup.moment? && @setup.envelope[i] %>
<% end %>
diff --git a/docs/guide/major-records.md b/docs/guide/major-records.md index d4c9f86..0307779 100644 --- a/docs/guide/major-records.md +++ b/docs/guide/major-records.md @@ -11,7 +11,7 @@ If it includes **instructions for continued airworthiness**, tick it and use **A The **W&B** tab keeps the history of empty weight and CG. - **+ New W&B**: date, reason, empty weight, empty CG (arm aft of the datum), max gross weight, the alteration that caused it and the W&B report document. The newest record is the current one. -- **Loading setup**: from the POH, the stations (seats, baggage, fuel tanks) with their arms and maximums, the fuel weight per gallon, and the corners of the CG envelope. +- **Loading setup**: from the POH, the stations (seats, baggage, fuel tanks) with their arms and maximums, the fuel weight per gallon, and the corners of the CG envelope. Enter the envelope either as CG in inches aft of the datum or as moment in lb-in/1000 (as POH moment charts and Garmin Pilot show it, e.g. 2300 lb at 104.5): pick which under **Envelope values are**. Moments are converted to CG (moment × 1000 ÷ weight) and the CG is shown next to each point. - **Loading calculator**: enter people, baggage and fuel to see takeoff and landing weight and CG against max gross and the envelope. The calculator is a planning aid from the figures entered. Check against the POH and the aircraft's current W&B report. diff --git a/test/integration/group_four_test.rb b/test/integration/group_four_test.rb index d6dd948..b4e9c89 100644 --- a/test/integration/group_four_test.rb +++ b/test/integration/group_four_test.rb @@ -137,6 +137,22 @@ class GroupFourTest < ActionDispatch::IntegrationTest assert_includes response.body, "2026-01-01: 1500.0 lb @ 39.50 in" get aircraft_report_path(@a) assert_includes response.body, "Empty weight 1,500.0 lb" + + # Garmin Pilot / POH moment envelope (lb-in/1000), converted to CG and shown back as entered + patch aircraft_loading_setup_path(@a), params: { setup: { fuel_lb_per_gal: "6", envelope_unit: "moment", stations: { + "0" => { name: "Front seats", arm: "37" } }, + envelope: { "0" => { weight: "1500", cg: "52.5" }, "1" => { weight: "2300", cg: "88.55" }, "2" => { weight: "2300", cg: "108.79" }, "3" => { weight: "1500", cg: "70.95" } } } } + setup = @a.reload.loading_setup + assert setup.moment? + assert_in_delta 35.0, setup.envelope[0].last, 0.001 + assert_in_delta 47.3, setup.envelope[2].last, 0.001 + assert_equal [ 2300, 108.79 ], setup.entered_envelope[2].map(&:to_f) + get edit_aircraft_loading_setup_path(@a) + assert_select "input[name='setup[envelope][2][cg]'][value='108.79']" + assert_includes response.body, "47.3 in" + patch aircraft_loading_setup_path(@a), params: { setup: { fuel_lb_per_gal: "6", envelope_unit: "moment", stations: { "0" => { name: "Front seats", arm: "37" } }, + envelope: { "0" => { weight: "0", cg: "1" }, "1" => { weight: "2300", cg: "88" }, "2" => { weight: "2300", cg: "108" } } } } + assert_response :unprocessable_entity end FR_RESPONSE = { results: [