【问题标题】:Pan Tilt mechnanism云台机构
【发布时间】:2014-11-04 01:51:46
【问题描述】:

我是初学者,我有几个月的 opencv 和 python 经验。我正在尝试项目。我正在研究 python、opencv 和 ubuntu。我找到了这段代码(如下),我想在其中调用云台机制并使用 arduino 和伺服系统。我如何在其中定义伺服位置?代码任何人都可以指导我完成这个..我参考了很多例子,但我没有在这段代码中实现它..

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2, math
import numpy as np
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
class ColourTracker:
      def __init__(self):
       cv2.namedWindow("ColourTrackerWindow", cv2.CV_WINDOW_AUTOSIZE)
       self.capture = cv2.VideoCapture(0)
       self.scale_down = 4
      def run(self):
          while True:
            f, orig_img = self.capture.read()
            orig_img = cv2.flip(orig_img, 1)
            img = cv2.GaussianBlur(orig_img, (5,5), 0)
        laplacian = cv2.Laplacian(orig_img,cv2.CV_64F)
            sobelx = cv2.Sobel(orig_img,cv2.CV_64F,1,0,ksize=5)
                sobely = cv2.Sobel(orig_img,cv2.CV_64F,0,1,ksize=5)
            img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2HSV)
            img = cv2.resize(img, (len(orig_img[0]) / self.scale_down, len(orig_img) / self.scale_down))
            red_lower = np.array([0, 150, 0],np.uint8)
            red_upper = np.array([5, 255, 255],np.uint8)
            red_binary = cv2.inRange(img, red_lower, red_upper)
            dilation = np.ones((15, 15), "uint8")
            red_binary = cv2.dilate(red_binary, dilation)
        edge = cv2.Canny(red_binary,200,300,apertureSize = 3)
            contours, hierarchy = cv2.findContours(red_binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            max_area = 0
            largest_contour = None
            for idx, contour in enumerate(contours):
                area = cv2.contourArea(contour)
                if area > max_area:
                   max_area = area
                   largest_contour = contour
                if not largest_contour == None:
                    moment = cv2.moments(largest_contour)
                    if moment["m00"] > 1000 / self.scale_down:
                            rect = cv2.minAreaRect(largest_contour)
                            rect = ((rect[0][0] * self.scale_down, rect[0][1] * self.scale_down), (rect[1][0] * self.scale_down, rect[1][1] * self.scale_down), rect[2])
                            box = cv2.cv.BoxPoints(rect)
                            box = np.int0(box)
                            cv2.drawContours(orig_img,[box], 0, (0, 0, 255), 2)
                            cv2.imshow("ColourTrackerWindow", orig_img)
                            k = cv2.waitKey(10)
                        if cv2.waitKey(10) == 27:
                                cv2.destroyWindow("ColourTrackerWindow")
                                self.capture.release()
                                break
if __name__ == "__main__":
   colour_tracker = ColourTracker()
   colour_tracker.run()

提前谢谢你!!

Arduino 代码:

#include <Servo.h>

int p_fltXYRadius[0];

Servo servo;
Servo servo1;
int servoPosition = 90;
int servoPosition1=90 ;

int incomingByte = 0;   // for incoming serial data

void setup()
{
  Serial.begin(9600); // // opens serial port, sets data rate to 9600 bps

  servo.attach(9); // attaches the servo on pin 9 to the servo object
  servo1.attach(10);// attaches the servo1 on pin 10 to the servo object
  servo.write(servoPosition); // set the servo at the mid position
  servo.write(servoPosition1);// set the servo1 at the mid position

}

void loop()
{
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    switch(incomingByte)
    {
      // Rotate camera left
    case 'l':
      servoPosition+=1;
      delay(20);
      if (servoPosition > 180)
      {
        servoPosition = 180;
      }

      break;

      // Rotate camera right
    case 'r': 
      servoPosition-=1;
      delay(20);
      if (servoPosition < 0)
      {
        servoPosition = 0;
      }

      break;

      // Center camera
    case 'c':
      servoPosition = 90;
      delay(20);
      break;

      // Camera in upward direction
    case 'u':
      servoPosition1+=5;
      delay(100);
      if (servoPosition1 > 160)
      {
        servoPosition1 = 160;
      }

      break;

      // Camera in downward direction
    case 'd':
      servoPosition1-=5;

      if (servoPosition1 < 140)
      {
        servoPosition1 = 140;
      }

      break;  

      // Camera would move upward if it finds an object moving up
    case 'f':
      servoPosition1+=1;
      delay(100);
      if (servoPosition1 > 180)
      {
        servoPosition1 = 180;
      }

      break;

      // Camera would move downward if it finds an object moving down
    case 'e':
      servoPosition1-=1;
      delay(100);
      if (servoPosition1 < 0)
      {
        servoPosition1 = 0;
      }

      break;
    }
    servo.write(servoPosition);
    servo1.write(servoPosition1);

  }
}

【问题讨论】:

  • 您遇到了哪些具体问题?如果您陈述您的确切问题并提供说明问题的基本示例,我们将能够为您提供更好的帮助。
  • 我想在这段代码和arduino之间设置一个串行通信。我不了解如何定义关于 aduino 代码的串行通信
  • 好的,我不知道如何实现串口通信,所以这里帮不了你。但是,如果您说明错误是什么或代码如何失败以及以何种方式失败,它将帮助您获得答案。我希望你能尽快解决你的问题!
  • 感谢 hosch250!问题是如何调整python代码,设置串行通信b/w this和arduino ..我需要设置参数..

标签: python opencv arduino


【解决方案1】:

一切似乎都很好。 Arduino 正在等待您的命令。

在你的 python 代码中,你应该像这样向 arduino 发送命令:

ser.write('l')

使用适当的命令更改l

【讨论】:

  • 感谢 ahaltindis!我可能需要您的更多帮助,我应该如何以及在哪里声明“if 语句”并编写该代码?
猜你喜欢
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 2020-12-18
  • 2020-05-09
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多