我对编程和我做一个程序来说是很新的,我想用旋转编码器来用cars API改变汽车OTA中的温度。我在Raspberry Pi上用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秒来完成对car的请求并退出while循环。因此,如果你把编码器4点击向右,它只发送一个请求。
我该怎么解决这个问题?
发布于 2022-09-10 13:57:00
如果没有具体的实现,这是很难回答的,也许正在调用一些未优化的代码、函数或类似的代码?
一个琐碎的答案似乎是使用更快的语言,如C,或C++,如果速度是问题。
如果您想改进python代码本身,我建议您研究一下多线程。一个用于添加操作,另一个用于在一个change by +/- x temp中执行当前的操作总数(即a可能是操作的总和)。检查multithreading with observer pattern作为起点。
一个可能的加速是合并操作。为什么要执行多个增加/减少值的操作,你能一次完成吗?
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 = 0https://stackoverflow.com/questions/73672307
复制相似问题