How to (correctly) install and import pymavlink using PIP

I am using OS X Yosemite and am trying to set up pymavlink with pip and python 2.7. Typed sudo pip install pymavlink

and it installs nicely. Tried importing it with

from pymavlink import mavlinkv10 as mavlink

      

and the interpreter throws this error:

File "/Library/Python/2.7/site-packages/pymavlink/mavlinkv10.py", line 10, in from ... generator.mavcrc import x25crc ValueError: Attempted relative import outside of top level package

I think I understand the error and can manually "fix" it by changing the implicit import "... generator" to the explicit "pymavlink.generator". My question is what is the correct way to install and import this package using pip. I may not be the first person to have this problem :)

+3


source to share


2 answers


It doesn't seem like anyone is clicking this question with a tutorial answer, so I'll go ahead and post how I combined installing python mushlink in a makeshift listing on a Raspberry Pi and hopefully someone will post a legitimate solution in the future on how do it correctly with PIP. Any comments / corrections are welcome!

install pymavlink by typing "sudo pip install pymavlink". This will install the pymavlink module (and the mavlinkv10.py file, which you will have to change) to the following directory on your pi "/usr/local/lib/python2.7/dist-packages/pymavlink/"

import the pymavlink module with the following line at the beginning of your Python code "from pymavlink import mavlinkv10 as mavlink"

When running the pymavlink example code from the pymavlink repository, the interpreter first throws the following error:

File "/Library/Python/2.7/site-packages/pymavlink/mavlinkv10.py"
in from ...generator.mavcrc import x25crc 
ValueError: Attempted relative import beyond toplevel package

      

It looks like mavlinkv10.py is trying to do relative imports from the three parent directories "up" (I guess?). Since the "generator" directory is actually a child directory of mavlinkv10.py, I changed the following in mavlinkv10.py:

changed this:  from ...generator.mavcrc import x25crc
to this:       from generator.mavcrc import x25crc

      



Next, the interpreter will throw the following error when running the mavlink python code that tries to create (encode) a new mavlink message:

File "/usr/local/lib/python2.7/dist-packages/pymavlink/mavlinkv10.py", line 1053, in pack
return MAVLink_message.pack(self, mav, 137, struct.pack('<QI', self.time_unix_usec, self.time_boot_ms))
File "/usr/local/lib/python2.7/dist-packages/pymavlink/mavlinkv10.py", line 110, in pack
crc.accumulate(chr(crc_extra))
File "/usr/local/lib/python2.7/dist packages/pymavlink/generator/mavcrc.py", line 18, in accumulate
tmp = b ^ (accum & 0xff)
TypeError: unsupported operand type(s) for ^: 'str' and 'int'

      

It looks like mavlinkv10 doesn't work with the extra crc computation in the pack () function. You can "fix" this error by disabling additional crc files in the mavlinkv10 pack () function. Comment out these 2 lines in mavlinkv10.py:

#if True: # using CRC extra
#    crc.accumulate(chr(crc_extra))

      

The last thing. You also need to disable crc2 computation in the decode () function in mavlinkv10.py. Comment these lines in the mavlinkv10.py file:

#if True: # using CRC extra 
#    crc2.accumulate(chr(crc_extra))
#if crc != crc2.crc:
#    raise MAVError('invalid MAVLink CRC in msgID 

      

At this point, you should be able to run the pymavlink example code, although I don't understand how turning off the "extra CRC" calculation would affect mavlink reliability (perhaps not positively).

+4


source


Instead of using

from pymavlink import mavlinkv10 as mavlink

      

try



from pymavlink.dialects.v10 import common as mavlink

      

replacing common

with any dialect you may want (see the XML files at https://github.com/mavlink/mavlink/tree/master/message_definitions/v1.0 for all options)

+1


source







All Articles