Skip to content
Snippets Groups Projects
Commit 9dffb532 authored by bav6096's avatar bav6096
Browse files

added script for playing animations

parent 591889c8
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
import argparse
from visualising.arduino import Arduino
from visualising.animation import Animation
from visualising.connection import Connection
parser = argparse.ArgumentParser(description="script to test animations")
parser.add_argument("-f", "--frames", help="frames to be played", required=True)
parser.add_argument("-t", "--time", type=int, help="time between frames", required=True)
parser.add_argument("-i", "--iterations", type=int, help="number of iterations", required=True)
args = vars(parser.parse_args())
animation = args["frames"]
frame_time = args["time"]
num_iter = args["iterations"]
port = "/dev/ttyUSB0"
baud = 57600
arduino = Arduino(Connection(port, baud))
arduino.stream_animation(Animation(Animation.read_frames_from_file(animation), frame_time, num_iter))
......@@ -7,7 +7,7 @@ from . import animations
class Animation:
def __init__(self, frames, frame_time, num_iter):
def __init__(self, frames, frame_time=0, num_iter=1):
num_frames = len(frames)
if num_frames % 2 != 0:
......@@ -41,9 +41,9 @@ class Animation:
if not isinstance(frame_time, int):
raise TypeError("Frame time must be an integer!")
if not frame_time < 0:
if frame_time < 0:
raise ValueError("Frame time must be positive!")
if not frame_time > 4294967295:
if frame_time > 4294967295:
raise ValueError("Frame time is to high!")
# The frame time is given in milliseconds.
......@@ -62,16 +62,16 @@ class Animation:
def read_frames_from_file(filename):
file = importlib.resources.open_text(animations, filename)
animation = []
frames = []
for line in file:
frame = []
for value in line.split(','):
frame.append(int(re.search(r'\d+', value).group()))
animation.append(frame)
frames.append(frame)
return animation
return frames
@staticmethod
def create_empty_ensemble():
......
0,0,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
\ No newline at end of file
......@@ -8,26 +8,23 @@ from visualising.connection import ArduinoException
class Arduino:
resend = 5 # Number of times a message is resend to the Arduino.
def __init__(self, connection):
self.connection = connection
# Creates instruction message and sends it to the Arduino,
# to play a loaded animation.
def start_playback(self):
self.connection.confirm_msg(InstrMsg('B').create_msg(), [b'\x1f'], resend)
self.connection.confirm_msg(InstrMsg('B').create_msg(), [b'\x1f'], 5)
# Creates instruction message and sends it to the Arduino,
# to pause a playing animation.
def pause_playback(self):
self.connection.confirm_msg(InstrMsg('C').create_msg(), [b'\x5f'], resend)
self.connection.confirm_msg(InstrMsg('C').create_msg(), [b'\x5f'], 5)
# Creates instruction message and sends it to the Arduino,
# to resume playback of a loaded animation.
def resume_playback(self):
self.connection.confirm_msg(InstrMsg('D').create_msg(), [b'\x6f'], resend)
self.connection.confirm_msg(InstrMsg('D').create_msg(), [b'\x6f'], 5)
# Creates one instruction messages and multiple frame messages to load an animation
# onto the Arduino.
......@@ -35,10 +32,10 @@ class Arduino:
if animation.num_ensembles > 16:
raise ValueError("Animation has to many frames!")
else:
self.connection.confirm_msg(InstrMsg('A', animation).create_msg(), [b'\x0f'], resend)
self.connection.confirm_msg(InstrMsg('A', animation).create_msg(), [b'\x0f'], 5)
for frame in animation.frames:
self.connection.confirm_msg(FrameMsg(frame).create_msg(), [b'\x2f', b'\x3f'], resend)
self.connection.confirm_msg(FrameMsg(frame).create_msg(), [b'\x2f', b'\x3f'], 10)
# First, an animation is loaded onto the Arduino. The playback of this
# animation is then started. In addition, it is checked whether the
......
......@@ -3,9 +3,6 @@
import time
import serial
from visualising.exception import VisualiseException
from serial.serialutil import SerialException
class ArduinoException(Exception):
def __init__(self, desc):
......@@ -91,14 +88,14 @@ class Connection:
if not num_resend > 0:
raise ValueError("Number of resends must be greater than zero!")
self.connection.send_msg(msg)
response = self.connection.receive_msg()
self.send_msg(msg)
response = self.receive_msg()
condition = evaluate()
while condition:
num_resend = num_resend - 1
self.connection.send_msg(msg)
response = self.connection.receive_msg()
self.send_msg(msg)
response = self.receive_msg()
condition = evaluate()
if not num_resend > 0:
......
#!/usr/bin/env python
from visualising.animation import Animation
class Emotion:
def __init__(self):
self.frame_buffer = []
self.
self.happy = Animation(Animation.read_frames_from_file("animation_happy.txt"), 100, 1)
self.ok = Animation(Animation.read_frames_from_file("animation_ok.txt"), 100, 1)
self.sad = Animation(Animation.read_frames_from_file("animation_sad.txt"), 100, 1)
def transition_start(self, animation):
def transition_change(self, animation):
def visualise(self, animation):
while not rospy.is_shutdown():
if self.watchdog.aggregated_domains > 0.7:
self.stream_animation(sad)
else:
if self.watchdog.aggregated_domains > 0.4:
self.stream_animation(ok)
else:
self.stream_animation(happy)
\ No newline at end of file
......@@ -25,21 +25,6 @@ class Visualiser:
else:
rospy.logwarn("Visualiser: The specified strategy is not recognised by the visualiser!")
# TODO: Add all basic emotions!
def mood_mode(self):
happy = Animation(Animation.read_frames_from_file("animation_happy.txt"), 100, 1)
ok = Animation(Animation.read_frames_from_file("animation_ok.txt"), 100, 1)
sad = Animation(Animation.read_frames_from_file("animation_sad.txt"), 100, 1)
while not rospy.is_shutdown():
if self.watchdog.aggregated_domains > 0.7:
self.stream_animation(sad)
else:
if self.watchdog.aggregated_domains > 0.4:
self.stream_animation(ok)
else:
self.stream_animation(happy)
def info_mode(self):
print("Hallo!")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment