rdt_dict = {
    50:  [184, 184, 184, 184, 279, 184, 198, 279, 192, 326],
    100: [345, 501, 350, 350, 492, 350, 350, 496, 495, 350],
    150: [501, 648, 648, 648, 501, 648, 648, 648, 501, 567],
    200: [800, 800, 800, 800, 690, 800, 800, 800, 660, 800],
    250: [960, 960, 960, 960, 960, 960, 837, 856, 955, 955]
}

df_gpio = pd.DataFrame(rdt_dict)

"""
Explanation: Return Delay Time
Return Delay Time @57600 bps
Measured time (with an oscilloscope) between the end of the last bit of the Instruction Packet and the beginning of the first bit of the Status Packet with respect to the Return Delay Time value set within the tested Dynamixel (address 0x05 in the internal Control Table).
<img src="rdt.png" width="75%"></img>
Raspberry Pi using GPIO
End of explanation
"""


rdt_dict = {
    50:  [190, 190, 185, 187, 246, 190, 190, 190, 190, 332],
    100: [480, 492, 492, 346, 346, 346, 495, 450, 487, 421],
    150: [510, 650, 650, 506, 645, 496, 496, 645, 525, 503],
    200: [800, 660, 660, 800, 800, 800, 800, 742, 800, 800],
    250: [965, 955, 955, 965, 816, 965, 965, 955, 955, 965]
}

df_usb2dynamixel = pd.DataFrame(rdt_dict)

"""
Explanation: Raspberry Pi using USB2Dynamixel
End of explanation
"""


fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(10, 8))

df_gpio.plot(ax=ax1,
             kind="box",
             color="r",
             legend=True,
             label="GPIO")

df_usb2dynamixel.plot(ax=ax2,
                      kind="box",
                      color="b",
                      legend=True,
                      label="USB2Dynamixel");

ax1.grid(True)
ax2.grid(True)

ax1.set_xlabel("Return Delay Time", fontsize=12)
ax1.set_ylabel("Measured Return Delay Time (µs)", fontsize=12)

ax2.set_xlabel("Return Delay Time", fontsize=12)
ax2.set_ylabel("Measured Return Delay Time (µs)", fontsize=12)

ax1.set_title("Return Delay Time (@57600bps)", fontsize=16)

fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(10, 5))

df_gpio.mean().plot(ax=ax1,
                    yerr=df_gpio.std(),
                    legend=True,
                    label="GPIO",
                    color="r",
                    linewidth=1)

df_usb2dynamixel.mean().plot(ax=ax1,
                             yerr=df_usb2dynamixel.std(),
                             legend=True,
                             label="USB2Dynamixel",
                             color="b",
                             linewidth=1);

ax1.set_xlim(left=45, right=255)

ax1.grid(True)

ax1.set_xlabel("Return Delay Time", fontsize=12)
ax1.set_ylabel("Measured Return Delay Time (µs)", fontsize=12)
ax1.set_title("Return Delay Time (@57600bps)", fontsize=16)

"""
Explanation: Plots
End of explanation
"""


br_array = np.array([
    [9600,   1039, 1040],
    [19200,  519,  520],
    [57600,  173,  173],
    [115200, 87,   86],
    [230400, 1039, 43],
    [460800, 1039, 21],
    [921600, 1039, 10]
])

# Actual dt = 1 / baud_rate * (8 bits + 1 start bit + 1 stop bit) * 1000000 microsec
actual_dt = 1. / br_array[:,0] * 10. * 1000000.

df = pd.DataFrame(data=np.hstack([br_array[:,1:], actual_dt.reshape([-1, 1])]),
                  index=br_array[:,0],
                  columns=["delta time per byte using GPIO (µs)",
                           "delta time per byte using USB2Dynamixel (µs)",
                           "actual delta time per byte (µs)"])
df.index.name = "baud rate"
df

fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(10, 5))

df.plot(ax=ax1,
        color=["red", "blue", "green"],
        logx=True,
        logy=True,
        legend=True);

"""
Explanation: Baud rate
Measured time (with an oscilloscope) between the start and the end of a byte.
<img src="baudrate_115200.png" width="75%"></img>
End of explanation
"""


