Altitude Type Error

await drone.offboard.set_position_global(
PositionGlobalYaw(
pid_controller.target_latitude,
pid_controller.target_longitude,
pid_controller.target_altitude,
0.0,
altitude_type=0
)
)
An error occurred: ‘int’ object has no attribute ‘translate_to_rpc’

I wrote this code with respect to documentation. However, it raises an error because of altitude_type.

Please share a bit more of the code. Best a small reproducible example.

import asyncio
import rospy
from mavsdk import System
from mavsdk.offboard import (OffboardError, PositionGlobalYaw)

async def run():
    drone = System()
    await drone.connect(system_address="udp://:14540")

    rospy.loginfo("Waiting for drone to connect...")
    async for state in drone.core.connection_state():
        if state.is_connected:
            rospy.loginfo("-- Connected to drone!")
            break

    rospy.loginfo("Waiting for drone to have a global position estimate...")
    async for health in drone.telemetry.health():
        if health.is_global_position_ok and health.is_home_position_ok:
            rospy.loginfo("-- Global position estimate OK")
            break

    target_latitude = 47.4084163
    target_longitude = 8.5554128
    target_altitude = 575.1094879880884

    rospy.loginfo("-- Arming")
    await drone.action.arm()

    rospy.loginfo("-- Taking Off")
    await drone.action.takeoff()
    await asyncio.sleep(10)

    rospy.loginfo("-- Setting initial setpoint")
    await drone.offboard.set_position_global(PositionGlobalYaw(47.3984163, 8.5454128, 568.1094879880884, 0.0, 1))
    rospy.loginfo("-- Starting offboard")
    try:
        await drone.offboard.start()
    except OffboardError as error:
        rospy.logerr(f"Starting offboard mode failed with error code: {error._result.result}")
        rospy.loginfo("-- Disarming")
        await drone.action.disarm()
        return

    await drone.offboard.set_position_global(PositionGlobalYaw(
                target_latitude,
                target_longitude,
                target_altitude,
                0.0,
                1
                )
    )

    rospy.loginfo("-- Returning")
    await drone.action.return_to_launch()

    rospy.loginfo("-- Stopping offboard")
    try:
        await drone.offboard.stop()
    except OffboardError as error:
        rospy.logerr(f"Stopping offboard mode failed with error code: {error._result.result}")
        rospy.loginfo("-- Attempting to land")
        await drone.action.land()

if __name__ == "__main__":
    rospy.init_node('drone_control_script')
    asyncio.run(run())

I regulated the code a bit.

Error:
Traceback (most recent call last):
  File "/home/orkan/Desktop/VSC/MavSDK/position_ned.py", line 66, in <module>
    asyncio.run(run())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/orkan/Desktop/VSC/MavSDK/position_ned.py", line 34, in run
    await drone.offboard.set_position_global(PositionGlobalYaw(47.3984163, 8.5454128, 568.1094879880884, 0.0, 1))
  File "/home/orkan/.local/lib/python3.8/site-packages/mavsdk/offboard.py", line 1342, in set_position_global
    position_global_yaw.translate_to_rpc(request.position_global_yaw)
  File "/home/orkan/.local/lib/python3.8/site-packages/mavsdk/offboard.py", line 626, in translate_to_rpc
    rpcPositionGlobalYaw.altitude_type = self.altitude_type.translate_to_rpc()
AttributeError: 'int' object has no attribute 'translate_to_rpc'

I see. Thanks for providing that. I’ve added ``` around the code to format it nicer.

Alright, so it looks like the fifth argument altitude type is an int intead of the enum type that it is supposed to be.

Can you try with something like:

PositionGlobalYaw(47.3984163, 8.5454128, 568.1094879880884, 0.0, PositionGlobalYaw.AltitudeType.AMSL)