motor_node.cc
Go to the documentation of this file.
1 
31 #include <dynamic_reconfigure/server.h>
32 #include <ros/ros.h>
33 #include <time.h>
34 #include <ubiquity_motor/PIDConfig.h>
38 #include <boost/thread.hpp>
39 #include <string>
41 
42 static const double BILLION = 1000000000.0;
43 
47 
48 void PID_update_callback(const ubiquity_motor::PIDConfig& config,
49  uint32_t level) {
50  if (level == 1) {
51  firmware_params.pid_proportional = config.PID_P;
52  } else if (level == 2) {
53  firmware_params.pid_integral = config.PID_I;
54  } else if (level == 4) {
55  firmware_params.pid_derivative = config.PID_D;
56  } else if (level == 8) {
57  firmware_params.pid_denominator = config.PID_C;
58  } else if (level == 16) {
59  firmware_params.pid_moving_buffer_size = config.PID_W;
60  } else {
61  ROS_ERROR("Unsupported dynamic_reconfigure level %u", level);
62  }
63 }
64 
65 int main(int argc, char* argv[]) {
66  ros::init(argc, argv, "motor_node");
67  ros::NodeHandle nh;
68 
69  firmware_params = FirmwareParams(nh);
70  serial_params = CommsParams(nh);
71  node_params = NodeParams(nh);
72 
73  MotorHardware robot(nh, serial_params, firmware_params);
75 
77  spinner.start();
78 
79  dynamic_reconfigure::Server<ubiquity_motor::PIDConfig> server;
80  dynamic_reconfigure::Server<ubiquity_motor::PIDConfig>::CallbackType f;
81 
82  f = boost::bind(&PID_update_callback, _1, _2);
83  server.setCallback(f);
84 
85  robot.setParams(firmware_params);
86 
87  ros::Rate r(node_params.controller_loop_rate);
88  robot.requestVersion();
89 
90  int times = 0;
91  while(robot.firmware_version == 0) {
92  if(times >= 10)
93  throw std::runtime_error("Firmware not reporting its version");
94  robot.readInputs();
95  r.sleep();
96  times++;
97  }
98 
99  ros::Time last_time;
100  ros::Time current_time;
101  ros::Duration elapsed;
102  last_time = ros::Time::now();
103 
104  for (int i = 0; i < 5; i++) {
105  r.sleep();
106  robot.sendParams();
107  }
108 
109  while (ros::ok()) {
110  current_time = ros::Time::now();
111  elapsed = last_time - current_time;
112  last_time = current_time;
113  robot.readInputs();
114  cm.update(ros::Time::now(), elapsed);
115  robot.setParams(firmware_params);
116  robot.sendParams();
117  robot.writeSpeeds();
118 
119  r.sleep();
120  }
121 
122  return 0;
123 }
int32_t pid_proportional
f
double controller_loop_rate
int32_t pid_denominator
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int32_t pid_moving_buffer_size
void spinner()
static FirmwareParams firmware_params
Definition: motor_node.cc:44
int32_t pid_integral
int32_t pid_derivative
ROSCPP_DECL bool ok()
void setParams(FirmwareParams firmware_params)
void PID_update_callback(const ubiquity_motor::PIDConfig &config, uint32_t level)
Definition: motor_node.cc:48
bool sleep()
static CommsParams serial_params
Definition: motor_node.cc:45
static NodeParams node_params
Definition: motor_node.cc:46
static Time now()
int main(int argc, char *argv[])
Definition: motor_node.cc:65
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
static const double BILLION
Definition: motor_node.cc:42
#define ROS_ERROR(...)


ubiquity_motor
Author(s):
autogenerated on Mon Jun 10 2019 15:37:24