Go to the documentation of this file.00001 #include <carl_dynamixel/servo_pan_tilt.h>
00002
00003 using namespace std;
00004
00005 servoPanTilt::servoPanTilt()
00006 {
00007
00008 asusServoControllerPublisher = node.advertise<std_msgs::Float64>("asus_controller/command", 1);
00009 creativeServoControllerPublisher = node.advertise<std_msgs::Float64>("creative_controller/command", 1);
00010 tiltCommandSubscriber = node.subscribe("asus_controller/tilt", 1, &servoPanTilt::tiltCallback, this);
00011 panCommandSubscriber = node.subscribe("creative_controller/pan", 1, &servoPanTilt::panCallback, this);
00012 backJointStateSubscriber = node.subscribe("dynamixel_back", 1, &servoPanTilt::backJointStateCallback, this);
00013 frontJointStateSubscriber = node.subscribe("dynamixel_front", 1, &servoPanTilt::frontJointStateCallback, this);
00014
00015 lookAtPointServer = node.advertiseService("asus_controller/look_at_point", &servoPanTilt::lookAtPoint, this);
00016 lookAtFrameServer = node.advertiseService("asus_controller/look_at_frame", &servoPanTilt::lookAtFrame, this);
00017
00018
00019 backServoPos = 0.0;
00020 frontServoPos = 0.0;
00021 }
00022
00023 void servoPanTilt::backJointStateCallback(const sensor_msgs::JointState::ConstPtr& msg)
00024 {
00025 backServoPos = msg->position[0];
00026 }
00027
00028 void servoPanTilt::frontJointStateCallback(const sensor_msgs::JointState::ConstPtr& msg)
00029 {
00030 frontServoPos = msg->position[0];
00031 }
00032
00033 void servoPanTilt::tiltCallback(const std_msgs::Float64::ConstPtr& msg)
00034 {
00035
00036 std_msgs::Float64 tiltCommand;
00037 tiltCommand.data = backServoPos + msg->data/30.0;
00038
00039
00040 asusServoControllerPublisher.publish(tiltCommand);
00041 }
00042
00043 void servoPanTilt::panCallback(const std_msgs::Float64::ConstPtr& msg)
00044 {
00045
00046 std_msgs::Float64 panCommand;
00047 panCommand.data = frontServoPos + msg->data/30.0;
00048
00049
00050 creativeServoControllerPublisher.publish(panCommand);
00051 }
00052
00053 bool servoPanTilt::lookAtPoint(carl_dynamixel::LookAtPoint::Request &req, carl_dynamixel::LookAtPoint::Response &res)
00054 {
00055 geometry_msgs::PointStamped transformedPoint;
00056 tfListener.transformPoint("base_footprint", req.targetPoint, transformedPoint);
00057
00058 float servoAngle = calculateLookAngle(transformedPoint.point.x, transformedPoint.point.z);
00059 std_msgs::Float64 cmd;
00060 cmd.data = servoAngle;
00061 asusServoControllerPublisher.publish(cmd);
00062
00063 return true;
00064 }
00065
00066 bool servoPanTilt::lookAtFrame(carl_dynamixel::LookAtFrame::Request &req, carl_dynamixel::LookAtFrame::Response &res)
00067 {
00068 tf::StampedTransform transform;
00069 tfListener.lookupTransform("base_footprint", req.frame, ros::Time(0), transform);
00070 float servoAngle = calculateLookAngle(transform.getOrigin().x(), transform.getOrigin().z());
00071 std_msgs::Float64 cmd;
00072 cmd.data = servoAngle;
00073 asusServoControllerPublisher.publish(cmd);
00074
00075 return true;
00076 }
00077
00078 float servoPanTilt::calculateLookAngle(float x, float z)
00079 {
00080 tf::StampedTransform transform;
00081 tfListener.lookupTransform("base_footprint", "asus_servo_arm_link", ros::Time(0), transform);
00082 float px = x - transform.getOrigin().x();
00083 float pz = z - transform.getOrigin().z();
00084 float angle = atan2(pz, px);
00085
00086
00087 return -(angle + .7854);
00088 }
00089
00090 int main(int argc, char **argv)
00091 {
00092
00093 ros::init(argc, argv, "servo_pan_tilt");
00094
00095 servoPanTilt s;
00096
00097 ros::spin();
00098
00099 return EXIT_SUCCESS;
00100 }