Hello, I am working on a project that uses pymavlink to programmatically issue commands to a simulated plane. I have a problem where pymavlink crashes whenever I send a message using pymavlink’s wrapper functions or when I use recv_match to wait for a message due to one of three errors:
bytearray / array index out of bounds:
Traceback (most recent call last):
File "/mavsim/mavproxy_utils/ardupilot.py", line 55, in mavMsgHandler
msg = m.recv_msg()
File "/usr/lib/python2.7/site-packages/pymavlink/mavutil.py", line 926, in recv_msg
m = self.mav.parse_char(s)
File "/usr/lib/python2.7/site-packages/pymavlink/dialects/v10/ardupilotmega.py", line 9587, in parse_char
m = self.__parse_char_legacy()
File "/usr/lib/python2.7/site-packages/pymavlink/dialects/v10/ardupilotmega.py", line 9604, in __parse_char_legacy
if self.buf_len() >= 1 and self.buf[self.buf_index] == PROTOCOL_MARKER_V2:
and a “local variable “magic” has been referenced before assignment” error. I don’t have a stack trace for this one, but I do know that the line where it occurs is in a generated python file. The line that generates it can be found in pymavlink/generator/mavgen_python.py on line 559.
An example of one of my functions that sends commands, where self.craft.mav is the reference to pymavlink’s mavutil.py:
def set_wp(self, wp_list):
'''
@description:
Set the craft's mission in terms of waypoints according to the stored waypoints
@params:
wp_list: list of tuples that represent waypoints
'''
# Send number of waypoints to expect
self.craft.mav.waypoint_count_send(len(wp_list))
count = 0
for wp in wp_list:
if self.craft.mav.recv_match(type='MISSION_REQUEST', blocking=True, timeout=5) is None:
settings.logger.error('Waypoint ' + str(wp[0]) + 'was not set due to timeout')
else:
self.craft.mav.setMissionItem(int(wp[0]), int(wp[1]), int(wp[2]),
int(wp[3]), float(wp[4]), float(wp[5]),
float(wp[6]), float(wp[7]), float(wp[8]),
float(wp[9]), float(wp[10]), int(wp[11]))
settings.logger.debug('Waypoint ' + wp)
count += 1
settings.logger.info('Set ' + count + '/' + len(wp_list) + 'waypoints')
From what I can tell, I am probably overloading mavlink with too many messages. I have tried to space out message sending by 5 seconds, but I still receive these errors intermittently. I have attempted this using code similar to mavparm.py example and still got one of the three errors. Any suggestions?