Skip to content

Commit

Permalink
fix for when Floaty sometimes emits null values
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed Sep 17, 2024
1 parent fae176f commit fae1732
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
26 changes: 21 additions & 5 deletions src/components/Details.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
let voltsPerCell = $derived(batterySpecs.cellCount ? data.voltage / batterySpecs.cellCount : NaN);
let cellVoltsLow = $derived(voltsPerCell && batterySpecs.cellMinVolt && voltsPerCell < batterySpecs.cellMinVolt);
let formatSpeed = $derived((x: number) => mapSpeed(x).toFixed(1));
let formatSpeed = $derived((x: number) => (Number.isNaN(x) ? '??' : mapSpeed(x).toFixed(1)));
const getStateColor = (state: string): string | undefined => {
switch (state.toLowerCase()) {
Expand Down Expand Up @@ -84,8 +84,16 @@
<div class={itemClass}>
<List
items={[
{ label: 'Duty', value: `${data.duty}%`, color: data.duty > 80 ? 'red' : ChartColours.DutyCycle },
{ label: 'Motor Current', value: `${data.current_motor} A`, color: ChartColours.CurrentMotor },
{
label: 'Duty',
value: `${Number.isNaN(data.duty) ? '?? ' : data.duty}%`,
color: data.duty > 80 ? 'red' : ChartColours.DutyCycle,
},
{
label: 'Motor Current',
value: `${Number.isNaN(data.current_motor) ? '??' : data.current_motor} A`,
color: ChartColours.CurrentMotor,
},
...(Number.isNaN(data.current_field_weakening)
? []
: [
Expand All @@ -107,13 +115,21 @@
items={[
{ label: 'Spec', value: batterySpecs.cellCount ? `${batterySpecs.cellCount}S` : configureButton },
'-',
{ label: 'Batt V (total)', value: `${data.voltage} V`, color: ChartColours.BatteryVoltage },
{
label: 'Batt V (total)',
value: `${Number.isNaN(data.voltage) ? '?? ' : data.voltage} V`,
color: ChartColours.BatteryVoltage,
},
{
label: 'Batt V (cell)',
value: `${voltsPerCell ? voltsPerCell.toFixed(1) : '??'} V`,
color: cellVoltsLow ? 'red' : ChartColours.BatteryVoltage,
},
{ label: 'Batt Current', value: `${data.current_battery} A`, color: ChartColours.CurrentBattery },
{
label: 'Batt Current',
value: `${Number.isNaN(data.current_battery) ? '??' : data.current_battery} A`,
color: ChartColours.CurrentBattery,
},
]}
/>
</div>
Expand Down
10 changes: 5 additions & 5 deletions src/lib/parse/floaty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function rowsFromFloatyJson(json: ZFloatyJson): Row[] {
bms_fault: NaN,
bms_temp: NaN,
bms_temp_battery: NaN,
current_battery: log.batteryCurrent,
current_battery: log.batteryCurrent ?? NaN,
current_booster: NaN,
current_field_weakening: NaN,
current_motor: log.motorCurrent,
current_motor: log.motorCurrent ?? NaN,
distance: log.tripDistance,
duty: log.dutyCycle,
duty: log.dutyCycle ?? NaN,
erpm: NaN,
gps_accuracy: location.accuracy,
gps_latitude: location.latitude,
Expand All @@ -34,15 +34,15 @@ function rowsFromFloatyJson(json: ZFloatyJson): Row[] {
setpoint_remote: NaN,
setpoint_torque_tilt: NaN,
setpoint: NaN,
speed: log.speed,
speed: log.speed ?? NaN,
state_raw: log.state,
state: stateCodeMap[log.state] ?? '??',
temp_battery: NaN,
temp_mosfet: log.controllerTemp,
temp_motor: log.motorTemp,
time: (log.timestamp - json.startTime) / 1000,
true_pitch: log.truePitchAngle,
voltage: log.batteryVolts,
voltage: log.batteryVolts ?? NaN,
wh_charged: NaN,
wh: log.wattHours,
});
Expand Down
10 changes: 5 additions & 5 deletions src/lib/parse/floaty.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export const LocationSchema = z.object({
export type ZLog = z.infer<typeof LogSchema>;
export const LogSchema = z.object({
timestamp: z.number(),
speed: z.number(),
dutyCycle: z.number(),
batteryVolts: z.number(),
speed: z.number().nullable(),
dutyCycle: z.number().nullable(),
batteryVolts: z.number().nullable(),
batteryPercent: z.number(),
batteryCurrent: z.number(),
motorCurrent: z.number(),
batteryCurrent: z.number().nullable(),
motorCurrent: z.number().nullable(),
motorTemp: z.number(),
controllerTemp: z.number(),
tripDistance: z.number(),
Expand Down

0 comments on commit fae1732

Please sign in to comment.