tilt_motor_single_sweep_subscribe.cpp
Go to the documentation of this file.
00001 #include<ros/ros.h>
00002 #include<std_msgs/Float64.h>
00003 #include<dynamixel_msgs/JointState.h>
00004 #include<cmath>
00005 #include<laser_assembler/AssembleScans.h>
00006 #include<ros/time.h>
00007 #include<std_msgs/Time.h>
00008 #include<std_msgs/Empty.h>
00009 
00010 /*This code allows the motor to move back and forth to a maximum and minimum number of degrees defined in parameters.
00011   This iteration performs initializes the servo and then waits for messages through and empty message to perform sweeps.
00012   This version also sends times to cloud_compiler_subscriber to assemble point clouds.*/
00013 
00014 using namespace std;
00015 
00016 //global variables
00017 float error;
00018 int max_angle;
00019 int min_angle;
00020 float pause_time;
00021 
00022 //obtains error from message
00023 void obtainValues(const dynamixel_msgs::JointState &msg) {
00024     error = msg.error;
00025 }
00026 
00027 //creates all commands for the motor
00028 class Dynamixel {
00029     private:
00030     ros::NodeHandle nh;
00031     ros::Publisher pub_1;
00032     ros::Publisher pub_2;
00033     ros::Publisher pub_3;
00034     ros::Subscriber sub;
00035 
00036     public:
00037     Dynamixel();
00038     void checkError();
00039     void moveMotor(double position);
00040     void startTime();
00041     void endTime();
00042 };
00043 
00044 //creates publishers and subscriber
00045 Dynamixel::Dynamixel() {
00046     //publishe motor movements
00047     pub_1 = nh.advertise<std_msgs::Float64>("/tilt_controller/command", 10);
00048     //publish start time of sweep
00049     pub_2 = nh.advertise<std_msgs::Time>("/time/start_time", 1);
00050     //publish end time of sweep
00051     pub_3 = nh.advertise<std_msgs::Time>("/time/end_time", 1);
00052     //subscribe to /state for motor error
00053     sub   = nh.subscribe("/tilt_controller/state", 1, &obtainValues);
00054 }
00055 
00056 //creates message and publishes -> degree to radian to publish
00057 void Dynamixel::moveMotor(double position) {
00058     double convert = (position * 3.14/180);
00059     std_msgs::Float64 aux;
00060     aux.data = convert;
00061     pub_1.publish(aux);
00062     ROS_INFO_STREAM(aux);
00063 }
00064 
00065 //ensures proper alignment
00066 void Dynamixel::checkError() {
00067     ros::spinOnce(); //get starting value of motor position
00068 
00069     while((abs (error))>0.05) {  //keep waiting and checking error until < 0.05
00070         ros::Duration(.1).sleep();
00071         ros::spinOnce();
00072     }    
00073 }
00074 
00075 //publishes start time for cloud compiler
00076 void Dynamixel::startTime() {
00077     std_msgs::Time msg;
00078     msg.data = ros::Time::now();
00079     pub_2.publish(msg);
00080 }
00081 
00082 //publishes end time for cloud compiler
00083 void Dynamixel::endTime() {
00084     std_msgs::Time msg;
00085     msg.data = ros::Time::now();
00086     pub_3.publish(msg);
00087 }
00088 
00089 //initilazies motor to min angle
00090 void initialize() {
00091     Dynamixel motor_1;
00092 
00093     motor_1.moveMotor(min_angle);
00094     ros::Duration(pause_time).sleep();
00095     motor_1.checkError();
00096     ros::Duration(pause_time).sleep();
00097 }
00098 
00099 //performs one sweep min -> max -> min
00100 void sweep(const std_msgs::Empty &msg) {
00101     Dynamixel motor;
00102 
00103     motor.startTime();
00104     motor.moveMotor(max_angle);
00105     ros::Duration(pause_time).sleep();
00106     motor.checkError();
00107     ros::Duration(pause_time).sleep();
00108 
00109     motor.moveMotor(min_angle);
00110     ros::Duration(pause_time).sleep();
00111     motor.checkError();
00112     motor.endTime();
00113     ros::Duration(pause_time).sleep();
00114     ROS_INFO("Finished One Sweep!");
00115 }
00116 
00117 //main
00118 int main(int argc, char **argv) {
00119 
00120     //initialize
00121     ros::init(argc, argv, "Motor_Tilt");
00122     ros::NodeHandle nh;
00123 
00124     //variables
00125     int max;
00126     int min;
00127     double pause;
00128     Dynamixel motor;
00129 
00130     //get parameters
00131     nh.param("maximum", max, 92);
00132     nh.param("minimum", min, -92);
00133     nh.param("pause", pause, 0.1);
00134 
00135     //transfer parameters to global variables
00136     max_angle = max;
00137     min_angle = min;
00138     pause_time = pause;
00139 
00140     //Wait for servo init by waiting for /state topic
00141     ros::topic::waitForMessage<dynamixel_msgs::JointState>("/tilt_controller/state", ros::Duration(100));
00142 
00143     //subscribe to empty message to run sweep
00144     ros::Subscriber sub=nh.subscribe("/perform_sweep", 1, &sweep);
00145     
00146     //pause to allow motor object to initialize and set to min_angle
00147     ros::Duration(1).sleep();
00148     initialize(); 
00149     
00150     //wait for empty messages to perform sweep
00151     ros::spin();
00152 }


spin_hokuyo
Author(s):
autogenerated on Sat Jun 8 2019 20:41:35