I’ve been using the SITL on Linux for a while, and I wanted to migrate to HIL. There have been lots of issues posted about this, but I think there are some underlying problems that I’ve yet to solve with HIL. I have solved a few that I’ve not seen posted, so I wanted to share those and perhaps someone at the same point as me can help solve the FPS issue.
My issue is that I have managed to get the HIL working, but the framerate is sub-Hertz (i.e. about 1 or two frames during each 3 second reporting interval, or effectively 0 FPS). This makes the simulation not only painful to watch, but the plane will eventually crash as there is such a lag between control and feedback. That I got this far was an exercise in frustration that I hope to save my fellow members from experiencing.
First, there is no separate firmware for HIL, despite a link on the “Install Firmware” tab. If you click on HIL Simulator>Plane, you will get a “URI” error. I recommend that you use AP 3.3.0 as the newer version (esp. 3.5) dont’ seem to work. To enable HIL, connect to the Pixhawk, go to the “Config/Tuning” tab and select the “Full Parameter List”. Search for “HIL” and you will see 12 hits. The most important are the “HIL_MODE” and GPS_TYPE parameters. Enter 1 for HIL_MODE and 7 for the GPS_TYPE (do both for good measure). A reboot is needed after writing parameters for it to take effect (unplug and plug back in).
Okay, now for some code hacking. The mavproxy_HIL.py file has several errors that will prevent the HIL from working. In the function check_sim_in(), the code needs to be modified as such:
[code]def check_sim_in(self):
’’‘check for FDM packets from runsim’’‘
try:
pkt = self.sim_in.recv(188 + 4) # was 178 + 4
except socket.error as e:
if not e.errno in [ errno.EAGAIN, errno.EWOULDBLOCK ]:
raise
#print(“Socket error in check_sim_in”)
return
if len(pkt) != 188 + 4: # was 178 + 4
# wrong size, discard it
print(“wrong size %u” % len(pkt))
return
(timestamp, latitude, longitude, altitude, heading, v_north, v_east, v_down,
ax, ay, az,
phidot, thetadot, psidot,
roll, pitch, yaw,
vcas, check) = struct.unpack(’<Q17dI’, pkt) # was <17dI, added timestamp output variable Q (not sure if that is what the xtra 8 byes are)
(p, q, r) = self.convert_body_frame(radians(roll), radians(pitch), radians(phidot), radians(thetadot), radians(psidot))
try:
self.hil_state_msg = self.master.mav.hil_state_encode(int(time.time()*1e6),
radians(roll),
radians(pitch),
radians(yaw),
p,
q,
r,
int(latitude*1.0e7),
int(longitude*1.0e7),
int(altitude*1.0e3),
int(v_north*100),
int(v_east*100),
0,
int(ax*1000/9.81),
int(ay*1000/9.81),
int(az*1000/9.81))
except Exception:
return[/code]
The major error is that the sim is returning 8 more bytes that the code expects, resulting in a buffer overflow error. Additionally, you have to assign that extra 8 bytes; I don’t know what the value is, but I’m guessing it is a timestamp (see runsim.py in Tools/autotest folder). Note that you have to change the struct unpack to also account for it (adding ‘Q’ for long long unsigned int).
At this point, you should be able to run sim_vehicle.sh with the -H option to get the HIL module to load in mavproxy and see your Pixhawk. Look for output like this in the mavproxy windows:
Auto-detected serial ports are:
COM6 : PX4 FMU (COM6) : USB VID:PID=26AC:0011 SNR=0
Connecting to COM6 : PX4 FMU (COM6) : USB VID:PID=26AC:0011 SNR=0
Connect COM6 source_system=255
SRTMDownloader - server= firmware.diydrones.com, directory=/SRTM/.
SRTMDownloader - server= firmware.diydrones.com, directory=/SRTM/.
Loaded module console
Loaded module HIL
Loaded module graph
module console already loaded
Loaded module help
Log Directory:
Telemetry log: mav.tlog
Waiting for heartbeat from COM6MAV>
AUTO> Received 543 parameters
Saved 543 parameters to mav.parm
The console should show all “green” (see image).
Now, I modified the runsim.py file to print out the frames processed between reports. Initially it is 15 FPS, but quickly goes to 0 ( or two frames per 3 second reporting period). I have tried to eliminate sections of the main_loop() function, but so far no luck.
I’m hoping someone has hit this before, or will get to this point and can help me figure this out. Thanks in advance for your help.