Skip to content

Commit

Permalink
Fix issue saving PID values
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewda committed Jul 4, 2024
1 parent 525acb0 commit 8870b47
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/domain/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class ConnectionManager {

try {
socket?.send(message, robotAddress, robotPort);
DesiredStateModel().saveRecallState = 1;

// Reset to recall state after sending a message
DesiredStateModel().setSaveRecallState(SaveRecallState.recall);
} catch (e) {
print('Could not send data, ensure robot is connected');
}
Expand Down
19 changes: 16 additions & 3 deletions lib/domain/dtos/command_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import 'dart:typed_data';
import '../../models/config_model.dart';
import '../../models/desired_state_model.dart';

enum SaveRecallState {
none(0),
recall(1),
save(2);

const SaveRecallState(this.value);

final int value;
}

double remap(
double value,
double low1,
Expand All @@ -25,7 +35,7 @@ class CommandMessage {
double speedP = 0;
double speedI = 0;
double speedD = 0;
int saveRecallState = 1;
int saveRecallState = SaveRecallState.recall.value;

int speed = 0;
int turn = 0;
Expand Down Expand Up @@ -83,8 +93,11 @@ class CommandMessage {
commandMessage.saveRecallState = object.saveRecallState;
} else if (object is DesiredStateModel) {
commandMessage.enabled = object.enabled;
commandMessage.speed = remap(object.speed * (config?.speedScalar ?? 1), -1, 1, 0, 200).round();
commandMessage.turn = remap(object.turn * (config?.turnScalar ?? 1), -1, 1, 0, 200).round();
commandMessage.speed =
remap(object.speed * (config?.speedScalar ?? 1), -1, 1, 0, 200)
.round();
commandMessage.turn =
remap(object.turn * (config?.turnScalar ?? 1), -1, 1, 0, 200).round();
commandMessage.auxiliary = object.auxiliary;
commandMessage.advanced = object.advanced;
commandMessage.angleP = object.angleP;
Expand Down
23 changes: 13 additions & 10 deletions lib/models/desired_state_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_joystick/flutter_joystick.dart';

import '../domain/dtos/command_message.dart';

class DesiredStateModel extends ChangeNotifier {
static final DesiredStateModel _instance = DesiredStateModel._internal();

Expand All @@ -19,7 +21,7 @@ class DesiredStateModel extends ChangeNotifier {
double speedP = 0;
double speedI = 0;
double speedD = 0;
int saveRecallState = 1;
int saveRecallState = SaveRecallState.recall.value;

// -1 to 1... mapped to 0 to 200 in the command message
double speed = 0;
Expand Down Expand Up @@ -48,20 +50,21 @@ class DesiredStateModel extends ChangeNotifier {
bool save = false,
}) {
advanced = true;
saveRecallState = save ? 2 : 1;
saveRecallState =
save ? SaveRecallState.save.value : SaveRecallState.recall.value;

angleP = angleP;
angleI = angleI;
angleD = angleD;
speedP = speedP;
speedI = speedI;
speedD = speedD;
this.angleP = angleP;
this.angleI = angleI;
this.angleD = angleD;
this.speedP = speedP;
this.speedI = speedI;
this.speedD = speedD;

notifyListeners();
}

void setSaveRecallState(int value) {
saveRecallState = value;
void setSaveRecallState(SaveRecallState state) {
saveRecallState = state.value;

notifyListeners();
}
Expand Down

0 comments on commit 8870b47

Please sign in to comment.