Skip to content

Commit

Permalink
Added serial monitor and data generator scripts (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Lesches authored May 9, 2024
1 parent 898ad39 commit 54713fe
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 35 deletions.
63 changes: 63 additions & 0 deletions bin/data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import "dart:math";

import "package:burt_network/burt_network.dart";
import "package:subsystems/subsystems.dart";

class SwingingIterator implements Iterator<double> {
final double min;
final double max;
final double increment;
SwingingIterator(this.min, this.max, this.increment) : current = min;

bool forward = true;
@override double current;

@override
bool moveNext() {
if (current >= max && forward) forward = false;
if (current <= min && !forward) forward = true;
if (forward) {
current += increment;
} else {
current -= increment;
}
return true;
}
}

Future<void> main() async {
final server = SubsystemsServer(port: 8001);
await server.init();
final throttle = SwingingIterator(0, 1, 0.01);
final voltage = SwingingIterator(24, 30, 0.1);
final current = SwingingIterator(0, 30, 0.1);
final motor = SwingingIterator(0, pi, 0.01);
final motor2 = SwingingIterator(0, 2*pi, 0.05);
while (true) {
throttle.moveNext();
voltage.moveNext();
current.moveNext();
motor.moveNext();
motor2.moveNext();
final data = DriveData(
left: 1,
setLeft: true,
right: -1,
setRight: true,
throttle: throttle.current,
setThrottle: true,
batteryVoltage: voltage.current,
batteryCurrent: current.current,
);
server.sendMessage(data);
final data2 = ArmData(
base: MotorData(angle: motor2.current),
shoulder: MotorData(angle: motor.current),
elbow: MotorData(angle: motor.current),
);
server.sendMessage(data2);
final data3 = GripperData(lift: MotorData(angle: motor.current));
server.sendMessage(data3);
await Future<void>.delayed(const Duration(milliseconds: 10));
}
}
87 changes: 57 additions & 30 deletions bin/serial.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "dart:io";
import "dart:typed_data";
import "package:burt_network/burt_network.dart";
import "package:libserialport/libserialport.dart";
Expand All @@ -6,41 +7,67 @@ final logger = BurtLogger();

bool ascii = false;

void main(List<String> args) async {
if (args.isEmpty) {
logger.info("Ports: ${SerialPort.availablePorts}");
return;
} else if (args.contains("-h") || args.contains("--help")) {
logger.info("Usage: dart run -r :serial [port] [-a | --ascii]");
return;
}
var port = args.first;
if (!port.startsWith("/dev")) port = "/dev/$port";
if (args.contains("-a") || args.contains("--ascii")) {
logger.info("Running in ASCII mode");
ascii = true;
}
logger.info("Connecting to $port...");
final device = SerialDevice(
portName: port,
readInterval: const Duration(milliseconds: 100),
Future<void> listenToDevice(String port) async {
logger.info("Connecting to $port...");
final device = SerialDevice(
portName: port,
readInterval: const Duration(milliseconds: 100),
logger: logger,
);
if (!await device.init()) {
logger.critical("Could not connect to $port");
return;
}
logger.info("Connected. Listening...");
device.stream.listen(process);
device.startListening();
}

Future<void> listenToFirmware(String port) async {
logger.info("Connecting to $port...");
final device = BurtFirmwareSerial(
port: port,
logger: logger,
);
if (!await device.init()) {
);
if (!await device.init()) {
logger.critical("Could not connect to $port");
return;
}
logger.info("Connected. Listening...");
device.stream.listen(process);
device.startListening();
logger.info("Connected? ${device.isReady}. Listening...");
device.stream?.listen(process);
}

void main(List<String> args) async {
if (args.isEmpty) {
logger.info("Ports: ${SerialPort.availablePorts}");
return;
} else if (args.contains("-h") || args.contains("--help")) {
logger.info("Usage: dart run -r :serial [port] [-a | --ascii]");
return;
}
var port = args.first;
if (!Platform.isWindows && !port.startsWith("/dev")) port = "/dev/$port";
if (args.contains("-a") || args.contains("--ascii")) {
logger.info("Running in ASCII mode");
ascii = true;
}
if (args.contains("-f") || args.contains("--firmware")) {
await listenToFirmware(port);
} else {
await listenToDevice(port);
}
}

void process(Uint8List buffer) {
if (ascii) {
final s = String.fromCharCodes(buffer).trim();
logger.debug("Got string: $s");
} else {
final data = DriveData.fromBuffer(buffer);
logger.debug("Got data: ${data.toProto3Json()}");
}
if (ascii) {
final s = String.fromCharCodes(buffer).trim();
logger.debug("Got string: $s");
} else {
try {
final data = ScienceData.fromBuffer(buffer);
logger.debug("Got data: ${data.toProto3Json()}");
} catch (error) {
logger.error("Could not decode DriveData: $error\n Buffer: $buffer");
}
}
}
2 changes: 0 additions & 2 deletions lib/src/messages/can.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "service.dart";

/// Maps CAN IDs to [WrappedMessage.name] for data messages.
final Map<int, String> dataCanIDs = {
0x13: ElectricalData().messageName,
0x14: DriveData().messageName,
0x15: ArmData().messageName,
0x16: GripperData().messageName,
Expand All @@ -28,7 +27,6 @@ final Map<String, int> commandCanIDs = {
GripperCommand().messageName: 0x33,
ScienceCommand().messageName: 0x43,
DriveCommand().messageName: 0x53,
ElectricalCommand().messageName: 0x63,
};

/// Manages a CAN socket on the subsystems program.
Expand Down
4 changes: 1 addition & 3 deletions lib/src/messages/serial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import "service.dart";
final nameToDevice = <String, Device>{
ArmCommand().messageName: Device.ARM,
GripperCommand().messageName: Device.GRIPPER,
ElectricalCommand().messageName: Device.ELECTRICAL,
DriveCommand().messageName: Device.ELECTRICAL,
DriveCommand().messageName: Device.DRIVE,
ScienceCommand().messageName: Device.SCIENCE,
};

Expand Down Expand Up @@ -79,7 +78,6 @@ class SerialService extends MessageService {
final name = switch (serial.device) {
Device.ARM => ArmData().messageName,
Device.DRIVE => DriveData().messageName,
Device.ELECTRICAL => ElectricalData().messageName,
Device.GRIPPER => GripperData().messageName,
Device.SCIENCE => ScienceData().messageName,
_ => null,
Expand Down

0 comments on commit 54713fe

Please sign in to comment.