2016-11-11 44 views
0

我有问题(或缺乏人才)与我的C#代码。我使用这个代码来控制通过蓝牙机器人和我有它的工作很好,当我按: W - 前进, 的S - 向后, A - 左, d - 右, Q - 左前方, ë - 前进右, ž - 向后左, X - 向后右, Ø - 解除机器人手臂, P - 放下手臂, N - 打开的抓地力, 米 - 关闭握, ÿ - 停止所有命令以上,如何在我的代码中使用Control.KeyUp事件?

现在,这些都是通过Keydown命令处理,他们工作正常。问题是我不知道当我没有按下任何按键时如何停止这些命令。作为临时修复,我一直使用“Y”键来停止所有操作,但我相信有更好的方法来使用keyUp或类似的东西来解决这个问题。

你能给我一个例子,也许我的命令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 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事件? (请示例代码)](http://stackoverflow.com/questions/3674810/how-do-i-catch-keyup-event-sample-of-code-please) – meJustAndrew

+0

只是一个注释:我建议使用一个'''switch case'''或者至少'''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

谢谢Jupalli。这是有道理的。但我在哪里插入此代码?全部KeyDown逻辑之后还是在W命令之后? – Slavisha

+0

把它作为Form1_KeyDown的方法 –

+0

我试着按照你的建议去做。我在第一篇文章中发布了编辑好的代码。但那不行以太。我编辑了错误的代码吗? – Slavisha

1

首先,不建议使用非实时系统来控制搬东西。这包括蓝牙通信协议和PC本身(更具体地说,是Windows操作系统)。这样的系统将永远无法通过任何商业或工业测试 - 出现明显的安全问题。

现在回到您的问题。我会做的是,

  • 以“状态”方式而不是事件驱动方式定义PC和机器人控制器之间的协议。例如,不是发送刚刚按下或释放“前进”键的事件,而是发送描述是否正在按下“前进”键的状态。机器人不需要知道按键何时被按下或释放,只需要知道它是否应该向前移动。

  • 在PC端,维持一个状态机,在运行模式下,定时发送键状态,比如说每10ms。

  • 检测当前国家重点,您可以直接通过Keyboard.IsKeyDown()方法检测它,如果你使用WPF,或KEYUP /的KeyDown在机器人与WinForm的

  • 事件保持自己的状态控制器方面,事情变得相对容易,因为它们中的大多数以状态机的方式运行,并且最终你必须将控制命令转换成单比特数字输出到电机或伺服。而基于状态的通信协议完全适用于此。例如,“正在按下前进键”状态直接转换为“为X轴电机打开数字输出xxx”。

希望它有帮助。

+0

谢谢卡尔文。我拥有天体物理学学位,并且你只是用你所说的全心全意。 :-)。我对机器人学,尤其是编程方面非常陌生,我正在努力学习刚才所展示的技能和知识。在我头顶的那一刻。 – Slavisha

+0

注释了一下,我知道一个像这样受伤的人。他操作了一台带有像这样的键盘接口的机器人玻璃装载机。在剥下几张玻璃后,他注意到一块碎片并发出停止命令进行清理。控制器没有注意到并保持装载机移动。把他困在后面,做了最糟糕的事情,把他的脸推到了玻璃包的一角。它把脸碎成碎片,拿了9块手术把它们放回去。伤痕和不适从未得到修复。 –

相关问题