- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想通过 PC 使用鼠标 x-y 坐标控制两个 Helm 机。光标x-y坐标发送到串口:
对于 arduino 部分,我得到 6 个字符串并将其分成两部分。然后将这些部分转换为整数值并发送到arduino引脚以设置 Helm 机位置:
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
}
void loop() {}
void serialEvent() {
while (Serial.available()) {
delay(2);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 3); //get the first three characters
servo2 = readString.substring(3, 6); //get the next three characters
Serial.println(servo1); //print ot serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
myservo1.write(n1); //set servo position
myservo2.write(n2);
readString="";
}
}
但是,代码很慢。我需要非常缓慢地移动鼠标以使 Helm 机移动。从比方说从 50 度到 170 度的即时移动需要 1 秒来移动伺服。在这种情况下,您能否提供更好的选择来控制两个伺服系统?
只控制一个 Helm 机效果很好,它可以立即移动 Helm 机,没有任何滞后:
#include <Servo.h>
Servo x;
int xval;
void setup() {
Serial.begin(9600);
x.attach(9);
}
void loop() {
}
void serialEvent() {
xval = Serial.parseInt();
if(xval!=0) {
x.write(xval);
}
}
C# 代码:
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
init();
}
private void init()
{
port = new SerialPort();
port.PortName = "COM1";
port.BaudRate = 9600;
//porty = new SerialPort();
try
{
port.Open();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
int x = 0, y = 0;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Graphics g = CreateGraphics();
Pen p = new Pen(Color.Navy);
Pen erase = new Pen(Color.White);
x = e.X; y = e.Y;
double curWidth = this.Width / 180;
double x2 = Math.Round(x / curWidth);
double curHeight = this.Height / 180;
double y2 = Math.Round(y / curHeight);
label1.Text = x2.ToString(); label2.Text = y2.ToString();
string valx = x2.ToString();
string valy = y2.ToString();
while(valx.Length < 3)
{
valx = '0' + valx;
}
while(valy.Length < 3)
{
valy = '0' + valy;
}
string valsum = valx+valy;
label3.Text = valsum.ToString();
if (port.IsOpen)
{
port.WriteLine(valsum);
}
}
}
在上面的代码中,我获取了 x 和 y 坐标并将其转换为大约 180 的范围。在我将这些值连接成一个字符串以通过串行端口发送它之后。
最佳答案
为了提高代码的性能,您可以删除延迟并使用最大波特率 (115200)。为了简单起见,我在下面压缩了您的 Arduino 代码:
#include <Servo.h>
// create 2 servos x and y
Servo x;
Servo y;
void setup() {
Serial.begin(115200);
x.attach(6);
y.attach(7);
}
void loop() {
//empty
}
void serialEvent() {
//set servo position.
if (Serial.available()>0) {
//Receives 2 angles separated per a comma
x.write(Serial.parseInt()); //Returns first angle
y.write(Serial.parseInt()); //Returns second angle
Serial.read(); //Read the '\n'. I am not sure if it is necessary.
}
}
同样在 C# 源代码中增加波特率:
port.BaudRate = 115200;
另外,建议大家把逗号隔开的角通过连载发过去。所以,而不是:
while(valx.Length < 3) {
valx = '0' + valx;
}
while(valy.Length < 3) {
valy = '0' + valy;
}
string valsum = valx+valy;
label3.Text = valsum.ToString();
if (port.IsOpen) {
port.WriteLine(valsum);
}
替换为:
if (port.IsOpen) {
port.WriteLine(valx + ',' + valy); //Note that WriteLine appends a '\n' character in the end.
}
关于c# - 使用鼠标arduino通过PC控制两个伺服系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383873/
大家好,我完成了这个基本的 C 程序,它向输入任何给定数字集的用户显示有序集、最小值、最大值、平均值和中值。我遇到的问题是,当我打印数字时,我必须使用诸如“3.2%f”之类的东西来设置标准的精度,我怎
我有这个基于 Python 的服务守护进程,它正在执行大量多路复用 IO(选择)。 从另一个脚本(也是 Python)我想查询这个服务守护进程的状态/信息和/或控制处理(例如暂停它、关闭它、更改一些参
我读到 Fortran 对表达式求值的顺序有严格的规则。对于某些数值算法来说,这一点非常重要。 数值 C 程序如何控制浮点运算的顺序并防止编译器“优化”到不需要的运算顺序,例如将 (a*b)*c 更改
上下文: 整个问题可以概括为我正在尝试复制调用system(或fork)的行为,但在 mpi 环境中。 (事实证明,你不能并行调用system。)这意味着我有一个程序在许多节点上运行,每个节点上有一个
我考虑过控制scanf来接受c中的任何输入。我的概念是等待10秒(或任何其他时间)来接受任何输入。10秒后它将退出并且不再接收任何输入。 int main(){ int a,b,c,d; sca
我正在尝试使用生成器停止 setTimeOut 上的执行流程。我究竟做错了什么?我无法让 console.log 每 1500 毫秒退出一次。我是 node 的新手,如果我在做一件非常愚蠢的事情,请不
我希望我的应用程序的 Activity 堆栈包含同一 Activity 的多个实例,每个实例处理不同的数据。因此,我将让 Activity A 在我的 Activity 堆栈中处理数据 a、b、c 和
我有这个 bash 文件,它向设备询问 OpenSSH 的 IP、密码等。 现在,如果我使用 ssh root@ip,我必须输入密码。这真的很烦人。第二;我不能让我的脚本向它发送命令。 这就是我想要的
我正在尝试测试我有权访问的机器的缓存属性。为此,我正在尝试读取内存并对其计时。我改变工作集大小和步幅访问模式以获得不同的测量值。 代码如下所示: clock1 = get_ticks() for (i
我正在尝试编写一个 makefile 来替换用于构建相当大的应用程序的脚本之一。 当前脚本一次编译一个文件,使用 make 的主要原因是并行化构建过程。使用 make -j 16 我目前在办公室服务器
我正在制作一个小的测试程序,它演示了一个粗糙的控制台界面。 该程序是一个低于标准的典型获取行、响应程序,它甚至不识别“退出”,并希望您通过按 control-c 强制退出。在 Mingw32 上完成。
好的,我有一个 VOIP 电话。我知道电话的 IP 地址和端口,并且可以完全访问电话,我正在使用它通过 SIP 中继调用 SIP 电话。 我基本上想随时查看手机上发生的事情,但我不知道从哪里开始。 如
是否可以指定 CWinApp::WriteProfileString() 使用的应用程序名称? 如果我使用 CWinApp::SetRegistryKey 将我的公司名称设置为“MyCompany”,
我正在尝试用 Python 控制 Tor。我在 stackoverflow 上阅读了其他几个关于这个主题的问题,但没有一个能回答这个问题。 我正在寻找一种方法,以便在命令运行时为您提供“新身份”、新
最近在做一个项目,涉及到iPhone设备和手表传输数据、控制彼此界面跳转,在网上找了很多资料,发现国内的网站这方面介绍的不多,而国外的网站写的也不是很全,所以在这写这篇文章,给大家参考一下,望大神指
我想增加图中值的范围。在示例中,值的范围从 50 到 200。但是,我需要按如下方式分配值:50 75 100 125 150 175 200 并且最好使用 scale_fill_gradientn
我有一个IconButton,当按下时波纹效果是圆形的并且比按钮的面积大,我怎样才能减少点击按钮时波纹效果的大小? IconButton( constraints
我正在使用代码契约(Contract)为我的项目生成附属程序集。基本上它为项目的 MyAssembly.dll 创建一个 MyAssembly.Contracts.dll。这应该放在你的程序集旁边,但
我想使用分面绘制图形,其中面板之间的边缘不同。面板按字母顺序自动排序(按照 ggplot 中的惯例)。一个简单的例子: library(igraph) library(ggraph) g <- mak
我想为我的 Android 应用程序创建一个小部件,以显示有关位置的一些实时详细信息,例如天气。但我想在任何时候允许最多 3 个小部件实例,每个实例都有不同的位置。我不确定该怎么做,也找不到任何信息。
我是一名优秀的程序员,十分优秀!