简体   繁体   English

使用 Arduino 和 Python 通过串行连续控制多个伺服系统

[英]Controlling many servos using Arduino and Python over serial continuously

I've been working on this piece of code to control a large array of servos on Arduino using Python.我一直在研究这段代码,以使用 Python 控制 Arduino 上的大量伺服系统。 The code works, but I've seen some strange behavior surrounding timing.该代码有效,但我看到了一些围绕计时的奇怪行为。 Between each update to the servos, I need a sleep command.在每次更新伺服系统之间,我需要一个睡眠命令。 If I don't insert a sleep, the program will work the first time I run it, but if the Python code gets stopped, then I try to connect over serial with Python again, the Arduino is non responsive.如果我不插入睡眠,程序将在我第一次运行时运行,但是如果 Python 代码停止,那么我再次尝试通过串行与 Python 连接,Arduino 没有响应。 What could be happening to the serial port and how could I prevent this from happening?串行端口可能会发生什么,我该如何防止这种情况发生?

Also, am I parsing the data coming into the Arduino the most efficient way possible using memcpy?另外,我是否使用 memcpy 以最有效的方式解析进入 Arduino 的数据? Or is there a better or more standard way to do this?或者有没有更好或更标准的方法来做到这一点?

I've read about using serialEvent, is there a benefit to using that command for parsing serial data in this situation?我已经阅读了有关使用 serialEvent 的信息,在这种情况下使用该命令解析串行数据是否有好处?

//ARDUINO CODE
#include <Servo.h> 

int servoPins[] = {9, 10};
//int servoPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38};
//int servoPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34};
const int numServos = sizeof(servoPins)/sizeof(int);
Servo servos[numServos];

char serialData[numServos * 3];
char tempData[3];

void setup() {
  Serial.begin(9600);
  Serial.println("Ready");
  for (int i=0; i < numServos; i++) {
    servos[i].attach(servoPins[i]);
    //servos[i].write(20);
  }
}

void loop() {
  if (Serial.available()) {
    Serial.readBytesUntil('\0', serialData, numServos * 3);

    for (int i=0; i < numServos; i++) {
      memmove(tempData, serialData + i * 3, 3);
      servos[i].write(atoi(tempData));
    }
  }
}


#PYTHON CODE
""" Control n servos on n arduinos over serial

"""

import glob
import platform
import serial
from time import sleep
import sys

'''
Servo
Id number, angle for servo, Arduino port servo is on
For now the port is used as an index, so please number them going from 0-n

'''
class Servo:
    id_num = 0
    angle = 0
    port = 0

    def __init__(self, id_num, angle, port):
        self.id_num = id_num
        self.angle = angle
        self.port = port

'''
ServoDriver
-Stores a list of servos
-Open ports to Arduinos
-Stores a list of those ports
-Creates a map of which servos are on which ports
-Provides a way to update the angle of all servos on all ports
'''
class ServoDriver:
    def __init__(self, servos):
        self.ports = self.open_ports()
        self.servos = servos

    # looks for all devices that have the same name pattern as an Arduino and opens them
    def open_ports(self):
        # find arduinos
        # note: currently this is Mac only
        devices = glob.glob('/dev/tty.usbmodem*')
        print devices

        if len(devices) == 0:
            print "No Arduinos found"
            sys.ext(1)

        ports = []
        for device in devices:
            try:
                # connect to serial port
                ports.append(serial.Serial(device, 9600))
            except:
                print 'Failed to open port'
                sys.ext(1)


        # need a short delay right after serial port is started for the Arduino to initialize
        sleep(2)
        return ports

    # update the angle of all servos on all ports
    def update(self, servos):
        debug = True
        servo_data = []
        for p in self.ports:
            servo_data.append('')
        for servo_update in servos:
            for servo_stored in self.servos:
                if servo_stored.id_num == servo_update.id_num:
                    this_port = servo_stored.port
                    break

            # constrain servo angles to avoid damaging servos
            if servo_update.angle > 135:
                servo_update.angle = 135
            else if servo_update.angle < 45:
                servo_update.angle = 45

            # append angle to the datum for this port
            servo_data[this_port] = servo_data[this_port] + str(servo_update.angle).zfill(3)

        for servo_datum in servo_data:
            # append null byte for arduino to recognize end of data
            servo_datum = servo_datum + "\0"

        # send data to the Arduinos
        for port,servo_datum in zip(self.ports,servo_data):
            port.write(servo_datum)

    def close_ports(self):
        print 'closing ports'
        for port in self.ports:
            port.close()

# generates values for making a servo sweep back and forth
def servo_iter():
    l = []
    #for i in range(0,1):
    l.append(40)
    #for i in range(0,1):
    l.append(80)
    for pos in l:
        yield pos

def servo_iter_2(total):
    for i in range(0,total):
        yield i

if __name__ == "__main__":
    # create a list of servos with mappings to ports
    # if you have the wrong number of servos it acts weird
    #num_servos = 32
    num_servos = 2
    servos = []
    servos.append(Servo(0, 40, 0))
    servos.append(Servo(1, 40, 0))
    '''
    servos.append(Servo(2, 40, 0))
    servos.append(Servo(3, 40, 0))
    servos.append(Servo(4, 40, 0))
    servos.append(Servo(5, 40, 0))
    servos.append(Servo(6, 40, 0))
    servos.append(Servo(7, 40, 0))
    servos.append(Servo(8, 40, 0))
    servos.append(Servo(9, 40, 0))
    servos.append(Servo(10, 40, 0))
    servos.append(Servo(11, 40, 0))
    servos.append(Servo(12, 40, 0))
    servos.append(Servo(13, 40, 0))
    servos.append(Servo(14, 40, 0))
    servos.append(Servo(15, 40, 0))
    servos.append(Servo(16, 40, 0))
    servos.append(Servo(17, 40, 0))
    servos.append(Servo(18, 40, 0))
    servos.append(Servo(19, 40, 0))
    servos.append(Servo(20, 40, 0))
    servos.append(Servo(21, 40, 0))
    servos.append(Servo(22, 40, 0))
    servos.append(Servo(23, 40, 0))
    servos.append(Servo(24, 40, 0))
    servos.append(Servo(25, 40, 0))
    servos.append(Servo(26, 40, 0))
    servos.append(Servo(27, 40, 0))
    servos.append(Servo(28, 40, 0))
    servos.append(Servo(29, 40, 0))
    servos.append(Servo(30, 40, 0))
    servos.append(Servo(31, 40, 0))
    servos.append(Servo(32, 40, 0))
    '''

    #if len(servos) != num_servos:
    #   print 'wrong number of servos'
    #   sys.ext(1)

    # comment out the next two lines if you only have 1 arduino
    #servos.append(Servo(2, 40, 1))
    #servos.append(Servo(3, 40, 1))
    #servos.append(Servo(4, 40, 2))
    #servos.append(Servo(5, 40, 2))

    angles = []
    for i in range(0,len(servos)):
        angles.append(40)

    try:
        # instantiate a driver
        # must happen inside try-finally
        driver = ServoDriver(servos)

        iter1 = False
        if iter1:
            pos = servo_iter()
        else:
            pos = servo_iter_2(len(servos))

        while True:
            try:
                x = pos.next()
            except StopIteration:
                if iter1:
                    pos = servo_iter()
                else:   
                    pos = servo_iter_2(len(servos))
                x = pos.next()
            # create a list of servos with ids and angles to update positions of servos
            if iter1:
                for servo in servos:
                    servo.angle = x
            else:
                for i,servo in zip(angles,servos):
                    servo.angle = i
            # call the driver with the list of servos
            driver.update(servos)
            sleep(0.5)

            for i in range(0, len(servos)):
                if i == x:
                    angles[i] = 80
                else:
                    angles[i] = 40

    # close the serial port on exit, or you will have to unplug the arduinos to connect again
    finally:
        driver.close_ports()

I think the right answer here was to add a delay to the Arduino.我认为这里的正确答案是为 Arduino 添加延迟。

Your Arduino will reset every time you make a serial connection over usb.每次通过 USB 进行串行连接时,您的 Arduino 都会重置。 If you want a temporary disable, you can insert a 10 microfarad capacitor between ground and reset.如果您想要临时禁用,您可以在接地和复位之间插入一个 10 微法的电容器。 This will keep it from auto-reseting, and can be easily removed for program uploading.这将防止它自动重置,并且可以轻松删除以进行程序上传。

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

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