Skip to content

Instantly share code, notes, and snippets.

@pingswept
Created October 18, 2012 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingswept/3914181 to your computer and use it in GitHub Desktop.
Save pingswept/3914181 to your computer and use it in GitHub Desktop.
scrap of motor code from server.py
# Mostly originally written by Richard Klancer.
# Licensed under MIT license: http://opensource.org/licenses/MIT
class Motor:
SERIAL_SPEED = 115200
TIMEOUT_INTERVAL = 3
def __init__(self):
import time
self.last_command_string = ""
self.last_command_time = time.time()
self.timed_out = False
def send_command_string(self, command_string):
import pytronics
self.last_command_string = command_string
pytronics.serialWrite(command_string, self.__class__.SERIAL_SPEED)
print command_string
def repeat_last_command_or_time_out(self):
import time
now = time.time()
if (now - self.last_command_time) > self.__class__.TIMEOUT_INTERVAL:
self.timed_out = True
return
self.send_command_string(self.last_command_string)
def send_command(self, left, right):
import time
if (abs(left) > 1000) or (abs(right) > 1000):
raise ValueError("Motor command out of range")
self.last_command_time = time.time()
self.timed_out = False
command_string = "!M %d %d\r" % (-right, left)
self.send_command_string(command_string)
def interpret_joystick_command(x, y):
"""Accepts a joystick position (x, y) where x and y represent a point within the unit circle.
Returns a tuple (left, right) of the corresponding motor speeds for a left and right motor in
a differential drive setup. For convenient use with the MDC2200, left and right are integers
in the range -1000..1000"""
from math import atan2, sin, cos, pi, sqrt
angle = atan2(y, x) - pi/4
# interpret the distance from the
speed = sqrt(x*x + y*y)
if speed > 1.0:
raise ValueError("Joystick input outside the unit circle")
left = sin(angle)
right = cos(angle)
# scale the motor speeds so that, if the joystick is pushed to its maximum distance from the
# center, the faster motor is at max speed regardless of the angle
scale = 1000 * speed / max(abs(left), abs(right))
return (int(scale * left + 0.5), int(scale * right + 0.5))
motor = Motor()
@public.route('/joystick', methods=['POST'])
def joystick():
x = float(request.form['x'])
y = float(request.form['y'])
motor.send_command(*interpret_joystick_command(x, y))
return "ok"
@rbtimer(1)
def repeat_last_motor_command_or_time_out(num):
motor.repeat_last_command_or_time_out()
@public.route('/forward', methods=['POST'])
def forward():
import pytronics
pytronics.serialWrite('!M -750 750\r', speed=115200)
return 'Tried to move motors'
@public.route('/back', methods=['POST'])
def back():
import pytronics
pytronics.serialWrite('!M 750 -750\r', speed=115200)
return 'Tried to move motors'
@public.route('/left', methods=['POST'])
def left():
import pytronics
pytronics.serialWrite('!M 750 750\r', speed=115200)
return 'Tried to move motors'
@public.route('/right', methods=['POST'])
def right():
import pytronics
pytronics.serialWrite('!M -750 -750\r', speed=115200)
return 'Tried to move motors'
@public.route('/stop', methods=['POST'])
def stop():
import pytronics
pytronics.serialWrite('!M 0 0\r', speed=115200)
return 'Tried to move motors'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment