test_vel_control.cpp
Go to the documentation of this file.
00001 
00002 #include <yumi_test_controllers.h>
00003 
00004 
00005 sensor_msgs::JointState joints_state;
00006 
00007 std_msgs::Float64 left_command;
00008 std_msgs::Float64 right_command;
00009 
00010 
00011 int num_joints = 14;
00012 int num_joints_arm = 7;
00013 int test_joint_number = 3;
00014 int left_joint_state_idx = 4;
00015 int right_joint_state_idx = 4;
00016 
00017 
00018 auto last_sample_time = std::chrono::high_resolution_clock::now();
00019 auto last_call_time = std::chrono::high_resolution_clock::now();
00020 auto curr_call_time = std::chrono::high_resolution_clock::now();;
00021 
00022 double sine_period = 10.0;
00023 double sine_freq = 1 / sine_period;
00024 double sine_amp = 0.3;
00025 
00026 double sampling_freq = 200;
00027 double sampling_period = 1/sampling_freq;
00028 
00029 ros::Publisher left_controller_pub;
00030 ros::Publisher right_controller_pub;
00031 ros::Publisher left_state_pub;
00032 ros::Publisher right_state_pub;
00033 ros::Publisher vel_signal_pub;
00034 ros::Subscriber sub;
00035 
00036 bool g_quit;
00037 void quitRequested(int sig)
00038 {
00039         g_quit = true;
00040         left_command.data = 0.0;
00041         right_command.data = 0.0;
00042 
00043         left_controller_pub.publish(left_command);
00044         right_controller_pub.publish(right_command);
00045 
00046         ros::shutdown();
00047 }
00048 
00049 void send_sinusoidal_vel_joints()
00050 {
00051         curr_call_time = std::chrono::high_resolution_clock::now();
00052         std::chrono::duration<double> sine_elapsed = curr_call_time - last_call_time;
00053 
00054         std::chrono::duration<double> sampling_elapsed = curr_call_time - last_sample_time;
00055 
00056         if(sampling_elapsed.count() > sampling_period)
00057         {
00058                 double sin_val = sine_amp * sin(sine_elapsed.count() * 2 * M_PI * sine_freq);
00059 
00060                 vel_signal_pub.publish(sin_val);
00061                 cout << "Time since last joint state received = " << sine_elapsed.count() << endl;
00062                 // cout << "Sine value = " << sin_val << endl;
00063 
00064                 left_command.data = sin_val;
00065 
00066                 right_command.data = sin_val;
00067 
00068                 // cout << "Publishing command" << endl;
00069                 left_controller_pub.publish(left_command);
00070                 // right_controller_pub.publish(right_command);
00071 
00072                 last_sample_time = std::chrono::high_resolution_clock::now();
00073         }
00074 
00075 
00076         if(sine_elapsed.count() > sine_period)
00077         {
00078                 last_call_time = std::chrono::high_resolution_clock::now();
00079         }
00080 }
00081 
00082 
00083 
00084 void joint_states_callback(const sensor_msgs::JointState &msg)
00085 {
00086         // ROS_INFO("Joint states update!");
00087         joints_state.name = msg.name;
00088         joints_state.position = msg.position;
00089         joints_state.velocity = msg.velocity;
00090         joints_state.effort = msg.effort;
00091 
00092         left_state_pub.publish(joints_state.position[left_joint_state_idx]);
00093         right_state_pub.publish(joints_state.position[right_joint_state_idx]);
00094 }
00095 
00096 
00097 
00098 
00099 int main( int argc, char* argv[] )
00100 {
00101         ros::init(argc, argv, "test_joint_vel_control");
00102         ros::NodeHandle nh;
00103 
00104     sub = nh.subscribe("/yumi/joint_states", 1000, joint_states_callback);
00105 
00106         signal(SIGTERM, quitRequested);
00107         signal(SIGINT, quitRequested);
00108         signal(SIGHUP, quitRequested);
00109 
00110         vel_signal_pub = nh.advertise<std_msgs::Float64>("/yumi/vel_signal", 1000);
00111     left_controller_pub = nh.advertise<std_msgs::Float64>("/yumi/joint_vel_controller_" + std::to_string(test_joint_number) + "_l/command", 1000);
00112         right_controller_pub = nh.advertise<std_msgs::Float64>("/yumi/joint_vel_controller_" + std::to_string(test_joint_number) + "_r/command", 1000);
00113 
00114     left_state_pub = nh.advertise<std_msgs::Float64>("/yumi/joint_" + std::to_string(test_joint_number) + "_l/state", 1000);
00115         right_state_pub = nh.advertise<std_msgs::Float64>("/yumi/joint_" + std::to_string(test_joint_number) + "_r/state", 1000);
00116 
00117         while( !g_quit && ros::ok() )
00118         {
00119                 ros::spinOnce();
00120                 send_sinusoidal_vel_joints();
00121         }
00122 
00123         return EXIT_SUCCESS;
00124 }
00125 


yumi_test_controllers
Author(s): Yoshua Nava
autogenerated on Sun Jul 2 2017 03:15:52