Skip to content

feat: Ported robotic arm #2729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/communication/science_lab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,44 @@ class ScienceLab {
logger.e(e);
}
}

Future<void> servo4(
double angle1, double angle2, double angle3, double angle4) async {
const int period = 10000;
const int base = 750;
const int range = 1900;
const int params = (1 << 5) | 2;

try {
mPacketHandler.sendByte(mCommandsProto.wavegen);

mPacketHandler.sendByte(mCommandsProto.sqr4);

mPacketHandler.sendInt(period);

int pulse1 = base + (angle1 * range ~/ 180);
mPacketHandler.sendInt(pulse1);

mPacketHandler.sendInt(0);

int pulse2 = base + (angle2 * range ~/ 180);
mPacketHandler.sendInt(pulse2);

mPacketHandler.sendInt(0);

int pulse3 = base + (angle3 * range ~/ 180);
mPacketHandler.sendInt(pulse3);

mPacketHandler.sendInt(0);

int pulse4 = base + (angle4 * range ~/ 180);
mPacketHandler.sendInt(pulse4);

mPacketHandler.sendByte(params);

await mPacketHandler.getAcknowledgement();
} catch (e) {
logger.e("Error in servo4(): $e");
}
}
}
22 changes: 22 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ String maxValue = 'Max: ';
String gyroscopeTitle = "Gyroscope";
String gyroscopeAxisLabel = 'rad/s';
String noData = 'No data available';
String degreeSymbol = '°';
String enterAngleRange = 'Enter angle (0 - 360)';
String errorCannotBeEmpty = 'Cannot be empty';
String servoValidNumberRange = 'Please enter a valid number between 0 and 360';
String ok = 'Ok';
String roboticArm = 'Robotic Arm';
String play = 'Play';
String pause = 'Pause';
String stop = 'Stop';
String controls = 'Controls';
String saveData = 'Save Data';
String showGuide = 'Show Guide';
String showLoggedDataKey = 'show_logged_data';
String showLoggedData = 'Show Logged Data';
String setAngle = 'Set angle for Servo';
String angleDialog = 'AngleDialog';
List<String> servoLabels = [
'Servo 1',
'Servo 2',
'Servo 3',
'Servo 4',
];
String xyPlot = 'XY Plot';
String enablePlot = 'Enable XY Plot';
String trigger = 'Trigger';
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:pslab/view/gyroscope_screen.dart';
import 'package:pslab/view/instruments_screen.dart';
import 'package:pslab/view/luxmeter_screen.dart';
import 'package:pslab/view/oscilloscope_screen.dart';
import 'package:pslab/view/robotic_arm_screen.dart';
import 'package:pslab/view/settings_screen.dart';
import 'package:pslab/view/about_us_screen.dart';
import 'package:pslab/view/software_licenses_screen.dart';
Expand Down Expand Up @@ -54,6 +55,7 @@ class MyApp extends StatelessWidget {
'/softwareLicenses': (context) => const SoftwareLicensesScreen(),
'/accelerometer': (context) => const AccelerometerScreen(),
'/gyroscope': (context) => const GyroscopeScreen(),
'/roboticArm': (context) => const RoboticArmScreen(),
'/luxmeter': (context) => const LuxMeterScreen(),
'/soundmeter': (context) => const SoundMeterScreen(),
},
Expand Down
127 changes: 127 additions & 0 deletions lib/providers/robotic_arm_state_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pslab/communication/science_lab.dart';
import 'package:pslab/others/logger_service.dart';
import '../others/science_lab_common.dart';

class RoboticArmStateProvider extends ChangeNotifier {
final List<double> servoValues = [0, 0, 0, 0];
final int totalTimelineItems = 60;
final double scrollAmountPerTick = 120;
final List<List<double>> timelineDegrees =
List.generate(60, (_) => List.filled(4, 0));
int timelinePosition = 0;
bool isPlaying = false;

late ScienceLab scienceLab;
Timer? _debounceTimer;
Timer? _timelineTimer;
final ScrollController timelineScrollController = ScrollController();

Future<void> initialize() async {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
scienceLab =
ScienceLabCommon(ScienceLabCommon.communicationHandler).getScienceLab();
await scienceLab.connect();
}

void disposeResources() {
_timelineTimer?.cancel();
_debounceTimer?.cancel();
timelineScrollController.dispose();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}

void updateServoValue(int index, double value) {
servoValues[index] = value;
notifyListeners();
_sendAllServoCommands();
}

void updateTimelineDegree(int timeIndex, int servoIndex, double value) {
timelineDegrees[timeIndex][servoIndex] = value;
notifyListeners();
}

void _sendAllServoCommands() {
_debounceTimer?.cancel();
_debounceTimer = Timer(const Duration(milliseconds: 200), () async {
try {
await scienceLab.servo4(
servoValues[0],
servoValues[1],
servoValues[2],
servoValues[3],
);
} catch (e) {
logger.e(e);
}
});
}

void togglePlayPause() {
if (isPlaying) {
_timelineTimer?.cancel();
isPlaying = false;
notifyListeners();
} else {
_timelineTimer =
Timer.periodic(const Duration(seconds: 1), (timer) async {
if (timelinePosition >= totalTimelineItems) {
stopScrolling(resetPosition: false);
return;
}

timelineScrollController.animateTo(
timelineScrollController.offset + scrollAmountPerTick,
duration: const Duration(milliseconds: 50),
curve: Curves.easeInOut,
);

final angles = timelineDegrees[timelinePosition];

if (scienceLab.isConnected()) {
try {
await scienceLab.servo4(
angles[0],
angles[1],
angles[2],
angles[3],
);
} catch (e) {
logger.e(e);
}
}

timelinePosition++;
notifyListeners();
});

isPlaying = true;
notifyListeners();
}
}

void stopScrolling({bool resetPosition = true}) {
_timelineTimer?.cancel();
isPlaying = false;
timelinePosition = 0;
notifyListeners();

if (resetPosition) {
timelineScrollController.animateTo(
0,
duration: const Duration(milliseconds: 50),
curve: Curves.easeOut,
);
}
}
}
12 changes: 12 additions & 0 deletions lib/view/instruments_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ class _InstrumentsScreenState extends State<InstrumentsScreen> {
);
}
break;
case 12:
if (Navigator.canPop(context) &&
ModalRoute.of(context)?.settings.name == '/roboticArm') {
Navigator.popUntil(context, ModalRoute.withName('/roboticArm'));
} else {
Navigator.pushNamedAndRemoveUntil(
context,
'/roboticArm',
(route) => route.isFirst,
);
}
break;
case 15:
if (Navigator.canPop(context) &&
ModalRoute.of(context)?.settings.name == '/soundmeter') {
Expand Down
Loading