public override void Start() { try { serialPort.Open(); } catch { hasError = true; return; } byte[] buffer = new byte[EMGPacket.PACKET_SIZE]; while (running) { bool readOk = true; EMGPacket packet = new EMGPacket(); for (int i = 0; i < buffer.Length && readOk; i++) { int val = serialPort.ReadByte(); if (val == -1) { running = false; } byte readByte = (byte)val; if ((i == 0 && readByte != EMGPacket.SYNC0_BYTE) || (i == 1 && readByte != EMGPacket.SYNC1_BYTE) || (i == 2 && readByte != EMGPacket.VERSION_BYTE)) { readOk = false; } buffer[i] = readByte; } if (readOk) { packet.Unpack(buffer); packetQueue.Enqueue(packet); while (packetQueue.Count > maxQueueSize) { EMGPacket temp; packetQueue.TryDequeue(out temp); } } } serialPort.Close(); }
internal static List <TrainingValue> GetTrainingValues(List <EMGPacket> packets, bool enableSkip) { List <TrainingValue> values = new List <TrainingValue>(EMGProcessor.FFT_SAMPLE_SIZE); int skipsRemaining = 0; for (int i = 0; i < packets.Count / EMGProcessor.FFT_SAMPLE_SIZE; i++) { if (enableSkip && skipsRemaining > 0) { skipsRemaining--; continue; } Complex[] data = new Complex[EMGProcessor.FFT_SAMPLE_SIZE]; int start = i * EMGProcessor.FFT_SAMPLE_SIZE; int end = start + EMGProcessor.FFT_SAMPLE_SIZE; MuscleState startMuscleState = packets[start].muscleStateHint; for (int j = start; j < end; j++) { EMGPacket packet = packets[j]; if (packet.muscleStateHint != startMuscleState) { skipsRemaining += EMGProcessor.SKIPS_AFTER_TRANSITION; break; } data[j - start] = new Complex(EMGProcessor.ValueFromPacket(packet), 0); } if (enableSkip && skipsRemaining > 0) { continue; } FourierTransform.FFT(data, FourierTransform.Direction.Forward); List <Complex> fftResults = new List <Complex>(data); TrainingValue trainingValue = new TrainingValue((int)packets[start].muscleStateHint, EMGProcessor.FEATURE_COUNT); EMGProcessor.FillTrainingValue(ref trainingValue, fftResults); values.Add(trainingValue); } return(values); }
public override void Start() { byte[] buffer = new byte[EMGPacket.PACKET_SIZE_W_HINT]; while (running) { EMGPacket packet = new EMGPacket(); bool readOk = true; for (int i = 0; i < buffer.Length; i++) { int val = GetNextByte(); if (val == -1) { running = false; readOk = false; } byte readByte = (byte)val; buffer[i] = readByte; } if (readOk) { if (emulateSerialDelay) { Thread.Sleep(2); } packet.Unpack(buffer); packetQueue.Enqueue(packet); } while (maxQueueSize != -1 && packetQueue.Count > maxQueueSize) { EMGPacket temp; packetQueue.TryDequeue(out temp); } } fileStream.Close(); }
public static float ValueFromPacket(EMGPacket packet) { return(packet.channels[0]); }