简体   繁体   中英

How to (correctly) install and import pymavlink using PIP

I'm using OS X Yosemite and trying to setup pymavlink using pip and python 2.7. Typed sudo pip install pymavlink and it installs beautifully. Tried to import 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 beyond toplevel package

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

Doesn't look like anyone is chiming in on this one with a textbook answer, so I'll go ahead and post how I cobbled together a bush-league python mavlink installation on a Raspberry Pi and, hopefully, someone will post a legit solution in the future for how to correctly do it with PIP. Welcome any comments/fixes!

install pymavlink by typing "sudo pip install pymavlink". This will install the pymavlink module (and the mavlinkv10.py file you'll have to modify) to 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 will first throw 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 a relative import from 3 parent directories "up" (I think?). Since the "generator" directory is actually a child directory under where mavlinkv10.py resides, 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 you run python mavlink 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 appears the mavlinkv10 is failing on the extra crc calculation in the pack() function. You can "fix" this error by turning off the extra crc stuff in mavlinkv10's pack() function. Comment out these 2 lines in mavlinkv10.py:

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

Last thing. You'll also need to also disable the crc2 calculation in the decode() function in mavlinkv10.py. Comment out these lines in mavlinkv10.py:

#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 pymavlink exemplar code, although I'm unclear how disabling the "extra CRC" calculation will impact mavlink reliability (probably not positively).

Instead of using

from pymavlink import mavlinkv10 as mavlink

try

from pymavlink.dialects.v10 import common as mavlink

replacing common with whatever dialect you may want (see the XML files in https://github.com/mavlink/mavlink/tree/master/message_definitions/v1.0 for all of the options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM