app-BeoPilot.cpp

00001 /*
00002  * Copyright (C) 2010, Chin-Kai Chang 
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 #include "ros/ros.h"
00028 #include "std_msgs/String.h"
00029 #include "BeoPilot.H"
00030 #include <geometry_msgs/Twist.h>
00031 #include <tf/transform_broadcaster.h>
00032 #include <nav_msgs/Odometry.h>
00033 #include <sstream>
00034 
00035 //joy -> joy/Joy.msg -> teleop -> geometry_msgs/Twist.msg -> this
00036 
00037 BeoPilot * beopilot;
00038 void cmd_vel_Callback(geometry_msgs::Twist msg)
00039 {
00040         float trans = msg.linear.x;//Trans
00041         float rots = msg.angular.z;//Rot
00042         ROS_INFO("cmd_vel_callback: %f, %f", trans, rots);
00043         beopilot->SetMotorsPid(rots,trans);
00044 }
00045 
00046 int main(int argc, char **argv)
00047 {
00048         ros::init(argc, argv, "beopilot");
00049         ros::NodeHandle n;
00050         ros::NodeHandle n_priv("~");
00051         ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("/beobot2/odom", 1);
00052         tf::TransformBroadcaster odom_broadcaster;
00053         beopilot = new BeoPilot();
00054         beopilot->start();
00055         ros::Subscriber motor_sub = n.subscribe("/beobot2/cmd_vel",1,cmd_vel_Callback);
00056 
00057         ros::Rate pub_rate(10);
00058         double x = 0.0f;
00059         double y = 0.0f;
00060         double th = 0.0f;
00061 
00062         double vx = 0.0f;
00063         double vy = 0.0f;
00064         double vth = 0.0f;
00065 
00066         ros::Time current_time, last_time;
00067         current_time = ros::Time::now();
00068         last_time = ros::Time::now();
00069         
00070         beopilot->resetEncoder();
00071         ROS_INFO("x %f y %f th %f vx %f vy %f vth %f", beopilot->itsPosition.x, beopilot->itsPosition.y, beopilot->itsPosition.z, beopilot->itsVelocity.x, beopilot->itsVelocity.y, beopilot->itsVelocity.z);
00072         
00073         while (ros::ok())
00074         {
00075                 beopilot->UpdateRCStatus();
00076                 x = -beopilot->itsPosition.x;
00077                 y = beopilot->itsPosition.y;
00078                 th = -beopilot->itsPosition.z;
00079                 
00080                 vx = -beopilot->itsVelocity.x;
00081                 vy = beopilot->itsVelocity.y;
00082                 vth = -beopilot->itsVelocity.z;
00083                 
00084                 ROS_INFO("x %f y %f th %f vx %f vy %f vth %f", x, y, th, vx, vy, vth);
00085                 
00086                 //update velocities here
00087                 
00088                 current_time = ros::Time::now();
00089 
00090                 //compute odometry in a typical way given the velocities of the robot
00091                 //double dt = (current_time - last_time).toSec();
00092                 //double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
00093                 //double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
00094                 //double delta_th = vth * dt;
00095 
00096                 //x += delta_x;
00097                 //y += delta_y;
00098                 //th += delta_th;
00099 
00100                 //since all odometry is 6DOF we'll need a quaternion created from yaw
00101                 geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
00102 
00103                 //first, we'll publish the transform over tf
00104                 geometry_msgs::TransformStamped odom_trans;
00105                 odom_trans.header.stamp = current_time;
00106                 odom_trans.header.frame_id = "/beobot2/odom";
00107                 odom_trans.child_frame_id = "/beobot2/base_link";
00108 
00109                 odom_trans.transform.translation.x = x;
00110                 odom_trans.transform.translation.y = y;
00111                 odom_trans.transform.translation.z = 0.0f;
00112                 odom_trans.transform.rotation = odom_quat;
00113 
00114                 //send the transform
00115                 odom_broadcaster.sendTransform(odom_trans);
00116 
00117                 //next, we'll publish the odometry message over ROS
00118                 nav_msgs::Odometry odom;
00119                 odom.header.stamp = current_time;
00120                 odom.header.frame_id = "odom";
00121 
00122                 //set the position
00123                 odom.pose.pose.position.x = x;
00124                 odom.pose.pose.position.y = y;
00125                 odom.pose.pose.position.z = 0.0f;
00126                 odom.pose.pose.orientation = odom_quat;
00127 
00128                 //set the velocity
00129                 odom.child_frame_id = "base_link";
00130                 odom.twist.twist.linear.x = vx;
00131                 odom.twist.twist.linear.y = vy;
00132                 odom.twist.twist.angular.z = vth;
00133 
00134                 //publish the message
00135                 odom_pub.publish(odom);
00136 
00137                 last_time = current_time;
00138                 ros::spinOnce();
00139                 pub_rate.sleep();
00140         }
00141 
00142 
00143         return 0;
00144 }
00145 
00146 
00147 
00148 
00149 
Generated on Sun May 8 08:41:17 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3