motor_node.cc
Go to the documentation of this file.
00001 
00031 #include <ubiquity_motor/motor_hardware.h>
00032 #include <ubiquity_motor/motor_message.h>
00033 #include <string>
00034 #include <boost/thread.hpp>
00035 #include <time.h>
00036 #include "controller_manager/controller_manager.h"
00037 #include <ros/ros.h>
00038 
00039 static const double BILLION = 1000000000.0;
00040 struct timespec last_time;
00041 struct timespec current_time;
00042 
00043 
00044 // void controlLoop(
00045 //      MotorHardware &robot,
00046 //      controller_manager::ControllerManager &cm,
00047 //      timespec &last_time,
00048 //      timespec &current_time){
00049         
00050 //      clock_gettime(CLOCK_MONOTONIC, &current_time);
00051 //      ros::Duration elapsed = ros::Duration(current_time.tv_sec - last_time.tv_sec + (current_time.tv_nsec - last_time.tv_nsec) / BILLION);
00052 //      last_time = current_time;
00053 //      robot.sendPid();
00054 //      robot.readInputs();
00055 //      cm.update(ros::Time::now(), elapsed);
00056 //      robot.writeSpeeds();    
00057 
00058 // }
00059 
00060 main(int argc, char* argv[]) {
00061         ros::init(argc, argv, "motor_node");
00062         ros::NodeHandle nh;
00063         MotorHardware robot(nh);
00064         controller_manager::ControllerManager cm(&robot,nh);
00065 
00066         ros::AsyncSpinner spinner(1);
00067         spinner.start();
00068 
00069         int32_t pid_proportional;
00070         int32_t pid_integral;
00071         int32_t pid_derivative;
00072         int32_t pid_denominator;
00073 
00074         if (!nh.getParam("ubiquity_motor/pid_proportional", pid_proportional))
00075         {
00076                 pid_proportional = 450;
00077                 nh.setParam("ubiquity_motor/pid_proportional", pid_proportional);
00078         }
00079 
00080         if (!nh.getParam("ubiquity_motor/pid_integral", pid_integral))
00081         {
00082                 pid_integral = 120;
00083                 nh.setParam("ubiquity_motor/pid_integral", pid_integral);
00084         }
00085 
00086         if (!nh.getParam("ubiquity_motor/pid_derivative", pid_derivative))
00087         {
00088                 pid_derivative = 70;
00089                 nh.setParam("ubiquity_motor/pid_derivative", pid_derivative);
00090         }
00091 
00092         if (!nh.getParam("ubiquity_motor/pid_denominator", pid_denominator))
00093         {
00094                 pid_derivative = 1000;
00095                 nh.setParam("ubiquity_motor/pid_denominator", pid_denominator);
00096         }
00097 
00098         robot.setPid(pid_proportional,pid_integral,pid_derivative,pid_denominator);
00099         robot.sendPid();
00100         
00101         double controller_loop_rate;
00102         if (!nh.getParam("ubiquity_motor/controller_loop_rate", controller_loop_rate))
00103         {
00104                 controller_loop_rate = 10;
00105                 nh.setParam("ubiquity_motor/controller_loop_rate", controller_loop_rate);
00106         }
00107 
00108         ros::Rate r(controller_loop_rate);
00109         robot.requestVersion();
00110 
00111 
00112         struct timespec last_time;
00113         struct timespec current_time;
00114         clock_gettime(CLOCK_MONOTONIC, &last_time);
00115 
00116         while (ros::ok()) {
00117                 clock_gettime(CLOCK_MONOTONIC, &current_time);
00118                 ros::Duration elapsed = ros::Duration(current_time.tv_sec - last_time.tv_sec + (current_time.tv_nsec - last_time.tv_nsec) / BILLION);
00119                 last_time = current_time;
00120                 robot.sendPid();
00121                 robot.readInputs();
00122                 cm.update(ros::Time::now(), elapsed);
00123                 robot.writeSpeeds();
00124                 r.sleep();
00125         }
00126 }


ubiquity_motor
Author(s):
autogenerated on Thu Jun 6 2019 18:33:27