pid_settings_screen.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. class PIDSettingsScreen extends ConsumerWidget {
  5. const PIDSettingsScreen({super.key});
  6. @override
  7. Widget build(BuildContext context, WidgetRef ref) {
  8. final machineState = ref.watch(bluetoothServiceProvider);
  9. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  10. final isLegacyV4 = machineState.isLegacyV4Firmware;
  11. return Scaffold(
  12. appBar: AppBar(title: const Text('PID Tuning')),
  13. body: ListView(
  14. padding: const EdgeInsets.all(16),
  15. children: [
  16. Text(
  17. isLegacyV4
  18. ? 'V4 defaults: P 25, I 3, D 128, I limit 3750.'
  19. : 'Defaults: P 20, I 0.30, D 128, I limit 20%.',
  20. ),
  21. _PIDSlider(
  22. label: 'Proportional (P)',
  23. value: machineState.pidP,
  24. min: 0,
  25. max: 100,
  26. onChanged: (v) => bluetoothService.updatePIDSettings(p: v),
  27. ),
  28. _PIDSlider(
  29. label: 'Integral (I)',
  30. value: machineState.pidI,
  31. min: 0,
  32. max: isLegacyV4 ? 10 : 1,
  33. step: isLegacyV4 ? 1 : 0.01,
  34. decimalPlaces: isLegacyV4 ? 0 : 2,
  35. onChanged: (v) => bluetoothService.updatePIDSettings(i: v),
  36. ),
  37. _PIDSlider(
  38. label: 'Derivative (D)',
  39. value: machineState.pidD,
  40. min: 0,
  41. max: 256,
  42. onChanged: (v) => bluetoothService.updatePIDSettings(d: v),
  43. ),
  44. const Divider(),
  45. _PIDSlider(
  46. label: 'Pro-Active Percentage',
  47. value: machineState.proActivePercentage.toDouble(),
  48. min: 0,
  49. max: 100,
  50. onChanged: (v) => bluetoothService.updateProActive(v.toInt()),
  51. ),
  52. const Divider(),
  53. if (!isLegacyV4)
  54. _PIDSlider(
  55. label: 'I-Wind-down Min (%)',
  56. value: machineState.pidIWindDownMin,
  57. min: 0,
  58. max: 100,
  59. onChanged: (v) => bluetoothService.updatePIDLimitSettings(min: v),
  60. ),
  61. _PIDSlider(
  62. label: isLegacyV4 ? 'I-Wind-down Max (raw)' : 'I-Wind-down Max (%)',
  63. value: machineState.pidIWindDownMax,
  64. min: 0,
  65. max: isLegacyV4 ? 6000 : 100,
  66. step: isLegacyV4 ? 250 : 1,
  67. onChanged: (v) => bluetoothService.updatePIDLimitSettings(max: v),
  68. ),
  69. ListTile(
  70. title: const Text('Polling Interval (ms)'),
  71. subtitle: Text('${machineState.pidPollingInterval} ms'),
  72. trailing: SizedBox(
  73. width: 200,
  74. child: Slider(
  75. value: machineState.pidPollingInterval.toDouble().clamp(
  76. 1000.0,
  77. 10000.0,
  78. ),
  79. min: 1000,
  80. max: 10000,
  81. divisions: 18, // 500 ms steps
  82. label: '${machineState.pidPollingInterval} ms',
  83. onChanged: (v) {
  84. bluetoothService.updatePIDLimitSettings(interval: v.round());
  85. },
  86. ),
  87. ),
  88. ),
  89. ],
  90. ),
  91. );
  92. }
  93. }
  94. class _PIDSlider extends StatelessWidget {
  95. final String label;
  96. final double value;
  97. final double min;
  98. final double max;
  99. final double step;
  100. final int decimalPlaces;
  101. final ValueChanged<double> onChanged;
  102. const _PIDSlider({
  103. required this.label,
  104. required this.value,
  105. required this.min,
  106. required this.max,
  107. this.step = 1,
  108. this.decimalPlaces = 0,
  109. required this.onChanged,
  110. });
  111. @override
  112. Widget build(BuildContext context) {
  113. final rounded = value.toStringAsFixed(decimalPlaces);
  114. final reading = double.tryParse(rounded) == value
  115. ? rounded
  116. : value.toString();
  117. return Column(
  118. crossAxisAlignment: CrossAxisAlignment.start,
  119. children: [
  120. Padding(
  121. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  122. child: Text('$label: $reading'),
  123. ),
  124. Slider(
  125. value: value.clamp(min, max),
  126. min: min,
  127. max: max,
  128. divisions: ((max - min) / step).round(),
  129. label: reading,
  130. onChanged: (v) => onChanged(
  131. double.parse(
  132. (min + ((v - min) / step).round() * step).toStringAsFixed(
  133. decimalPlaces,
  134. ),
  135. ),
  136. ),
  137. ),
  138. ],
  139. );
  140. }
  141. }