Controlling Attitude with MAVlink

So basically, my goal is to be able to set or control the attitude of my quadcopter using MAVlink, I’ve downloaded pymavlink and dronekit and am sticking to python because it’s nice and easy for a beginner like me ( o.k at python, new to mavlink and general ardupilot programming).

So far I’ve tried the dronekit commands, these haven’t worked out too well, and the documentation even mentions this : “ArduPilot does not currently support controlling the vehicle by specifying acceleration/force components.” (http://python.dronekit.io/guide/copter/guided_mode.html) perhaps there is something I am missing, but this seems like another dead end.

I’ve also tried sending messages, by encoding and then sending through mavlink. The code looks something like this:

#msg= vehicle.message_factory.hil_controls_encode(0,0.174533,3,0,100,0,0,0,0,1,1)
msg= vehicle.message_factory.attitude_encode( 0,0.174533,0,0,1,1,1)
  print str(msg)
  for x in range(0,20):
   vehicle.send_mavlink(msg)
   print " Altitude: ", vehicle.location.global_relative_frame.alt
   print "Attitude: %s" % vehicle.attitude
   time.sleep(1)

I have tried various messages (All taken from :https://www.samba.org/tridge/UAV/pymavlink/apidocs/mavlink.MAVLink.html#hil_controls_encode
)
I have already tried hils_controls_encode,manual_control_encode, and set_roll_pitch_yaw_thrust_encode. They all do not work.

But it appears that this does not even work.Everything I try results in the drone staying still. When I change flightmodes from GUIDED to LOITER or STABILIZE, the drone just falls out of the air (in SITL). Does anyone happen to know how I can move my drone, without giving a gps coordinate in GUIDED mode?

Thanks!

You need the mavlink command #82.
http://mavlink.org/messages/common#SET_ATTITUDE_TARGET
Python command is similar.

Thanks! I guess that answered my question. The Quadcopter actually changes orientation now, but now I just can’t figure out how the quaternions work. I set it to w=0.866, x=0,y=0.5,z=0, which apparently is the same as a 60 degree incline on the y axis, and the drone seems to rotate 40-50 degrees on the z-axis while spinning in circles. Any Ideas? Or should I just figure it out my self. Nevertheless, thank you very much for the help!

EDIT- Well I figure the spinning may be due to the flight controller being in GUIDED mode and wanting to stay at the same position. However, when I change the flight mode to ALT_HOLD, the helicopter does not rotate and beings to descend. This behavior confuses me.

Check your “type_mask”, and set it as your need.

Blockquote
Mappings: If any of these bits are set, the corresponding input should be ignored: bit 1: body roll rate, bit 2: body pitch rate, bit 3: body yaw rate. bit 4-bit 6: reserved, bit 7: throttle, bit 8: attitude

We ususlly use yaw_rate rather thar yaw. The yaw_rate “0” means no rotating.
For mode, this command just work at GUIDED mode.

Can u share your script?
You can alternatively set the control commands by mimicking the RC 4 channels via dronekit toolbox. Each channel gets PWM command from 1000 to 2000. 1500 is set to zero by default (except throttle channel)