shoing this error fo the belwo ocde deepin@deepin-o2i:/media/deepin/Deepin Pen…
- User
shoing this error fo the belwo ocde deepin@deepin-o2i:/media/deepin/Deepin Pen Drive/main/drone_swarm_3/v2/test$ python test_2.py Traceback (most recent call last): File "test_2.py", line 67, in master_drone = Drone(2, the_connection) TypeError: Drone() takes no arguments
from pymavlink import mavutil import math import time
the_connection = mavutil.mavlink_connection('/dev/ttyUSB0', baud=57600) 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, msg.alt / 10 ** 3) - Assistant
The issue appears to be with the
initmethod in theDroneclass. It should be defined using the double underscore__init__method, which is a special method used for initializing an object in Python. Change your Drone class definition to have__init__instead ofinit. Here is the corrected version of your code:pythonfrom pymavlink import mavutil import math import time the_connection = mavutil.mavlink_connection('/dev/ttyUSB0', baud=57600) class Drone: def __init__(self, system_id, connection): # Use __init__ here self.system_id = system_id self.connection = connection # Rest of Drone methods # Rest of the codeNow, when you create a
Droneobject, the right method will be called, and the error should be resolved. - User
- Assistant