2011-12-02 00:13:50 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-01-04 04:13:47 -04:00
|
|
|
from multicopter import MultiCopter
|
2012-03-22 08:50:47 -03:00
|
|
|
import util, time, os, sys, math
|
2011-12-02 00:13:50 -04:00
|
|
|
import socket, struct
|
2012-04-27 00:32:22 -03:00
|
|
|
import select, errno
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'pymavlink'))
|
|
|
|
import fgFDM
|
2011-12-02 00:13:50 -04:00
|
|
|
|
2011-12-02 07:45:48 -04:00
|
|
|
def sim_send(m, a):
|
2011-12-02 00:13:50 -04:00
|
|
|
'''send flight information to mavproxy and flightgear'''
|
|
|
|
global fdm
|
2012-03-22 08:50:47 -03:00
|
|
|
from math import degrees
|
|
|
|
|
|
|
|
earth_rates = util.BodyRatesToEarthRates(a.dcm, a.gyro)
|
|
|
|
(roll, pitch, yaw) = a.dcm.to_euler()
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
fdm.set('latitude', a.latitude, units='degrees')
|
|
|
|
fdm.set('longitude', a.longitude, units='degrees')
|
|
|
|
fdm.set('altitude', a.altitude, units='meters')
|
2012-03-22 08:50:47 -03:00
|
|
|
fdm.set('phi', roll, units='radians')
|
|
|
|
fdm.set('theta', pitch, units='radians')
|
|
|
|
fdm.set('psi', yaw, units='radians')
|
|
|
|
fdm.set('phidot', earth_rates.x, units='rps')
|
|
|
|
fdm.set('thetadot', earth_rates.y, units='rps')
|
|
|
|
fdm.set('psidot', earth_rates.z, units='rps')
|
2011-12-02 00:13:50 -04:00
|
|
|
fdm.set('vcas', math.sqrt(a.velocity.x*a.velocity.x + a.velocity.y*a.velocity.y), units='mps')
|
|
|
|
fdm.set('v_north', a.velocity.x, units='mps')
|
|
|
|
fdm.set('v_east', a.velocity.y, units='mps')
|
2012-01-04 04:13:47 -04:00
|
|
|
# FG FDM protocol only supports 4 motors for display :(
|
2011-12-02 00:13:50 -04:00
|
|
|
fdm.set('num_engines', 4)
|
|
|
|
for i in range(4):
|
|
|
|
fdm.set('rpm', 1000*m[i], idx=i)
|
2011-12-02 02:16:23 -04:00
|
|
|
try:
|
|
|
|
fg_out.send(fdm.pack())
|
|
|
|
except socket.error as e:
|
|
|
|
if not e.errno in [ errno.ECONNREFUSED ]:
|
|
|
|
raise
|
2011-12-02 00:13:50 -04:00
|
|
|
|
2011-12-02 07:12:58 -04:00
|
|
|
buf = struct.pack('<16dI',
|
2012-03-22 08:50:47 -03:00
|
|
|
a.latitude, a.longitude, a.altitude, degrees(yaw),
|
2011-12-02 07:12:58 -04:00
|
|
|
a.velocity.x, a.velocity.y,
|
2012-03-23 02:24:52 -03:00
|
|
|
a.accelerometer.x, a.accelerometer.y, a.accelerometer.z,
|
2012-03-22 08:50:47 -03:00
|
|
|
degrees(earth_rates.x), degrees(earth_rates.y), degrees(earth_rates.z),
|
|
|
|
degrees(roll), degrees(pitch), degrees(yaw),
|
2011-12-02 07:12:58 -04:00
|
|
|
math.sqrt(a.velocity.x*a.velocity.x + a.velocity.y*a.velocity.y),
|
|
|
|
0x4c56414e)
|
2011-12-02 02:16:23 -04:00
|
|
|
try:
|
|
|
|
sim_out.send(buf)
|
|
|
|
except socket.error as e:
|
|
|
|
if not e.errno in [ errno.ECONNREFUSED ]:
|
|
|
|
raise
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
|
2011-12-12 22:44:53 -04:00
|
|
|
def sim_recv(m):
|
2011-12-02 00:13:50 -04:00
|
|
|
'''receive control information from SITL'''
|
2011-12-02 16:24:58 -04:00
|
|
|
try:
|
|
|
|
buf = sim_in.recv(22)
|
|
|
|
except socket.error as e:
|
|
|
|
if not e.errno in [ errno.EAGAIN, errno.EWOULDBLOCK ]:
|
|
|
|
raise
|
|
|
|
return
|
|
|
|
|
2011-12-02 07:45:48 -04:00
|
|
|
if len(buf) != 22:
|
2011-12-02 00:13:50 -04:00
|
|
|
return
|
2011-12-02 07:45:48 -04:00
|
|
|
pwm = list(struct.unpack('<11H', buf))
|
2012-01-04 04:13:47 -04:00
|
|
|
for i in range(11):
|
2011-12-02 07:45:48 -04:00
|
|
|
m[i] = (pwm[i]-1000)/1000.0
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
def interpret_address(addrstr):
|
|
|
|
'''interpret a IP:port string'''
|
|
|
|
a = addrstr.split(':')
|
|
|
|
a[1] = int(a[1])
|
|
|
|
return tuple(a)
|
|
|
|
|
|
|
|
##################
|
|
|
|
# main program
|
|
|
|
from optparse import OptionParser
|
2012-01-04 04:14:42 -04:00
|
|
|
parser = OptionParser("sim_multicopter.py [options]")
|
2011-12-02 00:13:50 -04:00
|
|
|
parser.add_option("--fgout", dest="fgout", help="flightgear output (IP:port)", default="127.0.0.1:5503")
|
|
|
|
parser.add_option("--simin", dest="simin", help="SIM input (IP:port)", default="127.0.0.1:5502")
|
|
|
|
parser.add_option("--simout", dest="simout", help="SIM output (IP:port)", default="127.0.0.1:5501")
|
|
|
|
parser.add_option("--home", dest="home", type='string', default=None, help="home lat,lng,alt,hdg (required)")
|
2011-12-12 22:44:53 -04:00
|
|
|
parser.add_option("--rate", dest="rate", type='int', help="SIM update rate", default=400)
|
2011-12-12 19:11:10 -04:00
|
|
|
parser.add_option("--wind", dest="wind", help="Simulate wind (speed,direction,turbulance)", default='0,0,0')
|
2012-01-04 04:13:47 -04:00
|
|
|
parser.add_option("--frame", dest="frame", help="frame type (+,X,octo)", default='+')
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
|
|
|
for m in [ 'home' ]:
|
|
|
|
if not opts.__dict__[m]:
|
|
|
|
print("Missing required option '%s'" % m)
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
parent_pid = os.getppid()
|
|
|
|
|
|
|
|
# UDP socket addresses
|
|
|
|
fg_out_address = interpret_address(opts.fgout)
|
|
|
|
sim_out_address = interpret_address(opts.simout)
|
|
|
|
sim_in_address = interpret_address(opts.simin)
|
|
|
|
|
|
|
|
# setup output to flightgear
|
|
|
|
fg_out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
fg_out.connect(fg_out_address)
|
|
|
|
fg_out.setblocking(0)
|
|
|
|
|
|
|
|
# setup input from SITL
|
|
|
|
sim_in = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
sim_in.bind(sim_in_address)
|
|
|
|
sim_in.setblocking(0)
|
|
|
|
|
|
|
|
# setup output to SITL
|
|
|
|
sim_out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
sim_out.connect(sim_out_address)
|
|
|
|
sim_out.setblocking(0)
|
|
|
|
|
|
|
|
# FG FDM object
|
|
|
|
fdm = fgFDM.fgFDM()
|
|
|
|
|
|
|
|
# create the quadcopter model
|
2012-01-04 04:13:47 -04:00
|
|
|
a = MultiCopter(frame=opts.frame)
|
2011-12-02 00:13:50 -04:00
|
|
|
|
2012-01-04 04:34:11 -04:00
|
|
|
print("Simulating %u motors for frame %s" % (len(a.motors), opts.frame))
|
|
|
|
|
2011-12-02 00:13:50 -04:00
|
|
|
# motors initially off
|
2012-01-04 04:13:47 -04:00
|
|
|
m = [0.0] * 11
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
lastt = time.time()
|
|
|
|
frame_count = 0
|
|
|
|
|
|
|
|
# parse home
|
|
|
|
v = opts.home.split(',')
|
|
|
|
if len(v) != 4:
|
|
|
|
print("home should be lat,lng,alt,hdg")
|
|
|
|
sys.exit(1)
|
|
|
|
a.home_latitude = float(v[0])
|
|
|
|
a.home_longitude = float(v[1])
|
|
|
|
a.home_altitude = float(v[2])
|
|
|
|
a.latitude = a.home_latitude
|
|
|
|
a.longitude = a.home_longitude
|
|
|
|
a.altitude = a.home_altitude
|
|
|
|
a.yaw = float(v[3])
|
|
|
|
a.ground_level = a.home_altitude
|
|
|
|
a.position.z = 0
|
2011-12-12 19:11:10 -04:00
|
|
|
a.wind = util.Wind(opts.wind)
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
print("Starting at lat=%f lon=%f alt=%.1f heading=%.1f" % (
|
|
|
|
a.home_latitude,
|
|
|
|
a.home_longitude,
|
|
|
|
a.home_altitude,
|
|
|
|
a.yaw))
|
|
|
|
|
2011-12-02 16:24:58 -04:00
|
|
|
frame_time = 1.0/opts.rate
|
2011-12-02 16:38:51 -04:00
|
|
|
sleep_overhead = 0
|
2011-12-02 16:24:58 -04:00
|
|
|
|
2011-12-02 00:13:50 -04:00
|
|
|
while True:
|
2011-12-02 16:24:58 -04:00
|
|
|
frame_start = time.time()
|
2011-12-12 22:44:53 -04:00
|
|
|
sim_recv(m)
|
2011-12-02 00:13:50 -04:00
|
|
|
|
|
|
|
m2 = m[:]
|
|
|
|
|
|
|
|
a.update(m2)
|
2011-12-02 07:45:48 -04:00
|
|
|
sim_send(m, a)
|
2011-12-02 00:13:50 -04:00
|
|
|
frame_count += 1
|
|
|
|
t = time.time()
|
|
|
|
if t - lastt > 1.0:
|
2012-01-10 00:23:37 -04:00
|
|
|
#print("%.2f fps zspeed=%.2f zaccel=%.2f h=%.1f a=%.1f yaw=%.1f yawrate=%.1f" % (
|
|
|
|
# frame_count/(t-lastt),
|
|
|
|
# a.velocity.z, a.accel.z, a.position.z, a.altitude,
|
|
|
|
# a.yaw, a.yaw_rate))
|
2011-12-02 00:13:50 -04:00
|
|
|
lastt = t
|
|
|
|
frame_count = 0
|
2011-12-02 16:24:58 -04:00
|
|
|
frame_end = time.time()
|
|
|
|
if frame_end - frame_start < frame_time:
|
2011-12-02 16:38:51 -04:00
|
|
|
dt = frame_time - (frame_end - frame_start)
|
|
|
|
dt -= sleep_overhead
|
|
|
|
if dt > 0:
|
|
|
|
time.sleep(dt)
|
|
|
|
sleep_overhead = 0.99*sleep_overhead + 0.01*(time.time() - frame_end)
|