class MotorThread(threading.Thread): """ This thread interacts with the plotter via the global 'c' which containts plotter commands. """ def __init__(self): self.motor_log = Logger("Motors") threading.Thread.__init__(self) self.plotter = RopePlotter(L_ROPE_0, R_ROPE_0, ROPE_ATTACHMENT_WIDTH, Kp=KP, Ki=TI, Kd=TD, max_spd=MAX_SPEED) self.throttle = Throttler(MOTOR_CMD_RATE) def run(self): global c print "Starting motor thread" while running: if type(c) == dict: # We got settings if 'kp' in c: #We got pid settings self.plotter.Kp = c['kp'] self.plotter.Ti = c['ti'] self.plotter.Td = c['td'] self.plotter.max_speed = c['max_speed'] wsSend("PID parameters set") if 'll' in c: #we got rope length settings self.plotter.l_rope_0 = c['ll'] self.plotter.r_rope_0 = c['lr'] self.plotter.att_dist = c['aw'] wsSend("Plotter settings set") c='' if c == 'left-fwd': #print "Running left motor fwd" self.plotter.left_fwd() elif c == 'left-stop': #print "Stopping left motor" self.plotter.left_stop() c = '' elif c == 'right-fwd': #print "Running right motor forward" self.plotter.right_fwd() elif c == 'right-back': #print "Running right motor back" self.plotter.right_back() elif c == 'right-stop': #print "Stopping right motor" self.plotter.right_stop() c = '' elif c == 'left-back': #print "Running left motor back" self.plotter.left_back() elif c == 'pu': #print "Pulling pen up" self.plotter.pen_up() c = '' elif c == 'pd': #print "Putting pen down" self.plotter.pen_down() c = '' elif c == 'stop': self.plotter.left_stop() self.plotter.right_stop() c = '' #print "Stopped" elif c == 'testdrive': self.plotter.test_drive() c = '' elif c == 'plot': # c stays 'plot' until another command is sent trough the socket plot_action = self.plotter.plot_from_file('uploads/coords.csv') c = 'plotting' elif c == 'plotting': try: pct_done = next(plot_action) wsSend("[ {0:.2f}V ] Plot {1:.2f}% done".format(self.plotter.battery.measured_voltage/1000000.0, pct_done)) except StopIteration: c = '' wsSend("Done plotting") elif c == 'zero': wsSend("zero motor positions") self.plotter.set_control_zeroes() c = '' elif c == 'plotcircles': wsSend("Plotting circles") # c stays 'plot' until another command is sent trough the socket plot_action = self.plotter.plot_circles() c = 'plotting' #BrickPiUpdateValues() # BrickPi updates the values for the motors self.throttle.throttle() #Don't go too fast. #Stopped running. Shutting down all motors. self.plotter.stop_all_motors()
def __init__(self): self.motor_log = Logger("Motors") threading.Thread.__init__(self) self.plotter = RopePlotter(L_ROPE_0, R_ROPE_0, ROPE_ATTACHMENT_WIDTH, Kp=KP, Ki=TI, Kd=TD, max_spd=MAX_SPEED) self.throttle = Throttler(MOTOR_CMD_RATE)
from ropeplotter import RopePlotter, Throttler, get_ip_address from settings import * #Ev3dev for drawing and buttons import ev3dev.auto as ev ######################### Globals. ################################ c = 0 # movement command. websockets = [] # list of open sockets. # instantiate a plotter object plotter = RopePlotter(L_ROPE_0, R_ROPE_0, ROPE_ATTACHMENT_WIDTH, Kp=KP, Ki=TI, Kd=TD, cm_to_deg=CM_TO_DEG, chalk=CHALK) logging.basicConfig( filename='plotter.log', format='%(asctime)s.%(msecs)03d - %(funcName)s: %(message)s', datefmt="%H:%M:%S") plotter_log = logging.getLogger("Plotter") ################# Set up web server & threads ##################### # Initialize Tornado to use 'GET' and load index.html class MainHandler(tornado.web.RequestHandler):