【问题标题】:Control on led by health bar percentage in league of legends在英雄联盟中控制由健康栏百分比领导
【发布时间】:2021-04-28 04:47:45
【问题描述】:

我编写了一个代码来控制英雄联盟中由健康百分比变化引起的变化。我使用 python 和 OCR 来检测健康状况并将健康状况数字发送到 Arduino 来控制 LED,使其在健康状况 > 80% 时延迟 3 秒闪烁,当健康状况

问题在于,对于所有健康百分比,LED 只会延迟 3 秒闪烁。我该怎么办 ?提前谢谢

Python 代码:

 import serial
import time
import numpy as np
import cv2
from PIL import ImageGrab, Image
import os
import pytesseract

ser = serial.Serial('COM3', 9600)
#This loop allows opencv to capture the screen continuously
while True:

  img = ImageGrab.grab(bbox=[840, 1020, 940, 1050 ])

  
  img_np = np.array(img)

  frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
  text = pytesseract.image_to_string(frame)
 
  v3=-1
  if "/" in text:
    x = text.split("/")
    try:
        v1 = int(x[0])
        v2 = int(x[1])
        v3 = int((v1/v2)*100) 
        if v3<=100: 
           print(str(v3))
           v4= str(abs(v3))
           ser.write(b'v4')
           time.sleep(0.1)
           time.sleep(2)
           

          
    except:
        pass
   
  
  
  cv2.imshow("Screen", frame)

  if cv2.waitKey(1) == 27:
    break

cv2.destroyAllWindows()

Arduino 代码

const int led=8;
String value;
int i;

void setup() 
   { 
      Serial.begin(9600); 
      pinMode(led, OUTPUT);
      Serial.println("Connection established...");
   }
 
void loop() 
   {
     while (Serial.available())
        {
             value = Serial.read();
             i = value.toInt();
             if (100 >= i > 80)
             {
                digitalWrite(led, HIGH);    
                delay(3000);
                digitalWrite(led, LOW);
             }
             else if (0 <= i < 80)
             {
                digitalWrite(led, HIGH);    
                delay(500);
                digitalWrite(led, LOW);
             }
             else { digitalWrite(led, LOW);
             }
        }
     
   }

在此处输入代码

【问题讨论】:

  • 这不是调试服务。你应该自己缩小问题的范围。例如,您应该打印 Arduino 收到的值,以找出问题出在项目的 Arduino 方面还是 Python 方面……您不需要我们将您的项目分解为多个部分并检查每个部分他们。你得到正确的 OCR 结果吗,你发送了正确的值,你收到了正确的值。这就是你可以通过简单的印刷品找到的所有东西。您不需要我们的专业知识
  • 感谢您的建议,我真的很感激。我确保 python 端没问题。但是如何监控 Arduino 输出?因为当我尝试使用串行监视器时,它给了我一个端口繁忙的错误,因为 python 使用该端口
  • 要么将第二个串行接口连接到您的 arduino(ttl 到 USB 适配器或逻辑分析仪),或者更简单地让您的 Arduino 将每个接收到的字节发送回 python

标签: python arduino serial-port ocr tesseract


【解决方案1】:

我在ser.write(b'v4') 中假设您不需要“'”,因为现在您发送的只是字符串“v4”,无论它的值如何。

【讨论】:

  • 感谢您的建议。但是python发送一个错误,它是一个Unicode字符串
  • 您仍然需要将 v4 转换为字节字符串,这就是 ser.write(b'v4') 中的 'b' 的用途。但由于您不能再使用它,我认为ser.write( str.encode(v4) ) 可能适合您的情况。
【解决方案2】:

Serial.read() 只读取 1 个字节的数据。如果在您的delay() 期间有更多字节进入,仍然会读取最旧的字节。这意味着您的串行端口可能完全填充了大于 80 的值。 你应该做以下两件事:

  1. 分解您的 arduino 程序,以确保您在合理的时间内实际接收并评估端口上的正确值。
  2. 在没有delay() 的情况下让 LED 闪烁。这是如何做到这一点的众多教程之一:https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html

【讨论】:

  • 谢谢你的建议,我真的很感激。如何监控 Arduino 输出?因为当我尝试使用串行监视器时,它给了我一个端口繁忙的错误,因为 python 使用该端口。
  • 你必须用你的python程序来做,因为它使用的是串口。另一种可能性是使用串行控制台发送应由 arduino 使用的数据。如果你想使用 Arduino IDE 串行监视器,你必须知道,它总是发送 ASCII 字符,而不是二进制数字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 2021-07-22
  • 2020-09-22
  • 1970-01-01
相关资源
最近更新 更多