I tried to execute "Tutorial 1" of Honke PyODE-sama site.
・ Click here for the article on "Tutorial 3" (https://qiita.com/emuai/items/840c776f146ac12ac416)
This "Tutorial 1" just gives an initial velocity to one object and sees how it looks. In the sense of "checking the operation of PyODE", the result display is only the value display to the standard output.
In this article, I tried adding a plot display with Matplotlib.
↓ This is the original code plus Matplotlib plot call and plot array creation.
Tutorial-1_plot.py
# pyODE example 1: with MPL-plot
import ode
# Create a world object
world = ode.World()
world.setGravity( (0,-9.81,0) )
# Create a body inside the world
body = ode.Body(world)
M = ode.Mass()
M.setSphere(2500.0, 0.05)
M.mass = 1.0
body.setMass(M)
body.setPosition( (0,2,0) )
body.addForce( (0,200,0) )
# Do the simulation...
total_time = 0.0
dt = 0.04
import numpy as np
nt = 100
txyzuvw = np.zeros( (7,nt+1) )
tn=0
while total_time<2.0:
x,y,z = body.getPosition()
u,v,w = body.getLinearVel()
print( "%1.2fsec: pos=(%6.3f, %6.3f, %6.3f) vel=(%6.3f, %6.3f, %6.3f)" % \
(total_time, x, y, z, u,v,w) )
if tn <= nt:
txyzuvw[0][tn]=total_time
txyzuvw[1][tn]=x
txyzuvw[2][tn]=y
txyzuvw[3][tn]=z
#txyzuvw[4][tn]=u
#txyzuvw[5][tn]=v
#txyzuvw[6][tn]=w
world.step(dt)
total_time+=dt
tn += 1
end_tn = tn
import matplotlib.pyplot as plt
# MPL-Plot
plt.plot( txyzuvw[0][0:end_tn], txyzuvw[2][0:end_tn], label='Vertical position')
plt.xlabel('time [s]')
plt.ylabel('Vertical position [m]')
plt.savefig('./y.png')
↑ 20200506: Correction that the start position could not be recorded
(Please install numpy and matplotlib in advance.) When you run ...
↑ This graph image (PNG) is saved as the current. It is the time change of the height of the object thrown in the vertical direction (Y coordinate).
Recommended Posts