SVHSinTest.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of the SCHUNK SVH Driver suite.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2014 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
00012 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00013 //
00014 // -- END LICENSE BLOCK ------------------------------------------------
00015 
00016 //----------------------------------------------------------------------
00027 //----------------------------------------------------------------------
00028 
00029 // ROS includes.
00030 #include <ros/ros.h>
00031 
00032 // Messages
00033 #include <std_msgs/Int8.h>
00034 #include <std_msgs/Empty.h>
00035 #include <sensor_msgs/JointState.h>
00036 
00037 #include <icl_core/EnumHelper.h>
00038 
00039 #include <stdlib.h>     /* srand, rand */
00040 #include <time.h>       /* time */
00041 
00042 
00043 // Consts
00044 // Loop Rate (i.e Frequency) of the ROS node -> 50 = 50HZ
00045 const double loop_rate = 50;
00046 // Time of a half Sin. i.e. 10 = In 10 Seconds the selected fingers will perform a close and open (Sin to 1PI)
00047 const double sin_duration = 10;
00048 
00049 
00050 // Local Vars
00051 bool running = true;
00052 /*--------------------------------------------------------------------
00053  * Callback functions
00054  *------------------------------------------------------------------*/
00055 // Toggle on off
00056 void runCallback(const std_msgs::Empty&)
00057 {
00058   running = !running;
00059 }
00060 /*--------------------------------------------------------------------
00061  * main()
00062  * Main function to set up ROS node.
00063  *------------------------------------------------------------------*/
00064 
00065 int main(int argc, char **argv)
00066 {
00067   // Set up ROS.
00068   ros::init(argc, argv, "svh_sine_test");
00069   ros::NodeHandle nh("~");
00070 
00071   // Subscribe connect topic (Empty)
00072   ros::Subscriber run_sub = nh.subscribe("toggle_run", 1, runCallback);
00073 
00074   // Publish current target positions
00075   ros::Publisher channel_pos_pub = nh.advertise<sensor_msgs::JointState>("channel_targets", 1);
00076 
00077   // joint state message template
00078   sensor_msgs::JointState channel_pos;
00079   channel_pos.name.resize(9);
00080   channel_pos.position.resize(9, 0.0);
00081 
00082   // Pre Fill the Names of the Finger
00083   channel_pos.name[0] = "Thumb_Flexion";
00084   channel_pos.name[1] = "Thumb_Opposition";
00085   channel_pos.name[2] = "Index_Finger_Distal";
00086   channel_pos.name[3] = "Index_Finger_Proximal";
00087   channel_pos.name[4] = "Middle_Finger_Distal";
00088   channel_pos.name[5] = "Middle_Finger_Proximal";
00089   channel_pos.name[6] = "Ring_Finger";
00090   channel_pos.name[7] = "Pinky";
00091   channel_pos.name[8] = "Finger_Spread";
00092 
00093   // Set up the normalized time
00094   ros::Time counter = ros::Time::now();
00095   double normalized_time = 0;
00096 
00097   // Init the Random
00098   srand (time(NULL));
00099 
00100   // Tell ROS how fast to run this node. (100 = 100 Hz = 10 ms)
00101   ros::Rate rate(50);
00102 
00103   // Main loop.
00104   while (nh.ok())
00105   {
00106     // Only when toggled on (std empty message)
00107     if (running)
00108     {
00109       // Get a normalized time
00110       if ((ros::Time::now() - counter) > ros::Duration(sin_duration))
00111       {
00112         counter = ros::Time::now();
00113       }
00114       else
00115       {
00116         normalized_time = (ros::Time::now() - counter).toSec() / sin_duration;
00117       }
00118 
00119       // Publish channel positions in RAD.
00120       // Everything is 0 by default
00121       for (size_t channel = 0; channel < 9; ++channel)
00122       {
00123 
00124         double cur_pos = 0.0;
00125 
00126         channel_pos.position[channel] = cur_pos;
00127       }
00128 
00129       // Set the Spread to 0.5 (to avoid any collisions)
00130       channel_pos.position[8] = 0.5;
00131       // Calculate a halfe sin for the fingers
00132       double cur_pos = sin(normalized_time*3.14);
00133       // Set the 2 Test fingers to the sin value
00134       channel_pos.position[7] = cur_pos; //Pinky
00135       channel_pos.position[2] = cur_pos; //Index Distal
00136       channel_pos.position[3] = cur_pos; //Index proximal
00137       channel_pos.position[4] = cur_pos; //Middle Distal
00138       channel_pos.position[5] = cur_pos; //Middle proximal
00139       channel_pos.position[6] = cur_pos; //Ring Finger
00140 
00141       // Publish
00142       channel_pos_pub.publish(channel_pos);
00143     }
00144     rate.sleep();
00145     ros::spinOnce();
00146 
00147     // TO INDTRODUCE A VARIIING TIME RATE (in This case 50 - 100 HZ ) Uncomment this (discouraged! Will result in strange things obiously)
00148     // Was meant to test jitter in the trajectory generation
00149     //rate = 50+(rand() % 10 )*5;
00150   }
00151 
00152   return 0;
00153 } // end main()


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Fri Aug 28 2015 12:59:19