2016-11-11 1 views
0

C# 코드에 문제가 있거나 재능이 부족합니다. 저는 블루투스를 통해 로봇을 제어하기 위해이 코드를 사용하고 내가 누를 때 나는 그것이 잘 작동하고 있습니다 W - 앞으로, S - 뒤로, A - 왼쪽, D - 오른쪽, Q - 앞으로 왼쪽, E를 - 앞으로를 오른쪽, Z - 뒤로 왼쪽, 는 X - 뒤로 오른쪽, 는 O - 로봇 팔을 들어 올려 P는 - 팔을 내려 놓고, N - 그립을 열고 M이 - 닫 그립, Y - 모든 명령을 중지내 코드에서 Control.KeyUp 이벤트를 사용하는 방법?

이제는 Keydown 명령을 통해 처리되며 정상적으로 작동합니다. 문제는 내가 키를 누르지 않을 때 어떻게 이러한 명령을 멈추게되는지 모른다는 것입니다. 임시 수정으로 모든 작업을 중지하는 "Y"키를 사용했지만 keyUp 또는 이와 유사한 것을 사용하여이 문제를 해결하는 더 좋은 방법이 있다고 확신합니다.

제 명령으로 어쩌면 한 가지 예를 들어 주시겠습니까? W가 더 이상 눌러지지 않고 로봇을 멈추게하려면 어떻게해야합니까?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using AForge.Video; 

namespace RoverControl 
{ 
    public partial class Form1 : Form 
    { 
     MJPEGStream stream; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     // Declare the comands for Rover control// 
     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode.ToString() == "W")  // Keyboard characeter "W" // 
       try 
       { 
        serialPort1.Write("F");  // Passing the command "Forward" through letter "F" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); // Exception in the case letter "W" is pressed without connection being established// 
       } 

      if (e.KeyCode.ToString() == "S") // Keyboard characeter "S" // 
       try 
       { 
        serialPort1.Write("B");  // Passing the command "Backward" through letter "B" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "A") // Keyboard characeter "A" // 
       try 
       { 
        serialPort1.Write("L");  // Passing the command "Left" through letter "L" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "D") // Keyboard characeter "D" // 
       try 
       { 
        serialPort1.Write("R"); // Passing the command "Right" through letter "R" in arduino code// 
       } 
       catch (Exception) 
       { 
       MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "Q") // Keyboard characeter "Q" // 
       try 
       { 
       serialPort1.Write("G"); // Passing the command "Forward Left" through letter "G" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "E") // Keyboard characeter "E" // 
       try 
       { 
        serialPort1.Write("I"); // Passing the command "Forward Right" through letter "I" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "Z") // Keyboard characeter "Z" // 
       try 
       { 
        serialPort1.Write("H"); // Passing the command "Backward Left" through letter "H" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "X") // Keyboard characeter "X" // 
       try 
       { 
        serialPort1.Write("J"); // Passing the command "Backward Right" through letter "J" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "O") // Keyboard characeter "O" // 
       try 
       { 
        serialPort1.Write("O"); // Passing the command "Up" through letter "O" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "P") // Keyboard characeter "P" // 
       try 
       { 
        serialPort1.Write("P"); // Passing the command "Down" through letter "P" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "N") // Keyboard characeter "N" // 
       try 
       { 
        serialPort1.Write("N"); // Passing the command "Open" through letter "N" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "M") // Keyboard characeter "M" // 
       try 
       { 
        serialPort1.Write("M"); // Passing the command "Close" through letter "M" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "C") // Keyboard characeter "C" // 
       try 
       { 
        serialPort1.Write("C"); // Passing the command "" through letter "C" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "c") // Keyboard characeter "c" // 
       try 
       { 
        serialPort1.Write("c"); // Passing the command "" through letter "c" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "V") // Keyboard characeter "V" // 
       try 
       { 
        serialPort1.Write("V"); // Passing the command "" through letter "V" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "v") // Keyboard characeter "v" // 
       try 
       { 
        serialPort1.Write("v"); // Passing the command "" through letter "v" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "U") // Keyboard characeter "U" // 
       try 
       { 
        serialPort1.Write("U"); // Passing the command "" through letter "U" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "u") // Keyboard characeter "u" // 
       try 
       { 
        serialPort1.Write("u"); // Passing the command "" through letter "u" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "Y") // Keyboard characeter "Y" // 
       try 
       { 
        serialPort1.Write("S"); // Passing the command "Stop" through letter "S" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
     } 


      void stream_NewFrame(object sender, NewFrameEventArgs eventArgs) 
     { 
      Bitmap bmp = (Bitmap) eventArgs.Frame.Clone(); 
      pictureBox1.Image = bmp; 
     } 

     private void Start_Click(object sender, EventArgs e) 
     { 
      string IP = ""; 
      IP = textBox3.Text; 
      stream = new MJPEGStream(IP); 
      stream.NewFrame += stream_NewFrame; 
      try 
      { 
      stream.Start(); 
      } 
      catch (Exception) 
      { 
       MessageBox.Show("Please enter valid IP address."); 
      } 
     } 

     private void Stop_Click(object sender, EventArgs e) 
     { 
      stream.Stop(); 
     } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      string COM = ""; 
      COM = comboBox1.Text; 
      serialPort1.PortName = COM; 
      serialPort1.BaudRate = 9600; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       serialPort1.Open(); 
      } 
      catch (Exception) 
      { 
       var dialogResult = MessageBox.Show("Please select correct Comunication Port."); 
      } 
     } 

     private void Disconnect_Click(object sender, EventArgs e) 
     { 
      serialPort1.Close(); 
     } 

    } 
} 

좋아, 코드를 추가 할 때 나는 여전히 동일한 문제가 있다고 제안합니다. W 키를 눌러 앞으로 이동하고 W 키를 놓으면 계속 진행합니다. 코드를 잘못 편집 했습니까?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using AForge.Video; 

namespace RoverControl 
{ 
    public partial class Form1 : Form 
    { 
     MJPEGStream stream; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     // Declare the comands for Rover control// 
     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode.ToString() == "W")  // Keyboard characeter "W" // 
       try 
       { 
        serialPort1.Write("F");  // Passing the command "Forward" through letter "F" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); // Exception in the case letter "W" is pressed without connection being established// 
       } 

      if (e.KeyCode.ToString() == "S") // Keyboard characeter "S" // 
       try 
       { 
        serialPort1.Write("B");  // Passing the command "Backward" through letter "B" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "A") // Keyboard characeter "A" // 
       try 
       { 
        serialPort1.Write("L");  // Passing the command "Left" through letter "L" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "D") // Keyboard characeter "D" // 
       try 
       { 
        serialPort1.Write("R"); // Passing the command "Right" through letter "R" in arduino code// 
       } 
       catch (Exception) 
       { 
       MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "Q") // Keyboard characeter "Q" // 
       try 
       { 
       serialPort1.Write("G"); // Passing the command "Forward Left" through letter "G" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "E") // Keyboard characeter "E" // 
       try 
       { 
        serialPort1.Write("I"); // Passing the command "Forward Right" through letter "I" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "Z") // Keyboard characeter "Z" // 
       try 
       { 
        serialPort1.Write("H"); // Passing the command "Backward Left" through letter "H" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "X") // Keyboard characeter "X" // 
       try 
       { 
        serialPort1.Write("J"); // Passing the command "Backward Right" through letter "J" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "O") // Keyboard characeter "O" // 
       try 
       { 
        serialPort1.Write("O"); // Passing the command "Up" through letter "O" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 

      if (e.KeyCode.ToString() == "P") // Keyboard characeter "P" // 
       try 
       { 
        serialPort1.Write("P"); // Passing the command "Down" through letter "P" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "N") // Keyboard characeter "N" // 
       try 
       { 
        serialPort1.Write("N"); // Passing the command "Open" through letter "N" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "M") // Keyboard characeter "M" // 
       try 
       { 
        serialPort1.Write("M"); // Passing the command "Close" through letter "M" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "C") // Keyboard characeter "C" // 
       try 
       { 
        serialPort1.Write("C"); // Passing the command "" through letter "C" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "c") // Keyboard characeter "c" // 
       try 
       { 
        serialPort1.Write("c"); // Passing the command "" through letter "c" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "V") // Keyboard characeter "V" // 
       try 
       { 
        serialPort1.Write("V"); // Passing the command "" through letter "V" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "v") // Keyboard characeter "v" // 
       try 
       { 
        serialPort1.Write("v"); // Passing the command "" through letter "v" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "U") // Keyboard characeter "U" // 
       try 
       { 
        serialPort1.Write("U"); // Passing the command "" through letter "U" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "u") // Keyboard characeter "u" // 
       try 
       { 
        serialPort1.Write("u"); // Passing the command "" through letter "u" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
      if (e.KeyCode.ToString() == "Y") // Keyboard characeter "Y" // 
       try 
       { 
        serialPort1.Write("S"); // Passing the command "Stop" through letter "S" in arduino code// 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Please establish the connection with rover."); 
       } 
     } 
     void Form1_KeyUp(object sender, KeyEventArgs e) 
     { 
       try 
      { 
       serialPort1.Write("S"); // Passing the command "Stop" through letter "S" in arduino code// 
      } 
      catch (Exception) 
      { 
       MessageBox.Show("Please establish the connection with rover."); 
      } 
     } 

     void stream_NewFrame(object sender, NewFrameEventArgs eventArgs) 
     { 
      Bitmap bmp = (Bitmap) eventArgs.Frame.Clone(); 
      pictureBox1.Image = bmp; 
     } 

     private void Start_Click(object sender, EventArgs e) 
     { 
      string IP = ""; 
      IP = textBox3.Text; 
      stream = new MJPEGStream(IP); 
      stream.NewFrame += stream_NewFrame; 
      try 
      { 
      stream.Start(); 
      } 
      catch (Exception) 
      { 
       MessageBox.Show("Please enter valid IP address."); 
      } 
     } 

     private void Stop_Click(object sender, EventArgs e) 
     { 
      stream.Stop(); 
     } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      string COM = ""; 
      COM = comboBox1.Text; 
      serialPort1.PortName = COM; 
      serialPort1.BaudRate = 9600; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       serialPort1.Open(); 
      } 
      catch (Exception) 
      { 
       var dialogResult = MessageBox.Show("Please select correct Comunication Port."); 
      } 
     } 

     private void Disconnect_Click(object sender, EventArgs e) 
     { 
      serialPort1.Close(); 
     } 

    } 
} 
+0

[KeyUp 이벤트를 어떻게 catch합니까? (코드의 샘플을주세요)] (http://stackoverflow.com/questions/3674810/how-do-i-catch-keyup-event-sample-of-code-please) – meJustAndrew

+0

그냥 주석 : ''''else if'' 문 – Jules

+0

줄스, "W"포워드 행동에 대한 예를 들어 주시겠습니까? – Slavisha

답변

0

Repalce 생성자 :

 public Form1() 
     { 
      InitializeComponent(); 
      this.KeyUp += new KeyEventHandler(Form1_KeyUp); 
     } 

다음 우선

private void Form1_KeyUp(object sender, KeyEventArgs e) 
      {     
       if (e.KeyCode.ToString() == "S" || e.KeyCode.ToString() == "W")//Check conditions 
       { 
        try 
        { 
         serialPort1.Write("S"); // Passing the command "Stop" through letter "S" in arduino code// 
        } 
        catch (Exception) 
        { 
         MessageBox.Show("Please establish the connection with rover."); 
        } 
       } 
      } 
+0

주팔리 감사합니다. 이것은 의미가 있습니다. 하지만이 코드를 삽입 할 곳은 어디입니까? 모든 KeyDown 로직 이후 또는 W 명령 이후에? – Slavisha

+0

Form1_KeyDown 같은 방법으로 넣으십시오. –

+0

제안대로 시도했습니다. 나는 첫 번째 게시물에 편집 된 코드를 게시했습니다. 그러나 에테르는 효과가 없습니다. 코드를 잘못 편집 했습니까? – Slavisha

1

를 추가, 이동하는 것을 제어하는 ​​비 실시간 시스템을 사용하지 않는 것이 좋습니다. 여기에는 블루투스 통신 프로토콜과 PC 자체 (더 구체적으로 Windows OS)가 포함됩니다. 그러한 시스템은 명백한 안전 문제를 위해 상업적 또는 산업적 테스트를 통과 할 수 없습니다.

이제 질문에 답하십시오. 내가 뭘 할 것은

  • 대신 이벤트 구동 방식의하는 "상태"방식으로 PC와 로봇 컨트롤러 사이의 프로토콜을 정의합니다. 예를 들어 "앞으로 이동"키가 방금 눌렀다 놓았다는 이벤트를 보내는 대신 "앞으로 이동"키를 누르는 지 여부를 나타내는 상태를 보내야합니다. 로봇 측은 키를 눌렀다 놓을 때를 알 필요가 없으며 앞으로 움직여야하는지 여부 만 알면됩니다.

  • PC 측에서 실행 모드 일 때 정기적으로 키 상태를 보내는 상태 기계를 유지하십시오. 예를 들어 10ms마다 말하십시오.

  • 은 현재 키 상태를 감지, 당신은 당신이 WPF를 사용하는 경우 Keyboard.IsKeyDown() 메소드를 통해 직접 감지, 또는 로봇에의 WinForm

  • 와의 keyup /에서 KeyDown 이벤트와 자신의 상태를 유지할 수 있습니다 컨트롤러 쪽에서는 대부분이 상태 머신 방식으로 실행되기 때문에 상대적으로 쉬운 일이 발생합니다. 결국 제어 명령을 단일 비트 디지털 출력으로 변환하여 모터 나 서보로 전환해야합니다. 상태 기반 통신 프로토콜은 이와 완벽하게 작동합니다. 예를 들어 "앞으로 이동 키를 누르는 중"상태는 "X 축 모터의 디지털 출력 xxx 켜기"로 직접 변환됩니다.

도움이 되길 바랍니다.

+0

감사합니다. 나는 천체 물리학 학위를 가지고 있으며, 당신이 말한대로 내 마음을 날려 버릴뿐입니다. :-). 저는 로봇 공학에 특히 익숙하며 특히 프로그래밍 부분에 익숙합니다. 방금 설명한 기술과 지식을 배우기 위해 노력하고 있습니다. 그 순간에 내 머리를 조금 넘어. – Slavisha

+0

조금 주석을 달아서, 나는 이런 상처를 입은 남자를 알았다. 그는 이와 같은 키보드 인터페이스로 로봇 식 로더를 작동 시켰습니다. 유리 껍질을 벗긴 후 깨진 조각을 발견하고 청소 명령을 내렸다. 컨트롤러가 로더를 알아 차리고 움직이지 않게했습니다. 뒤에서 그를 붙잡고 가능한 한 최악의 상황을 만들었습니다. 그것은 유리 잔의 모퉁이에 바로 얼굴을 밀었습니다. 얼굴을 조각 냈다. 다시 수술하기 위해 9 번 수술을했다. 흉터와 불편 함은 결코 고쳐지지 않았습니다. –

관련 문제