当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Using seperate threads to execute a sequence of timed events using Queue.Queue

问题描述:

I am working on a Seniour Design project automating a power wheelchair allowing for different control methods like speech recognition. With the help of the of the wheelchair controller manufacturer a system was configured with python as the programming interface. With help from a software engineer the code below was structured to control the wheelchair direction and speed. The program uses threading.thread to pass dt.control events into a queue.queue to be executed until a new dt.control event is issued. I've been able to put together a speech recognition program that uses the login and control functions as a way into the thread to control the wheelchair. I now would like to issue a series of timed dt.control events using a timer to pass in the sequence of dt.control events to be executed by the queue one event at a time. I am fairly new to python an am having trouble coming up with a way to do this.

import serial

import time

import datetime

import Queue

import threading

class DriveThread(threading.Thread):

def __init__(self, ser):

threading.Thread.__init__(self)

self.queue = Queue.Queue()

self.ser = ser

self.incValue = 0

def run(self):

direction = None

speed = None

prof = 0

while 1:

try:

control = self.queue.get(timeout = 0.5)

speed = control[0]

direction = control[1]

except Queue.Empty:

pass

# PUBLIC FUNCTIONS

#

# interface into this thread.

#

def login(self):

checksum = 0xc1

#stuff

self.queue.put([0,0])

def control(self, direction, speed):

self.queue.put([self.sign(direction), self.sign(speed)])

def give_joystick_control(self):

#stuff

ser.write(string)

# PRIVATE FUNCTIONS

#

### These functions are private to the thread.

#

def drive(self, speed, direction, prof):

inc = int(self.inc_value())

#stuff

def inc_value(self):

if(self.incValue<255):

self.incValue = self.incValue+1

else:

self.incValue = 0

return self.incValue

def send_string(self, string):

self.ser.write(string)

def read(self):

#stuff

def sign(self, val):

#stuff

if __name__ == "__main__":

port = 'COM6'

ser = serial.Serial(port, 115200, timeout = 0)

time.sleep(0.1)

dt = DriveThread(ser)

dt.start()

while 1:

cmd = raw_input("Enter command: l=logon, n=neutral (default), f=forward, r=reverse, lr=looping right, ll=looping left, c=clockwise, cc=counterclockwise, and j=joystick: ")

if cmd == 'l':

dt.login()

if cmd == 'n':

dt.control(0, 0)

if cmd == 'f':

dt.control(0, 50)

if cmd == 'r':

dt.control(0, -50)

if cmd == 'lr':

dt.control(50, 50)

if cmd == 'll':

dt.control(-50, 50)

if cmd == 'c':

dt.control(50, 0)

if cmd == 'cc':

dt.control(-50, 0)

if cmd == 'j':

dt.give_joystick_control()

This following bit of code is based on an open source speech recognition project called Pocketsphinx and its implementation in a python library called speakpython. This code imports the code above as a module and speech recognition passes the functions below to dt.control callables. Both codes are working fine, what would a new implementation look like that assigns priority to a series of dt.control events so that they are executed in a sequence. I have been told that setting up seperate threads can possibly accomplish this but this is new territory for me and not sure how to go about that.

from bt_2 import DriveThread

import serial

import time

import sys;

import os;

from SpeakPython.SpeakPythonRecognizer import SpeakPythonRecognizer

ser = serial.Serial(port, 115200, timeout = 0)

time.sleep(.1)

dt = DriveThread(ser)

dt.start()

stop=False;

def quit():

stop = True;

print "quit";

sys.exit(0);

def login():

global cmd

cmd='l';

def neutral():

global cmd

cmd='n';

def forward():

global cmd

cmd='f';

def reverse():

global cmd

cmd='r';

def right():

global cmd

cmd='lr';

def left():

global cmd

cmd='ll';

def clockwise():

global cmd

cmd='c';

def cclockwise():

global cmd

cmd='cc';

def manual():

global cmd

cmd = raw_input("Enter command: l=logon, n=neutral (default), f=forward, r=reverse, lr=looping right, ll=looping left, c=clockwise, cc=counterclockwise, and j=joystick: ")

def execute(out_str):

print out_str;

if not out_str == None:

exec out_str;

else:

print "Couldn't recognize.";

print "Ready.";

recog = SpeakPythonRecognizer(execute, "chairCommands");

recog.setDebug(1);

try:

while not stop:

recog.recognize();

dt.login()

print cmd

if cmd == 'l':

dt.login()

if cmd == 'n':

dt.control(0, 0)

if cmd == 'f':

dt.control(0, 50)

if cmd == 'r':

dt.control(0, -50)

if cmd == 'lr':

dt.control(50, 50)

if cmd == 'll':

dt.control(-50, 50)

if cmd == 'c':

dt.control(40, 0)

if cmd == 'cc':

dt.control(-40, 0)

if cmd == 'j':

dt.give_joystick_control()

except KeyboardInterrupt:

stop = True;

print "Interrupted.";

finally:

print("program Terminated")

分享给朋友:
您可能感兴趣的文章:
随机阅读: