# Quadcopter skyrockets when I publish "mavros/setpoint\_raw/global"

**URL:** <https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051>\
**Category:** PX4 Autopilot\
**Created:** [August 8, 2020, 12:42pm UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051 "2020-08-08T12:42:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlyingToTheSpace](https://discuss.px4.io/user_avatar/discuss.px4.io/flyingtothespace/32/6784_2.png) [@FlyingToTheSpace](https://discuss.px4.io/u/FlyingToTheSpace)\
**Post date:** [August 8, 2020, 12:42pm UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/1 "2020-08-08T12:42:42Z")

</div>

Hello, everyone.  
I am doing research about quadcopter position control based on GPS coordinates.

I am using pixhawk4 mini for flight control.  
I have a landing target in a distance from the quadcopter and it subscribes the target’s GPS coordinates(latitude, longitude).

I am trying to move the drone to the landing target by GPS coordinates information.

I created

> sp\_glob\_pub = rospy.Publisher(‘mavros/setpoint\_raw/global’, GlobalPositionTarget, queue\_size=1)

and then later, I publish it.

> sp\_glob\_pub.publish(cnt.sp\_glob)

However, when the code runs, the drone does NOT move to the target but it skyrockets(goes high up).

We also tried

> sp\_pub = rospy.Publisher(‘mavros/setpoint\_raw/local’, PositionTarget, queue\_size=1)

> sp\_pub.publish(cnt.sp)

We still have the drone going up.

I think there might be some errors that we are missing in the whole code. Can anyone give some insights for this?

Followings are the full code.

> #!/usr/bin/env python  
> import rospy
> 
> from geometry\_msgs.msg import Point, PoseStamped # /opt/ros/melodic/share/geometry\_msgs  
> from mavros\_msgs.msg import \*  
> from mavros\_msgs.srv import \*  
> from sensor\_msgs.msg import NavSatFix
> 
> from targetDetection\_topic.msg import TargetPosition  
> from GPS\_topic.msg import Target\_GPS

> class Controller:
> 
> > def **init** (self):
> > 
> > > ```
> > > self.sp = PositionTarget()
> > > 
> > > ```
> 
> ```
> self.sp.type_mask = int('010111111000', 2) # set the flag to use position setpoints and yaw angle
> self.sp.coordinate_frame = 1 # FRAME_LOCAL_NED
> self.sp_glob = GlobalPositionTarget()
> self.sp_glob.type_mask = int('111111111000', 2)
> self.sp_glob.coordinate_frame = 6 # FRAME_GLOBAL_INT
> self.sp_glob.latitude = 0
> self.sp_glob.longitude = 0
> self.sp_glob.altitude = 0
> self.target_gps = Target_GPS()
> self.local_pos = Point(0.0, 0.0, 0.0)
> self.global_pos = Point(0.0, 0.0, 0.0)
> self.sp.position.x = 0.0 # geometry_msgs/Point position
> self.sp.position.y = 0.0
> def posCb(self, msg): # rospy.Subscriber('mavros/local_position/pose', PoseStamped, cnt.posCb)
> self.local_pos.x = msg.pose.position.x # PoseStamped(msg).Pose(pose).Point(position).x
> self.local_pos.y = msg.pose.position.y
> self.local_pos.z = msg.pose.position.z
> print('posCb called')
> 
> def posCb_glob(self, msg_glob): # rospy.Subscriber('mavros/global_position/global', NavSatFix, cnt.posCb_glob)
> self.global_pos.x = msg_glob.latitude # NavSatFix(msg_glob).latitude -> global_pos.x = latitude
> self.global_pos.y = msg_glob.longitude
> self.global_pos.z = msg_glob.altitude
> print('posCb_glob called')
> 
> def GPS_callback(self, msg):
> self.target_gps.latitude = msg.latitude
> self.target_gps.longitude = msg.longitude
> print('GPS_callback called')
> print(self.target_gps)
> 
> ## Update setpoint message
> def updateSp(self):
> self.sp.position.x = self.local_pos.x
> self.sp.position.y = self.local_pos.y
> 
> def updateSp_glob(self):
> self.sp_glob.latitude = self.target_gps.latitude
> self.sp_glob.longitude = self.target_gps.longitude
> self.sp_glob.altitude = 0
> print('updateSp_glob called')
> 
> def x_dir(self):
> self.sp.position.x = self.local_pos.x + 1
> self.sp.position.y = self.local_pos.y
> 
> def y_dir(self):
> self.sp.position.x = self.local_pos.x
> self.sp.position.y = self.local_pos.y + 1
> 
> ```
> 
> class fcuModes:  
> def **init** (self):  
> pass
> 
> ```
> def setArm(self):
> rospy.wait_for_service('mavros/cmd/arming')
> try:
> armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
> armService(True)
> print('Arming Succeeded')
> except rospy.ServiceException, e:
> print
> "Service arming call failed: %s" % e
> 
> def setOffboardMode(self):
> rospy.wait_for_service('mavros/set_mode')
> try:
> flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
> flightModeService(custom_mode='OFFBOARD')
> print('Changed to OffboardMode')
> except rospy.ServiceException, e:
> print
> "service set_mode call failed: %s. Offboard Mode could not be set." % e
> 
> def setTakeoff(self, global_pos):
> rospy.wait_for_service('mavros/cmd/takeoff')
> try:
> takeoffService = rospy.ServiceProxy('mavros/cmd/takeoff', mavros_msgs.srv.CommandTOL)
> takeoffService(latitude=global_pos.x, longitude=global_pos.y, altitude=1)
> print('TakeOff Succeeded')
> except rospy.ServiceException, e:
> print
> "Service takeoff call failed: %s" % e
> 
> def setAutoLandMode(self):
> rospy.wait_for_service('mavros/set_mode')
> try:
> flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
> flightModeService(custom_mode='AUTO.LAND')
> print('Auto Landing..')
> except rospy.ServiceException, e:
> print
> "service set_mode call failed: %s. Autoland Mode could not be set." % e
> 
> def setDisarm(self):
> rospy.wait_for_service('mavros/cmd/arming')
> try:
> armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
> armService(False)
> print('Disarming Succeeded')
> except rospy.ServiceException, e:
> print
> "Service disarming call failed: %s" % e
> 
> ```
> 
> # Main function
> 
> def main():  
> # initiate node  
> rospy.init\_node(‘SNU\_drone’, anonymous=True)
> 
> ```
> # controller object
> cnt = Controller()
> 
> # flight mode object
> modes = fcuModes()
> 
> # ROS loop rate
> rate = rospy.Rate(20.0)
> 
> # Subscribe to drone's local position
> rospy.Subscriber('mavros/local_position/pose', PoseStamped, cnt.posCb) # cnt.local_pos = Drone's local_position
> rospy.sleep(0.2)
> rospy.Subscriber('mavros/global_position/global', NavSatFix,
> cnt.posCb_glob) # cnt.global_pos = Drone's GPS position(lat, long, alt)
> rospy.sleep(0.2)
> rospy.Subscriber('/Target_GPS_msg', Target_GPS, cnt.GPS_callback)
> rospy.sleep(0.2)
> # Setpoint publisher
> sp_pub = rospy.Publisher('mavros/setpoint_raw/local', PositionTarget, queue_size=1)
> rospy.sleep(0.2)
> sp_glob_pub = rospy.Publisher('mavros/setpoint_raw/global', GlobalPositionTarget, queue_size=1)
> rospy.sleep(0.2)
> 
> ##########################################
> #### Now the Drone starts operation!! ####
> ##########################################
> 
> # # Arm the drone
> modes.setArm()
> rospy.sleep(1)
> 
> # # We need to send few setpoint messages, then activate OFFBOARD mode, to take effect
> k = 0
> while k < 10:
> sp_pub.publish(cnt.sp)
> rate.sleep()
> k = k + 1
> 
> # activate OFFBOARD mode
> modes.setOffboardMode()
> 
> # # TakeOff
> modes.setTakeoff(cnt.global_pos)
> rospy.sleep(3)
> 
> # # Update sp_glob to Target's GPS
> cnt.updateSp_glob()
> rospy.sleep(1)
> 
> # # # # Update sp to Target's local position
> cnt.updateSp()
> rospy.sleep(1)
> cnt.x_dir()
> rospy.sleep(0.2)
> 
> # # # Move the drone to Target's GPS position
> sp_glob_pub.publish(cnt.sp_glob)
> rospy.sleep(5)
> 
> # # Move the drone to Target's local position by Camera info
> sp_pub.publish(cnt.sp)
> rospy.sleep(3)
> 
> # Land the drone
> modes.setAutoLandMode()
> rospy.sleep(3)
> 
> # Disarm the drone
> modes.setDisarm()
> rospy.sleep(2)
> 
> ```
> 
> if **name** == ‘ **main** ’:  
> try:  
> main()  
> except rospy.ROSInterruptException:  
> pass

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [August 8, 2020, 1:48pm UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/2 "2020-08-08T13:48:29Z")

</div>

@FlyingToTheSpace Can you provide a flight log?

Also,

```auto
    self.sp.position.x = self.local_pos.x + 1

```

Looks problematic, It will continuously diverge as you will be constantly setting the setpoint 1m away from where the vehicle is

---

<div class="post-metadata">

**Author:** ![FlyingToTheSpace](https://discuss.px4.io/user_avatar/discuss.px4.io/flyingtothespace/32/6784_2.png) [@FlyingToTheSpace](https://discuss.px4.io/u/FlyingToTheSpace)\
**Post date:** [August 9, 2020, 3:32am UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/3 "2020-08-09T03:32:24Z")

</div>

To, Jaeyoung-Lim

 ![Capture](https://discuss.px4.io/uploads/default/original/2X/2/2d60c834064833127554f9978680803bd8e17fba.jpeg) ![Capture2](https://discuss.px4.io/uploads/default/original/2X/3/358d40960e0a8011249913dd250879c330bd7b95.jpeg)  
Following is the link that contains the full flight log.

[https://drive.google.com/file/d/1eKVmC-SF-JhIeSd8FjLq6Y5hLxGD8iVl/view?usp=sharing](https://drive.google.com/file/d/1eKVmC-SF-JhIeSd8FjLq6Y5hLxGD8iVl/view?usp=sharing)

Please let me know if there are any problems!  
Thank you

---

<div class="post-metadata">

**Author:** ![FlyingToTheSpace](https://discuss.px4.io/user_avatar/discuss.px4.io/flyingtothespace/32/6784_2.png) [@FlyingToTheSpace](https://discuss.px4.io/u/FlyingToTheSpace)\
**Post date:** [August 9, 2020, 3:37am UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/4 "2020-08-09T03:37:30Z")

</div>

> [@Jaeyoung-Lim](#):
>
> `self.sp.position.x = self.local_pos.x + `

why is it diverge?

> self.sp.position.x = self.local\_pos.x +1

We are calling

> cnt.x\_dir()

only one time.  
And we thought it will move the drone only 1m away.

Are we misunderstanding the flow of the code?

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [August 9, 2020, 6:34am UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/5 "2020-08-09T06:34:48Z")

</div>

@FlyingToTheSpace Could you share the flight log through [https://review.px4.io/](https://review.px4.io/) ? Somehow I can’t download the log file

For offboard control, you need to be continously streaming setpoints. However, briefly skimming the code you are sending setpoints and then stop streaming setpoints and activating offboard mode. Is this the intended way you were trying to control the vheicle?

Here is an example you can see how it should have been done when controlling through mavros with setpoint\_raw: [https://github.com/Jaeyoung-Lim/mavros\_controllers/blob/master/trajectory\_publisher/src/trajectoryPublisher.cpp#L148](https://github.com/Jaeyoung-Lim/mavros_controllers/blob/master/trajectory_publisher/src/trajectoryPublisher.cpp#L148)

---

<div class="post-metadata">

**Author:** ![FlyingToTheSpace](https://discuss.px4.io/user_avatar/discuss.px4.io/flyingtothespace/32/6784_2.png) [@FlyingToTheSpace](https://discuss.px4.io/u/FlyingToTheSpace)\
**Post date:** [August 9, 2020, 6:56am UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/6 "2020-08-09T06:56:29Z")

</div>

Following is the link!

[https://logs.px4.io/plot\_app?log=d2eb49fc-f3ee-4544-85d9-0b6110221644](https://logs.px4.io/plot_app?log=d2eb49fc-f3ee-4544-85d9-0b6110221644)

Thank you again

---

<div class="post-metadata">

**Author:** ![Jaeyoung-Lim](https://discuss.px4.io/user_avatar/discuss.px4.io/jaeyoung-lim/32/10829_2.png) [@Jaeyoung-Lim](https://discuss.px4.io/u/Jaeyoung-Lim)\
**Post date:** [August 9, 2020, 7:54am UTC](https://discuss.px4.io/t/quadcopter-skyrockets-when-i-publish-mavros-setpoint-raw-global/18051/7 "2020-08-09T07:54:27Z")

</div>

@FlyingToTheSpace So as suspected, the vehicle never went into offboard mode, and was just trying to takeoff
