From 9dffb5329409b92a062f3e468495c269d3429fe1 Mon Sep 17 00:00:00 2001
From: bav6096 <benedikt.deike@informatik.uni-hamburg.de>
Date: Sun, 14 Nov 2021 21:08:41 +0100
Subject: [PATCH] added script for playing animations

---
 scripts/play_animation                        | 23 ++++++++++++++
 src/visualising/animation.py                  | 12 ++++----
 .../animations/animation_happy.txt            |  2 +-
 src/visualising/arduino.py                    | 13 ++++----
 src/visualising/connection.py                 | 11 +++----
 src/visualising/countenance.py                | 30 +++++++++++++++++++
 src/visualising/visualiser.py                 | 15 ----------
 7 files changed, 69 insertions(+), 37 deletions(-)
 create mode 100755 scripts/play_animation
 create mode 100755 src/visualising/countenance.py

diff --git a/scripts/play_animation b/scripts/play_animation
new file mode 100755
index 0000000..af2e9f7
--- /dev/null
+++ b/scripts/play_animation
@@ -0,0 +1,23 @@
+#!/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))
+
diff --git a/src/visualising/animation.py b/src/visualising/animation.py
index 1898b1e..fc698b6 100755
--- a/src/visualising/animation.py
+++ b/src/visualising/animation.py
@@ -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():
diff --git a/src/visualising/animations/animation_happy.txt b/src/visualising/animations/animation_happy.txt
index 661bc2b..d7827c7 100644
--- a/src/visualising/animations/animation_happy.txt
+++ b/src/visualising/animations/animation_happy.txt
@@ -1,4 +1,4 @@
 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
diff --git a/src/visualising/arduino.py b/src/visualising/arduino.py
index 85c21dc..f1c6ef1 100755
--- a/src/visualising/arduino.py
+++ b/src/visualising/arduino.py
@@ -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
diff --git a/src/visualising/connection.py b/src/visualising/connection.py
index 43b2353..ae62629 100755
--- a/src/visualising/connection.py
+++ b/src/visualising/connection.py
@@ -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:
diff --git a/src/visualising/countenance.py b/src/visualising/countenance.py
new file mode 100755
index 0000000..c26f685
--- /dev/null
+++ b/src/visualising/countenance.py
@@ -0,0 +1,30 @@
+#!/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
diff --git a/src/visualising/visualiser.py b/src/visualising/visualiser.py
index 0b26222..aa16b85 100644
--- a/src/visualising/visualiser.py
+++ b/src/visualising/visualiser.py
@@ -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!")
 
-- 
GitLab