BlueROV-8: Functions to Drive the Vehicle
来源:互联网 发布:天正建筑软件下载 编辑:程序博客网 时间:2024/05/18 20:51
from dronekit import connect, VehicleMode, LocationGlobal, LocationGlobalRelativefrom pymavlink importmavutil # Needed for command message definitionsimport timeimport mathvehicle =connect('/dev/ttyACM0', wait_ready=True) defarm_and_takeoff(aTargetAltitude): print"Arming motors" # Coptershould arm in GUIDED mode vehicle.mode = VehicleMode("ALT_HOLD") vehicle.armed = True print"Taking off!" vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude whileTrue: print " Altitude: ",vehicle.location.global_relative_frame.alt if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95: print "Reached target altitude" break time.sleep(1) arm_and_takeoff(5)print("Completed")As the motors are spinning, I can use vehicle.mode = VehicleMode("LAND") to stop motors instantly. (Use "control+c" first so that the command will not be ignored.)
The battery connected to Ardusub cannot be sensed. I don't know why as the battery works well when it connects to the ArduCopter.
Battery: Voltage=0.0,current=None, level=None.
Note:A value of None foran attribute member indicates that the value has not yet been populated fromthe vehicle. For example, before GPS lock Vehicle.gps_0 will return a GPSInfo with None valuesfor eph, satellites_visible etc. Attributes will also return None if theassociated hardware is not present on the connected device.Building ArduPilot for Pixhawk/PX4 on Mac with Make (Download the ArduCopter in the BlueROV)http://ardupilot.org/dev/docs/building-px4-with-make-on-mac.html
In the terminal:
brew tap PX4/homebrew-px4brew updatebrew install genromfsbrew install gcc-arm-none-eabibrew install gawksudo easy_install pipsudo pip installpyserialmkdir -p px4cd px4git clone --recursive https://github.com/ArduPilot/ardupilot.git
Set the current path to:/Users/apple/px4/ardupilot/ArduCopter
make cleanmake pix-v2
https://www.bluerobotics.com/forums/topic/autonomous-operation/
The Vehicle.mode, Vehicle.armed , Vehicle.airspeed and Vehicle.groundspeed, attributes can allbe “directly” written (Vehicle.home_location can also bedirectly written, but has special considerations that are discussed below).
Commands to change a value are not guaranteed to succeed (oreven to be received) and code should be written with this in mind. For example, the code snippet below polls the attribute values to confirm they have changed before proceeding.
vehicle.mode = VehicleMode("GUIDED")while not vehicle.mode.name=='GUIDED' and not vehicle.armed and not api.exit: time.sleep(1)vehicle print " Getting ready to take off ..."vehicle.armed =True
Observing attribute changes:
http://python.dronekit.io/guide/vehicle_state_and_parameters.htmlYou can observe any of the vehicle attributes and monitor forchanges without the need for polling.
Listeners (“observer callback functions”) are invoked differentlybased on the type of observed attribute. Attributes that represent sensorvalues or other “streams of information” are updated whenever a message isreceived from the vehicle. Attributes which reflect vehicle “state” are onlyupdated when their values change (for example Vehicle.system_status, Vehicle.armed,and Vehicle.mode).
Callbacks are added using Vehicle.add_attribute_listener() ortheVehicle.on_attribute()decorator method. The main difference between these methods is that onlyattribute callbacks added with Vehicle.add_attribute_listener() canbe removed (see remove_attribute_listener())
- The observer callback function is invoked with thefollowing arguments:
- self - the associated Vehicle. This may be compared to a global vehicle handle to implement vehicle-specific callback handling (if needed).
- attr_name - the attribute name. This can be used to infer which attribute has triggered if the same callback is used for watching several attributes.
- value - the attribute value (so you don’t need to re-query the vehicle object)
The code snippet below shows how to add (and remove) a callback function to observe changes in Vehicle.location.global_frame using Vehicle.add_attribute_listener(). The two second sleep() is required because otherwise the observer might be removed before the the callback is first run.
#Callback to print the location in globalframes. 'value' is the updated value def location_callback(self,attr_name, value): print "Location (Global): ", value# Add a callback `location_callback` for the `global_frame`attribute. vehicle.add_attribute_listener('location.global_frame', location_callback)# Wait 2s so callback can be notified before the observer is removed time.sleep(2)# Remove observer - specifying the attribute and previouslyregistered callback function vehicle.remove_message_listener('location.global_frame', location_callback)
Note: The example above adds a listener on Vehicle to for attribute name 'location.global_frame'You can alternatively add (and remove) alistenerVehicle.locationfor the attribute name 'global_frame'. Both alternatives are shown below:
vehicle.add_attribute_listener('location.global_frame', location_callback)vehicle.location.add_attribute_listener('global_frame', location_callback)
The example below shows how you can declare an attribute callback using the Vehicle.on_attribute() decorator function.
last_rangefinder_distance = 0@vehicle.on_attribute('rangefinder') def rangefinder_callback(self, attr_name): #attr_name not used here. globallast_rangefinder_distance if last_rangefinder_distance == round(self.rangefinder.distance, 1): return last_rangefinder_distance = round(self.rangefinder.distance, 1) print " Rangefinder(metres): %s" % last_rangefinder_distanceNote: The fragment above stores the result of the previous callback and only prints the output when there is a significant change in Vehicle.rangefinder. You might want to perform caching like this to ignore updates that are not significant to your code.
The examples above show how you can monitor a single attribute. You can pass the special name (‘*‘) to specify a callback that will be called for any/all attribute changes:
# Demonstrate getting callback on any attribute changedef wildcard_callback(self, attr_name, value): print " CALLBACK: (%s): %s" % (attr_name,value)print "\nAdd attribute callback detecting any attribute change"vehicle.add_attribute_listener('*', wildcard_callback)print " Wait 1s so callback invoked before observer removed"time.sleep(1)print " Remove Vehicle attribute observer"#Remove observer added with `add_attribute_listener()`vehicle.remove_attribute_listener('*', wildcard_callback)
- BlueROV-8: Functions to Drive the Vehicle
- BlueROV-6: Quick Start of Dronekit and Connect Dronekit to the Vehicle
- BlueROV-15: Modify the Drive Modes in Mavlink
- Move the datafile to release the diskspace on the drive
- Re: Action to manually drive the robot ** Re: Keyboard input.
- How To get the usbdisk's drive letter properly
- C# How to get the system drive of remote server
- VirtualBox: PIIX3 cannot attach drive to the Secondary Master (VERR_SHARING_VIOLATION)
- Adding Hard Drive space to the Virtual Machine
- BlueROV-16: Solve the initialization problem
- Identifying the replacement drive
- Prefer member functions to algorithms with the same names
- Vehicle
- abercrombie france the vehicle service protection
- How to solve the linking errors when using the ATOMIC functions
- INTRODUCTION TO JavaScript Functions
- Pointers to Functions
- Passing pointers to functions
- Hello World 之 Programming Language
- svn报:验证位置时发生错误(url,用户名密码无错)
- 分布式配置管理平台
- 多线程之volatile关键字(五)
- Linux 如何测试 IO 性能(磁盘读写速度)
- BlueROV-8: Functions to Drive the Vehicle
- vim在bash命令行中的简单操作
- JS中的单引号与双引号的区别
- goroutine使用最佳实践
- csu:1914: John and his party
- Vim编辑器相关
- 基于redis的分布式锁的实现
- Discuz!论坛教程之设置帖子被访问后自动提升主题
- Hibernate4调用SQL和存储过程