is the below code corect? from pymavlink import mavutil import math import time class…
- User
is the below code corect? from pymavlink import mavutil import math import time
class Drone:
def __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): vx, vy, vz = calculate_velocity_components(wp, next_wp, speed) 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, msg.alt / 10 ** 3) def get_battery_status(self): msg = self.connection.recv_match(type='SYS_STATUS', blocking=False) if msg and msg.get_srcSystem() == self.system_id: return msg.battery_remain def get_rc_status(self): msg = self.connection.recv_match(type='RC_CHANNELS_RAW', blocking=False) if msg and msg.get_srcSystem() == self.system_id: return msg.chan8_raw def get_gps_status(self): msg = self.connection.recv_match(type='GPS_RAW_INT', blocking=False) if msg and msg.get_srcSystem() == self.system_id: return msg.satellites_visible, msg.ephclass PIDController:
def __init__(self, kp, ki, kd, limit): self.kp = kp self.ki = ki self.kd = kd self.limit = limit self.prev_error = 0 self.integral = 0 def update(self, error, dt): derivative = (error - self.prev_error) / dt self.integral += error * dt self.integral = max(min(self.integral, self.limit), -self.limit) # Clamp the integral term output = self.kp * error + self.ki * self.integral + self.kd * derivative self.prev_error = error return outputdistance = 5 # Distance in meters angle = 60 # Angle in degrees kp = 0.1 ki = 0.01 kd = 0.05 pid_limit = 0.0001 pid_lat = PIDController(kp, ki, kd, pid_limit) pid_lon = PIDController(kp, ki, kd, pid_limit)
- Assistant
The code looks generally correct but there is an issue with the check_safety function:
In the following line:
if master_gps_status is not None and master_gps_status[0] < 6 or follower_gps_status is not None and follower_gps_status[0] < 6 or <br/> master_gps_status is not None and master_gps_status[1] > 200 or <br/> follower_gps_status is not None and follower_gps_status[1] > 200:Remove the
<br/>tags:if master_gps_status is not None and master_gps_status[0] < 6 or follower_gps_status is not None and follower_gps_status[0] < 6 or master_gps_status is not None and master_gps_status[1] > 200 or follower_gps_status is not None and follower_gps_status[1] > 200:Also, the code is missing an
_exit()function for the normal exit, so you need to define one.Other than that, the code seems correct. Make sure to test the code in a safe and controlled environment to ensure proper functionality, as it is a drone flight control script.