1 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#! /usr/bin/env python
"""
This script tests the 'data stream' oriented feature of the socket interface.
"""
from morse.testing.testing import MorseTestCase
try:
# Include this import to be able to use your test file as a regular
# builder script, ie, usable with: 'morse [run|exec] <your test>.py
from morse.builder import *
except ImportError:
pass
import sys
import math
from pymorse import Morse
def send_angles(s, yaw, pitch, roll):
s.publish({'yaw' : yaw, 'pitch' : pitch, 'roll' : roll})
class OrientationTest(MorseTestCase):
def setUpEnv(self):
robot = RMax('robot')
robot.translate(10.0, 8.0, 20.0)
gyro = Gyroscope()
gyro.add_stream('socket')
robot.append(gyro)
orientation = Orientation('orientation')
orientation.add_stream('socket')
orientation.properties(speed = 0.5)
robot.append(orientation)
env = Environment('empty', fastmode = True)
env.add_service('socket')
def test_orientation(self):
""" Test if we can connect to the pose data stream, and read from it.
"""
with Morse() as morse:
gyro_stream = morse.robot.gyro
orientation_stream = morse.robot.orientation
precision = 0.05
angles = gyro_stream.get()
self.assertAlmostEqual(angles['yaw'], 0.0, delta=precision)
self.assertAlmostEqual(angles['pitch'], 0.0, delta=precision)
self.assertAlmostEqual(angles['roll'], 0.0, delta=precision)
send_angles(orientation_stream, 3.0, 0.0, 0.0)
morse.sleep(1.0)
angles = gyro_stream.get()
self.assertAlmostEqual(angles['yaw'], 0.5, delta = precision)
self.assertAlmostEqual(angles['pitch'], 0.0, delta=precision)
self.assertAlmostEqual(angles['roll'], 0.0, delta=precision)
morse.sleep(6.0)
angles = gyro_stream.get()
self.assertAlmostEqual(angles['yaw'], 3.0, delta = precision)
self.assertAlmostEqual(angles['pitch'], 0.0, delta=precision)
self.assertAlmostEqual(angles['roll'], 0.0, delta=precision)
# Be smart on the rotation order
send_angles(orientation_stream, -3.0, 0.0, 0.0)
morse.sleep(1.0)
angles = gyro_stream.get()
self.assertAlmostEqual(angles['yaw'], -3.0, delta = precision)
self.assertAlmostEqual(angles['pitch'], 0.0, delta=precision)
self.assertAlmostEqual(angles['roll'], 0.0, delta=precision)
# Turn in the order direction
send_angles(orientation_stream, 2.0, 0.0, 0.0)
morse.sleep(1.0)
angles = gyro_stream.get()
self.assertAlmostEqual(angles['yaw'], 2.8, delta = precision)
self.assertAlmostEqual(angles['pitch'], 0.0, delta=precision)
self.assertAlmostEqual(angles['roll'], 0.0, delta=precision)
morse.sleep(2.0)
angles = gyro_stream.get()
self.assertAlmostEqual(angles['yaw'], 2.0, delta = precision)
self.assertAlmostEqual(angles['pitch'], 0.0, delta=precision)
self.assertAlmostEqual(angles['roll'], 0.0, delta=precision)
########################## Run these tests ##########################
if __name__ == "__main__":
from morse.testing.testing import main
main(OrientationTest)
|