0

I have to create a pendulum simulation on plot. I have to read data from file, then only thing I have to solve is X and Y cords, what I did. It is specified that I am supposed to use "roll"/"pitch"/"yaw" columns and with one of them make a simulation. There is the file: https://pastebin.com/D2Tt3tBj Here is my code:

import pandas as pd
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from mpl_toolkits.mplot3d import Axes3D
from tkinter import ttk
from tkinter import scrolledtext
import numpy as np
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation

pd.set_option('display.max_rows', None)  # wyswietla caly plik


def odrzuc_n(df_full, n):
    df_full = df_full.iloc[n:]
    return df_full


def wczytaj_dane():
    data = pd.read_csv("outputPendulum02.log", delim_whitespace=True)
    data.columns = ["roll", "pitch", "yaw", "a_x", "a_y", "a_z", "m_x", "m_y", "m_z", "omega_x", "omega_y", "omega_z"]
    data = data.drop(columns=["a_x", "a_y", "a_z", "m_x", "m_y", "m_z", "omega_x", "omega_y", "omega_z"])
    data.index = [x for x in range(1, len(data.values) + 1)]
    data.index.name = 'id'
    df = pd.DataFrame(data)
    seconds = []
    x_cord = []
    y_cord = []
    which_angle = 'pitch'
    for (index_label, row_series) in df.iterrows():
        second = index_label * 0.04
        seconds.append(second)
        x = 50 * np.sin(row_series[which_angle])
        y = 50 - 50 * np.cos(row_series[which_angle])
        x_cord.append(x)
        y_cord.append(y)
    df['seconds'] = seconds
    df['x_cord'] = x_cord
    df['y_cord'] = y_cord
    # print(df)
    return df


df_full = wczytaj_dane()
how_many_to_delete = 500
df_full = odrzuc_n(df_full, how_many_to_delete)
data_cut = df_full

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')


def init():  # only required for blitting to give a clean slate.
    ax.set_xlim(df_full['x_cord'].min()-5, df_full['x_cord'].max()+5)
    ax.set_ylim(df_full['y_cord'].min()-5, df_full['y_cord'].max()+5)
    return ln,


def update(frame):
    xdata = (df_full.iloc[frame, 4])
    ydata = (df_full.iloc[frame, 5])
    ln.set_data(xdata, ydata)
    if frame > 1225:
        return 0
    return ln,

ani = FuncAnimation(fig, update, frames=range(how_many_to_delete, 1225),
                    init_func=init, blit=True)
plt.show()

It's little messy... "odrzuc_n" function is function that drops N rows from dataframe. "Wczytaj_dane" is reading data from file. Columns of file are described in the code. And finally my question. What's wrong with it? It shows an error at the end of animation. I don't really know is the X and Y determined properly. I have to make it in GUI that's why I have tkinter impoerted etc. But this part of code only generates plot.

novacco
  • 9
  • 1
  • You are performing a multi-step process. You should debug each of the steps and find the one whose behavior you cannot explain, and only then ask for help focused on that stage. – Amitai Irron May 11 '20 at 12:55
  • It seems you first are effectively deleting 'how_many_to_delete' values, which makes the valid values run from `0` to `1225-how_many_to_delete`. The easiest seems just to remove the call to `df_full = odrzuc_n(df_full, how_many_to_delete)` so that the range used in `FuncAnimation` is the correct one. – JohanC May 11 '20 at 13:00
  • well, my problem is to make a slider which controls time/row – novacco May 11 '20 at 17:33

0 Answers0