How to Move Servo Motor Sg90

Asked

Viewed 42 times

0

Like I would do where the print("RED"), print("GREEN") and print("BLUE") to move a servo motor at 180°, what happens is that the engine does not move, (I’m using Raspberry pi 3 b+).

if r >= 50 and g >= 50 and b >= 50:
        print("WHITE")
    elif r > g and r > b:
        print("RED")
        try:
            p.ChangeDutyCycle(7.5)
            time.sleep(1)
            p.ChangeDutyCycle(12.5)
            time.sleep(1)
            p.ChangeDutyCycle(2.5)
            time.sleep(1)
    elif g > r and g > b:
        print("GREEN")
        try:
            p.ChangeDutyCycle(7.5)
            time.sleep(1)
            p.ChangeDutyCycle(12.5)
            time.sleep(1)
            p.ChangeDutyCycle(2.5)
            time.sleep(1)
    else:
        print("BLUE")
        try:
            p.ChangeDutyCycle(7.5)
            time.sleep(1)
            p.ChangeDutyCycle(12.5)
            time.sleep(1)
            p.ChangeDutyCycle(2.5)
            time.sleep(1)
    return rect

import cv2
import numpy as np
import matplotlib.pyplot as plt
import RPi.GPIO as GPIO
import time
from matplotlib.animation import FuncAnimation

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

p = GPIO.PWM(7,50)
p.start(7.5)

def grab_frame(cap):
    ret,frame = cap.read()
    return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

def atualizar(i):
    img = grab_frame(captura)
    im1.set_data(img)
    im2.set_data(retangulo(img))

def close(event):
    if event.key == 'q':
        plt.close(event.canvas.figure)

def unique_count_app(a):
    colors, count = np.unique(a.reshape(-1,a.shape[-1]), axis=0, return_counts=True)
    return colors[count.argmax()]

def retangulo(img):
    r, g, b = contar_kmeans(img)
    h, w, c = img.shape
    rect = np.zeros((h, w, 3), np.uint8)
    rect[0:h, 0:w] = (r,g,b)
    if r >= 50 and g >= 50 and b >= 50:
        print("WHITE")
    elif r > g and r > b:
        print("RED")
        try:
            p.ChangeDutyCycle(7.5)
            time.sleep(1)
            p.ChangeDutyCycle(12.5)
            time.sleep(1)
            p.ChangeDutyCycle(2.5)
            time.sleep(1)
    elif g > r and g > b:
        print("GREEN")
        try:
            p.ChangeDutyCycle(7.5)
            time.sleep(1)
            p.ChangeDutyCycle(12.5)
            time.sleep(1)
            p.ChangeDutyCycle(2.5)
            time.sleep(1)
    else:
        print("BLUE")
        try:
            p.ChangeDutyCycle(7.5)
            time.sleep(1)
            p.ChangeDutyCycle(12.5)
            time.sleep(1)
            p.ChangeDutyCycle(2.5)
            time.sleep(1)
    return rect

def contar_kmeans(img):
    data = np.reshape(img, (-1,3))
    data = np.float32(data)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    flags = cv2.KMEANS_RANDOM_CENTERS
    compactness, labels, centers = cv2.kmeans(data, 1, None, criteria, 10, flags)
    return centers[0]

captura = cv2.VideoCapture(0)
imagem = grab_frame(captura)

#Cria os dois subplots
ax1 = plt.subplot(1,2,1)
    ax2 = plt.subplot(1,2,2)

    #Cria duas imagens nos subplots
    im1 = ax1.imshow(imagem)
    im2 = ax2.imshow(retangulo(imagem))

    #Animação e atualização
    ani = FuncAnimation(plt.gcf(), atualizar, interval=200)

    except KeyboardInterrupt:
    p.stop()

    GPIO.cleanup()
    #Fechar 
    cid = plt.gcf().canvas.mpl_connect("key_press_event", close)
    #Mostrar o gráfico
    plt.show()
  • What is the question?...

  • How to move a servo motor in IF..., I’ve tried a few ways and failed.

  • The command try does not work alone, without except or else or finally... Something is missing, it is difficult to understand the question

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.