msg = vehicle.message_factory.distance_sensor_encode(
0, # time since system boot, not used
5, # min distance cm
10000, # max distance cm
int(d), # current distance, must be int
0, # type = laser?
0, # onboard id, not used
mavutil.mavlink.MAV_SENSOR_ROTATION_YAW_45, #direction
0 # covariance, not used
)
print(msg)
vehicle.send_mavlink(msg)
@suryaprakash_lokula what Firmware version are you running? Copter 4.1?
Can you send your param list? I think @ppoirier is right. As far as I remember, we have hardcoded a minimum 2hz limit. Anything below that, and your vehicle will throw a pre arm error.
Set PRX_TYPE = 2, and then increase your rate of sending those mavlink messages to anything beyond 2 HZ, it should work fine. Make sure you’re also fill time_boot_ms mavlink field; in the screenshot its 0.
and secondly you mean i would like to calculate the running time of the system and send it in the mavlink command
By the way i am using ultrasonic sensor
i tried your suggestion but drone did not respond for the message
my drone kit code is below
from dronekit import connect, VehicleMode, LocationGlobalRelative
import RPi.GPIO as GPIO
from pymavlink import mavutil
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
TRIG = 27 # Trigger pin of the Ultrasonic Sensor
ECHO = 17 #Echo pin of the Ultrasonic Sensor
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
milliseconds=0
#Connects to the vehicle using serial port.
print('Connecting to vehicle on: %s' % "/dev/ttyAMA0")
vehicle = connect('/dev/ttyAMA0', wait_ready=True, baud=921600) #Function to convert distance and orientation into a mavlink message
#vehicle.wait_ready(True, raise_exception=False)
def sensor_data(d,o):
msg = vehicle.message_factory.distance_sensor_encode(
milliseconds, # time since system boot
5, # min distance cm
10000, # max distance cm
int(d), # current distance, must be int
0, # type = laser?
0, # onboard id, not used
mavutil.mavlink.MAV_SENSOR_ROTATION_YAW_45, #direction
0 # covariance, not used
)
print(msg)
vehicle.send_mavlink(msg) #Simple function to measure the distance using ultrasonic sensor
return
def measure():
dist1=250
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
echo_state=0
pulse_end=0
while echo_state == 0:
echo_state = GPIO.input(ECHO)
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
if(distance<250 and distance>5): #To filter out junk values
dist1=distance
sensor_data(dist1,4) #Sends measured distance(dist1) and orientation(0) as a mavlink message.
else:
dist1=0
sensor_data(0,4)
return dist1
def arm_and_takeoff(aTargetAltitude):
print("Basic pre-arm checks")
# Don't try to arm until autopilot is ready
# while not vehicle.is_armable:
# print(" Waiting for vehicle to initialise...")
# time.sleep(1)
print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
# Confirm vehicle armed before attempting to take off
while not vehicle.armed:
print(" Waiting for arming...")
time.sleep(1)
print("Taking off!")
vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude
# Wait until the vehicle reaches a safe height before processing the goto
# (otherwise the command after Vehicle.simple_takeoff will execute
# immediately).
while True:
print(" Altitude: ", vehicle.location.global_relative_frame.alt)
# Break and return from function just below target altitude.
if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
print("Reached target altitude")
break
time.sleep(1)
print("vehicle started ")
arm_and_takeoff(1)
vehicle.parameters['AVOID_ENABLE'] = 7
vehicle.parameters['PRX_TYPE'] = 2
#time.sleep(3)
while True:
milliseconds = milliseconds+1
print("started")
d = measure()
time.sleep(0.3)
print(d)
Sorry but there is not much we can do for your equipment, you need 8 channels in order to accomplish this and finally try using SONAR is a waste of time. Over and Out… good luck