简体   繁体   English

使用箭头控制步进电机

[英]Controlling step motor using arrows

I'm doing a program on a raspberry pi with which I can control a motor using the keyboard arrows.我在树莓派上做一个程序,我可以使用键盘箭头控制电机。 For that, I used from GPIO Zero module as the example for the Keyboard control robot.为此,我使用 GPIO Zero 模块作为键盘控制机器人的示例。 I managed to update the code for my needing, but when I'm pressing the keys, nothing is happening.我设法根据需要更新了代码,但是当我按下按键时,什么也没有发生。

Can someone help me with this problem?有人可以帮我解决这个问题吗? Thank you in advance先感谢您

For controlling the step motors I'm using the Adafruit module.为了控制步进电机,我使用了 Adafruit 模块。

import curses
import time
from gpiozero import Robot
from adafruit_servokit import ServoKit

kit = ServoKit(channels=8)

            
def cameraDown():
        kit.servo[0].angle = 0
        time.sleep(0.01)
        
def cameraUp():
        kit.servo[0].angle = 180
        time.sleep(0.01)
        
def cameraFront():
        kit.servo[0].angle = 90
        time.sleep(0.01)
        
def cameraFace():
        kit.servo[0].angle = 70
        time.sleep(0.01)

actions = {
    curses.KEY_UP:  cameraDown(),
    curses.KEY_DOWN:    cameraUp(),
    curses.KEY_LEFT:    cameraFront(),
    curses.KEY_RIGHT:   cameraFace(),
}



def main(window):
    next_key = None
    while True:
        curses.halfdelay(1)
        if next_key is None:
            key = window.getch()
        else:
            key = next_key
            next_key = None
        if key != -1:
            # KEY PRESSED
            curses.halfdelay(3)
            action = actions.get(key)
            if action is not None:
                action()
            next_key = key
            while next_key == key:
                next_key = window.getch()
            # KEY RELEASED
            cameraFace()


curses.wrapper(main)

The problem is in your actions dict, where you give each key the value returned by one of the functions.问题出在您的actions字典中,您在其中为每个键提供了其中一个函数返回的值。 What you mean to do here is to give it the function itself.你在这里的意思是给它 function 本身。 Just take the parentheses off:只需去掉括号:

actions = {
    curses.KEY_UP:  cameraDown,
    curses.KEY_DOWN:    cameraUp,
    curses.KEY_LEFT:    cameraFront,
    curses.KEY_RIGHT:   cameraFace
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM