From e31c1dbc4fe79bb567d2f0319b8d92c8b849e41d Mon Sep 17 00:00:00 2001
From: phfriedl <phfriedl@student.ethz.ch>
Date: Mon, 24 Apr 2017 15:56:02 +0200
Subject: [PATCH] fixed service race condition at startup, crazyflie resets
 before shutdown

---
 pps_ws/src/d_fall_pps/crazyradio/CrazyRadio.py | 2 ++
 pps_ws/src/d_fall_pps/src/PPSClient.cpp        | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/pps_ws/src/d_fall_pps/crazyradio/CrazyRadio.py b/pps_ws/src/d_fall_pps/crazyradio/CrazyRadio.py
index aec447cc..f39e25e7 100755
--- a/pps_ws/src/d_fall_pps/crazyradio/CrazyRadio.py
+++ b/pps_ws/src/d_fall_pps/crazyradio/CrazyRadio.py
@@ -129,6 +129,8 @@ if __name__ == '__main__':
         rospy.spin()
         rospy.loginfo("Turning off crazyflie")
         cf_client._send_to_commander(0, 0, 0, 0, 0, 0, 0, 0, CONTROLLER_MOTOR)
+        #wait for client to send its commands
+        time.sleep(1.0)
         cf_client._cf.close_link()
         rospy.loginfo("Link closed")
     else:
diff --git a/pps_ws/src/d_fall_pps/src/PPSClient.cpp b/pps_ws/src/d_fall_pps/src/PPSClient.cpp
index 8dee05ba..60830d4b 100644
--- a/pps_ws/src/d_fall_pps/src/PPSClient.cpp
+++ b/pps_ws/src/d_fall_pps/src/PPSClient.cpp
@@ -88,8 +88,7 @@ void ppsClientToController(ViconData data, bool autocontrolOn){
   				//onboardControllerType = ??????????????????????
 				
 				
-			}
-			else{
+			} else {
 				ROS_ERROR("Failed to call SafeControllerService");
 				//return 1; //return some useful stuff
 			}
@@ -171,6 +170,7 @@ int main(int argc, char* argv[]){
 
 	//service 
 		//to be expanded with additional services depending on controller (currently only one available)
+	ros::service::waitForService("/SafeControllerService/RateController");
 	safeController = nodeHandle.serviceClient<RateController>("/SafeControllerService/RateController", true);
 	
 	//safeController = nodeHandle.serviceClient<d_fall_pps::RateController>("/SafeControllerService/RateController", true);
-- 
GitLab