繁体   English   中英

Que while 循环

[英]Que while loop loops

我对编程很陌生,我正在制作一个程序,我想使用旋转编码器通过汽车 API 改变汽车 OTA 中的温度。 我在树莓派上用 Python 编码。 代码看起来像这样:

Que=0
While True:
      If encoder turns right:
         Que =+ 1
      if encoder turn left:
         Que =- 1
       
      while Que != 0
       if Que>0:
          Send request to car to turn up the temperature by 0.5 degrees
          Que =- 1
       if Que<0
          Send request to car to turn down the temperature by 0.5 degrees 
          Que =+ 1

当我单击它然后等待时,这非常有用。 但是,API 使用大约 0.5 秒来完成对汽车的请求并退出 while 循环。 因此,如果您将编码器向右转动 4 次,它只会发送一个请求。

我该如何解决这个问题?

如果没有具体的实现,这很难回答,可能是一些未优化的代码,function 或类似的被调用?

一个简单的答案似乎是使用更快的语言,如 C 或 C++ 如果速度是问题。

如果您想改进 python 代码本身,我建议您研究多线程。 一种用于添加操作,另一种用于在一次change by +/- x temp 以观察者模式为起点检查多线程

一种可能的加速方法是合并操作。 为什么要发出多个增加/减少值的操作,你能做到这一点吗?

Que=0
while True:
      if encoder turns right:
         Que =+ 1
      if encoder turn left:
         Que =- 1
       
      if Que != 0
       if Que>0:
          Send request to car to turn up the temperature by 0.5 degrees * Que
       elif Que<0
          Send request to car to turn down the temperature by 0.5 degrees * Que 
       Que = 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM