pid_settings_screen.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if (!isLegacyV4)
  53. _PIDSlider(
  54. label: 'Temperature Offset (°C)',
  55. value: machineState.temperatureOffset,
  56. min: -10,
  57. max: 10,
  58. step: 0.1,
  59. decimalPlaces: 1,
  60. onChanged: (v) => bluetoothService.updateOffset(v),
  61. ),
  62. const Divider(),
  63. if (!isLegacyV4)
  64. _PIDSlider(
  65. label: 'I-Wind-down Min (%)',
  66. value: machineState.pidIWindDownMin,
  67. min: 0,
  68. max: 100,
  69. onChanged: (v) => bluetoothService.updatePIDLimitSettings(min: v),
  70. ),
  71. _PIDSlider(
  72. label: isLegacyV4 ? 'I-Wind-down Max (raw)' : 'I-Wind-down Max (%)',
  73. value: machineState.pidIWindDownMax,
  74. min: 0,
  75. max: isLegacyV4 ? 6000 : 100,
  76. step: isLegacyV4 ? 250 : 1,
  77. onChanged: (v) => bluetoothService.updatePIDLimitSettings(max: v),
  78. ),
  79. ListTile(
  80. title: const Text('Polling Interval (ms)'),
  81. subtitle: Text('${machineState.pidPollingInterval} ms'),
  82. trailing: SizedBox(
  83. width: 200,
  84. child: Slider(
  85. value: machineState.pidPollingInterval.toDouble().clamp(
  86. 1000.0,
  87. 10000.0,
  88. ),
  89. min: 1000,
  90. max: 10000,
  91. divisions: 18, // 500 ms steps
  92. label: '${machineState.pidPollingInterval} ms',
  93. onChanged: (v) {
  94. bluetoothService.updatePIDLimitSettings(interval: v.round());
  95. },
  96. ),
  97. ),
  98. ),
  99. ],
  100. ),
  101. );
  102. }
  103. }
  104. class _PIDSlider extends StatelessWidget {
  105. final String label;
  106. final double value;
  107. final double min;
  108. final double max;
  109. final double step;
  110. final int decimalPlaces;
  111. final ValueChanged<double> onChanged;
  112. const _PIDSlider({
  113. required this.label,
  114. required this.value,
  115. required this.min,
  116. required this.max,
  117. this.step = 1,
  118. this.decimalPlaces = 0,
  119. required this.onChanged,
  120. });
  121. @override
  122. Widget build(BuildContext context) {
  123. final rounded = value.toStringAsFixed(decimalPlaces);
  124. final reading = double.tryParse(rounded) == value
  125. ? rounded
  126. : value.toString();
  127. return Column(
  128. crossAxisAlignment: CrossAxisAlignment.start,
  129. children: [
  130. Padding(
  131. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  132. child: Text('$label: $reading'),
  133. ),
  134. Slider(
  135. value: value.clamp(min, max),
  136. min: min,
  137. max: max,
  138. divisions: ((max - min) / step).round(),
  139. label: reading,
  140. onChanged: (v) => onChanged(
  141. double.parse(
  142. (min + ((v - min) / step).round() * step).toStringAsFixed(
  143. decimalPlaces,
  144. ),
  145. ),
  146. ),
  147. ),
  148. ],
  149. );
  150. }
  151. }