#!/usr/bin/python
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Willow Garage, Inc. nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

PKG = 'teleop_microscribe'
import roslib; roslib.load_manifest(PKG)

import rospy

from joy.msg import Joy
from geometry_msgs.msg import Twist
from pr2_cockpit_msgs.srv import *

rospy.init_node('teleop_base_pedals')

r_pedal_state = 0
l_pedal_state = 0
enabled = False

def enable(req):
    global enabled
    enabled = req.enable
    return EnableResponse(True, "")
rospy.Service('~enable', Enable, enable)

def l_pedal_callback(data):
    global l_pedal_state
    l_pedal_state = data

def r_pedal_callback(data):
    global r_pedal_state 
    r_pedal_state = data

def valid_pedals(data):
    if(data == 0):
        return False
    if(len(data.axes) != 3):
        return False
    return True

def teleop_base_pedals():
    global enabled
    pub = rospy.Publisher('cmd_vel', Twist)
    rospy.Subscriber("l_rudder_pedals", Joy, l_pedal_callback)
    rospy.Subscriber("r_rudder_pedals", Joy, r_pedal_callback)

    cmd_vel = Twist()
    max_rot_vel = 3
    max_y_vel = 0.3
    max_x_vel = 1
 
    while not rospy.is_shutdown():
        if enabled:
            if valid_pedals(l_pedal_state) and valid_pedals(r_pedal_state):
                cmd_vel.linear.x = (r_pedal_state.axes[2] - l_pedal_state.axes[2]) * 0.4 * max_x_vel
                cmd_vel.linear.y = (r_pedal_state.axes[0] - l_pedal_state.axes[1]) * 0.4 * max_y_vel
                cmd_vel.linear.z = 0
                cmd_vel.angular.x = 0
                cmd_vel.angular.y = 0
                cmd_vel.angular.z = (r_pedal_state.axes[2] + l_pedal_state.axes[2]) * 0.4 * max_rot_vel
                pub.publish(cmd_vel)
        rospy.sleep(0.05)

if __name__ == '__main__':
    teleop_base_pedals() 
