run_stop_auto_restart.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <ros/ros.h>
31 #include <pr2_msgs/PowerBoardState.h>
32 #include <std_srvs/Empty.h>
33 #include <pr2_power_board/PowerBoardCommand.h>
34 
35 
36 static bool last_e_stop_state_ = true;
37 
38 void powerStateCallback(const pr2_msgs::PowerBoardStateConstPtr& msg)
39 {
40  if (!last_e_stop_state_ && msg->run_stop){
41  ROS_INFO("Run stop re-enabled. Bringing up power and resetting motors.");
42 
43  // enable power
44  if (!ros::service::waitForService("power_board/control", ros::Duration(5.0))){
45  ROS_ERROR("Could not find power board control service");
46  return;
47  }
48  pr2_power_board::PowerBoardCommand power_board_cmd;
49  power_board_cmd.request.serial_number = msg->serial_num;
50  power_board_cmd.request.command = "start";
51  for (unsigned int i=0; i<3; i++){
52  power_board_cmd.request.breaker_number = i;
53  ros::service::call("power_board/control", power_board_cmd);
54  ROS_INFO(" - Enabling breaker %i: %i",i, power_board_cmd.response.retval);
55  }
56 
57  // reset motors
58  ros::Duration(2.0).sleep(); // give motors time to detect power up
59  if (!ros::service::waitForService("pr2_ethercat/reset_motors", ros::Duration(5.0))){
60  ROS_ERROR("Could not find reset motors service");
61  return;
62  }
63  std_srvs::Empty empty_cmd;
64  ros::service::call("pr2_ethercat/reset_motors", empty_cmd);
65  ROS_INFO(" - Reset motors");
66  }
67 
68  last_e_stop_state_ = msg->run_stop;
69 }
70 
71 
72 int main(int argc, char** argv)
73 {
74  ros::init(argc, argv, "run_stop_helper");
75  ros::NodeHandle node;
76 
77  // subscribe to state of power board
78  ros::Subscriber power_state_sub = node.subscribe("power_board/state", 1, powerStateCallback);
79 
80  ros::spin();
81 
82  return 0;
83 }
void powerStateCallback(const pr2_msgs::PowerBoardStateConstPtr &msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool call(const std::string &service_name, MReq &req, MRes &res)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_INFO(...)
ROSCPP_DECL void spin()
int main(int argc, char **argv)
static bool last_e_stop_state_
#define ROS_ERROR(...)
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


pr2_run_stop_auto_restart
Author(s): Wim Meeussen
autogenerated on Tue Jun 1 2021 02:50:55