tilt_motor_single_sweep_continuous.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 has the servo initilize and then move back and forth continuously.
00012 //It also sends times to the combine_clouds_subscriber to allow point clouds to be compiled.
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 dynamixel 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     //publish motor commands
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 motor error messages
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 error
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;  //Creates class object only used in the single instance that this function is run
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()
00101 {
00102     Dynamixel motor;
00103 
00104     motor.startTime();
00105     motor.moveMotor(max_angle);
00106     ros::Duration(pause_time).sleep();
00107     motor.checkError();
00108     ros::Duration(pause_time).sleep();
00109 
00110     motor.moveMotor(min_angle);
00111     ros::Duration(pause_time).sleep();
00112     motor.checkError();
00113     motor.endTime();
00114     ros::Duration(pause_time).sleep();
00115     ROS_INFO("Finished One Sweep!");
00116 }
00117 
00118 //main
00119 int main(int argc, char **argv) {
00120 
00121     //initialize
00122     ros::init(argc, argv, "Motor_Tilt");
00123     ros::NodeHandle nh;
00124 
00125     //variables
00126     int max;
00127     int min;
00128     double pause;
00129     Dynamixel motor;
00130 
00131     //establish parameters
00132     nh.param("maximum", max, 92);
00133     nh.param("minimum", min, -92);
00134     nh.param("pause", pause, 0.1);
00135 
00136     //transfer parameters to global variables
00137     max_angle = max;
00138     min_angle = min;
00139     pause_time = pause;
00140 
00141     //Wait for servo init by waiting for /state message
00142     ros::topic::waitForMessage<dynamixel_msgs::JointState>("/tilt_controller/state", ros::Duration(100));
00143 
00144     //pause to allow motor object to initialize and set to min_angle
00145     ros::Duration(1).sleep();
00146     initialize();    
00147 
00148     //continuously perform sweeps
00149     while(ros::ok()) {
00150         sweep();
00151     }
00152 }


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