首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Que时间循环

Que时间循环
EN

Stack Overflow用户
提问于 2022-09-10 13:46:39
回答 1查看 33关注 0票数 0

我对编程和我做一个程序来说是很新的,我想用旋转编码器来用cars API改变汽车OTA中的温度。我在Raspberry Pi上用Python编写代码。代码如下所示:

代码语言:javascript
复制
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秒来完成对car的请求并退出while循环。因此,如果你把编码器4点击向右,它只发送一个请求。

我该怎么解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2022-09-10 13:57:00

如果没有具体的实现,这是很难回答的,也许正在调用一些未优化的代码、函数或类似的代码?

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

如果您想改进python代码本身,我建议您研究一下多线程。一个用于添加操作,另一个用于在一个change by +/- x temp中执行当前的操作总数(即a可能是操作的总和)。检查multithreading with observer pattern作为起点。

一个可能的加速是合并操作。为什么要执行多个增加/减少值的操作,你能一次完成吗?

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73672307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档