SITL not providing right lat long and altitude information

hello everyone

i am using ardupilot sitl and can’t able to get correct lat , long and altitude , below is the code i used , please suggest me a mavlink message for fetching and print lat long and altitude , also a screenshot is attached for reference, the drone follows the path but doesnt provide right lat,lon and altitude , the function currently i am using is MAVLINK_MSG_ID_GLOBAL_POSITION_INT but it provides different data , like altitude = 605420 which is wrong , and lat long does not get updated ,
please provide me a message to fetch the lat,lon with altitude.

drive link- reference - Google Drive

Start a conn listening on a UDP port

conn = mavutil.mavlink_connection(‘tcp:127.0.0.1:5762’)
print(“Heartbeat from system (system %u component %u)” %
(conn.target_system, conn.target_component))
#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
0, # Confirmation
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT, # param1: Message
ID to be streamed
100, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT and response.result
== mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

time.sleep(1)

#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, # ID of command to send
0, # Confirmation
1,
0, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

#time.sleep(1)
#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, # ID of command to send
0,0,0,0,0,0,0,10
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

Start a conn listening on a UDP port

#----

#time.sleep(2)

message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
0, # Confirmation
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT, # param1: Message
ID to be streamed
100, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

conn.mav.send(message)
response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
if response and response.command ==
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:
print(“Command accepted”)
else:
print(“Command failed”)

msg2 = conn.mav.set_position_target_global_int_encode(
0, # time_boot_ms (not used)
0, 0, # target system, target component
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
0b0000111111111000, # type_mask (only speeds enabled)
-35.362792501e7, # lat_int - X Position in WGS84 frame in 1e7 * meters
149.16603920
1e7, # lon_int - Y Position in WGS84 frame in 1e7 * meters
10, # alt - Altitude in meters in AMSL altitude, not WGS84 if absolute
or relative, above terrain if GLOBAL_TERRAIN_ALT_INT
2, # X velocity in NED frame in m/s
2, # Y velocity in NED frame in m/s
2, # Z velocity in NED frame in m/s
0, 0, 0, # afx, afy, afz acceleration (not supported yet, ignored in
GCS_Mavlink)
0, 0)

r = conn.mav.send(msg2)
print(“After Send”)
print(r)

while True:
response = conn.recv_match(type=“MAVLINK_MSG_ID_GLOBAL_POSITION_INT”,
blocking=True)
print(response)
time.sleep(0.25)

Wait for the first heartbeat

This sets the system and component ID of remote system for the link

from pymavlink import mavutil
import time

Start a conn listening on a UDP port

conn = mavutil.mavlink_connection(‘tcp:127.0.0.1:5762’)

print(“Heartbeat from system (system %u component %u)” %
(conn.target_system, conn.target_component))
#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
0, # Confirmation
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT, # param1:
Message ID to be streamed
100, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT and response.result
== mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

time.sleep(1)

#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, # ID of command to send
0, # Confirmation
1,
0, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

#time.sleep(1)
#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, # ID of command to send
0,0,0,0,0,0,0,10
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

Start a conn listening on a UDP port

#----

#time.sleep(2)

message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
0, # Confirmation
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT, # param1:
Message ID to be streamed
100, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

conn.mav.send(message)
response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
if response and response.command ==
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:
print(“Command accepted”)
else:
print(“Command failed”)

msg2 = conn.mav.set_position_target_global_int_encode(
0, # time_boot_ms (not used)
0, 0, # target system, target component
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
0b0000111111111000, # type_mask (only speeds enabled)
-35.362792501e7, # lat_int - X Position in WGS84 frame in 1e7 * meters
149.16603920
1e7, # lon_int - Y Position in WGS84 frame in 1e7 * meters
10, # alt - Altitude in meters in AMSL altitude, not WGS84 if
absolute or relative, above terrain if GLOBAL_TERRAIN_ALT_INT
2, # X velocity in NED frame in m/s
2, # Y velocity in NED frame in m/s
2, # Z velocity in NED frame in m/s
0, 0, 0, # afx, afy, afz acceleration (not supported yet,
ignored in GCS_Mavlink)
0, 0)

r = conn.mav.send(msg2)
print(“After Send”)
print(r)

while True:
response = conn.recv_match(type=“MAVLINK_MSG_ID_GLOBAL_POSITION_INT”,
blocking=True)
print(response)
time.sleep(0.25)

Wait for the first heartbeat

This sets the system and component ID of remote system for the link

from pymavlink import mavutil
import time

Start a conn listening on a UDP port

conn = mavutil.mavlink_connection(‘tcp:127.0.0.1:5762’)

print(“Heartbeat from system (system %u component %u)” %
(conn.target_system, conn.target_component))
#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
0, # Confirmation
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT, # param1: Message
ID to be streamed
100, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT and response.result
== mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

time.sleep(1)

#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, # ID of command to send
0, # Confirmation
1,
0, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

#time.sleep(1)
#----
message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, # ID of command to send
0,0,0,0,0,0,0,10
)

Send the COMMAND_LONG

#conn.mav.send(message)

Wait for a response (blocking) to the MAV_CMD_SET_MESSAGE_INTERVAL

command and print result
#response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
#if response and response.command ==
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:

print(“Command accepted”)

#else:

print(“Command failed”)

Start a conn listening on a UDP port

#----

#time.sleep(2)

message = conn.mav.command_long_encode(
conn.target_system, # Target system ID
conn.target_component, # Target component ID
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL,
0, # Confirmation
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT, # param1: Message
ID to be streamed
100, # param2: Interval in microseconds
0, # param3 (unused)
0, # param4 (unused)
0, # param5 (unused)
0, # param5 (unused)
0 # param6 (unused)
)

Send the COMMAND_LONG

conn.mav.send(message)
response = conn.recv_match(type=‘COMMAND_ACK’, blocking=True)
if response and response.command ==
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL and response.result ==
mavutil.mavlink.MAV_RESULT_ACCEPTED:
print(“Command accepted”)
else:
print(“Command failed”)

msg2 = conn.mav.set_position_target_global_int_encode(
0, # time_boot_ms (not used)
0, 0, # target system, target component
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
0b0000111111111000, # type_mask (only speeds enabled)
-35.362792501e7, # lat_int - X Position in WGS84 frame in 1e7 * meters
149.16603920
1e7, # lon_int - Y Position in WGS84 frame in 1e7 * meters
10, # alt - Altitude in meters in AMSL altitude, not WGS84 if absolute
or relative, above terrain if GLOBAL_TERRAIN_ALT_INT
2, # X velocity in NED frame in m/s
2, # Y velocity in NED frame in m/s
2, # Z velocity in NED frame in m/s
0, 0, 0, # afx, afy, afz acceleration (not supported yet, ignored in
GCS_Mavlink)
0, 0)

r = conn.mav.send(msg2)
print(“After Send”)
print(r)

while True:
response = conn.recv_match(type=“MAVLINK_MSG_ID_GLOBAL_POSITION_INT”,
blocking=True)
print(response)
time.sleep(0.25)