innosim_relay_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from geometry_msgs.msg import QuaternionStamped
5 from nav_msgs.msg import Odometry
6 from mavros_msgs.msg import RCOut
7 from sensor_msgs.msg import Joy
8 from sensor_msgs.msg import NavSatFix
9 
10 attitude_pub = rospy.Publisher('/sim/attitude', QuaternionStamped, queue_size = 1)
11 joy_pub = rospy.Publisher('/sim/actuators', Joy, queue_size = 1)
12 
13 gps_pub = rospy.Publisher('/sim/gps_position', NavSatFix, queue_size = 1)
14 
15 
16 # Here is some magic needed to give global altitude to simulator.
17 # GLOBAL_POSITION_INT (/mavros/global_position/global) has altitude relative to starting point.
18 # GPS_RAW_INT (/mavros/global_position/raw/fix) has global altitude (we could tune it with `export PX4_HOME_ALT`), but publishing rate is low.
19 # So, we apply altitude correction in GLOBAL_POSITION_INT and send to sim.
20 global raw_alt, alt_init, alt_corr, has_raw_alt
21 raw_alt = 0.0
22 alt_init = False
23 alt_corr = 0.0
24 has_raw_alt = False
25 
27  global raw_alt, alt_init, alt_corr, has_raw_alt
28 
29  if alt_init:
30  data.altitude += alt_corr
31  gps_pub.publish(data)
32  else:
33  if has_raw_alt:
34  alt_corr = raw_alt - data.altitude
35  alt_init = True
36 
37 def raw_gps_callback(data):
38  global raw_alt, has_raw_alt
39 
40  raw_alt = data.altitude
41  has_raw_alt = True
42 
43 def odom_callback(data):
44  q = QuaternionStamped()
45  q.header = data.header
46  q.quaternion = data.pose.pose.orientation
47  attitude_pub.publish(q)
48 
49 def rc_callback(data):
50  # PX4 SITL VTOL channels:
51  # 1 FR, ccw (900 - 2000)
52  # 2 RL, ccw
53  # 3 FL, cw
54  # 4 RR, cw (1000 - 2000)
55  # 5 pusher (1000 - 2000)
56  # 6 aileron (1000 - 2000)
57  # 7 aileron
58  # 8 elevator
59  FR_pwm = data.channels[0]
60  RL_pwm = data.channels[1]
61  FL_pwm = data.channels[2]
62  RR_pwm = data.channels[3]
63  pusher_pwm = data.channels[4]
64  aileron1_pwm = data.channels[5]
65  aileron2_pwm = data.channels[6]
66  elevator_pwm = data.channels[7]
67 
68 
69  joy = Joy()
70  # Sim:
71  # FR, cw, rate, rpm (Front right motor speed)
72  # RL, cw, rate, rpm (Rear left motor speed)
73  # FL, ccw, rate, rpm (Front left motor speed)
74  # RR, ccw, rate, rpm (Rear right motor speed)
75  # aileron left, cw, deg
76  # aileron right, cw, deg
77  # elevator, cw, deg
78  # rudder, cw, deg
79  # thrust, pusher, rate, rpm
80 
81  FR_res = 15500*(FR_pwm - 900.0)/1100.0
82  RL_res = 15500*(RL_pwm - 900.0)/1100.0
83  FL_res = 15500*(FL_pwm - 900.0)/1100.0
84  RR_res = 15500*(RR_pwm - 900.0)/1100.0
85  aileron1_res = 70*(aileron1_pwm - 1500.0)/500.0
86  aileron2_res = 70*(aileron2_pwm - 1500.0)/500.0
87  elevator_res = 70*(elevator_pwm - 1500.0)/500.0
88  rudder_res = 0.0
89  thrust_res = 15500*(pusher_pwm - 1000.0)/1000.0
90 
91  joy.axes.append( round( FR_res, 1) )
92  joy.axes.append( round( RL_res, 1) )
93  joy.axes.append( round( FL_res, 1) )
94  joy.axes.append( round( RR_res, 1) )
95  joy.axes.append( round( aileron1_res, 1) )
96  joy.axes.append( round( aileron2_res, 1 ) )
97  joy.axes.append( round( elevator_res, 1) )
98  joy.axes.append( round( rudder_res, 1) )
99  joy.axes.append( round( thrust_res, 1) )
100 
101  for i in range(4):
102  if joy.axes[i] < 0:
103  joy.axes[i] = 0
104 
105  if joy.axes[8] < 0:
106  joy.axes[8] = 0
107 
108  joy_pub.publish(joy)
109 
110 
112  rospy.init_node('innosim_relay', anonymous=True)
113  rospy.Subscriber('/mavros/global_position/local', Odometry, odom_callback)
114  rospy.Subscriber('/mavros/rc/out', RCOut, rc_callback)
115  rospy.Subscriber('/mavros/global_position/global', NavSatFix, global_gps_callback)
116  rospy.Subscriber('/mavros/global_position/raw/fix', NavSatFix, raw_gps_callback)
117  rospy.spin()
118 
119 
120 if __name__ == '__main__':
121  try:
122  innosim_relay()
123  except rospy.ROSInterruptException:
124  pass
125 


inno_sim_interface
Author(s):
autogenerated on Wed Feb 8 2023 03:27:19