private void clearDriverState() { lock (_L_instructionCompletition) { _currentState = new StateInfo(); _plannedState = new StateInfo(); _sendQueue.Clear(); _incompleteInstructionQueue.Clear(); _incompleteInstructions = 0; Monitor.Pulse(_L_instructionCompletition); } lock (_L_confirmation) { _stateDataRemainingBytes = 0; _expectsConfirmation = false; Monitor.Pulse(_L_confirmation); } }
/// <summary> /// Sends a given part to the machine. /// </summary> /// <param name="instruction">Part to send.</param> public bool SEND(InstructionCNC instruction) { if (instruction.IsEmpty) { return(true); } var testState = _plannedState.Copy(); if (!checkBoundaries(instruction, ref testState)) { return(false); } _plannedState = testState; send(instruction); return(true); }
public DriverCNC2() { _plannedState = new StateInfo(); _currentState = new StateInfo(); _currentState.Initialize(); _plannedState = _currentState.Copy(); _positionEstimator = new Thread(runPositionEstimation); _positionEstimator.IsBackground = true; _positionEstimator.Priority = ThreadPriority.Lowest; _communicationWorker = new Thread(SERIAL_worker); _communicationWorker.IsBackground = true; _communicationWorker.Priority = ThreadPriority.Highest; if (LOG_STEPS) { StepLogger = new StepLogger("."); } }
private bool checkBoundaries(InstructionCNC instruction, ref StateInfo state) { state.Accept(instruction); return(state.CheckBoundaries()); }
private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { var data = readData(_port); foreach (var dataByte in data) { var response = (char)dataByte; if (_stateDataRemainingBytes > 0) { _stateDataBuffer[_stateDataBuffer.Length - _stateDataRemainingBytes] = (byte)response; --_stateDataRemainingBytes; if (_stateDataRemainingBytes == 0) { lock (_L_instructionCompletition) { _incompleteInstructionQueue.Dequeue(); _currentState.SetState(_stateDataBuffer); --_incompleteInstructions; if (_incompleteInstructionQueue.Count > 0) { throw new NotSupportedException("State retrieval requires empty queue"); } _plannedState = _currentState.Copy(); Monitor.Pulse(_L_instructionCompletition); } } continue; } if (_isCommentEnabled) { if (response == '\n') { _isCommentEnabled = false; } continue; } switch (response) { case '1': //_resetConnection = true; clearDriverState(); break; case 'a': //authentication is required clearDriverState(); _port.Write("$%#"); break; case 'A': //authentication succeeded break; case 'I': //first reset plans - the driver is blocked on expects confirmation clearDriverState(); send(new StateDataInstruction()); break; case 'D': lock (_L_confirmation) { _expectsConfirmation = false; Monitor.Pulse(_L_confirmation); _stateDataRemainingBytes = _stateDataBuffer.Length; } break; case 'H': lock (_L_confirmation) { _expectsConfirmation = false; Monitor.Pulse(_L_confirmation); } lock (_L_instructionCompletition) { onInstructionCompleted(); Monitor.Pulse(_L_instructionCompletition); } //homing was finished successfuly OnHomeCalibrated?.Invoke(); break; case 'Y': lock (_L_confirmation) { _expectsConfirmation = false; Monitor.Pulse(_L_confirmation); } break; case 'F': var incompleteInstrutionCount = 0; lock (_L_instructionCompletition) { onInstructionCompleted(); incompleteInstrutionCount = _incompleteInstructionQueue.Count; Monitor.Pulse(_L_instructionCompletition); } if (incompleteInstrutionCount == 0 && OnInstructionQueueIsComplete != null) { OnInstructionQueueIsComplete(); } break; case 'S': //scheduler was enabled System.Diagnostics.Debug.WriteLine("S: " + IncompleteInstructionCount); break; case 'M': //step time was missed break; case 'E': throw new NotImplementedException("Incomplete message erased"); case '|': _isCommentEnabled = true; break; default: // throw new NotImplementedException(); System.Diagnostics.Debug.Write(response); break; } } OnDataReceived?.Invoke(Encoding.ASCII.GetString(data.ToArray())); }