mecanum_controller_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
17 
18 #include <geometry_msgs/Twist.h>
19 #include <nav_msgs/Odometry.h>
20 #include <sensor_msgs/JointState.h>
21 #include <ros/ros.h>
22 
23 #include <exception>
25 {
26 public:
28  {
29  double lx, ly, r;
30  bool all_parameters_set = true;
31  ros::NodeHandle pnh("~");
32  if (!pnh.getParam("lx", lx))
33  {
34  ROS_ERROR_STREAM("Parameter lx was not declared in the scope");
35  all_parameters_set = false;
36  }
37  if (!pnh.getParam("ly", ly))
38  {
39  ROS_ERROR_STREAM("Parameter ly was not declared in the scope");
40  all_parameters_set = false;
41  }
42  if (!pnh.getParam("r", r))
43  {
44  ROS_ERROR_STREAM("Parameter r was not declared in the scope");
45  all_parameters_set = false;
46  }
47 
48  if (!all_parameters_set)
49  {
50  throw std::runtime_error("At least one parameter is missing.");
51  }
52 
53  static_frame_ = "map";
54  pnh.getParam("static_frame", static_frame_);
55  odom_frame_ = "map";
56  pnh.getParam("odom_frame", odom_frame_);
57 
58  controller_ = std::make_shared<cob_mecanum_controller::MecanumController>(lx, ly, r);
59 
60  odom_pub_ = nh_.advertise<nav_msgs::Odometry>("odom", 10, false);
61  joint_cmd_pub_ = nh_.advertise<sensor_msgs::JointState>("wheel_command", 10, false);
62 
63  twist_sub_ = nh_.subscribe<geometry_msgs::Twist>("cmd_vel", 1, &MecanumControllerNode::twistCallback, this);
64 
66  nh_.subscribe<sensor_msgs::JointState>("wheel_state", 1, &MecanumControllerNode::jointStateCallback, this);
67  }
68 
69 protected:
73 
76  std::shared_ptr<cob_mecanum_controller::MecanumController> controller_;
77 
78  std::string static_frame_;
79  std::string odom_frame_;
80 
81  void twistCallback(const geometry_msgs::Twist msg)
82  {
83  Eigen::Vector3d twist;
84  twist << msg.linear.x, msg.linear.y, msg.angular.z;
85  Eigen::Vector4d wheel_velocities = controller_->twistToWheel(twist);
86  sensor_msgs::JointState joint_command;
87  joint_command.velocity = std::vector<double>(
88  wheel_velocities.data(), wheel_velocities.data() + wheel_velocities.rows() * wheel_velocities.cols());
89  joint_cmd_pub_.publish(joint_command);
90  }
91 
92  void jointStateCallback(const sensor_msgs::JointState msg)
93  {
94  Eigen::Vector4d wheel_velocities(msg.velocity.data());
95  Eigen::Vector3d twist = controller_->wheelToTwist(wheel_velocities);
96  nav_msgs::Odometry odom_msg;
97  odom_msg.header.frame_id = static_frame_;
98  odom_msg.child_frame_id = odom_frame_;
99 
100  odom_msg.twist.twist.linear.x = twist.x();
101  odom_msg.twist.twist.linear.y = twist.y();
102  odom_msg.twist.twist.angular.z = twist.z();
103  odom_pub_.publish(odom_msg);
104  }
105 };
106 
107 int main(int argc, char* argv[])
108 {
109  ros::init(argc, argv, "mecanum_controller");
111  ros::spin();
112 }
void jointStateCallback(const sensor_msgs::JointState msg)
void twistCallback(const geometry_msgs::Twist msg)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::shared_ptr< cob_mecanum_controller::MecanumController > controller_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
bool getParam(const std::string &key, std::string &s) const
int main(int argc, char *argv[])
#define ROS_ERROR_STREAM(args)


cob_mecanum_controller
Author(s):
autogenerated on Thu Apr 8 2021 02:39:42