SVHSinTest.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of the SCHUNK SVH Driver suite.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2014 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
12 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany
13 //
14 // -- END LICENSE BLOCK ------------------------------------------------
15 
16 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
29 // ROS includes.
30 #include <ros/ros.h>
31 
32 // Messages
33 #include <sensor_msgs/JointState.h>
34 #include <std_msgs/Float32.h>
35 #include <std_msgs/Empty.h>
36 #include <std_msgs/Int8.h>
37 
38 #include <icl_core/EnumHelper.h>
39 
40 #include <stdlib.h> /* srand, rand */
41 #include <time.h> /* time */
42 
43 
44 // Consts
45 // Loop Rate (i.e Frequency) of the ROS node -> 50 = 50HZ
46 double loop_rate = 50;
47 // Time of a half Sin. i.e. 10 = In 10 Seconds the selected fingers will perform a close and open
48 // (Sin to 1PI)
49 double sin_duration = 10;
50 
51 
52 // Local Vars
53 bool running = true;
54 /*--------------------------------------------------------------------
55  * Callback functions
56  *------------------------------------------------------------------*/
57 // Toggle on off
58 void runCallback(const std_msgs::Empty&)
59 {
60  running = !running;
61 }
62 
63 void speedCallback(const std_msgs::Float32ConstPtr &msg){
64  sin_duration = msg->data;
65 }
66 void loopCallback(const std_msgs::Float32ConstPtr &msg){
67  loop_rate = msg->data;
68 }
69 
70 /*--------------------------------------------------------------------
71  * main()
72  * Main function to set up ROS node.
73  *------------------------------------------------------------------*/
74 
75 int main(int argc, char** argv)
76 {
79  std::string name_prefix;
80 
81  // Set up ROS.
82  ros::init(argc, argv, "svh_sine_test");
83  ros::NodeHandle nh("~");
84 
85  try
86  {
87  nh.param<std::string>("name_prefix", name_prefix, "left_hand");
88  }
90  {
91  ROS_ERROR("Parameter Error!");
92  }
93 
94  // Subscribe connect topic (Empty)
95  ros::Subscriber run_sub = nh.subscribe("toggle_run", 1, runCallback);
96  ros::Subscriber speed_sub = nh.subscribe("speed", 1, speedCallback);
97  ros::Subscriber loop_sub = nh.subscribe("loop", 1, loopCallback);
98 
99  // Publish current target positions
100  ros::Publisher channel_pos_pub = nh.advertise<sensor_msgs::JointState>("channel_targets", 1);
101 
102  // joint state message template
103  sensor_msgs::JointState channel_pos;
104  channel_pos.name.resize(9);
105  channel_pos.position.resize(9, 0.0);
106 
107  // Pre Fill the Names of the Finger
108  channel_pos.name[0] = name_prefix + "_" + "Thumb_Flexion";
109  channel_pos.name[1] = name_prefix + "_" + "Thumb_Opposition";
110  channel_pos.name[2] = name_prefix + "_" + "Index_Finger_Distal";
111  channel_pos.name[3] = name_prefix + "_" + "Index_Finger_Proximal";
112  channel_pos.name[4] = name_prefix + "_" + "Middle_Finger_Distal";
113  channel_pos.name[5] = name_prefix + "_" + "Middle_Finger_Proximal";
114  channel_pos.name[6] = name_prefix + "_" + "Ring_Finger";
115  channel_pos.name[7] = name_prefix + "_" + "Pinky";
116  channel_pos.name[8] = name_prefix + "_" + "Finger_Spread";
117 
118  // Set up the normalized time
119  ros::Time counter = ros::Time::now();
120  double normalized_time = 0;
121 
122  // Init the Random
123  srand(time(NULL));
124 
125  // Tell ROS how fast to run this node. (100 = 100 Hz = 10 ms)
126 
127  // Main loop.
128  while (nh.ok())
129  {
130  // Only when toggled on (std empty message)
131  if (running)
132  {
133  // Get a normalized time
134  if ((ros::Time::now() - counter) > ros::Duration(sin_duration))
135  {
136  counter = ros::Time::now();
137  normalized_time = 0;
138  }
139  else
140  {
141  normalized_time = (ros::Time::now() - counter).toSec() / sin_duration;
142  }
143 
144  // Publish channel positions in RAD.
145  // Everything is 0 by default
146  for (size_t channel = 0; channel < 9; ++channel)
147  {
148  double cur_pos = 0.0;
149 
150  channel_pos.position[channel] = cur_pos;
151  }
152 
153  channel_pos.header.stamp = ros::Time::now();
154 
155 
156  // Calculate a halfe sin for the fingers
157  double cur_pos = 0.4 + 0.3 * sin(normalized_time * 2 * 3.14);
158 
159  // Set the Spread to 0.3 (to avoid any collisions)
160  channel_pos.position[8] = 0.3; // Finger Spread
161 
162  channel_pos.position[0] = 1 - cur_pos; // Thumb_Flexion
163 
164  // Set the 2 Test fingers to the sin value
165  channel_pos.position[7] = cur_pos; // Pinky
166  channel_pos.position[2] = cur_pos; // Index Distal
167  channel_pos.position[3] = cur_pos; // Index proximal
168  channel_pos.position[4] = cur_pos; // Middle Distal
169  channel_pos.position[5] = cur_pos; // Middle proximal
170  channel_pos.position[6] = cur_pos; // Ring Finger
171 
172  // Publish
173  channel_pos_pub.publish(channel_pos);
174  }
175 
176  ros::Rate rate(loop_rate);
177  rate.sleep();
178  ros::spinOnce();
179 
180  // TO INDTRODUCE A VARIIING TIME RATE (in This case 50 - 100 HZ ) Uncomment this (discouraged!
181  // Will result in strange things obiously)
182  // Was meant to test jitter in the trajectory generation
183  // rate = 50+(rand() % 10 )*5;
184  }
185 
186  return 0;
187 } // end main()
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)
double loop_rate
Definition: SVHSinTest.cpp:46
int main(int argc, char **argv)
Definition: SVHSinTest.cpp:75
void runCallback(const std_msgs::Empty &)
Definition: SVHSinTest.cpp:58
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
static Time now()
bool running
Definition: SVHSinTest.cpp:53
bool ok() const
void speedCallback(const std_msgs::Float32ConstPtr &msg)
Definition: SVHSinTest.cpp:63
double sin_duration
Definition: SVHSinTest.cpp:49
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
void loopCallback(const std_msgs::Float32ConstPtr &msg)
Definition: SVHSinTest.cpp:66


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Mon Jun 10 2019 15:04:59