-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,64 @@ | ||
import typing as tp | ||
from pathlib import Path | ||
from typing import List | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from mcap_protobuf.reader import read_protobuf_messages | ||
from mcap_protobuf.writer import Writer | ||
|
||
STATE = ["phi", "theta", "phi_dot", "theta_dot"] | ||
from furuta.logging.protobuf.pendulum_state_pb2 import PendulumState | ||
from furuta.utils import STATE, State | ||
|
||
|
||
class SimpleLogger: | ||
def __init__(self): | ||
self.times: tp.List[float] = [] | ||
self.states: tp.List[np.ndarray] = [] | ||
|
||
def update(self, time: float, state: np.ndarray): | ||
self.times.append(time) | ||
self.states.append(state) | ||
|
||
def save(self, directory: Path): | ||
np.save(directory / "times.npy", self.times) | ||
np.save(directory / "states.npy", np.array(self.states)) | ||
|
||
def load(self, directory: Path): | ||
self.states = np.load(directory / "states.npy") | ||
self.times = np.load(directory / "times.npy") | ||
|
||
def plot(self): | ||
plt.figure(1) | ||
for i in range(len(STATE)): | ||
plt.subplot(2, 2, i + 1) | ||
plt.plot(self.times, np.array(self.states)[:, i]) | ||
plt.title(STATE[i]) | ||
|
||
def show(self): | ||
def __init__(self, log_path: (str | Path)): | ||
self.log_path = log_path | ||
|
||
def start(self): | ||
self.output_file = open(self.log_path, "wb") | ||
self.mcap_writer = Writer(self.output_file) | ||
|
||
def stop(self): | ||
self.mcap_writer.finish() | ||
self.output_file.close() | ||
|
||
def update(self, time_ns: int, state: State): | ||
self.mcap_writer.write_message( | ||
topic="/pendulum_state", | ||
message=PendulumState( | ||
motor_angle=state.motor_angle, | ||
pendulum_angle=state.pendulum_angle, | ||
motor_angle_velocity=state.motor_angle_velocity, | ||
pendulum_angle_velocity=state.pendulum_angle_velocity, | ||
reward=state.reward, | ||
action=state.action, | ||
), | ||
log_time=time_ns, | ||
publish_time=time_ns, | ||
) | ||
|
||
def load(self) -> tuple[np.ndarray, np.ndarray]: | ||
times: List[float] = list() | ||
states: List[np.ndarray] = list() | ||
for msg in read_protobuf_messages(self.log_path, log_time_order=True): | ||
p = msg.proto_msg | ||
state = np.array( | ||
[ | ||
p.motor_angle, | ||
p.pendulum_angle, | ||
p.motor_angle_velocity, | ||
p.pendulum_angle_velocity, | ||
p.reward, | ||
p.action, | ||
] | ||
) | ||
times.append(float(msg.log_time_ns * 1e-9)) | ||
states.append(state) | ||
return np.array(times), np.array(states) | ||
|
||
def plot(self, times: List[float], states: List[np.ndarray]): | ||
for title, idx in STATE.items(): | ||
plt.figure(idx + 1) | ||
plt.plot(times, states[:, idx]) | ||
plt.title(title) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import argparse | ||
|
||
from furuta.logger import SimpleLogger | ||
|
||
# from furuta.robot import RobotModel | ||
from furuta.viewer import Viewer2D # , Viewer3D | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-t", "--type", required=False, default="2D", choices=("2D", "3D")) | ||
parser.add_argument("-f", "--file_path", required=True) | ||
args = parser.parse_args() | ||
|
||
logger = SimpleLogger(args.file_path) | ||
times, states = logger.load() | ||
|
||
if args.type == "2D": | ||
viewer = Viewer2D() | ||
else: | ||
print("3D viewer is not supported yet") | ||
assert False | ||
# viewer = Viewer3D(RobotModel.robot) | ||
|
||
viewer.animate(times, states) | ||
viewer.close() | ||
|
||
logger.plot(times, states) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.