motor_controler.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <ros/console.h>
00003 #include <std_msgs/Float64.h>
00004 #include <geometry_msgs/Twist.h>
00005 #include <tf/transform_listener.h>
00006 #include <string>
00007 #include <vector>
00008 #include <cmath>
00009 
00010 // publisher of right and left motor velocity values
00011 ros::Publisher right_motor_pub;
00012 ros::Publisher left_motor_pub;
00013 
00014 // robot's index in ROS ecosystem (if multiple instances)
00015 std::string tf_prefix;
00016 double WHEELS_HALF_SPACING = 0.15;
00017 double WHEEL_RADIUS = 0.098;
00018 
00019 void callback_val(const geometry_msgs::Twist::ConstPtr &msg) {
00020 
00021   // creating new message
00022   std_msgs::Float64 msg_right_;
00023   std_msgs::Float64 msg_left_;
00024   double yaw = 0;
00025 
00026   if (msg.get()->angular.z < -0.0001 || msg.get()->angular.z > 0.0001)
00027     yaw = msg.get()->angular.z;
00028   else if (msg.get()->linear.y < -0.0001 || msg.get()->linear.y > 0.0001) {
00029     // yaw is not set-- trying to calculate from linear x and y
00030     ROS_WARN("DPL: in Twist msg no yaw component");
00031     yaw = std::atan2(msg.get()->linear.y, msg.get()->linear.x);
00032   }
00033 
00034   // calculate from linear x and yaw speed for each wheel
00035   msg_right_.data =
00036       (msg.get()->linear.x + WHEELS_HALF_SPACING * yaw) / WHEEL_RADIUS;
00037   msg_left_.data =
00038       (msg.get()->linear.x - WHEELS_HALF_SPACING * yaw) / WHEEL_RADIUS;
00039   // publishing wheel velocities
00040   right_motor_pub.publish(move(msg_right_));
00041   left_motor_pub.publish(move(msg_left_));
00042 }
00043 
00044 int main(int argc, char **argv) {
00045   std::string pub_right_ = "hal/rightMotor/setVel";
00046   std::string pub_left_ = "hal/leftMotor/setVel";
00047   std::string sub_twist_topic_ = "cmd_vel";
00048   ros::init(argc, argv, "motorControl");
00049 
00050   // read  from command line names of topics to publish data
00051   if (argc >= 3) {
00052     std::vector<std::string> argv_(argv + 1, argv + argc);
00053     pub_right_ = argv_[0];
00054     pub_left_ = argv_[1];
00055     if(argc==4) sub_twist_topic_ = argv_[2];
00056   }
00057   // set relative node namespace
00058   ros::NodeHandle n;
00059 
00060   std::string tf_prefix_path;
00061   if (n.searchParam("tf_prefix", tf_prefix_path))
00062   {
00063     n.getParam(tf_prefix_path, tf_prefix);
00064   }
00065 
00066   // tf listener for calculation of wheel spacing
00067   tf::TransformListener listener;
00068   tf::StampedTransform transform;
00069   // get length in between wheels
00070   try {
00071     listener.waitForTransform(tf_prefix + "/base/joint1",
00072                               tf_prefix + "/base/joint0",
00073                               ros::Time::now(), ros::Duration(10.0));
00074     listener.lookupTransform(tf_prefix + "/base/joint1",
00075                              tf_prefix + "/base/joint0",
00076                              ros::Time(0), transform);
00077     WHEELS_HALF_SPACING = transform.getOrigin().absolute().getY() / 2;
00078     ROS_INFO("DPL: motor_controler WHEELS_HALF_SPACING: %f",
00079              WHEELS_HALF_SPACING);
00080 
00081   } catch (tf::TransformException ex) {
00082     ROS_ERROR("Problem with transformation between wheels: %s", ex.what());
00083   }
00084 
00085   // read msg from standard ROS topic for receiving Twist commands
00086   ros::Subscriber twist_sub = n.subscribe(sub_twist_topic_, 1000, callback_val);
00087 
00088   right_motor_pub = n.advertise<std_msgs::Float64>(pub_right_, 1000);
00089   left_motor_pub = n.advertise<std_msgs::Float64>(pub_left_, 1000);
00090   ROS_INFO("DPL: motor_controler node initialized");
00091 
00092   // run event loop
00093   ros::spin();
00094   return 0;
00095 }


p3dx_dpl
Author(s):
autogenerated on Sat Jun 8 2019 20:22:37