1212 ParamSet , ParamGet
1313import tf
1414import time
15+ from enum import Enum
16+
17+ class States (Enum ):
18+
19+ ARMING = 1
20+ TAKEOFF = 2
21+ LANDING = 3
22+ FLYING = 4
23+ DISARMING = 5
1524
1625class RotorsConstraints :
1726 MIN_SPEED = 10
@@ -23,15 +32,15 @@ def constraint_speed(self, s):
2332 '''
2433 return self .MIN_SPEED if s < self .MIN_SPEED else self .MAX_SPEED if s > self .MAX_SPEED else s
2534
26- class Rotors_Driver ():
35+ class RotorsDriver ():
2736 CONSTRAINTS = RotorsConstraints ()
2837
2938 def __init__ (self ):
30- self .sample_time = 1.0
31- self .mav_name = " firefly"
32- self .__is_armed = False
33- self .__is_flying = False
34- self .__state_dict = {}
39+ self .sample_time = rospy . get_param ( 'sample_time' , 1.0 )
40+ self .mav_name = rospy . get_param ( 'drone_model' , ' firefly' )
41+ self .drone_state_upd_freq = rospy . get_param ( 'drone_state_timer_frequency' , 0.01 )
42+ self .misc_state_upd_freq = rospy . get_param ( 'misc_state_timer_frequency' , 1.0 )
43+ self .drone_flight_state = States . LANDING
3544 self .current_state = Odometry ()
3645 self .current_x = self .current_state .pose .pose .position .x
3746 self .current_y = self .current_state .pose .pose .position .y
@@ -59,20 +68,20 @@ def __init__(self):
5968 self .set_param = rospy .Service ('mavros/param/set' , ParamSet , self .rotors_param_set )
6069 self .get_param = rospy .Service ('mavros/param/get' , ParamGet , self .rotors_param_get )
6170
62- self .rqt_extended_state_publisher = rospy .Publisher ('mavros/extended_state' , ExtendedState ,
71+ self .extended_state_publisher = rospy .Publisher ('mavros/extended_state' , ExtendedState ,
6372 queue_size = 1 )
64- self .rqt_state_publisher = rospy .Publisher ('mavros/state' , State ,
73+ self .state_publisher = rospy .Publisher ('mavros/state' , State ,
6574 queue_size = 1 )
6675 self .battery_state_publisher = rospy .Publisher ('mavros/battery' , BatteryState , queue_size = 1 )
67- self .rqt_pose_publisher = rospy .Publisher ( 'mavros/local_position/pose' , PoseStamped ,
76+ self .pose_publisher = rospy .Publisher ( 'mavros/local_position/pose' , PoseStamped ,
6877 queue_size = 1 )
69- self .rqt_global_position_publisher = rospy .Publisher ( 'mavros/global_position/global' , NavSatFix ,
78+ self .global_position_publisher = rospy .Publisher ( 'mavros/global_position/global' , NavSatFix ,
7079 queue_size = 1 )
71- self .rqt_velocity_body_publisher = rospy .Publisher ('mavros/local_position/velocity_body' ,
80+ self .velocity_body_publisher = rospy .Publisher ('mavros/local_position/velocity_body' ,
7281 TwistStamped , queue_size = 1 )
73- self .rqt_cam_frontal_publisher = rospy .Publisher ('/' + self .mav_name + '/cam_frontal/image_raw' , Image ,
82+ self .cam_frontal_publisher = rospy .Publisher ('/' + self .mav_name + '/cam_frontal/image_raw' , Image ,
7483 queue_size = 1 )
75- self .rqt_cam_ventral_publisher = rospy .Publisher ('/' + self .mav_name + '/cam_ventral/image_raw' , Image ,
84+ self .cam_ventral_publisher = rospy .Publisher ('/' + self .mav_name + '/cam_ventral/image_raw' , Image ,
7685 queue_size = 1 )
7786
7887
@@ -82,59 +91,52 @@ def __init__(self):
8291 rospy .Subscriber ("mavros/setpoint_raw/local" , PositionTarget , self .publish_position_desired )
8392
8493
85- rospy .Subscriber ("/firefly/ ground_truth/odometry" , Odometry , self .get_pose )
86- rospy .Subscriber ("/firefly /frontal_cam/camera_nadir/image_raw" , Image , self .cam_frontal )
87- rospy .Subscriber ("/firefly /ventral_cam/camera_nadir/image_raw" , Image , self .cam_ventral )
94+ rospy .Subscriber ('/' + self . mav_name + "/ ground_truth/odometry" , Odometry , self .odom_callback )
95+ rospy .Subscriber ('/' + self . mav_name + " /frontal_cam/camera_nadir/image_raw" , Image , self .cam_frontal )
96+ rospy .Subscriber ('/' + self . mav_name + " /ventral_cam/camera_nadir/image_raw" , Image , self .cam_ventral )
8897 self .firefly_command_publisher = rospy .Publisher ('/firefly/command/trajectory' , MultiDOFJointTrajectory , queue_size = 10 )
8998 time .sleep (1.0 )
90- rospy .Timer (rospy .Duration (1.0 / 10.0 ), self .timer_callback )
99+ rospy .Timer (rospy .Duration (self .drone_state_upd_freq ), self .drone_state_update_callback )
100+ rospy .Timer (rospy .Duration (self .misc_state_upd_freq ), self .misc_state_update_callback )
101+
102+ def drone_state_update_callback (self , event = None ):
91103
92- def timer_callback (self , event = None ):
93- landed_state = 2 if self .__is_flying else 1
104+ pose = PoseStamped (pose = Pose (position = Point (x = self .current_x , y = self .current_y , z = self .current_z ),
105+ orientation = Quaternion (x = float (self .quaternion [0 ]), y = float (self .quaternion [1 ]), z = float (self .quaternion [2 ]),
106+ w = float (self .quaternion [3 ]))),header = Header (frame_id = self .frame_id ))
107+ self .pose_publisher .publish (pose )
108+
109+ twist = TwistStamped (twist = Twist (linear = Vector3 (x = self .current_vx , y = self .current_vy , z = self .current_vz ),
110+ angular = Vector3 (x = self .current_ang_vx , y = self .current_ang_vy , z = self .current_ang_vz )),
111+ header = Header (frame_id = self .frame_id ))
112+ self .velocity_body_publisher .publish (twist )
113+
114+
115+ self .cam_frontal_publisher .publish (self .frontal_image )
116+ self .cam_ventral_publisher .publish (self .ventral_image )
117+
118+ def misc_state_update_callback (self , event = None ):
119+ landed_state = 2 if self .drone_flight_state == 4 else 1
94120 ext_state = ExtendedState (vtol_state = 0 , landed_state = landed_state )
95- self .rqt_extended_state_publisher .publish (ext_state )
121+ self .extended_state_publisher .publish (ext_state )
96122
97123
98124 state = State (mode = "OFFBOARD" , armed = True )
99- self .rqt_state_publisher .publish (state )
125+ self .state_publisher .publish (state )
100126 rospy .logdebug ('State updated' )
101127
102-
103- try :
104- bat_percent = int (self .__state_dict ["bat" ])
105- except KeyError :
106- bat_percent = float ('nan' )
107- # rospy.logwarn("Battery state unknown.")
108128 bat = BatteryState (voltage = 0.0 , current = float ('nan' ), charge = float ('nan' ),
109- capacity = float ('nan' ), design_capacity = float ('nan' ), percentage = bat_percent ,
129+ capacity = float ('nan' ), design_capacity = float ('nan' ), percentage = float ( 'nan' ) ,
110130 power_supply_status = 0 , power_supply_health = 0 , power_supply_technology = 0 , present = True ,
111131 cell_voltage = [float ('nan' )], location = "0" ,
112132 serial_number = "" )
113133 self .battery_state_publisher .publish (bat )
114134
115-
116- pose = PoseStamped (pose = Pose (position = Point (x = self .current_x , y = self .current_y , z = self .current_z ),
117- orientation = Quaternion (x = float (self .quaternion [0 ]), y = float (self .quaternion [1 ]), z = float (self .quaternion [2 ]),
118- w = float (self .quaternion [3 ]))),header = Header (frame_id = self .frame_id ))
119- self .rqt_pose_publisher .publish (pose )
120-
121-
122135 # Empty, global pos not known
123136 nav_sat = NavSatFix (status = NavSatStatus (status = - 1 , service = 0 ), latitude = float ('nan' ),
124137 longitude = float ('nan' ), altitude = float ('nan' ), position_covariance = [float ('nan' )] * 9 ,
125138 position_covariance_type = 0 )
126- self .rqt_global_position_publisher .publish (nav_sat )
127-
128-
129- twist = TwistStamped (twist = Twist (linear = Vector3 (x = self .current_vx , y = self .current_vy , z = self .current_vz ),
130- angular = Vector3 (x = self .current_ang_vx , y = self .current_ang_vy , z = self .current_ang_vz )),
131- header = Header (frame_id = self .frame_id ))
132- self .rqt_velocity_body_publisher .publish (twist )
133-
134-
135- self .rqt_cam_frontal_publisher .publish (self .frontal_image )
136- self .rqt_cam_ventral_publisher .publish (self .ventral_image )
137-
139+ self .global_position_publisher .publish (nav_sat )
138140
139141 def cam_frontal (self , msg ):
140142 self .frontal_image = msg
@@ -144,7 +146,7 @@ def cam_ventral(self, msg):
144146 self .ventral_image = msg
145147 rospy .logdebug ('Ventral image updated' )
146148
147- def get_pose (self ,msg ):
149+ def odom_callback (self ,msg ):
148150 self .current_state = msg
149151
150152 self .current_x = self .current_state .pose .pose .position .x
@@ -165,9 +167,9 @@ def get_pose(self,msg):
165167 self .frame_id = self .current_state .header .frame_id
166168
167169 if self .current_z < 0.1 :
168- self .__is_flying = False
170+ self .drone_flight_state = States . LANDING
169171 else :
170- self .__is_flying = True
172+ self .drone_flight_state = States . FLYING
171173
172174
173175 def rotors_takeoff_land (self ,req ):
@@ -199,9 +201,9 @@ def rotors_takeoff_land(self,req):
199201 time .sleep (1 ) #change the time if altitude val doesn't change
200202 if req and 0.8 < self .current_z < 1.2 :
201203 return True , 0
202- elif not req and 0.0 <= self .current_z < 0.3 :
204+ elif not req and 0.0 <= self .current_z < 0.1 :
203205 rospy .loginfo ("Firefly Disarming" )
204- self .__is_armed = False
206+ self .drone_flight_state = States . DISARMING
205207 return True , 0
206208 else :
207209 return False ,1
@@ -215,13 +217,13 @@ def rotors_arm(self, req):
215217 # uint8 result
216218 if req .value :
217219 rospy .loginfo ("Firefly Arming" )
218- self .__is_armed = True
220+ self .drone_flight_state = States . ARMING
219221 time .sleep (1 ) # waits 1 sec
220222 tk_req = CommandTOL ()
221223 return True , 1
222224 else :
223225 rospy .loginfo ("Firefly Disarming" )
224- self .__is_armed = False
226+ self .drone_flight_state = States . DISARMING
225227 return False , 0
226228
227229 def rotors_set_mode (self , req ):
@@ -233,10 +235,9 @@ def rotors_set_mode(self, req):
233235 def rotors_land (self , req ):
234236
235237 rospy .loginfo ("Landing!" )
236- self .__is_flying = False
238+ self .drone_flight_state = States .LANDING
239+
237240
238- cmd = CommandBool ()
239- cmd .value = False
240241 success , result = self .rotors_takeoff_land (0 )
241242 return success , result
242243
@@ -337,6 +338,10 @@ def publish_position_desired(self, msg):
337338 # time.sleep(0.1) #commented out for vel control
338339 self .firefly_command_publisher .publish (traj )
339340
341+ # Deleting (Calling destructor)
342+ def __del__ (self ):
343+ print ('Destructor called, objs deleted.' )
344+
340345
341346
342347
@@ -345,12 +350,12 @@ def publish_position_desired(self, msg):
345350
346351
347352 rospy .init_node ("rotors_driver" , anonymous = True )
348- rotors = Rotors_Driver ()
353+ rotors = RotorsDriver ()
349354
350355
351356 rospy .spin ()
352357
353358
354359 except rospy .ROSInterruptException :
355360 print ("ROS Terminated" )
356- pass
361+ del rotors
0 commit comments