程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python3 and python2 mixed operation

編輯:Python

problem : In the use of osqp Library writing MPC After procedure , Trolley coordinates need tf transformation , and tf Libraries can only be used in python2 Environmental use , It's caused python Environmental conflict

Solutions : The safer idea is , Create a new workspace , Configure to python3 After the environment ,source Take a look at this workspace , You can run dual environments .

1. Create a workspace py3_ws, Clone Source geometry and geometry2 To src in

Here is a workspace that has been created :

link :py3_ws
Extraction code :pyws

2. install python3 rely on

sudo apt install python3-catkin-pkg-modules python3-rospkg-modules python3-empy ipython3

3.numpy install

pip3 install numpy

4. Delete CMakeLists.txt

Because it's in my Linux On the system , therefore CMakeLists.txt The path inside will conflict with your system path , So delete , Recompile

cd py3_ws/src

sudo rm CMakeLists.txt

 5. recompile

If python3 The version is 3.5 Or something , First, install other versions python, And then 3.6 Change it to your own python edition .

catkin_make --cmake-args -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so

 6.source working space

source py3_ws/devel/setup.bash

  Every time you need to mix two versions source Take a look at this workspace , If it's too much trouble , You can add this command to .bashrc In file .

sudo gedit ~/.bashrc

  Add the full path at the bottom

source /home/zhz/gzrobot_ws/devel/setup.bash # Original workspace
source /home/zhz/py3_ws/devel/setup.bash # New workspace 

test :

There is an existing workspace in our system ( example :gzrobot_ws), There is also a newly created workspace py3_ws, We only need to operate in the former , Writing python When you file , Interpreter selection python3. The test procedure is as follows :

#!/usr/bin/env python3
# -*- coding=utf-8 -*-
import osqp
import rospy
import tf
if __name__ == "__main__":
rospy.init_node('test', anonymous=True)
while not rospy.is_shutdown():
rate = rospy.Rate(1)
br = tf.TransformBroadcaster()
br.sendTransform((0.0, 0.0, 1.0),
tf.transformations.quaternion_from_euler(0, 0, 3.1415926/2),
rospy.Time.now(),'camera','map')
rate.sleep()

The completed function is to call osqp library , establish tf Coordinate transformation , stay rviz Observe the relationship between the camera and the map

Just run this program , give the result as follows :

 

Can run python2 Of tf, It can also run python3 Of osqp


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved