Skip to content

Instantly share code, notes, and snippets.

@auxiliary-character
Created January 22, 2015 18:50
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 auxiliary-character/ece860bd929c1d7e6346 to your computer and use it in GitHub Desktop.
Save auxiliary-character/ece860bd929c1d7e6346 to your computer and use it in GitHub Desktop.
import threading
import wpilib
import serial
import re
class IMUSimple(threading.Thread):
float_regex = """([\+\-]\d{3}\.\d{2})"""
int8_regex = """([0-9A-Fa-f]{2})"""
int16_regex = """([0-9A-Fa-f]{4})"""
termination_regex = int8_regex + "\n\r"
yprc_packet_regex = re.compile("!y"+
float_regex+ #Yaw
float_regex+ #Pitch
float_regex+ #Roll
float_regex+ #Compass Heading
termination_regex)
def _parse(self, line):
match = yprc_packet_regex.search(line)
if match:
groups = match.groups()
yaw = float(groups[0])
pitch = float(groups[1])
roll = float(groups[2])
compass = float(groups[3])
return yaw, pitch, roll, compass
def __init__(self):
self.serial = serial.Serial(0, 57500)
super().__init__(name="IMU Listener", daemon=True)
self.start()
self.mutex = threading.RLock()
self.yaw = 0.0
self.pitch = 0.0
self.roll = 0.0
self.compass = 0.0
def run(self):
while True:
line = self.serial.readline(eol="\n\r")
yaw, pitch, roll, compass= self._parse(line)
with self.mutex:
self.yaw = yaw
self.pitch = pitch
self.roll = roll
self.compass = compass
def getYaw(self):
with self.mutex:
return self.yaw
def getPitch(self):
with self.mutex:
return self.pitch
def getRoll(self):
with self.mutex:
return self.Roll
def getCompass(self):
with self.mutex:
return self.compass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment