简体   繁体   English

pi2go - 将机器人移动 90º 或仅向前移动 10 厘米并停止?

[英]pi2go - move robot in 90º or move foward just 10 cm and stop?

I bought a pi2go robot, and it's fantastic, but I have some questions, I want to make the robot我买了一个 pi2go 机器人,它很棒,但我有一些问题,我想制作机器人

  1. Spin Left/Right just 90º degrees and stop向左/向右旋转 90º 度并停止
  2. Move Foward/Backward just 10 cm and stop向前/向后移动 10 厘米并停止

I wanted to do 4 scripts in python just to do this like left.py , forward.py .我想做4个脚本蟒蛇只是要做到这一点像left.pyforward.py

In the pi2go helpfiles there is a script that allows you to control the robot with the keyboard, but I don't want that I just want to call for example sudo python left.py one time and exit.在 pi2go 帮助文件中有一个脚本可以让你用键盘控制机器人,但我不希望我只想调用例如sudo python left.py一次然后退出。

Here is the pi2go example :这是 pi2go 示例:

import pi2go, time

import sys
import tty
import termios

UP = 0
DOWN = 1
RIGHT = 2
LEFT = 3

def readchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    if ch == '0x03':
        raise KeyboardInterrupt
    return ch

def readkey(getchar_fn=None):
    getchar = getchar_fn or readchar
    c1 = getchar()
    if ord(c1) != 0x1b:
        return c1
    c2 = getchar()
    if ord(c2) != 0x5b:
        return c1
    c3 = getchar()
    return ord(c3) - 65  # 0=Up, 1=Down, 2=Right, 3=Left arrows

speed = 30

pi2go.init()

try:
    while True:
        keyp = readkey()
        if keyp == 'w' or keyp == UP:
            pi2go.forward(speed)
            print 'Forward', speed
        elif keyp == 's' or keyp == DOWN:
            pi2go.reverse(speed)
            print 'Backward', speed
        elif keyp == 'd' or keyp == RIGHT:
            pi2go.spinRight(speed)
            print 'Spin Right', speed
        elif keyp == 'a' or keyp == LEFT:
            pi2go.spinLeft(speed)
            print 'Spin Left', speed
        elif keyp == '.' or keyp == '>':
            speed = min(100, speed+10)
            print 'Speed+', speed
        elif keyp == ',' or keyp == '<':
            speed = max (0, speed-10)
            print 'Speed-', speed
        elif keyp == ' ':
            pi2go.stop()
            print 'Stop'
        elif ord(keyp) == 3:
            break


    pi2go.cleanup()

But I want to create just one script that does this and exits, like:但我只想创建一个执行此操作并退出的脚本,例如:

pi2go.spinLeft(100) 
pi2go.stop()

I think your problem is that you're not giving the motors any time to turn.我认为你的问题是你没有给电机任何时间转动。 If I look at your code snippet, you're telling the motors to spin left, and then immediately stopping the motors.如果我查看您的代码片段,您是在告诉电机向左旋转,然后立即停止电机。

Try doing something like:尝试执行以下操作:

import time
import pi2go

pi2go.spinLeft(100)
time.sleep(1)
pi2go.stop()

to give the motors time to actually do their thing.给电机时间来真正做他们的事情。

You left out the initialization command pi2go.init() , and it looks like you also left out the all-important import statement that imports the pi2go module.您遗漏了初始化命令pi2go.init() ,看起来您也遗漏了import pi2go模块的最重要的import语句。

This should work:这应该有效:

import pi2go

pi2go.init()
pi2go.spinLeft(100) 
pi2go.stop()

pi2go.cleanup()

Obviously, I can't test this code... unless you buy me a robot.显然,我无法测试这段代码……除非你给我买一个机器人。 :) :)

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

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