fork(2) download
  1. import math
  2. import time
  3.  
  4. import dronekit
  5.  
  6. copter = dronekit.connect('tcp:', wait_ready=True)
  7.  
  8. print("connected")
  9.  
  10.  
  11. def send_attitude_target(self, roll_angle=0.0, pitch_angle=0.0,
  12. yaw_angle=0.0, yaw_rate=10, thrust=0.5):
  13. msg = copter.message_factory.set_attitude_target_encode(
  14. 0,
  15. 0, # target system
  16. 0, # target component
  17. 0b00000000, # type mask: bit 1 is LSB
  18. to_quaternion(roll_angle, pitch_angle, yaw_angle), # q
  19. 0, # body roll rate in radian
  20. 0, # body pitch rate in radian
  21. yaw_rate, # body yaw rate in radian
  22. thrust) # thrust
  23. copter.send_mavlink(msg)
  24.  
  25.  
  26. def to_quaternion(self, roll=0.0, pitch=0.0, yaw=0.0):
  27. """Convert degrees to quaternions."""
  28. t0 = math.cos(yaw * 0.5)
  29. t1 = math.sin(yaw * 0.5)
  30. t2 = math.cos(roll * 0.5)
  31. t3 = math.sin(roll * 0.5)
  32. t4 = math.cos(pitch * 0.5)
  33. t5 = math.sin(pitch * 0.5)
  34. w = t0 * t2 * t4 + t1 * t3 * t5
  35. x = t0 * t3 * t4 - t1 * t2 * t5
  36. y = t0 * t2 * t5 + t1 * t3 * t4
  37. z = t1 * t2 * t4 - t0 * t3 * t5
  38. return [w, x, y, z]
  39.  
  40.  
  41. while True:
  42. if(copter and copter.mode == dronekit.VehicleMode("GUIDED")):
  43. send_attitude_target(0, # roll
  44. 0, # pitch
  45. 0, # yaw_angle
  46. 0, # yaw_rate
  47. 0.5) # thrust
  48. print("attitude command sent")
  49. time.sleep(0.1)# your code goes here
Runtime error #stdin #stdout #stderr 0s 23352KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 4, in <module>
ImportError: No module named dronekit