![](/img/trans.png)
[英]I have a problem in integration using Trapezoidal method using python. My function tends to infinity at the upper limit. How to handle this?
[英]Sending int from python to arduino, but there is an upper limit. How do I solve it?
在我的项目中,我将 int 从 python 发送到 arduino,这是步进电机应该采取的步数。 Python 代码:
while(1):
userInput = input('Get data point?')
ser.write("s{}".format(int(131.0268562*userInput)).encode())
if userInput == 'c':
break;
Arduino 代码:
if (Serial.available() > 0){
int receivedValue = Serial.parseInt();
digitalWrite(4,HIGH);
if (receivedValue>=0){
for(Index = 0; Index < receivedValue; Index++)
{
digitalWrite(5,HIGH);
delayMicroseconds(s);
digitalWrite(5,LOW);
delayMicroseconds(s);
}
}
}
但是当值大于 30000 时,代码不起作用。 当我在 arduino 端打印值时,它要么是负数,要么是一些小的正数
为什么会发生,我该如何解决?
编辑:感谢“quamrana”和“Serge Ballesta”的帮助,我已经解决了它
如果有人偶然发现类似的问题。 只需使用:
long receivedValue = Serial.Stream::parseInt();
我会假设限制恰好是 32767,即 2**15 - 1。这暗示 Arduino parseInt
返回 16 位,2 的补码 integer。
如果你在 Arduino 上有更大的整数类型(长?),你应该尝试使用它。 另一种方法是使用不同的编码传递两个整数,每个整数最多 15 位。 最后,您将循环高值时间 32767 次 + 低值时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.