ok so there are 2 problems in the below code, we went to testing today, we found 2…
- User
ok so there are 2 problems in the below code, we went to testing today, we found 2 problems
- master is not going to the waypoints, but follower is moving
- speed is not set to 3 meter per second, it is moving by default value
please tell why!
from pymavlink import mavutil import math import time
the_connection = mavutil.mavlink_connection('COM14', baud=57600) the_connection.wait_heartbeat()
msg = the_connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True) master_waypoint = (msg.lat / 10 ** 7, msg.lon / 10 ** 7, 10)
waypoints = [ master_waypoint, (28.5861327, 77.3420592, 10), (28.5860912, 77.3420042, 10), # Repeat the first waypoint to make the drone return to its starting point ] distance = 5 # Distance in meters angle = 60 # Angle in degrees kp = 0.1 ki = 0.01 kd = 0.05 pid_limit = 0.0001
class Drone:
codedef __init__(self, system_id, connection): self.system_id = system_id self.connection = connection def set_mode(self, mode): self.connection.mav.set_mode_send( self.system_id, mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, mode ) def arm(self, arm=True): self.connection.mav.command_long_send(self.system_id, self.connection.target_component, mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, int(arm), 0, 0, 0, 0, 0, 0) def takeoff(self, altitude): self.connection.mav.command_long_send(self.system_id, self.connection.target_component, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, altitude) def send_waypoint(self, wp, next_wp, speed): # Print wp and next_wp print("Current waypoint: {} | Next waypoint: {}".format(wp, next_wp)) vx, vy, vz = calculate_velocity_components(wp, next_wp, speed) # Print velocity components print("Velocity components: vx={}, vy={}, vz={}".format(vx, vy, vz)) self.connection.mav.send(mavutil.mavlink.MAVLink_set_position_target_global_int_message( 10, self.system_id, self.connection.target_component, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, int(0b110111111000), int(wp[0] * 10 ** 7), int(wp[1] * 10 ** 7), wp[2], vx, vy, vz, 0, 0, 0, 0, 0 # Set vx, vy, and vz from calculated components )) def get_position(self): self.connection.mav.request_data_stream_send( self.system_id, self.connection.target_component, mavutil.mavlink.MAV_DATA_STREAM_POSITION, 1, 1) while True: msg = self.connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True) if msg.get_srcSystem() == self.system_id: return (msg.lat / 10 ** 7, msg.lon / 10 ** 7, 10) - Assistant
It looks like the code is not setting the speed of 3 meters per second correctly. Let's try to fix this issue first.
The speed should be set in the
send_waypointfunction when sending the waypoint to the drone. Currently, the function calculates the velocity components but doesn't consider the speed value. We should multiply thevx,vy, andvzby the speed when sending the waypoint message.Update the
send_waypointfunction like this:python