dynamixel_loop.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2015, University of Colorado, Boulder
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Univ of CO, Boulder nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Original Author: Dave Coleman (https://github.com/davetcoleman/ros_control_boilerplate) */
36 /* Edited by: Dorian Goepp (https://github.com/resibots/dynamixel_control_hw/) */
37 /* Edited by: Max Svetlik (https://github.com/svenzvarobotics) */
38 
39 // for runtime_error
40 #include <stdexcept>
41 
43 #include <std_msgs/Float64MultiArray.h>
44 #include <sensor_msgs/JointState.h>
45 
46 namespace dynamixel {
48  ros::NodeHandle& nh,
50  : _nh(nh), _hardware_interface(hardware_interface)
51  {
52  // Create the controller manager
54 
55  // Load rosparams
56  int error = 0;
57  ros::NodeHandle np("~");
58  error += !np.getParam("loop_frequency", _loop_hz);
59  error += !np.getParam("cycle_time_error_threshold", _cycle_time_error_threshold);
60  if (error > 0) {
61  char error_message[] = "could not retrieve one of the required parameters\n\tdynamixel_hw/loop_hz or dynamixel_hw/cycle_time_error_threshold";
62  ROS_ERROR_STREAM(error_message);
63  throw std::runtime_error(error_message);
64  }
65 
66  torque_pub = np.advertise<std_msgs::Float64MultiArray>("/revel/motor_controller/pos_torque", 1);
67  js_sub = np.subscribe("/joint_states", 10, &DynamixelLoop::jsCallback, this);
68 
69  ros::Duration(0.5).sleep();
70 
71  // Get current time for use with first update
72  clock_gettime(CLOCK_MONOTONIC, &_last_time);
73 
74  // Start timer that will periodically call DynamixelLoop::update
76  _non_realtime_loop = _nh.createTimer(_desired_update_freq, &DynamixelLoop::update, this);
77  }
78 
79  void DynamixelLoop::jsCallback(const sensor_msgs::JointState::ConstPtr& msg){
80  joint_state = *msg;
81  }
82 
84  {
85  // Get change in time
86  clock_gettime(CLOCK_MONOTONIC, &_current_time);
88  _current_time.tv_sec - _last_time.tv_sec + (_current_time.tv_nsec - _last_time.tv_nsec) / BILLION);
90 
91  // Check cycle time for excess delay
92  const double cycle_time_error = (_elapsed_time - _desired_update_freq).toSec();
93  if (cycle_time_error > _cycle_time_error_threshold) {
94  ROS_WARN_STREAM("Cycle time exceeded error threshold by: "
95  << cycle_time_error - _cycle_time_error_threshold << ", cycle time: " << _elapsed_time
96  << ", threshold: " << _cycle_time_error_threshold);
97  }
98 
99  // Input
100  // get the hardware's state
101  //ROS_INFO_STREAM(joint_state);
102  _hardware_interface->read_joints(joint_state);
103 
104 
105  // Control
106  // let the controller compute the new command (via the controller manager)
108 
109 
110  // Output
111  // send the new command to hardware
112  _hardware_interface->write_joints();
113 
114  std_msgs::Float64MultiArray goals;
115  std::vector<double> cmds = _hardware_interface->write_joints();
116 
117  for (unsigned int i = 0; i < 6; i++) {
118  //convert torque to motor units.
119  //double torque = (float)cmds[i] / 1.0837745;
120  //torque = torque / .00336;
121  double torque = cmds[i];
122  goals.data.push_back(torque);
123  }
124  torque_pub.publish(goals);
125  }
126 }
void publish(const boost::shared_ptr< M > &message) const
static const double BILLION
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool sleep() const
boost::shared_ptr< dynamixel::DynamixelHardwareInterface > _hardware_interface
Abstract Hardware Interface for your robot.
void jsCallback(const sensor_msgs::JointState::ConstPtr &js)
ros::Duration _desired_update_freq
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_STREAM(args)
DynamixelLoop(ros::NodeHandle &nh, boost::shared_ptr< dynamixel::DynamixelHardwareInterface > hardware_interface)
boost::shared_ptr< controller_manager::ControllerManager > _controller_manager
void update(const ros::TimerEvent &e)
bool getParam(const std::string &key, std::string &s) const
static Time now()
sensor_msgs::JointState joint_state
struct timespec _current_time
struct timespec _last_time
#define ROS_ERROR_STREAM(args)


svenzva_drivers
Author(s): Max Svetlik
autogenerated on Wed Oct 28 2020 03:31:27