BlueROV-8: Functions to Drive the Vehicle

Some functions like goto function or speed setting function are limited because they are GPS dependent. GPS is not available for an underwater robot. If I change the vehicle mode to "ALT_HOLD", the arm_and_takeoff function will work:
from dronekit import connect, VehicleMode, LocationGlobal, LocationGlobalRelative
from pymavlink importmavutil # Needed for command message definitions
import time
import math
vehicle =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 ephsatellites_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-px4
brew update
brew install genromfs
brew install gcc-arm-none-eabi
brew install gawk
sudo easy_install pip
sudo pip installpyserial
mkdir -p px4
cd px4
git clone --recursive https://github.com/ArduPilot/ardupilot.git

Set the current path to:/Users/apple/px4/ardupilot/ArduCopter

make clean
make pix-v2

https://www.bluerobotics.com/forums/topic/autonomous-operation/


Setting attributes:
The Vehicle.modeVehicle.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.html
You 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_statusVehicle.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_distance

Note: 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 change
def 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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值